| 1 |
// Copyright 2017 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package cpu_test |
| 6 |
|
| 7 |
import ( |
| 8 |
. "internal/cpu" |
| 9 |
"internal/godebug" |
| 10 |
"internal/testenv" |
| 11 |
"os/exec" |
| 12 |
"runtime" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
func MustHaveDebugOptionsSupport(t *testing.T) { |
| 17 |
switch runtime.GOOS { |
| 18 |
case "aix", "darwin", "ios", "dragonfly", "freebsd", "netbsd", "openbsd", "illumos", "solaris", "linux": |
| 19 |
default: |
| 20 |
t.Skipf("skipping test: cpu feature options not supported by OS") |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
func MustSupportFeatureDetection(t *testing.T) { |
| 25 |
// TODO: add platforms that do not have CPU feature detection support. |
| 26 |
} |
| 27 |
|
| 28 |
func runDebugOptionsTest(t *testing.T, test string, options string) { |
| 29 |
MustHaveDebugOptionsSupport(t) |
| 30 |
|
| 31 |
env := "GODEBUG=" + options |
| 32 |
|
| 33 |
cmd := exec.Command(testenv.Executable(t), "-test.run=^"+test+"$") |
| 34 |
cmd.Env = append(cmd.Env, env) |
| 35 |
|
| 36 |
output, err := cmd.CombinedOutput() |
| 37 |
if err != nil { |
| 38 |
t.Fatalf("%s with %s: run failed: %v output:\n%s\n", |
| 39 |
test, env, err, string(output)) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestDisableAllCapabilities(t *testing.T) { |
| 44 |
MustSupportFeatureDetection(t) |
| 45 |
runDebugOptionsTest(t, "TestAllCapabilitiesDisabled", "cpu.all=off") |
| 46 |
} |
| 47 |
|
| 48 |
func TestAllCapabilitiesDisabled(t *testing.T) { |
| 49 |
MustHaveDebugOptionsSupport(t) |
| 50 |
|
| 51 |
if godebug.New("#cpu.all").Value() != "off" { |
| 52 |
t.Skipf("skipping test: GODEBUG=cpu.all=off not set") |
| 53 |
} |
| 54 |
|
| 55 |
for _, o := range Options { |
| 56 |
want := false |
| 57 |
if got := *o.Feature; got != want { |
| 58 |
t.Errorf("%v: expected %v, got %v", o.Name, want, got) |
| 59 |
} |
| 60 |
} |
| 61 |
} |