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

View as plain text