#![allow(clippy::all)]
use rstest::*;
use std::env;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use trustformers::config_management::ConfigurationManager;
use trustformers::error::TrustformersError;
use trustformers::hub::get_cache_dir;
use trustformers::{GlobalMemoryPool, GlobalProfiler, ZeroCopyTensorView};
#[cfg(test)]
mod compatibility_tests {
use super::*;
#[rstest]
fn test_platform_path_compatibility() {
let cache_dir = get_cache_dir().expect("Should get cache directory");
let model_path = cache_dir.join("models").join("test-model");
assert!(model_path.is_absolute());
let components: Vec<_> = model_path.components().collect();
assert!(!components.is_empty());
let path_str = model_path.to_string_lossy();
assert!(!path_str.is_empty());
}
#[rstest]
fn test_memory_alignment_compatibility() {
let pool = GlobalMemoryPool::instance();
for alignment in [8, 16, 32, 64, 128] {
let size = 1024;
let ptr =
pool.allocate_aligned(size, alignment).expect("Should allocate aligned memory");
assert_eq!(
ptr as usize % alignment,
0,
"Memory should be aligned to {} bytes",
alignment
);
unsafe { pool.deallocate(ptr, size) };
}
}
#[rstest]
fn test_endianness_compatibility() {
let test_value: u32 = 0x12345678;
let bytes = test_value.to_le_bytes();
let reconstructed = u32::from_le_bytes(bytes);
assert_eq!(test_value, reconstructed);
let float_value: f32 = 3.14159;
let float_bytes = float_value.to_le_bytes();
let reconstructed_float = f32::from_le_bytes(float_bytes);
assert!((float_value - reconstructed_float).abs() < f32::EPSILON);
}
#[rstest]
fn test_cpu_architecture_compatibility() {
#[cfg(target_arch = "x86_64")]
{
let features = ["sse", "sse2", "avx"];
for feature in features {
println!("Testing CPU feature: {}", feature);
}
}
#[cfg(target_arch = "aarch64")]
{
let features = ["neon"];
for feature in features {
println!("Testing ARM feature: {}", feature);
}
}
assert_eq!(
std::mem::size_of::<usize>(),
std::mem::size_of::<*const u8>()
);
}
#[rstest]
fn test_thread_safety_compatibility() {
use std::sync::Barrier;
let num_threads = 4;
let barrier = Arc::new(Barrier::new(num_threads));
let pool = Arc::new(GlobalMemoryPool::instance());
let handles: Vec<_> = (0..num_threads)
.map(|i| {
let barrier = barrier.clone();
let pool = pool.clone();
thread::spawn(move || {
barrier.wait();
let size = 1024 + i * 512;
let ptr = pool.allocate(size).expect("Should allocate memory");
thread::sleep(Duration::from_millis(10));
unsafe { pool.deallocate(ptr, size) };
})
})
.collect();
for handle in handles {
handle.join().expect("Thread should complete successfully");
}
}
#[rstest]
fn test_filesystem_compatibility() {
let temp_dir = std::env::temp_dir();
let test_file = temp_dir.join("trustformers_test.tmp");
std::fs::write(&test_file, b"test data").expect("Should write test file");
assert!(test_file.exists());
let content = std::fs::read(&test_file).expect("Should read test file");
assert_eq!(content, b"test data");
std::fs::remove_file(&test_file).expect("Should remove test file");
assert!(!test_file.exists());
}
#[rstest]
fn test_environment_variable_compatibility() {
let test_var = "TRUSTFORMERS_TEST_VAR";
let test_value = "test_value_123";
env::set_var(test_var, test_value);
assert_eq!(
env::var(test_var).expect("operation failed in test"),
test_value
);
env::remove_var(test_var);
assert!(env::var(test_var).is_err());
}
#[rstest]
fn test_numeric_precision_compatibility() {
let a: f32 = 0.1;
let b: f32 = 0.2;
let c: f32 = 0.3;
let sum = a + b;
assert!((sum - c).abs() < 1e-6, "f32 precision should be consistent");
let a64: f64 = 0.1;
let b64: f64 = 0.2;
let c64: f64 = 0.3;
let sum64 = a64 + b64;
assert!(
(sum64 - c64).abs() < 1e-15,
"f64 precision should be consistent"
);
}
#[rstest]
fn test_zero_copy_compatibility() {
let data = vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0];
let shape = vec![2, 3];
let tensor_view =
ZeroCopyTensorView::from_slice(&data, &shape).expect("Should create zero-copy view");
assert_eq!(tensor_view.shape(), &shape);
assert_eq!(tensor_view.data().len(), data.len());
let subview = tensor_view.subview(&[0..1, 0..2]).expect("Should create subview");
assert_eq!(subview.shape(), &[1, 2]);
}
#[rstest]
#[allow(unexpected_cfgs)]
fn test_compilation_features_compatibility() {
#[cfg(feature = "std")]
{
println!("std feature is available");
}
#[cfg(feature = "alloc")]
{
println!("alloc feature is available");
}
#[cfg(feature = "cuda")]
{
println!("CUDA feature is available");
}
#[cfg(not(feature = "cuda"))]
{
println!("CUDA feature is not available");
}
#[cfg(feature = "mkl")]
{
println!("MKL feature is available");
}
#[cfg(not(feature = "mkl"))]
{
println!("MKL feature is not available");
}
}
#[rstest]
fn test_config_serialization_compatibility() {
let config = serde_json::json!({
"model_type": "bert",
"hidden_size": 768,
"num_attention_heads": 12,
"num_hidden_layers": 12,
"intermediate_size": 3072,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"attention_probs_dropout_prob": 0.1,
"max_position_embeddings": 512,
"type_vocab_size": 2,
"vocab_size": 30522
});
let json_str = serde_json::to_string(&config).expect("Should serialize to JSON");
let deserialized: serde_json::Value =
serde_json::from_str(&json_str).expect("Should deserialize from JSON");
assert_eq!(config, deserialized);
let pretty_json = serde_json::to_string_pretty(&config).expect("Should pretty print");
assert!(pretty_json.len() > json_str.len());
}
#[rstest]
fn test_simd_compatibility() {
let data1 = [1.0f32, 2.0, 3.0, 4.0];
let data2 = [5.0f32, 6.0, 7.0, 8.0];
let mut result = vec![0.0f32; 4];
for i in 0..4 {
result[i] = data1[i] + data2[i];
}
let expected = vec![6.0f32, 8.0, 10.0, 12.0];
assert_eq!(result, expected);
}
#[tokio::test]
async fn test_async_runtime_compatibility() {
use tokio::time::{sleep, Duration};
let start = std::time::Instant::now();
sleep(Duration::from_millis(10)).await;
let elapsed = start.elapsed();
assert!(elapsed >= Duration::from_millis(9));
assert!(elapsed < Duration::from_millis(100));
let handle = tokio::spawn(async {
sleep(Duration::from_millis(1)).await;
42
});
let result = handle.await.expect("Task should complete");
assert_eq!(result, 42);
}
#[rstest]
fn test_error_handling_compatibility() {
let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
let trustformers_error = TrustformersError::Io {
message: io_error.to_string(),
path: None,
suggestion: Some("Check file permissions and disk space".to_string()),
};
let error_msg = format!("{}", trustformers_error);
assert!(!error_msg.is_empty());
let debug_msg = format!("{:?}", trustformers_error);
assert!(!debug_msg.is_empty());
}
#[rstest]
fn test_profiler_compatibility() {
let profiler = GlobalProfiler::instance();
let session_id = profiler.start_session("compatibility_test");
assert!(session_id.is_ok());
let session_id = session_id.unwrap();
profiler.start_operation(&session_id, "test_operation", None);
thread::sleep(Duration::from_millis(1));
profiler.end_operation(&session_id, "test_operation");
let result = profiler.end_session(&session_id);
assert!(result.is_ok());
}
#[rstest]
fn test_config_validation_compatibility() {
let config_manager = ConfigurationManager::new();
let config = serde_json::json!({
"cache_dir": get_cache_dir().expect("operation failed in test"),
"model_type": "bert",
"hidden_size": 768
});
let validation_result = config_manager.validate_config("model", &config);
println!("Validation result: {:?}", validation_result);
}
}
#[cfg(test)]
mod platform_specific_tests {
use super::*;
#[cfg(target_os = "windows")]
mod windows_tests {
use super::*;
#[rstest]
fn test_windows_path_handling() {
let cache_dir = get_cache_dir().expect("Should get cache directory");
let path_str = cache_dir.to_string_lossy();
if path_str.contains('\\') {
println!("Windows-style path detected: {}", path_str);
}
if path_str.starts_with(r"\\") {
println!("UNC path detected: {}", path_str);
}
}
}
#[cfg(target_os = "linux")]
mod linux_tests {
use super::*;
#[rstest]
fn test_linux_path_handling() {
let cache_dir = get_cache_dir().expect("Should get cache directory");
let path_str = cache_dir.to_string_lossy();
assert!(
path_str.contains('/'),
"Linux paths should contain forward slashes"
);
assert!(path_str.starts_with('/'), "Linux paths should be absolute");
}
}
#[cfg(target_os = "macos")]
mod macos_tests {
use super::*;
#[rstest]
fn test_macos_path_handling() {
let cache_dir = get_cache_dir().expect("Should get cache directory");
let path_str = cache_dir.to_string_lossy();
assert!(
path_str.contains('/'),
"macOS paths should contain forward slashes"
);
assert!(path_str.starts_with('/'), "macOS paths should be absolute");
assert!(
path_str.starts_with("/Users")
|| path_str.starts_with("/System")
|| path_str.starts_with("/Library")
);
}
}
}
#[cfg(test)]
mod architecture_tests {
use super::*;
#[cfg(target_arch = "x86_64")]
mod x86_64_tests {
use super::*;
#[rstest]
fn test_x86_64_features() {
assert_eq!(std::mem::size_of::<usize>(), 8);
assert_eq!(std::mem::size_of::<*const u8>(), 8);
assert_eq!(std::mem::align_of::<f64>(), 8);
assert_eq!(std::mem::align_of::<i64>(), 8);
}
}
#[cfg(target_arch = "aarch64")]
mod aarch64_tests {
use super::*;
#[rstest]
fn test_aarch64_features() {
assert_eq!(std::mem::size_of::<usize>(), 8);
assert_eq!(std::mem::size_of::<*const u8>(), 8);
assert_eq!(std::mem::align_of::<f64>(), 8);
assert_eq!(std::mem::align_of::<i64>(), 8);
}
}
#[cfg(target_arch = "x86")]
mod x86_tests {
use super::*;
#[rstest]
fn test_x86_features() {
assert_eq!(std::mem::size_of::<usize>(), 4);
assert_eq!(std::mem::size_of::<*const u8>(), 4);
}
}
}