siprng_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package captcha
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. func TestSiphash(t *testing.T) {
  7. good := uint64(0xe849e8bb6ffe2567)
  8. cur := siphash(0, 0, 0)
  9. if cur != good {
  10. t.Fatalf("siphash: expected %x, got %x", good, cur)
  11. }
  12. }
  13. func TestSiprng(t *testing.T) {
  14. m := make(map[uint64]interface{})
  15. var yes interface{}
  16. r := siprng{}
  17. r.Seed([16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
  18. for i := 0; i < 100000; i++ {
  19. v := r.Uint64()
  20. if _, ok := m[v]; ok {
  21. t.Errorf("siphash: collision on %d: %x", i, v)
  22. }
  23. m[v] = yes
  24. }
  25. }
  26. func TestSiprngBytes(t *testing.T) {
  27. r := siprng{}
  28. r.Seed([16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
  29. x := r.Bytes(32)
  30. if len(x) != 32 {
  31. t.Fatalf("siphash: wrong length: expected 32, got %d", len(x))
  32. }
  33. y := r.Bytes(32)
  34. if bytes.Equal(x, y) {
  35. t.Fatalf("siphash: stream repeats: %x = %x", x, y)
  36. }
  37. r.Seed([16]byte{})
  38. z := r.Bytes(32)
  39. if bytes.Equal(z, x) {
  40. t.Fatalf("siphash: outputs under different keys repeat: %x = %x", z, x)
  41. }
  42. }
  43. func BenchmarkSiprng(b *testing.B) {
  44. b.SetBytes(8)
  45. p := &siprng{}
  46. for i := 0; i < b.N; i++ {
  47. p.Uint64()
  48. }
  49. }