github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/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  	"strings"
    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 runDebugOptionsTest(t *testing.T, test string, options string) {
    23  	MustHaveDebugOptionsSupport(t)
    24  
    25  	testenv.MustHaveExec(t)
    26  
    27  	env := "GODEBUGCPU=" + options
    28  
    29  	cmd := exec.Command(os.Args[0], "-test.run="+test)
    30  	cmd.Env = append(cmd.Env, env)
    31  
    32  	output, err := cmd.CombinedOutput()
    33  	lines := strings.Fields(string(output))
    34  	lastline := lines[len(lines)-1]
    35  
    36  	got := strings.TrimSpace(lastline)
    37  	want := "PASS"
    38  	if err != nil || got != want {
    39  		t.Fatalf("%s with %s: want %s, got %v", test, env, want, got)
    40  	}
    41  }
    42  
    43  func TestDisableAllCapabilities(t *testing.T) {
    44  	runDebugOptionsTest(t, "TestAllCapabilitiesDisabled", "all=off")
    45  }
    46  
    47  func TestAllCapabilitiesDisabled(t *testing.T) {
    48  	MustHaveDebugOptionsSupport(t)
    49  
    50  	if os.Getenv("GODEBUGCPU") != "all=off" {
    51  		t.Skipf("skipping test: GODEBUGCPU=all=off not set")
    52  	}
    53  
    54  	for _, o := range Options {
    55  		want := o.Required
    56  		if got := *o.Feature; got != want {
    57  			t.Errorf("%v: expected %v, got %v", o.Name, want, got)
    58  		}
    59  	}
    60  }