Debian Codegraph @

bytesource/bytesource_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 Copyright 2014 Google Inc. All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16 package bytesource
17
18 import (
19 "math/rand"
20 "testing"
21 )
22
23 func TestByteSource(t *testing.T) {
24 t.Parallel()
25
26 randFromSource := func(s ...byte) *rand.Rand {
27 return rand.New(New(s))
28 }
29
30 randFromSeed := func(seed int64) *rand.Rand {
31 var s1 ByteSource
32 s1.Seed(seed)
33 return rand.New(&s1)
34 }
35
36 t.Run("Inputs with identical 8 byte prefix", func(t *testing.T) {
37 rand1 := randFromSource(1, 2, 3, 4, 5, 6, 7, 8, 9)
38 rand2 := randFromSource(1, 2, 3, 4, 5, 6, 7, 8, 9)
39 if rand1.Int() != rand2.Int() {
40 t.Errorf("Inputs with identical 9 byte prefix result in different 1st output.")
41 }
42 if rand1.Int() != rand2.Int() {
43 t.Errorf("Inputs with identical 9 byte prefix result in different 2nd output.")
44 }
45 })
46
47 t.Run("Inputs with different 8 byte prefix", func(t *testing.T) {
48 rand2 := randFromSource(1, 2, 3, 4, 5, 6, 7, 1, 9)
49 rand1 := randFromSource(1, 2, 3, 4, 5, 6, 7, 8, 9)
50 if rand1.Int() != rand2.Int() {
51 t.Errorf("Inputs with identical 9th byte prefix result in different 1st output.")
52 }
53 if rand1.Int() == rand2.Int() {
54 t.Errorf("Inputs with different 8 bytes prefix result in identical 2nd output.")
55 }
56 })
57
58 t.Run("Multiple invocation", func(t *testing.T) {
59 // First random from input byte, second from random source.
60 r := randFromSource(1, 2, 3, 4, 6, 7, 8, 9)
61 if r.Int() == r.Int() {
62 t.Errorf("Two random numbers are identical.")
63 }
64 // First and second numbers from random source.
65 r = randFromSource(1)
66 if r.Int() == r.Int() {
67 t.Errorf("Two random numbers are identical.")
68 }
69 })
70
71 t.Run("Seed", func(t *testing.T) {
72 if randFromSeed(42).Int() != randFromSeed(42).Int() {
73 t.Error("Two random numbers from the same seed differ.")
74 }
75 if randFromSeed(42).Int() == randFromSeed(43).Int() {
76 t.Error("Two random numbers from different seeds are identical.")
77 }
78 })
79 }
80
81 func TestByteSourceValues(t *testing.T) {
82 t.Parallel()
83
84 // Data in chunks of 8 bytes.
85 data := []byte{
86 99, 12, 23, 12, 65, 34, 12, 12,
87 99, 12, 23, 12, 25, 34, 15, 13,
88 99, 12, 23, 42, 25, 34, 11, 14,
89 99, 12, 54, 12, 25, 34, 99, 11,
90 }
91
92 r := rand.New(New(data))
93
•94 got := []int64{r.Int63(), r.Int63(), r.Int63(), r.Int63(), r.Int63()}
95
•96 want := []int64{
97 3568552425102051206,
98 3568552489526560135,
99 3568569467532292485,
100 7616166771204380295,
101 5210010188159375967,
102 }
103
104 for i := range got {
105 if want[i] != got[i] {
106 t.Errorf("want[%d] = %d, got: %d", i, want[i], got[i])
107 }
108 }
109 }