github.com/JimmyHuang454/JLS-go@v0.0.0-20230831150107-90d536585ba0/internal/cpu/cpu_test.go (about)

     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"
    12  	"os/exec"
    13  	"testing"
    14  )
    15  
    16  func MustHaveDebugOptionsSupport(t *testing.T) {
    17  	if !DebugOptions {
    18  		t.Skipf("skipping test: cpu feature options not supported by OS")
    19  	}
    20  }
    21  
    22  func MustSupportFeatureDetection(t *testing.T) {
    23  	// TODO: add platforms that do not have CPU feature detection support.
    24  }
    25  
    26  func runDebugOptionsTest(t *testing.T, test string, options string) {
    27  	MustHaveDebugOptionsSupport(t)
    28  
    29  	testenv.MustHaveExec(t)
    30  
    31  	env := "GODEBUG=" + options
    32  
    33  	cmd := exec.Command(os.Args[0], "-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  }