Skip to content

Commit 18c2a41

Browse files
committed
update tests
Change-Id: I2d7f4a902877c209b6aa793f108856b750f25d8b
1 parent 9cea191 commit 18c2a41

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/internal/chacha8rand/rand_test.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,35 @@ func TestOutput(t *testing.T) {
3131
}
3232
}
3333

34-
func TestOutputBytes(t *testing.T) {
35-
var s State
36-
s.Init(seed)
37-
34+
func TestFillRand(t *testing.T) {
3835
expect := make([]byte, 0, len(output)*8)
3936
for _, v := range output {
4037
expect = byteorder.LeAppendUint64(expect, v)
4138
}
4239

43-
got := make([]byte, len(expect))
44-
s.FillRand(got)
40+
cases := []struct {
41+
expect [][]byte
42+
}{
43+
{[][]byte{expect}},
44+
{[][]byte{expect[:1], expect[8:9]}},
45+
{[][]byte{expect[:4], expect[8:12]}},
46+
{[][]byte{expect[:4], expect[8:12]}},
47+
{[][]byte{expect[:8], expect[8:16]}},
48+
{[][]byte{expect[:128], expect[128:256]}},
49+
{[][]byte{expect[:128], expect[128:]}},
50+
{[][]byte{expect[:100], expect[104:]}},
51+
}
4552

46-
if !bytes.Equal(expect, got) {
47-
t.Errorf("got = %#x; want = %#x", got, expect)
53+
var s State
54+
for _, tt := range cases {
55+
s.Init(seed)
56+
for _, expect := range tt.expect {
57+
got := make([]byte, len(expect))
58+
s.FillRand(got)
59+
if !bytes.Equal(expect, got) {
60+
t.Errorf("got = %#x; want = %#x", got, expect)
61+
}
62+
}
4863
}
4964
}
5065

src/math/rand/v2/chacha8_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,21 @@ var chacha8marshal = []string{
531531
"chacha8:\x00\x00\x00\x00\x00\x00\x00{K3\x9bB!,\x94\x9d\x975\xce'O_t\xee|\xb21\x87\xbb\xbb\xfd)\x8f\xe52\x01\vP\fk",
532532
}
533533

534+
func TestChaCha8Read(t *testing.T) {
535+
c := NewChaCha8(chacha8seed)
536+
n, err := c.Read(make([]byte, 128))
537+
if n != 128 || err != nil {
538+
t.Errorf("(*ChaCha8).Read(buf) = (%v, %v); want = (128, nil)", n, err)
539+
}
540+
541+
n, err = c.Read(make([]byte, 0))
542+
if n != 0 || err != nil {
543+
t.Errorf("(*ChaCha8).Read(buf) = (%v, %v); want = (0, nil)", n, err)
544+
}
545+
}
546+
534547
func BenchmarkRead(b *testing.B) {
535-
for _, v := range []int{1, 3, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 1024 * 2, 1024 * 16} {
548+
for _, v := range []int{1, 2, 3, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 1024 * 2, 1024 * 16} {
536549
b.Run(strconv.FormatInt(int64(v), 10), func(b *testing.B) {
537550
r := NewChaCha8(chacha8seed)
538551
buf := make([]byte, v)

0 commit comments

Comments
 (0)