File: uuid_test.go

package info (click to toggle)
golang-github-rogpeppe-fastuuid 0.0~git20150106.0.6724a57-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 60 kB
  • sloc: makefile: 2
file content (50 lines) | stat: -rw-r--r-- 931 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package fastuuid

import (
	"bytes"
	"crypto/rand"
	"testing"
)

func TestUUID(t *testing.T) {
	var buf [24]byte
	for i := range buf {
		buf[i] = byte(i) + 1
	}
	oldReader := rand.Reader
	rand.Reader = bytes.NewReader(buf[:])
	g, err := NewGenerator()
	rand.Reader = oldReader
	if err != nil {
		t.Fatalf("cannot make generator: %v", err)
	}
	uuid := g.Next()
	buf[0] = 1 ^ 1
	if uuid != buf {
		t.Fatalf("unexpected UUID; got %x; want %x", uuid, buf)
	}
	uuid = g.Next()
	buf[0] = 1 ^ 2
	if uuid != buf {
		t.Fatalf("unexpected next UUID; got %x; want %x", uuid, buf)
	}
}

func TestUniqueness(t *testing.T) {
	g := MustNewGenerator()
	m := make(map[[24]byte]int)
	for i := 0; i < 65537; i++ {
		uuid := g.Next()
		if old, ok := m[uuid]; ok {
			t.Fatalf("non-unique uuid seq at %d, other %d", i, old)
		}
		m[uuid] = i
	}
}

func BenchmarkNext(b *testing.B) {
	g := MustNewGenerator()
	for i := 0; i < b.N; i++ {
		g.Next()
	}
}