#![allow(dead_code, unused_variables, clippy::assertions_on_constants)]
#[cfg(target_arch = "x86_64")]
mod x86_stub_tests {
#[cfg(target_feature = "simd128")]
use archmage::Wasm128Token;
use archmage::{NeonAesToken, NeonCrcToken, NeonSha3Token, NeonToken, SimdToken};
#[test]
fn neon_token_is_stub_on_x86() {
assert!(
NeonToken::summon().is_none(),
"NeonToken::summon() should return None on x86_64 (stub)"
);
}
#[test]
fn neon_aes_token_is_stub_on_x86() {
assert!(
NeonAesToken::summon().is_none(),
"NeonAesToken::summon() should return None on x86_64 (stub)"
);
}
#[test]
fn neon_sha3_token_is_stub_on_x86() {
assert!(
NeonSha3Token::summon().is_none(),
"NeonSha3Token::summon() should return None on x86_64 (stub)"
);
}
#[test]
fn neon_crc_token_is_stub_on_x86() {
assert!(
NeonCrcToken::summon().is_none(),
"NeonCrcToken::summon() should return None on x86_64 (stub)"
);
}
}
#[cfg(target_arch = "aarch64")]
mod arm_stub_tests {
#[cfg(feature = "avx512")]
use archmage::{Avx512Fp16Token, X64V4Token, X64V4xToken};
use archmage::{SimdToken, X64V2Token, X64V3Token};
#[test]
fn x64v2_token_is_stub_on_arm() {
assert!(
X64V2Token::summon().is_none(),
"X64V2Token::summon() should return None on aarch64 (stub)"
);
}
#[test]
fn x64v3_token_is_stub_on_arm() {
assert!(
X64V3Token::summon().is_none(),
"X64V3Token::summon() should return None on aarch64 (stub)"
);
}
#[cfg(feature = "avx512")]
#[test]
fn x64v4_token_is_stub_on_arm() {
assert!(
X64V4Token::summon().is_none(),
"X64V4Token::summon() should return None on aarch64 (stub)"
);
}
}
#[cfg(target_arch = "aarch64")]
mod arm_real_tests {
use archmage::{NeonToken, SimdToken};
#[test]
fn neon_token_is_real_on_arm() {
assert!(
NeonToken::summon().is_some(),
"NeonToken::summon() should return Some on aarch64 (NEON is baseline)"
);
}
}
#[cfg(all(target_arch = "x86_64", feature = "avx512"))]
mod avx512_feature_tests {
use archmage::{Avx512Fp16Token, SimdToken, X64V4Token, X64V4xToken};
#[test]
fn avx512_tokens_exist_with_feature() {
let _v4: Option<X64V4Token> = X64V4Token::summon();
let _v4x: Option<X64V4xToken> = X64V4xToken::summon();
let _fp16: Option<Avx512Fp16Token> = Avx512Fp16Token::summon();
}
#[test]
fn avx512_token_hierarchy() {
if let Some(v4) = X64V4Token::summon() {
let v3 = v4.v3();
let v2 = v3.v2();
let _ = v2;
}
}
}
#[cfg(target_arch = "x86_64")]
const IS_X86: bool = true;
#[cfg(not(target_arch = "x86_64"))]
const IS_X86: bool = false;
#[cfg(target_arch = "aarch64")]
const IS_ARM: bool = true;
#[cfg(not(target_arch = "aarch64"))]
const IS_ARM: bool = false;
#[cfg(target_arch = "wasm32")]
const IS_WASM: bool = true;
#[cfg(not(target_arch = "wasm32"))]
const IS_WASM: bool = false;
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "wasm32"
))]
const _: () = {
let count = IS_X86 as u8 + IS_ARM as u8 + IS_WASM as u8;
assert!(count == 1, "Exactly one platform const should be true");
};
#[test]
fn platform_consts_are_correct() {
#[cfg(target_arch = "x86_64")]
{
assert!(IS_X86);
assert!(!IS_ARM);
assert!(!IS_WASM);
}
#[cfg(target_arch = "aarch64")]
{
assert!(!IS_X86);
assert!(IS_ARM);
assert!(!IS_WASM);
}
#[cfg(target_arch = "wasm32")]
{
assert!(!IS_X86);
assert!(!IS_ARM);
assert!(IS_WASM);
}
}
#[cfg(target_arch = "x86_64")]
static PLATFORM_NAME: &str = "x86_64";
#[cfg(target_arch = "aarch64")]
static PLATFORM_NAME: &str = "aarch64";
#[cfg(target_arch = "wasm32")]
static PLATFORM_NAME: &str = "wasm32";
#[cfg(not(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "wasm32"
)))]
static PLATFORM_NAME: &str = "unknown";
#[test]
fn platform_name_matches_cfg() {
#[cfg(target_arch = "x86_64")]
assert_eq!(PLATFORM_NAME, "x86_64");
#[cfg(target_arch = "aarch64")]
assert_eq!(PLATFORM_NAME, "aarch64");
#[cfg(target_arch = "wasm32")]
assert_eq!(PLATFORM_NAME, "wasm32");
}
fn get_simd_width() -> usize {
#[cfg(target_arch = "x86_64")]
{
#[cfg(feature = "avx512")]
return 512;
#[cfg(not(feature = "avx512"))]
return 256;
}
#[cfg(target_arch = "aarch64")]
{
return 128;
}
#[cfg(target_arch = "wasm32")]
{
return 128;
}
#[cfg(not(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "wasm32"
)))]
{
return 0; }
}
#[test]
fn simd_width_is_correct_for_platform() {
let width = get_simd_width();
#[cfg(all(target_arch = "x86_64", feature = "avx512"))]
assert_eq!(
width, 512,
"x86_64 with avx512 feature should report 512-bit"
);
#[cfg(all(target_arch = "x86_64", not(feature = "avx512")))]
assert_eq!(
width, 256,
"x86_64 without avx512 feature should report 256-bit"
);
#[cfg(target_arch = "aarch64")]
assert_eq!(width, 128, "aarch64 should report 128-bit");
#[cfg(target_arch = "wasm32")]
assert_eq!(width, 128, "wasm32 should report 128-bit");
}
#[cfg(target_arch = "x86_64")]
mod x86_type_tests {
use archmage::{Desktop64, X64V3Token};
use core::any::type_name;
#[test]
fn desktop64_is_x64v3token() {
assert_eq!(
type_name::<Desktop64>(),
type_name::<X64V3Token>(),
"Desktop64 should be an alias for X64V3Token"
);
}
}
#[cfg(target_arch = "aarch64")]
mod arm_type_tests {
use archmage::{Arm64, NeonToken};
use core::any::type_name;
#[test]
fn arm64_is_neon_token() {
assert_eq!(
type_name::<Arm64>(),
type_name::<NeonToken>(),
"Arm64 should be an alias for NeonToken"
);
}
}
#[cfg(target_arch = "x86_64")]
mod runtime_vs_compiletime {
use archmage::{SimdToken, X64V3Token};
#[test]
fn cfg_vs_runtime_detection() {
let runtime_available = X64V3Token::summon().is_some();
println!(
"X64V3Token (AVX2+FMA) runtime available: {}",
runtime_available
);
let _ = runtime_available;
}
const COMPILED_FOR_X86: bool = cfg!(target_arch = "x86_64");
#[test]
fn cfg_macro_is_compile_time() {
assert!(
COMPILED_FOR_X86,
"cfg!(target_arch = \"x86_64\") should be true"
);
const HAS_AVX512_FEATURE: bool = cfg!(feature = "avx512");
#[cfg(feature = "avx512")]
assert!(HAS_AVX512_FEATURE);
#[cfg(not(feature = "avx512"))]
assert!(!HAS_AVX512_FEATURE);
}
}
#[cfg(target_arch = "x86_64")]
mod x86_only_module {
pub const VALUE: u32 = 0x86;
}
#[cfg(target_arch = "aarch64")]
mod arm_only_module {
pub const VALUE: u32 = 0xA4AA; }
#[test]
fn module_elision_works() {
#[cfg(target_arch = "x86_64")]
assert_eq!(x86_only_module::VALUE, 0x86);
#[cfg(target_arch = "aarch64")]
assert_eq!(arm_only_module::VALUE, 0xA4AA);
}
#[cfg(target_arch = "x86_64")]
fn platform_specific_fn() -> &'static str {
"x86_64"
}
#[cfg(target_arch = "aarch64")]
fn platform_specific_fn() -> &'static str {
"aarch64"
}
#[cfg(target_arch = "wasm32")]
fn platform_specific_fn() -> &'static str {
"wasm32"
}
#[cfg(not(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "wasm32"
)))]
fn platform_specific_fn() -> &'static str {
"unsupported"
}
#[test]
fn function_elision_selects_right_impl() {
let result = platform_specific_fn();
#[cfg(target_arch = "x86_64")]
assert_eq!(result, "x86_64");
#[cfg(target_arch = "aarch64")]
assert_eq!(result, "aarch64");
#[cfg(target_arch = "wasm32")]
assert_eq!(result, "wasm32");
}
const AVX2_COMPILETIME: bool = cfg!(target_feature = "avx2");
const AVX512_COMPILETIME: bool = cfg!(target_feature = "avx512f");
const NEON_COMPILETIME: bool = cfg!(target_feature = "neon");
#[test]
fn detect_compiletime_features() {
println!("Compile-time AVX2: {}", AVX2_COMPILETIME);
println!("Compile-time AVX-512: {}", AVX512_COMPILETIME);
println!("Compile-time NEON: {}", NEON_COMPILETIME);
#[cfg(target_arch = "aarch64")]
assert!(NEON_COMPILETIME, "NEON should be compile-time on aarch64");
}
#[cfg(target_arch = "x86_64")]
mod compiletime_dispatch_elision {
use archmage::{SimdToken, X64V3Token};
pub fn sum_with_elision(data: &[f32]) -> f32 {
#[cfg(target_feature = "avx2")]
{
let token = X64V3Token::summon().unwrap();
return sum_avx2(token, data);
}
#[cfg(not(target_feature = "avx2"))]
{
if let Some(token) = X64V3Token::summon() {
return sum_avx2(token, data);
}
sum_scalar(data)
}
}
#[archmage::arcane]
fn sum_avx2(_token: X64V3Token, data: &[f32]) -> f32 {
data.iter().sum()
}
fn sum_scalar(data: &[f32]) -> f32 {
data.iter().sum()
}
#[test]
fn test_compiletime_dispatch() {
let data = [1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
let result = sum_with_elision(&data);
assert_eq!(result, 36.0);
}
}
#[test]
fn cfg_macro_detects_target_features() {
let has_sse2 = cfg!(target_feature = "sse2");
let has_avx = cfg!(target_feature = "avx");
let has_avx2 = cfg!(target_feature = "avx2");
let has_fma = cfg!(target_feature = "fma");
let has_neon = cfg!(target_feature = "neon");
#[cfg(target_arch = "x86_64")]
{
assert!(has_sse2, "SSE2 should be compile-time on x86_64");
println!("Compile-time AVX: {}", has_avx);
println!("Compile-time AVX2: {}", has_avx2);
println!("Compile-time FMA: {}", has_fma);
}
#[cfg(target_arch = "aarch64")]
{
assert!(has_neon, "NEON should be compile-time on aarch64");
}
}
#[cfg(all(target_arch = "x86_64", not(target_feature = "avx2")))]
static SCALAR_VARIANT_EXISTS: bool = true;
#[cfg(all(target_arch = "x86_64", target_feature = "avx2"))]
static AVX2_COMPILETIME_PATH: bool = true;
#[cfg(target_arch = "x86_64")]
#[test]
fn verify_elision_markers() {
#[cfg(target_feature = "avx2")]
{
assert!(AVX2_COMPILETIME_PATH);
}
#[cfg(not(target_feature = "avx2"))]
{
assert!(SCALAR_VARIANT_EXISTS);
}
}
#[cfg(target_arch = "x86_64")]
mod compiled_with_tests_x86 {
#[cfg(feature = "avx512")]
use archmage::X64V4Token;
use archmage::{NeonToken, SimdToken, X64V2Token, X64V3Token};
#[test]
fn neon_compiled_with_returns_false_on_x86() {
assert_eq!(
NeonToken::compiled_with(),
Some(false),
"NeonToken::compiled_with() should be Some(false) on x86_64"
);
}
#[test]
fn x64v2_compiled_with_is_not_false() {
assert_ne!(
X64V2Token::compiled_with(),
Some(false),
"X64V2Token::compiled_with() should NOT be Some(false) on x86_64"
);
}
#[test]
fn x64v3_compiled_with_is_not_false() {
assert_ne!(
X64V3Token::compiled_with(),
Some(false),
"X64V3Token::compiled_with() should NOT be Some(false) on x86_64"
);
}
#[test]
#[cfg(feature = "avx512")]
fn x64v4_compiled_with_is_not_false() {
assert_ne!(
X64V4Token::compiled_with(),
Some(false),
"X64V4Token::compiled_with() should NOT be Some(false) on x86_64"
);
}
#[test]
#[cfg(target_feature = "avx2")]
fn summon_unwrap_safe_when_compiled_with_true() {
assert_eq!(
X64V3Token::compiled_with(),
Some(true),
"X64V3Token::compiled_with() should be Some(true) when target_feature=avx2"
);
let _token = X64V3Token::summon().unwrap();
}
}
#[cfg(target_arch = "aarch64")]
mod compiled_with_tests_arm {
use archmage::{NeonToken, SimdToken, X64V2Token, X64V3Token};
#[test]
fn x86_compiled_with_returns_false_on_arm() {
assert_eq!(
X64V2Token::compiled_with(),
Some(false),
"X64V2Token::compiled_with() should be Some(false) on aarch64"
);
assert_eq!(
X64V3Token::compiled_with(),
Some(false),
"X64V3Token::compiled_with() should be Some(false) on aarch64"
);
}
#[test]
fn neon_compiled_with_on_arm() {
let result = NeonToken::compiled_with();
assert_ne!(
result,
Some(false),
"NeonToken::compiled_with() should NOT be Some(false) on aarch64"
);
}
}