git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/guid/guid_test.go (about) 1 package guid 2 3 import ( 4 "fmt" 5 "runtime" 6 "testing" 7 ) 8 9 func TestNewTimeBased(t *testing.T) { 10 for i := 0; i < 10000; i += 1 { 11 id := NewTimeBased() 12 if id.Equal(Empty) { 13 t.Error("GUID is empty") 14 } 15 } 16 } 17 18 func TestNewRandom(t *testing.T) { 19 for i := 0; i < 10000; i += 1 { 20 id := NewRandom() 21 if id.Equal(Empty) { 22 t.Error("GUID is empty") 23 } 24 } 25 } 26 27 func TestEqual(t *testing.T) { 28 for i := 0; i < 10000; i += 1 { 29 id := NewRandom() 30 id2 := NewRandom() 31 32 if id.Equal(Empty) { 33 t.Error("GUID is empty") 34 } 35 36 if !id.Equal(id) { 37 t.Error("Equal(GUID) == false") 38 } 39 40 if id.Equal(id2) { 41 t.Error("Equal(GUID_2) == true") 42 } 43 } 44 } 45 46 func TestParse(t *testing.T) { 47 for i := 0; i < 10000; i += 1 { 48 id := NewRandom() 49 parsed, err := Parse(id.String()) 50 if err != nil { 51 t.Errorf("parsing GUID: %s", err) 52 } 53 if !id.Equal(parsed) { 54 t.Errorf("parsed (%s) != original GUID (%s)", parsed.String(), id.String()) 55 } 56 } 57 } 58 59 func BenchmarkNewRandomPool(b *testing.B) { 60 numberOfGoroutines := []int{ 61 1, 62 10, 63 500, 64 1000, 65 2000, 66 } 67 goMaxProc := runtime.GOMAXPROCS(0) 68 for _, n := range numberOfGoroutines { 69 b.Run(fmt.Sprintf("goroutines-%d", n*goMaxProc), func(b *testing.B) { 70 b.SetParallelism(n) 71 b.RunParallel(func(pb *testing.PB) { 72 for pb.Next() { 73 id, err := newRandomFromPool() 74 if err != nil { 75 b.Error(err) 76 } 77 _ = id 78 } 79 }) 80 }) 81 } 82 } 83 84 func BenchmarkNewRandomReader(b *testing.B) { 85 numberOfGoroutines := []int{ 86 10, 87 500, 88 1000, 89 2000, 90 10_000, 91 } 92 goMaxProc := runtime.GOMAXPROCS(0) 93 for _, n := range numberOfGoroutines { 94 b.Run(fmt.Sprintf("goroutines-%d", n*goMaxProc), func(b *testing.B) { 95 b.SetParallelism(n) 96 b.RunParallel(func(pb *testing.PB) { 97 for pb.Next() { 98 id, err := newRandomFromReader(randomSource) 99 if err != nil { 100 b.Error(err) 101 } 102 _ = id 103 } 104 }) 105 }) 106 } 107 }