package platform import ( "errors" "runtime" "testing" "github.com/klauspost/cpuid/v2" ) func TestCheckCPUFeatures(t *testing.T) { tests := []struct { name string features []string avx2 bool avx512 bool neon bool }{ {"empty", nil, false, false, false}, {"avx2 present", []string{"sse4_2", "avx2"}, true, false, false}, {"avx512 present", []string{"avx2", "avx512f"}, true, true, false}, {"neon present", []string{"arm64", "neon"}, false, false, true}, {"linux arm64 asimd is neon", []string{"asimd"}, false, false, true}, {"case insensitive", []string{"AVX2", "NEON"}, true, false, true}, {"avx1 is not avx2", []string{"avx", "sse"}, false, false, false}, {"avx512 implies avx512 flag check only", []string{"avx512dq"}, false, true, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := checkAVX2(tt.features); got != tt.avx2 { t.Errorf("checkAVX2(%v) = %v, want %v", tt.features, got, tt.avx2) } if got := checkAVX512(tt.features); got != tt.avx512 { t.Errorf("checkAVX512(%v) = %v, want %v", tt.features, got, tt.avx512) } if got := checkNEON(tt.features); got != tt.neon { t.Errorf("checkNEON(%v) = %v, want %v", tt.features, got, tt.neon) } }) } } func TestGetVendorDisplay(t *testing.T) { tests := []struct { vendor string want string }{ {"GenuineIntel", "Intel"}, {"AuthenticAMD", "AMD"}, {"nvidia", "NVIDIA"}, {"amd", "AMD"}, {"intel", "Intel"}, {"apple", "Apple"}, {"VIA", "VIA"}, // unknown vendors pass through unchanged {"", ""}, } for _, tt := range tests { t.Run(tt.vendor, func(t *testing.T) { if got := getVendorDisplay(tt.vendor); got != tt.want { t.Errorf("getVendorDisplay(%q) = %q, want %q", tt.vendor, got, tt.want) } }) } } // An AVX2-only feature view must never select an x86-64-v3 executable. func TestX64BaselineSelection(t *testing.T) { required := []cpuid.FeatureID{cpuid.CMOV, cpuid.CMPXCHG8, cpuid.X87, cpuid.FXSR, cpuid.MMX, cpuid.SYSCALL, cpuid.SSE, cpuid.SSE2, cpuid.CX16, cpuid.LAHF, cpuid.POPCNT, cpuid.SSE3, cpuid.SSE4, cpuid.SSE42, cpuid.SSSE3, cpuid.AVX, cpuid.AVX2, cpuid.BMI1, cpuid.BMI2, cpuid.F16C, cpuid.FMA3, cpuid.LZCNT, cpuid.MOVBE, cpuid.OSXSAVE} cpu := cpuid.CPUInfo{} cpu.Enable(required...) if got := x64ArchitectureLevel(cpu); got != "x86-64-v3" { t.Fatalf("complete baseline = %s", got) } for _, feature := range required { t.Run(feature.String(), func(t *testing.T) { masked := cpu masked.Disable(feature) p := Platform{Arch: "amd64", CPUInfo: CPUInfo{ ArchitectureLevel: x64ArchitectureLevel(masked), SupportsAVX2: masked.Supports(cpuid.AVX2), }} if p.SupportsX86_64_v3() { t.Fatalf("incomplete %s baseline selected v3", p.CPUInfo.ArchitectureLevel) } }) } cpu.Enable(cpuid.AVX512F, cpuid.AVX512BW, cpuid.AVX512CD, cpuid.AVX512DQ, cpuid.AVX512VL) if got := x64ArchitectureLevel(cpu); got != "x86-64-v4" { t.Fatalf("complete v4 baseline = %s", got) } } func TestDetectCPULinux(t *testing.T) { if runtime.GOOS != "linux" { t.Skip("Linux-only detection path") } info := detectCPULinux() // /proc/cpuinfo is always present on Linux; on x86 it carries a vendor and // a model name, on any arch it should at least not panic. if runtime.GOARCH == "amd64" { if info.Vendor == "" { t.Error("detectCPULinux() returned empty vendor on amd64 Linux") } if info.Model == "" { t.Error("detectCPULinux() returned empty model on amd64 Linux") } if len(info.Features) == 0 { t.Error("detectCPULinux() returned no features on amd64 Linux") } } } func TestDetectCPU(t *testing.T) { if runtime.GOOS != "linux" { t.Skip("assertions target the Linux detection path") } info := detectCPU() if info.VendorDisplay == "" && info.Vendor != "" { t.Error("detectCPU() must populate VendorDisplay") } if info.ArchitectureLevel == "" { t.Error("detectCPU() must populate ArchitectureLevel") } // The vendor must be normalized to the display form. if info.Vendor != info.VendorDisplay { t.Errorf("detectCPU() vendor %q not normalized to display form %q", info.Vendor, info.VendorDisplay) } // Feature flags must agree with the parsed feature list. if info.SupportsAVX2 != checkAVX2(info.Features) { t.Error("detectCPU() SupportsAVX2 disagrees with parsed features") } } func TestDetectCPUDarwinWithRunner(t *testing.T) { if runtime.GOOS == "darwin" { t.Skip("covered natively on darwin") } t.Run("sysctl output sets the model", func(t *testing.T) { runner := &fakeProbeRunner{responses: map[string][]fakeProbeResponse{ "sysctl": {{output: "Apple M4 Pro\n"}}, }} info := detectCPUDarwinWithRunner(runner) if info.Vendor != "Apple" { t.Errorf("Vendor = %q, want Apple", info.Vendor) } if info.Model != "Apple M4 Pro" { t.Errorf("Model = %q, want Apple M4 Pro", info.Model) } if len(info.Features) == 0 { t.Error("Features must be populated for the architecture") } }) t.Run("sysctl failure falls back to architecture defaults", func(t *testing.T) { runner := &fakeProbeRunner{responses: map[string][]fakeProbeResponse{ "sysctl": {{err: errors.New("no sysctl")}}, }} info := detectCPUDarwinWithRunner(runner) wantModel := "Intel" if runtime.GOARCH == "arm64" { wantModel = "Apple Silicon" } if info.Model != wantModel { t.Errorf("Model = %q, want %q", info.Model, wantModel) } }) } func TestSupportsX86_64_v3(t *testing.T) { tests := []struct { name string p Platform want bool }{ {"amd64 with avx2", Platform{Arch: "amd64", CPUInfo: CPUInfo{SupportsAVX2: true}}, false}, {"x86_64 alias with avx2", Platform{Arch: "x86_64", CPUInfo: CPUInfo{SupportsAVX2: true}}, false}, {"amd64 full v3", Platform{Arch: "amd64", CPUInfo: CPUInfo{ArchitectureLevel: "x86-64-v3"}}, true}, {"amd64 full v4", Platform{Arch: "amd64", CPUInfo: CPUInfo{ArchitectureLevel: "x86-64-v4"}}, true}, {"x86_64 full v3", Platform{Arch: "x86_64", CPUInfo: CPUInfo{ArchitectureLevel: "x86-64-v3"}}, true}, {"amd64 v2 with AVX2", Platform{Arch: "amd64", CPUInfo: CPUInfo{ArchitectureLevel: "x86-64-v2", SupportsAVX2: true}}, false}, {"amd64 without avx2", Platform{Arch: "amd64", CPUInfo: CPUInfo{}}, false}, {"arm64 never", Platform{Arch: "arm64", CPUInfo: CPUInfo{SupportsAVX2: true, SupportsNEON: true}}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := tt.p.SupportsX86_64_v3(); got != tt.want { t.Errorf("SupportsX86_64_v3() = %v, want %v", got, tt.want) } }) } } func TestDetectCPUWindowsStub(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("real implementation on Windows") } // The non-Windows stub compiles everywhere and returns an empty CPUInfo. info := detectCPUWindows() if len(info.Features) != 0 { t.Errorf("detectCPUWindows() stub Features = %v, want empty", info.Features) } }