TestGenerate draws words: its seed gave only two indices

The seed of TestGenerate, bytes.Repeat([]byte{7, 1, 200, 33}, 64), only
ever gives indices 1793 and 2081: both calls of Generate ended in EOF, the
test ignored the errors and compared two empty results. It now draws from
a SHA-256 counter stream and checks the errors and the count, draws an
index twice to see that it is drawn again, and keeps the old seed as the
case of a source that runs out. Found by the review of the Dart port.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
v0.15
dev 10 hours ago
parent 27a75eefcd
commit 96d89927a4

@ -102,11 +102,28 @@ func TestGenerate(t *testing.T) {
seen[w] = true
}
// The same random bytes draw the same words: Generate reads nothing else.
seed := bytes.Repeat([]byte{7, 1, 200, 33}, 64)
a, _ := Generate(list, 6, bytes.NewReader(seed))
b, _ := Generate(list, 6, bytes.NewReader(seed))
if strings.Join(a, " ") != strings.Join(b, " ") {
t.Errorf("%q and %q", a, b)
var seed []byte
for i := range 8 {
h := sha256.Sum256([]byte{byte(i)})
seed = append(seed, h[:]...)
}
a, err := Generate(list, 6, bytes.NewReader(seed))
if err != nil {
t.Fatal(err)
}
b, err := Generate(list, 6, bytes.NewReader(seed))
if err != nil || len(a) != 6 || strings.Join(a, " ") != strings.Join(b, " ") {
t.Errorf("%q and %q: %v", a, b, err)
}
// An index drawn twice is drawn again: each pair of bytes is an index
// below 8192, and 0 1 is index 1.
again := []byte{0, 1, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6}
if w, err := Generate(list, 6, bytes.NewReader(again)); err != nil || strings.Join(w, " ") != strings.Join(list[1:7], " ") {
t.Errorf("Generate with index 1 twice: %q, %v", w, err)
}
// Bytes that only ever give two indices, 1793 and 2081, run out.
if _, err := Generate(list, 6, bytes.NewReader(bytes.Repeat([]byte{7, 1, 200, 33}, 64))); err == nil || !strings.Contains(err.Error(), "EOF") {
t.Errorf("Generate with two indices: %v", err)
}
for _, c := range []struct {
n int

Loading…
Cancel
Save

Powered by TurnKey Linux.