github.com/AESNooper/go/src@v0.0.0-20220218095104-b56a4ab1bbbb/crypto/rand/rand_batched_test.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build linux || freebsd || dragonfly || solaris
     6  
     7  package rand
     8  
     9  import (
    10  	"bytes"
    11  	"testing"
    12  )
    13  
    14  func TestBatched(t *testing.T) {
    15  	fillBatched := batched(func(p []byte) bool {
    16  		for i := range p {
    17  			p[i] = byte(i)
    18  		}
    19  		return true
    20  	}, 5)
    21  
    22  	p := make([]byte, 13)
    23  	if !fillBatched(p) {
    24  		t.Fatal("batched function returned false")
    25  	}
    26  	expected := []byte{0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2}
    27  	if !bytes.Equal(expected, p) {
    28  		t.Errorf("incorrect batch result: got %x, want %x", p, expected)
    29  	}
    30  }
    31  
    32  func TestBatchedError(t *testing.T) {
    33  	b := batched(func(p []byte) bool { return false }, 5)
    34  	if b(make([]byte, 13)) {
    35  		t.Fatal("batched function should have returned false")
    36  	}
    37  }
    38  
    39  func TestBatchedEmpty(t *testing.T) {
    40  	b := batched(func(p []byte) bool { return false }, 5)
    41  	if !b(make([]byte, 0)) {
    42  		t.Fatal("empty slice should always return true")
    43  	}
    44  }