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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
// Written in 2011-2014 by Dmitry Chestnykh
//
// The author(s) have dedicated all copyright and related and
// neighboring rights to this software to the public domain
// worldwide. Distributed without any warranty.
// http://creativecommons.org/publicdomain/zero/1.0/
package uniuri
import (
"bytes"
"testing"
)
func validateBytes(t *testing.T, u []byte, chars []byte) {
for _, c := range u {
var present bool
for _, a := range chars {
if a == c {
present = true
}
}
if !present {
t.Fatalf("chars not allowed in %q", u)
}
}
}
func validateChars(t *testing.T, u string, chars []byte) {
for _, c := range u {
var present bool
for _, a := range chars {
if rune(a) == c {
present = true
}
}
if !present {
t.Fatalf("chars not allowed in %q", u)
}
}
}
func TestNew(t *testing.T) {
u := New()
// Check length
if len(u) != StdLen {
t.Fatalf("wrong length: expected %d, got %d", StdLen, len(u))
}
// Check that only allowed characters are present
validateChars(t, u, StdChars)
// Generate 1000 uniuris and check that they are unique
uris := make([]string, 1000)
for i := range uris {
uris[i] = New()
}
for i, u := range uris {
for j, u2 := range uris {
if i != j && u == u2 {
t.Fatalf("not unique: %d:%q and %d:%q", i, u, j, u2)
}
}
}
}
func TestNewLen(t *testing.T) {
for i := 0; i < 100; i++ {
u := NewLen(i)
if len(u) != i {
t.Fatalf("request length %d, got %d", i, len(u))
}
}
}
func TestNewLenCharsBytes(t *testing.T) {
length := 10
chars := []byte("01234567")
u := NewLenCharsBytes(length, chars)
// Check length
if len(u) != length {
t.Fatalf("wrong length: expected %d, got %d", StdLen, len(u))
}
// Check that only allowed characters are present
validateBytes(t, u, chars)
// Check that two generated strings are different
u2 := NewLenCharsBytes(length, chars)
if bytes.Equal(u, u2) {
t.Fatalf("not unique: %q and %q", u, u2)
}
}
func TestNewLenChars(t *testing.T) {
length := 10
chars := []byte("01234567")
u := NewLenChars(length, chars)
// Check length
if len(u) != length {
t.Fatalf("wrong length: expected %d, got %d", StdLen, len(u))
}
// Check that only allowed characters are present
validateChars(t, u, chars)
// Check that two generated strings are different
u2 := NewLenChars(length, chars)
if u == u2 {
t.Fatalf("not unique: %q and %q", u, u2)
}
}
func TestNewLenCharsMaxLength(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("didn't panic")
}
}()
chars := make([]byte, 257)
NewLenChars(32, chars)
}
func TestBias(t *testing.T) {
chars := []byte("abcdefghijklmnopqrstuvwxyz")
slen := 100000
s := NewLenChars(slen, chars)
counts := make(map[rune]int)
for _, b := range s {
counts[b]++
}
avg := float64(slen) / float64(len(chars))
for k, n := range counts {
diff := float64(n) / avg
if diff < 0.95 || diff > 1.05 {
t.Errorf("Possible bias on '%c': expected average %f, got %d", k, avg, n)
}
}
}
var (
sixtyFourChars = append(StdChars, []byte{'+', '/'}...)
sixtyFiveChars = append(sixtyFourChars, []byte{'.'}...)
threeChars = []byte{'a', 'b', 'c'}
)
func BenchmarkLen16Chars65(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewLenChars(StdLen, sixtyFiveChars)
}
}
func BenchmarkLen16Chars64(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewLenChars(StdLen, sixtyFourChars)
}
}
func BenchmarkLen16Chars62(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewLenChars(StdLen, StdChars)
}
}
func BenchmarkLen16Chars3(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewLenChars(StdLen, threeChars)
}
}
func BenchmarkLen1024Chars65(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewLenChars(1024, sixtyFiveChars)
}
}
func BenchmarkLen1024Chars64(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewLenChars(1024, sixtyFourChars)
}
}
func BenchmarkLen1024Chars62(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewLenChars(1024, StdChars)
}
}
func BenchmarkLen1024Chars3(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewLenChars(1024, threeChars)
}
}
|