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