Black Lives Matter. Support the Equal Justice Initiative.

Source file src/crypto/rand/rand_batched_test.go

Documentation: crypto/rand

     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  // +build linux freebsd dragonfly solaris
     7  
     8  package rand
     9  
    10  import (
    11  	"bytes"
    12  	"testing"
    13  )
    14  
    15  func TestBatched(t *testing.T) {
    16  	fillBatched := batched(func(p []byte) bool {
    17  		for i := range p {
    18  			p[i] = byte(i)
    19  		}
    20  		return true
    21  	}, 5)
    22  
    23  	p := make([]byte, 13)
    24  	if !fillBatched(p) {
    25  		t.Fatal("batched function returned false")
    26  	}
    27  	expected := []byte{0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2}
    28  	if !bytes.Equal(expected, p) {
    29  		t.Errorf("incorrect batch result: got %x, want %x", p, expected)
    30  	}
    31  }
    32  
    33  func TestBatchedError(t *testing.T) {
    34  	b := batched(func(p []byte) bool { return false }, 5)
    35  	if b(make([]byte, 13)) {
    36  		t.Fatal("batched function should have returned false")
    37  	}
    38  }
    39  
    40  func TestBatchedEmpty(t *testing.T) {
    41  	b := batched(func(p []byte) bool { return false }, 5)
    42  	if !b(make([]byte, 0)) {
    43  		t.Fatal("empty slice should always return true")
    44  	}
    45  }
    46  

View as plain text