File: math_test.go

package info (click to toggle)
golang-github-pion-randutil 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, experimental, forky, sid, trixie
  • size: 144 kB
  • sloc: makefile: 2
file content (90 lines) | stat: -rw-r--r-- 1,660 bytes parent folder | download
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package randutil

import (
	"regexp"
	"testing"
)

func TestMathRandomGenerator(t *testing.T) {
	g := NewMathRandomGenerator()
	isLetter := regexp.MustCompile(`^[a-zA-Z]+$`).MatchString

	for i := 0; i < 10000; i++ {
		s := g.GenerateString(10, runesAlpha)
		if len(s) != 10 {
			t.Error("Generator returned invalid length")
		}
		if !isLetter(s) {
			t.Errorf("Generator returned unexpected character: %s", s)
		}
	}
}

func TestIntn(t *testing.T) {
	g := NewMathRandomGenerator()

	min := 100
	max := 0
	for i := 0; i < 10000; i++ {
		r := g.Intn(100)
		if r < 0 || r >= 100 {
			t.Fatalf("Out of range of Intn(100): %d", r)
		}
		if r < min {
			min = r
		}
		if r > max {
			max = r
		}
	}
	if min > 10 {
		t.Error("Value around lower boundary was not generated")
	}
	if max < 90 {
		t.Error("Value around upper boundary was not generated")
	}
}

func TestUint64(t *testing.T) {
	g := NewMathRandomGenerator()

	min := uint64(0xFFFFFFFFFFFFFFFF)
	max := uint64(0)
	for i := 0; i < 10000; i++ {
		r := g.Uint64()
		if r < min {
			min = r
		}
		if r > max {
			max = r
		}
	}
	if min > 0x1000000000000000 {
		t.Error("Value around lower boundary was not generated")
	}
	if max < 0xF000000000000000 {
		t.Error("Value around upper boundary was not generated")
	}
}

func TestUint32(t *testing.T) {
	g := NewMathRandomGenerator()

	min := uint32(0xFFFFFFFF)
	max := uint32(0)
	for i := 0; i < 10000; i++ {
		r := g.Uint32()
		if r < min {
			min = r
		}
		if r > max {
			max = r
		}
	}
	if min > 0x10000000 {
		t.Error("Value around lower boundary was not generated")
	}
	if max < 0xF0000000 {
		t.Error("Value around upper boundary was not generated")
	}
}