github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/internal/linkname/runtime_test.go (about)

     1  package linkname
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func compileRuntimeFunctions() {
    10  	call(Runtime_fastrand)
    11  	call(Runtime_fastrand64)
    12  	call(Runtime_memhash32)
    13  	call(Runtime_memhash64)
    14  	call(Runtime_stringHash)
    15  	call(Runtime_bytesHash)
    16  	call(Runtime_efaceHash)
    17  	call(Runtime_typehash)
    18  	call(Runtime_activeModules)
    19  }
    20  
    21  func TestRuntime_fastrand(t *testing.T) {
    22  	var sum uint32
    23  	for i := 0; i < 10; i++ {
    24  		sum += Runtime_fastrand()
    25  	}
    26  	if sum == 0 {
    27  		t.Errorf("fastrand got all zero values")
    28  	}
    29  }
    30  
    31  func TestRuntime_fastrand64(t *testing.T) {
    32  	var sum uint64
    33  	for i := 0; i < 10; i++ {
    34  		sum += Runtime_fastrand64()
    35  	}
    36  	if sum == 0 {
    37  		t.Errorf("fastrand64 got all zero values")
    38  	}
    39  }
    40  
    41  func TestPid(t *testing.T) {
    42  	var wg sync.WaitGroup
    43  	for i := 0; i < 15; i++ {
    44  		wg.Add(1)
    45  		go func() {
    46  			defer wg.Done()
    47  			time.Sleep(10 * time.Millisecond)
    48  			pid := Pid()
    49  			t.Logf("Pid got %d", pid)
    50  		}()
    51  	}
    52  	wg.Wait()
    53  }
    54  
    55  var runtimeSourceCode = []SourceCodeTestCase{
    56  	{
    57  		MaxVer:   newVer(1, 21, 999),
    58  		FileName: "runtime/stubs.go",
    59  		Lines: []string{
    60  			"func fastrand() uint32",
    61  			"func fastrand64() uint64",
    62  		},
    63  	},
    64  	{
    65  		MinVer:   newVer(1, 22, 0),
    66  		FileName: "runtime/rand.go",
    67  		Lines: []string{
    68  			"func rand() uint64",
    69  		},
    70  	},
    71  	{
    72  		FileName: "runtime/proc.go",
    73  		Lines: []string{
    74  			"func procPin() int",
    75  			"func procUnpin()",
    76  		},
    77  	},
    78  	{
    79  		FileName: "runtime/alg.go",
    80  		Lines: []string{
    81  			"func memhash32(p unsafe.Pointer, h uintptr) uintptr",
    82  			"func memhash64(p unsafe.Pointer, h uintptr) uintptr",
    83  			"func stringHash(s string, seed uintptr) uintptr",
    84  			"func bytesHash(b []byte, seed uintptr) uintptr",
    85  			"func efaceHash(i any, seed uintptr) uintptr",
    86  			"func typehash(t *_type, p unsafe.Pointer, h uintptr) uintptr",
    87  		},
    88  	},
    89  	{
    90  		FileName: "runtime/symtab.go",
    91  		Lines: []string{
    92  			"func activeModules() []*moduledata",
    93  		},
    94  	},
    95  }