|
| 1 | +package rndstr |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/rand" |
| 6 | + "encoding/binary" |
| 7 | + "io" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestReaduint32(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + input []byte |
| 15 | + want uint32 |
| 16 | + wantErr bool |
| 17 | + }{ |
| 18 | + {"zero", []byte{0, 0, 0, 0}, 0, false}, |
| 19 | + {"one", []byte{1, 0, 0, 0}, 1, false}, |
| 20 | + {"max", []byte{255, 255, 255, 255}, 0xFFFFFFFF, false}, |
| 21 | + {"mixed", []byte{0x78, 0x56, 0x34, 0x12}, 0x12345678, false}, |
| 22 | + {"short", []byte{1, 2, 3}, 0, true}, |
| 23 | + {"empty", []byte{}, 0, true}, |
| 24 | + } |
| 25 | + |
| 26 | + for _, tt := range tests { |
| 27 | + t.Run(tt.name, func(t *testing.T) { |
| 28 | + r := bytes.NewReader(tt.input) |
| 29 | + got, err := readuint32(r) |
| 30 | + if (err != nil) != tt.wantErr { |
| 31 | + t.Errorf("readuint32() error = %v, wantErr %v", err, tt.wantErr) |
| 32 | + return |
| 33 | + } |
| 34 | + if !tt.wantErr && got != tt.want { |
| 35 | + t.Errorf("readuint32() = %v, want %v", got, tt.want) |
| 36 | + } |
| 37 | + }) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestReadint31n(t *testing.T) { |
| 42 | + tests := []struct { |
| 43 | + name string |
| 44 | + n int32 |
| 45 | + wantErr error |
| 46 | + }{ |
| 47 | + {"positive", 100, nil}, |
| 48 | + {"one", 1, nil}, |
| 49 | + {"large", 1000000, nil}, |
| 50 | + {"zero", 0, ErrInvalidRange}, |
| 51 | + {"negative", -1, ErrInvalidRange}, |
| 52 | + } |
| 53 | + |
| 54 | + for _, tt := range tests { |
| 55 | + t.Run(tt.name, func(t *testing.T) { |
| 56 | + got, err := readint31n(rand.Reader, tt.n) |
| 57 | + if err != tt.wantErr { |
| 58 | + t.Errorf("readint31n(rand.Reader, %d) error = %v, want %v", tt.n, err, tt.wantErr) |
| 59 | + return |
| 60 | + } |
| 61 | + if tt.wantErr == nil { |
| 62 | + if got < 0 || got >= tt.n { |
| 63 | + t.Errorf("readint31n(rand.Reader, %d) = %d, want [0, %d)", tt.n, got, tt.n) |
| 64 | + } |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestReadint31nDistribution(t *testing.T) { |
| 71 | + const n = 10 |
| 72 | + const iterations = 10000 |
| 73 | + counts := make([]int, n) |
| 74 | + |
| 75 | + for i := 0; i < iterations; i++ { |
| 76 | + v, err := readint31n(rand.Reader, n) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("readint31n error: %v", err) |
| 79 | + } |
| 80 | + if v < 0 || v >= n { |
| 81 | + t.Fatalf("readint31n returned out of range value: %d", v) |
| 82 | + } |
| 83 | + counts[v]++ |
| 84 | + } |
| 85 | + |
| 86 | + expected := float64(iterations) / float64(n) |
| 87 | + tolerance := expected * 0.2 // 20% tolerance |
| 88 | + |
| 89 | + for i, count := range counts { |
| 90 | + if float64(count) < expected-tolerance || float64(count) > expected+tolerance { |
| 91 | + t.Errorf("readint31n distribution: value %d appeared %d times, expected ~%.0f (tolerance %.0f)", |
| 92 | + i, count, expected, tolerance) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestReadint31nReadError(t *testing.T) { |
| 98 | + r := bytes.NewReader([]byte{1, 2}) // only 2 bytes, need 4 |
| 99 | + _, err := readint31n(r, 10) |
| 100 | + if err == nil { |
| 101 | + t.Error("readint31n with short reader should return error") |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +type deterministicReader struct { |
| 106 | + values []uint32 |
| 107 | + pos int |
| 108 | +} |
| 109 | + |
| 110 | +func (d *deterministicReader) Read(p []byte) (n int, err error) { |
| 111 | + if d.pos >= len(d.values) { |
| 112 | + return 0, io.EOF |
| 113 | + } |
| 114 | + binary.LittleEndian.PutUint32(p, d.values[d.pos]) |
| 115 | + d.pos++ |
| 116 | + return 4, nil |
| 117 | +} |
| 118 | + |
| 119 | +func TestReadint31nRejectionSampling(t *testing.T) { |
| 120 | + // Test that rejection sampling works correctly |
| 121 | + // For n=3, threshold = (2^32 - 3) % 3 = 1 |
| 122 | + // Values where (v * n) & 0xFFFFFFFF < threshold should be rejected |
| 123 | + |
| 124 | + // Create a reader that first returns a value that should be rejected, |
| 125 | + // then a value that should be accepted |
| 126 | + dr := &deterministicReader{ |
| 127 | + values: []uint32{0, 0x55555555}, // First rejected, second accepted |
| 128 | + } |
| 129 | + |
| 130 | + got, err := readint31n(dr, 3) |
| 131 | + if err != nil { |
| 132 | + t.Fatalf("readint31n error: %v", err) |
| 133 | + } |
| 134 | + // The second value 0x55555555 * 3 = 0xFFFFFFFF |
| 135 | + // High 32 bits = 0, so result should be 1 |
| 136 | + expected := int32((uint64(0x55555555) * 3) >> 32) |
| 137 | + if got != expected { |
| 138 | + t.Errorf("readint31n with rejection = %d, want %d", got, expected) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func BenchmarkReadint31n(b *testing.B) { |
| 143 | + for i := 0; i < b.N; i++ { |
| 144 | + readint31n(rand.Reader, 62) |
| 145 | + } |
| 146 | +} |
0 commit comments