github.com/SandwichDev/go-internals@v0.0.0-20210605002614-12311ac6b2c5/cpu/cpu_s390x_test.go (about)

     1  // Copyright 2018 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  	"errors"
     9  	"os"
    10  	"regexp"
    11  	"testing"
    12  
    13  	. "github.com/SandwichDev/go-internals/cpu"
    14  )
    15  
    16  func getFeatureList() ([]string, error) {
    17  	cpuinfo, err := os.ReadFile("/proc/cpuinfo")
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	r := regexp.MustCompile("features\\s*:\\s*(.*)")
    22  	b := r.FindSubmatch(cpuinfo)
    23  	if len(b) < 2 {
    24  		return nil, errors.New("no feature list in /proc/cpuinfo")
    25  	}
    26  	return regexp.MustCompile("\\s+").Split(string(b[1]), -1), nil
    27  }
    28  
    29  func TestS390XAgainstCPUInfo(t *testing.T) {
    30  	// mapping of linux feature strings to S390X fields
    31  	mapping := make(map[string]*bool)
    32  	for _, option := range Options {
    33  		mapping[option.Name] = option.Feature
    34  	}
    35  
    36  	// these must be true on the machines Go supports
    37  	mandatory := make(map[string]bool)
    38  	mandatory["zarch"] = false
    39  	mandatory["eimm"] = false
    40  	mandatory["ldisp"] = false
    41  	mandatory["stfle"] = false
    42  
    43  	features, err := getFeatureList()
    44  	if err != nil {
    45  		t.Error(err)
    46  	}
    47  	for _, feature := range features {
    48  		if _, ok := mandatory[feature]; ok {
    49  			mandatory[feature] = true
    50  		}
    51  		if flag, ok := mapping[feature]; ok {
    52  			if !*flag {
    53  				t.Errorf("feature '%v' not detected", feature)
    54  			}
    55  		} else {
    56  			t.Logf("no entry for '%v'", feature)
    57  		}
    58  	}
    59  	for k, v := range mandatory {
    60  		if !v {
    61  			t.Errorf("mandatory feature '%v' not detected", k)
    62  		}
    63  	}
    64  }