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