use helia_interface::Helia;
use std::sync::Arc;
pub mod test_utils {
use super::*;
pub async fn verify_helia_basic(helia: Arc<dyn Helia>) -> Result<(), String> {
let _blockstore = helia.blockstore();
let _datastore = helia.datastore();
let _pins = helia.pins();
println!("✓ All basic interfaces accessible");
Ok(())
}
pub mod patterns {
use bytes::Bytes;
pub fn generate_test_data(size: usize) -> Bytes {
let data: Vec<u8> = (0..size).map(|i| (i % 256) as u8).collect();
Bytes::from(data)
}
pub const HELLO_WORLD: &[u8] = b"Hello, World!";
pub const EMPTY: &[u8] = b"";
pub const LARGE_TEXT: &[u8] = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. \
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
}
}
pub mod compat {
#[derive(Debug, Clone)]
pub struct VersionInfo {
pub major: u32,
pub minor: u32,
pub patch: u32,
}
impl VersionInfo {
pub fn new(major: u32, minor: u32, patch: u32) -> Self {
Self {
major,
minor,
patch,
}
}
pub fn is_compatible_with(&self, other: &VersionInfo) -> bool {
self.major == other.major
}
}
impl std::fmt::Display for VersionInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}
}
pub mod bench {
use std::time::{Duration, Instant};
#[derive(Debug, Clone)]
pub struct BenchResult {
pub operation: String,
pub duration: Duration,
pub iterations: usize,
}
impl BenchResult {
pub fn avg_duration(&self) -> Duration {
self.duration / self.iterations as u32
}
pub fn operations_per_second(&self) -> f64 {
self.iterations as f64 / self.duration.as_secs_f64()
}
}
impl std::fmt::Display for BenchResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}: {} iterations in {:?} ({:.2} ops/sec)",
self.operation,
self.iterations,
self.duration,
self.operations_per_second()
)
}
}
pub async fn bench_async<F, Fut>(name: &str, iterations: usize, mut f: F) -> BenchResult
where
F: FnMut() -> Fut,
Fut: std::future::Future<Output = ()>,
{
let start = Instant::now();
for _ in 0..iterations {
f().await;
}
let duration = start.elapsed();
BenchResult {
operation: name.to_string(),
duration,
iterations,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::compat::VersionInfo;
use crate::test_utils::patterns;
#[test]
fn test_generate_test_data() {
let data = patterns::generate_test_data(10);
assert_eq!(data.len(), 10);
assert_eq!(data[0], 0);
assert_eq!(data[9], 9);
}
#[test]
fn test_version_compatibility() {
let v1 = VersionInfo::new(1, 0, 0);
let v2 = VersionInfo::new(1, 5, 3);
let v3 = VersionInfo::new(2, 0, 0);
assert!(v1.is_compatible_with(&v2));
assert!(!v1.is_compatible_with(&v3));
}
#[test]
fn test_version_display() {
let v = VersionInfo::new(1, 2, 3);
assert_eq!(v.to_string(), "1.2.3");
}
#[tokio::test]
async fn test_bench_async() {
let result = bench::bench_async("test_op", 10, || async {
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
})
.await;
assert_eq!(result.iterations, 10);
assert!(result.duration.as_millis() >= 10);
}
}