use archmage::SimdToken;
use archmage::testing::{CompileTimePolicy, for_each_token_permutation, lock_token_testing};
#[test]
fn permutations_include_all_enabled() {
let mut saw_all_enabled = false;
let report = for_each_token_permutation(CompileTimePolicy::Warn, |perm| {
if perm.disabled.is_empty() {
saw_all_enabled = true;
}
});
assert!(
saw_all_enabled,
"should always include 'all enabled' permutation"
);
assert!(
report.permutations_run >= 1,
"should run at least the 'all enabled' permutation"
);
}
#[cfg(target_arch = "x86_64")]
#[test]
fn x86_has_multiple_permutations() {
let report = for_each_token_permutation(CompileTimePolicy::Warn, |_perm| {
});
if report.warnings.is_empty() {
assert!(
report.permutations_run >= 3,
"expected >=3 permutations without compile-time tokens, got {}",
report.permutations_run
);
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn disabled_tokens_fail_summon() {
let report = for_each_token_permutation(CompileTimePolicy::Warn, |perm| {
for &name in &perm.disabled {
if name == archmage::X64V2Token::NAME {
assert!(
archmage::X64V2Token::summon().is_none(),
"V2 should be None when disabled"
);
}
if name == archmage::X64V3Token::NAME {
assert!(
archmage::X64V3Token::summon().is_none(),
"V3 should be None when disabled"
);
}
}
});
assert!(report.permutations_run >= 1);
}
#[cfg(target_arch = "x86_64")]
#[test]
fn tokens_reenabled_between_permutations() {
let _lock = lock_token_testing();
let v2_available = archmage::X64V2Token::summon().is_some();
let v3_available = archmage::X64V3Token::summon().is_some();
let report = for_each_token_permutation(CompileTimePolicy::Warn, |perm| {
if perm.disabled.is_empty() {
if v2_available {
assert!(
archmage::X64V2Token::summon().is_some(),
"V2 should be available in 'all enabled' permutation"
);
}
if v3_available {
assert!(
archmage::X64V3Token::summon().is_some(),
"V3 should be available in 'all enabled' permutation"
);
}
}
});
assert_eq!(
archmage::X64V2Token::summon().is_some(),
v2_available,
"V2 should be re-enabled after permutations"
);
assert_eq!(
archmage::X64V3Token::summon().is_some(),
v3_available,
"V3 should be re-enabled after permutations"
);
assert!(report.permutations_run >= 1);
}
#[cfg(target_arch = "x86_64")]
#[test]
fn cascade_disabling_works() {
let _ = for_each_token_permutation(CompileTimePolicy::Warn, |perm| {
if perm.disabled.contains(&archmage::X64V3Token::NAME) {
assert!(
archmage::X64V4Token::summon().is_none(),
"V4 should be None when V3 is disabled (cascade)"
);
}
});
}
#[cfg(target_arch = "x86_64")]
#[test]
fn no_duplicate_effective_states() {
let mut states: Vec<(bool, bool, bool, bool, bool, bool, bool, bool)> = Vec::new();
let _ = for_each_token_permutation(CompileTimePolicy::Warn, |_perm| {
let state = (
archmage::X64V1Token::summon().is_some(),
archmage::X64V2Token::summon().is_some(),
archmage::X64CryptoToken::summon().is_some(),
archmage::X64V3Token::summon().is_some(),
archmage::X64V3CryptoToken::summon().is_some(),
archmage::X64V4Token::summon().is_some(),
archmage::X64V4xToken::summon().is_some(),
archmage::Avx512Fp16Token::summon().is_some(),
);
assert!(
!states.contains(&state),
"duplicate effective state: {state:?}"
);
states.push(state);
});
}
#[test]
fn warn_policy_does_not_panic() {
let report = for_each_token_permutation(CompileTimePolicy::Warn, |_perm| {});
assert!(report.permutations_run >= 1);
}
#[cfg(feature = "testable_dispatch")]
#[test]
fn fail_policy_succeeds_with_testable_dispatch() {
let report = for_each_token_permutation(CompileTimePolicy::Fail, |perm| {
if perm.disabled.is_empty() {
#[cfg(target_arch = "x86_64")]
assert!(
archmage::X64V1Token::summon().is_some(),
"baseline x86_64 token should be available"
);
#[cfg(target_arch = "aarch64")]
assert!(
archmage::NeonToken::summon().is_some(),
"NEON token should be available on aarch64"
);
}
for &name in &perm.disabled {
#[cfg(target_arch = "x86_64")]
{
if name == archmage::X64V2Token::NAME {
assert!(archmage::X64V2Token::summon().is_none());
}
if name == archmage::X64V3Token::NAME {
assert!(archmage::X64V3Token::summon().is_none());
}
}
}
});
assert!(
report.warnings.is_empty(),
"testable_dispatch should prevent compile-time guarantees, but got warnings: {:?}",
report.warnings
);
assert!(
report.permutations_run >= 2,
"expected at least 2 permutations, got {}",
report.permutations_run
);
}
#[test]
fn report_display() {
let report = for_each_token_permutation(CompileTimePolicy::Warn, |_perm| {});
let display = format!("{report}");
assert!(
display.contains("permutations run"),
"display should mention permutation count"
);
for w in &report.warnings {
assert!(display.contains(w), "display should include warning: {w}");
}
}