Debian Codegraph @

uniuri_test.go

raw symbols SCIP
1 // Written in 2011-2014 by Dmitry Chestnykh
2 //
3 // The author(s) have dedicated all copyright and related and
4 // neighboring rights to this software to the public domain
5 // worldwide. Distributed without any warranty.
6 // http://creativecommons.org/publicdomain/zero/1.0/
7
8 package uniuri
9
10 import (
11 "bytes"
12 "testing"
13 )
14
15 func validateBytes(t *testing.T, u []byte, chars []byte) {
16 for _, c := range u {
17 var present bool
18 for _, a := range chars {
19 if a == c {
20 present = true
21 }
22 }
23 if !present {
24 t.Fatalf("chars not allowed in %q", u)
25 }
26 }
27 }
28
29 func validateChars(t *testing.T, u string, chars []byte) {
30 for _, c := range u {
31 var present bool
32 for _, a := range chars {
33 if rune(a) == c {
34 present = true
35 }
36 }
37 if !present {
38 t.Fatalf("chars not allowed in %q", u)
39 }
40 }
41 }
42
43 func TestNew(t *testing.T) {
44 u := New()
45 // Check length
46 if len(u) != StdLen {
47 t.Fatalf("wrong length: expected %d, got %d", StdLen, len(u))
48 }
49 // Check that only allowed characters are present
50 validateChars(t, u, StdChars)
51
52 // Generate 1000 uniuris and check that they are unique
53 uris := make([]string, 1000)
54 for i := range uris {
55 uris[i] = New()
56 }
57 for i, u := range uris {
58 for j, u2 := range uris {
59 if i != j && u == u2 {
60 t.Fatalf("not unique: %d:%q and %d:%q", i, u, j, u2)
61 }
62 }
63 }
64 }
65
66 func TestNewLen(t *testing.T) {
67 for i := 0; i < 100; i++ {
68 u := NewLen(i)
69 if len(u) != i {
70 t.Fatalf("request length %d, got %d", i, len(u))
71 }
72 }
73 }
74
75 func TestNewLenCharsBytes(t *testing.T) {
76 length := 10
77 chars := []byte("01234567")
78 u := NewLenCharsBytes(length, chars)
79
80 // Check length
81 if len(u) != length {
82 t.Fatalf("wrong length: expected %d, got %d", StdLen, len(u))
83 }
84 // Check that only allowed characters are present
85 validateBytes(t, u, chars)
86
87 // Check that two generated strings are different
88 u2 := NewLenCharsBytes(length, chars)
89 if bytes.Equal(u, u2) {
90 t.Fatalf("not unique: %q and %q", u, u2)
91 }
92 }
93
94 func TestNewLenChars(t *testing.T) {
95 length := 10
96 chars := []byte("01234567")
97 u := NewLenChars(length, chars)
98
99 // Check length
100 if len(u) != length {
101 t.Fatalf("wrong length: expected %d, got %d", StdLen, len(u))
102 }
103 // Check that only allowed characters are present
104 validateChars(t, u, chars)
105
106 // Check that two generated strings are different
107 u2 := NewLenChars(length, chars)
108 if u == u2 {
109 t.Fatalf("not unique: %q and %q", u, u2)
110 }
111 }
112
113 func TestNewLenCharsMaxLength(t *testing.T) {
114 defer func() {
115 if r := recover(); r == nil {
116 t.Fatal("didn't panic")
117 }
118 }()
119 chars := make([]byte, 257)
120 NewLenChars(32, chars)
121 }
122
123 func TestBias(t *testing.T) {
124 chars := []byte("abcdefghijklmnopqrstuvwxyz")
125 slen := 100000
126 s := NewLenChars(slen, chars)
127 counts := make(map[rune]int)
128 for _, b := range s {
129 counts[b]++
130 }
131 avg := float64(slen) / float64(len(chars))
132 for k, n := range counts {
133 diff := float64(n) / avg
134 if diff < 0.95 || diff > 1.05 {
135 t.Errorf("Possible bias on '%c': expected average %f, got %d", k, avg, n)
136 }
137 }
138 }
139
140 var (
141 sixtyFourChars = append(StdChars, []byte{'+', '/'}...)
142 sixtyFiveChars = append(sixtyFourChars, []byte{'.'}...)
143 threeChars = []byte{'a', 'b', 'c'}
144 )
145
146 func BenchmarkLen16Chars65(b *testing.B) {
147 for i := 0; i < b.N; i++ {
148 _ = NewLenChars(StdLen, sixtyFiveChars)
149 }
150 }
151
152 func BenchmarkLen16Chars64(b *testing.B) {
153 for i := 0; i < b.N; i++ {
154 _ = NewLenChars(StdLen, sixtyFourChars)
155 }
156 }
157
158 func BenchmarkLen16Chars62(b *testing.B) {
159 for i := 0; i < b.N; i++ {
160 _ = NewLenChars(StdLen, StdChars)
161 }
162 }
163
164 func BenchmarkLen16Chars3(b *testing.B) {
165 for i := 0; i < b.N; i++ {
166 _ = NewLenChars(StdLen, threeChars)
167 }
168 }
169
170 func BenchmarkLen1024Chars65(b *testing.B) {
171 for i := 0; i < b.N; i++ {
172 _ = NewLenChars(1024, sixtyFiveChars)
173 }
174 }
175
176 func BenchmarkLen1024Chars64(b *testing.B) {
177 for i := 0; i < b.N; i++ {
178 _ = NewLenChars(1024, sixtyFourChars)
179 }
180 }
181
182 func BenchmarkLen1024Chars62(b *testing.B) {
183 for i := 0; i < b.N; i++ {
184 _ = NewLenChars(1024, StdChars)
185 }
186 }
187
188 func BenchmarkLen1024Chars3(b *testing.B) {
189 for i := 0; i < b.N; i++ {
190 _ = NewLenChars(1024, threeChars)
191 }
192 }