2014-12-07 08:41:24 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-12-07 08:41:24 -07:00
|
|
|
|
2016-05-26 00:02:56 -07:00
|
|
|
package rand
|
2014-12-07 08:41:24 -07:00
|
|
|
|
2016-04-10 12:36:38 -07:00
|
|
|
import "testing"
|
2014-12-07 08:41:24 -07:00
|
|
|
|
|
|
|
func TestSeedFromBytes(t *testing.T) {
|
|
|
|
// should always return the same seed for the same bytes
|
|
|
|
tcs := []struct {
|
|
|
|
bs []byte
|
|
|
|
v int64
|
|
|
|
}{
|
|
|
|
{[]byte("hello world"), -3639725434188061933},
|
|
|
|
{[]byte("hello worlx"), -2539100776074091088},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcs {
|
2016-03-25 00:35:18 -07:00
|
|
|
if v := SeedFromBytes(tc.bs); v != tc.v {
|
2014-12-07 08:41:24 -07:00
|
|
|
t.Errorf("Unexpected seed value %d != %d", v, tc.v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRandomString(t *testing.T) {
|
|
|
|
for _, l := range []int{0, 1, 2, 3, 4, 8, 42} {
|
2016-05-26 00:02:56 -07:00
|
|
|
s := String(l)
|
2014-12-07 08:41:24 -07:00
|
|
|
if len(s) != l {
|
2014-12-08 08:19:08 -07:00
|
|
|
t.Errorf("Incorrect length %d != %d", len(s), l)
|
2014-12-07 08:41:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
strings := make([]string, 1000)
|
|
|
|
for i := range strings {
|
2016-05-26 00:02:56 -07:00
|
|
|
strings[i] = String(8)
|
2014-12-07 08:41:24 -07:00
|
|
|
for j := range strings {
|
|
|
|
if i == j {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings[i] == strings[j] {
|
|
|
|
t.Errorf("Repeated random string %q", strings[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRandomInt64(t *testing.T) {
|
|
|
|
ints := make([]int64, 1000)
|
|
|
|
for i := range ints {
|
2016-05-26 00:02:56 -07:00
|
|
|
ints[i] = Int64()
|
2014-12-07 08:41:24 -07:00
|
|
|
for j := range ints {
|
|
|
|
if i == j {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ints[i] == ints[j] {
|
|
|
|
t.Errorf("Repeated random int64 %d", ints[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|