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