git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/cpuinfo/os_darwin_test.go (about)

     1  package cpuinfo
     2  
     3  import (
     4  	"os/exec"
     5  	"strconv"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  // Tests that the returned L1 instruction cache value matches the value returned from
    11  // a call to the OS `sysctl` utility. Skips the test if we can't run it.
    12  func TestDarwinL1ICache(t *testing.T) {
    13  	out, err := exec.Command("/usr/sbin/sysctl", "-n", "hw.l1icachesize").Output()
    14  	if err != nil {
    15  		t.Skipf("cannot run sysctl utility: %v", err)
    16  		return
    17  	}
    18  	v, err := strconv.ParseInt(strings.TrimSpace(string(out)), 10, 0)
    19  	if err != nil {
    20  		t.Skipf("sysctl output %q could not be parsed as int: %v", string(out), err)
    21  		return
    22  	}
    23  	if CPU.Cache.L1I != int(v) {
    24  		t.Fatalf("sysctl output %q did not match CPU.Cache.L1I %d", string(out), CPU.Cache.L1I)
    25  	}
    26  }
    27  
    28  // Tests that the returned L1 data cache value matches the value returned from a call
    29  // to the OS `sysctl` utility. Skips the test if we can't run it.
    30  func TestDarwinL1DCache(t *testing.T) {
    31  	out, err := exec.Command("/usr/sbin/sysctl", "-n", "hw.l1dcachesize").Output()
    32  	if err != nil {
    33  		t.Skipf("cannot run sysctl utility: %v", err)
    34  		return
    35  	}
    36  	v, err := strconv.ParseInt(strings.TrimSpace(string(out)), 10, 0)
    37  	if err != nil {
    38  		t.Skipf("sysctl output %q could not be parsed as int: %v", string(out), err)
    39  		return
    40  	}
    41  	if CPU.Cache.L1D != int(v) {
    42  		t.Fatalf("sysctl output %q did not match CPU.Cache.L1D %d", string(out), CPU.Cache.L1D)
    43  	}
    44  }