Source file src/internal/cpu/cpu_test.go

     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  	"testing"
    13  )
    14  
    15  func MustHaveDebugOptionsSupport(t *testing.T) {
    16  	if !DebugOptions {
    17  		t.Skipf("skipping test: cpu feature options not supported by OS")
    18  	}
    19  }
    20  
    21  func MustSupportFeatureDetection(t *testing.T) {
    22  	// TODO: add platforms that do not have CPU feature detection support.
    23  }
    24  
    25  func runDebugOptionsTest(t *testing.T, test string, options string) {
    26  	MustHaveDebugOptionsSupport(t)
    27  
    28  	env := "GODEBUG=" + options
    29  
    30  	cmd := exec.Command(testenv.Executable(t), "-test.run=^"+test+"$")
    31  	cmd.Env = append(cmd.Env, env)
    32  
    33  	output, err := cmd.CombinedOutput()
    34  	if err != nil {
    35  		t.Fatalf("%s with %s: run failed: %v output:\n%s\n",
    36  			test, env, err, string(output))
    37  	}
    38  }
    39  
    40  func TestDisableAllCapabilities(t *testing.T) {
    41  	MustSupportFeatureDetection(t)
    42  	runDebugOptionsTest(t, "TestAllCapabilitiesDisabled", "cpu.all=off")
    43  }
    44  
    45  func TestAllCapabilitiesDisabled(t *testing.T) {
    46  	MustHaveDebugOptionsSupport(t)
    47  
    48  	if godebug.New("#cpu.all").Value() != "off" {
    49  		t.Skipf("skipping test: GODEBUG=cpu.all=off not set")
    50  	}
    51  
    52  	for _, o := range Options {
    53  		want := false
    54  		if got := *o.Feature; got != want {
    55  			t.Errorf("%v: expected %v, got %v", o.Name, want, got)
    56  		}
    57  	}
    58  }
    59  

View as plain text