Black Lives Matter. Support the Equal Justice Initiative.

Source file src/internal/cpu/cpu_test.go

Documentation: internal/cpu

     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/testenv"
    10  	"os"
    11  	"os/exec"
    12  	"runtime"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  func TestMinimalFeatures(t *testing.T) {
    18  	// TODO: maybe do MustSupportFeatureDectection(t) ?
    19  	if runtime.GOARCH == "arm64" {
    20  		switch runtime.GOOS {
    21  		case "linux", "android", "darwin":
    22  		default:
    23  			t.Skipf("%s/%s is not supported", runtime.GOOS, runtime.GOARCH)
    24  		}
    25  	}
    26  
    27  	for _, o := range Options {
    28  		if o.Required && !*o.Feature {
    29  			t.Errorf("%v expected true, got false", o.Name)
    30  		}
    31  	}
    32  }
    33  
    34  func MustHaveDebugOptionsSupport(t *testing.T) {
    35  	if !DebugOptions {
    36  		t.Skipf("skipping test: cpu feature options not supported by OS")
    37  	}
    38  }
    39  
    40  func MustSupportFeatureDectection(t *testing.T) {
    41  	// TODO: add platforms that do not have CPU feature detection support.
    42  }
    43  
    44  func runDebugOptionsTest(t *testing.T, test string, options string) {
    45  	MustHaveDebugOptionsSupport(t)
    46  
    47  	testenv.MustHaveExec(t)
    48  
    49  	env := "GODEBUG=" + options
    50  
    51  	cmd := exec.Command(os.Args[0], "-test.run="+test)
    52  	cmd.Env = append(cmd.Env, env)
    53  
    54  	output, err := cmd.CombinedOutput()
    55  	lines := strings.Fields(string(output))
    56  	lastline := lines[len(lines)-1]
    57  
    58  	got := strings.TrimSpace(lastline)
    59  	want := "PASS"
    60  	if err != nil || got != want {
    61  		t.Fatalf("%s with %s: want %s, got %v", test, env, want, got)
    62  	}
    63  }
    64  
    65  func TestDisableAllCapabilities(t *testing.T) {
    66  	MustSupportFeatureDectection(t)
    67  	runDebugOptionsTest(t, "TestAllCapabilitiesDisabled", "cpu.all=off")
    68  }
    69  
    70  func TestAllCapabilitiesDisabled(t *testing.T) {
    71  	MustHaveDebugOptionsSupport(t)
    72  
    73  	if os.Getenv("GODEBUG") != "cpu.all=off" {
    74  		t.Skipf("skipping test: GODEBUG=cpu.all=off not set")
    75  	}
    76  
    77  	for _, o := range Options {
    78  		want := o.Required
    79  		if got := *o.Feature; got != want {
    80  			t.Errorf("%v: expected %v, got %v", o.Name, want, got)
    81  		}
    82  	}
    83  }
    84  

View as plain text