#![allow(clippy::pedantic, clippy::assertions_on_constants)]
use std::time::{Duration, Instant};
mod zero_cost_abstractions {
use super::*;
#[test]
fn test_reexport_no_overhead() {
let start = Instant::now();
use quantrs2::core::qubit::QubitId;
for i in 0..10_000 {
let q = QubitId::new(i);
std::hint::black_box(q);
}
let facade_time = start.elapsed();
let start = Instant::now();
use quantrs2_core::qubit::QubitId as DirectQubitId;
for i in 0..10_000 {
let q = DirectQubitId::new(i);
std::hint::black_box(q);
}
let direct_time = start.elapsed();
let ratio = facade_time.as_nanos() as f64 / direct_time.as_nanos().max(1) as f64;
assert!(
ratio < 3.0,
"Facade overhead is too high: {ratio:.2}x (facade: {facade_time:?}, direct: {direct_time:?})"
);
}
#[test]
fn test_prelude_zero_cost() {
use quantrs2::prelude::essentials::*;
let start = Instant::now();
for i in 0..10_000 {
let q = QubitId::new(i);
std::hint::black_box(q);
}
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_millis(100),
"Prelude access is too slow: {elapsed:?}"
);
}
}
mod error_handling_overhead {
use super::*;
use quantrs2::error::{QuantRS2Error, QuantRS2Result};
#[test]
fn test_error_creation_performance() {
let start = Instant::now();
for i in 0..10_000 {
let err = QuantRS2Error::InvalidQubitId(i);
std::hint::black_box(err);
}
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_millis(50),
"Error creation is too slow: {elapsed:?}"
);
}
#[test]
fn test_result_type_no_overhead() {
fn facade_result() -> QuantRS2Result<u64> {
Ok(42)
}
fn std_result() -> Result<u64, QuantRS2Error> {
Ok(42)
}
let start = Instant::now();
for _ in 0..100_000 {
let r = facade_result();
let _ = std::hint::black_box(r);
}
let facade_time = start.elapsed();
let start = Instant::now();
for _ in 0..100_000 {
let r = std_result();
let _ = std::hint::black_box(r);
}
let std_time = start.elapsed();
let ratio = facade_time.as_nanos() as f64 / std_time.as_nanos().max(1) as f64;
assert!(ratio < 5.0, "Result type overhead: {ratio:.2}x");
}
}
mod feature_detection_overhead {
use super::*;
#[test]
fn test_diagnostics_caching() {
use quantrs2::diagnostics;
let _ = diagnostics::run_diagnostics();
let start = Instant::now();
for _ in 0..100 {
let report = diagnostics::run_diagnostics();
std::hint::black_box(report);
}
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_secs(5),
"Diagnostics calls too slow: {elapsed:?}"
);
}
#[test]
fn test_config_access_fast() {
use quantrs2::config::Config;
let cfg = Config::global();
let start = Instant::now();
for _ in 0..100_000 {
let snapshot = cfg.snapshot();
std::hint::black_box(snapshot);
}
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_millis(500),
"Config access too slow: {elapsed:?}"
);
}
}
mod version_checking_overhead {
use super::*;
#[test]
fn test_version_constants_are_const() {
use quantrs2::version;
const V1: &str = version::VERSION;
const V2: &str = version::QUANTRS2_VERSION;
const V3: &str = version::SCIRS2_VERSION;
const V4: &str = version::RUSTC_VERSION;
const V5: &str = version::TARGET_TRIPLE;
const V6: &str = version::BUILD_PROFILE;
assert!(!V1.is_empty());
assert!(!V2.is_empty());
assert!(!V3.is_empty());
assert!(!V4.is_empty());
assert!(!V5.is_empty());
assert!(!V6.is_empty());
}
#[test]
fn test_version_info_fast() {
use quantrs2::version::VersionInfo;
let start = Instant::now();
for _ in 0..10_000 {
let info = VersionInfo::current();
std::hint::black_box(info);
}
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_millis(100),
"VersionInfo creation too slow: {elapsed:?}"
);
}
#[test]
fn test_compatibility_check_fast() {
use quantrs2::version::check_compatibility;
let start = Instant::now();
for _ in 0..1_000 {
let result = check_compatibility();
let _ = std::hint::black_box(result);
}
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_millis(500),
"Compatibility check too slow: {elapsed:?}"
);
}
}
mod utility_function_overhead {
use super::*;
use quantrs2::utils;
#[test]
fn test_memory_estimation_fast() {
let start = Instant::now();
for qubits in 1..30 {
let mem = utils::estimate_statevector_memory(qubits);
std::hint::black_box(mem);
}
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_millis(10),
"Memory estimation too slow: {elapsed:?}"
);
}
#[test]
fn test_quantum_constants_are_const() {
const SQRT2: f64 = utils::SQRT_2;
const INV_SQRT2: f64 = utils::INV_SQRT_2;
const PI_2: f64 = utils::PI_OVER_2;
const PI_4: f64 = utils::PI_OVER_4;
const PI_8: f64 = utils::PI_OVER_8;
const PI: f64 = utils::PI_CONST;
assert!(SQRT2.mul_add(INV_SQRT2, -1.0).abs() < 1e-15);
assert!(PI_2.mul_add(2.0, -PI).abs() < 1e-15);
assert!(PI_4.mul_add(4.0, -PI).abs() < 1e-15);
assert!(PI_8.mul_add(8.0, -PI).abs() < 1e-15);
}
#[test]
fn test_formatting_fast() {
let start = Instant::now();
for i in 0..10_000 {
let mem_str = utils::format_memory(i * 1024);
std::hint::black_box(mem_str);
}
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_millis(100),
"Memory formatting too slow: {elapsed:?}"
);
}
}
mod deprecation_overhead {
use super::*;
use quantrs2::deprecation;
#[test]
fn test_is_deprecated_fast() {
let start = Instant::now();
for _ in 0..100_000 {
let result = deprecation::is_deprecated("some_nonexistent_api");
std::hint::black_box(result);
}
let elapsed = start.elapsed();
let threshold = if cfg!(debug_assertions) {
Duration::from_millis(500)
} else {
Duration::from_millis(100)
};
assert!(
elapsed < threshold,
"Deprecation check too slow: {elapsed:?}"
);
}
#[test]
fn test_module_stability_fast() {
let start = Instant::now();
for _ in 0..100_000 {
let stability = deprecation::get_module_stability("quantrs2::core");
std::hint::black_box(stability);
}
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_millis(500),
"Module stability lookup too slow: {elapsed:?}"
);
}
#[test]
fn test_migration_report_fast() {
let start = Instant::now();
for _ in 0..100 {
let report = deprecation::migration_report();
std::hint::black_box(report);
}
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_millis(500),
"Migration report too slow: {elapsed:?}"
);
}
}
mod bench_utilities_overhead {
use super::*;
use quantrs2::bench;
#[test]
fn test_timer_overhead() {
let start = Instant::now();
for _ in 0..10_000 {
let timer = bench::BenchmarkTimer::start();
let elapsed = timer.stop();
std::hint::black_box(elapsed);
}
let total = start.elapsed();
let per_op = total.as_nanos() / 10_000;
assert!(
per_op < 10_000, "Timer overhead too high: {per_op}ns per operation"
);
}
#[test]
fn test_stats_aggregation_efficient() {
let mut stats = bench::BenchmarkStats::new("test");
let start = Instant::now();
for i in 0..10_000 {
stats.record(Duration::from_nanos(i * 100));
}
let recording_time = start.elapsed();
assert!(
recording_time < Duration::from_millis(50),
"Stats recording too slow: {recording_time:?}"
);
let start = Instant::now();
for _ in 0..10 {
let _ = stats.mean();
let _ = stats.median();
let _ = stats.std_dev();
let _ = stats.min();
let _ = stats.max();
}
let calc_time = start.elapsed();
assert!(
calc_time < Duration::from_secs(2),
"Stats calculations too slow: {calc_time:?}"
);
}
}
mod testing_utilities_overhead {
use super::*;
use quantrs2::testing;
#[test]
fn test_assertions_fast() {
let start = Instant::now();
for _ in 0..10_000 {
testing::assert_approx_eq(1.0, 1.0 + 1e-12, 1e-8);
}
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_millis(50),
"Assertion too slow: {elapsed:?}"
);
}
#[test]
fn test_random_data_generation_efficient() {
let start = Instant::now();
for seed in 0..100 {
let data = testing::generate_random_test_data(1000, seed);
std::hint::black_box(data);
}
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_millis(500),
"Random data generation too slow: {elapsed:?}"
);
}
}
mod inlining_verification {
#[test]
fn test_key_functions_inline() {
use quantrs2::utils::{INV_SQRT_2, SQRT_2};
use quantrs2::version::VERSION;
const _V: &str = VERSION;
const _S: f64 = SQRT_2;
const _I: f64 = INV_SQRT_2;
assert!(!_V.is_empty());
assert!(_S.mul_add(_I, -1.0).abs() < 1e-15);
}
}
mod memory_efficiency {
use super::*;
#[test]
fn test_error_size() {
use quantrs2::error::QuantRS2Error;
let size = std::mem::size_of::<QuantRS2Error>();
assert!(size < 256, "QuantRS2Error is too large: {size} bytes");
}
#[test]
fn test_config_snapshot_size() {
use quantrs2::config::ConfigData;
let size = std::mem::size_of::<ConfigData>();
assert!(size < 256, "ConfigData is too large: {size} bytes");
}
#[test]
fn test_deprecation_info_size() {
use quantrs2::deprecation::DeprecationInfo;
let size = std::mem::size_of::<DeprecationInfo>();
assert!(size < 512, "DeprecationInfo is too large: {size} bytes");
}
}