...
Run Format

Source file src/internal/cpu/cpu_test.go

Documentation: internal/cpu

     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 MustHaveDebugOptionsEnabled(t *testing.T) {
    17  	if !DebugOptions {
    18  		t.Skipf("skipping test: cpu feature options not enabled")
    19  	}
    20  }
    21  
    22  func runDebugOptionsTest(t *testing.T, test string, options string) {
    23  	MustHaveDebugOptionsEnabled(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  	got := strings.TrimSpace(string(output))
    34  	want := "PASS"
    35  	if err != nil || got != want {
    36  		t.Fatalf("%s with %s: want %s, got %v", test, env, want, got)
    37  	}
    38  }
    39  
    40  func TestDisableAllCapabilities(t *testing.T) {
    41  	runDebugOptionsTest(t, "TestAllCapabilitiesDisabled", "all=0")
    42  }
    43  
    44  func TestAllCapabilitiesDisabled(t *testing.T) {
    45  	MustHaveDebugOptionsEnabled(t)
    46  
    47  	if os.Getenv("GODEBUGCPU") != "all=0" {
    48  		t.Skipf("skipping test: GODEBUGCPU=all=0 not set")
    49  	}
    50  
    51  	for _, o := range Options {
    52  		if got := *o.Feature; got != false {
    53  			t.Errorf("%v: expected false, got %v", o.Name, got)
    54  		}
    55  	}
    56  }
    57  

View as plain text