Source file src/crypto/rand/rand_batched_test.go

     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  	"errors"
    12  	"testing"
    13  )
    14  
    15  func TestBatched(t *testing.T) {
    16  	fillBatched := batched(func(p []byte) error {
    17  		for i := range p {
    18  			p[i] = byte(i)
    19  		}
    20  		return nil
    21  	}, 5)
    22  
    23  	p := make([]byte, 13)
    24  	if err := fillBatched(p); err != nil {
    25  		t.Fatalf("batched function returned error: %s", err)
    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) error { return errors.New("") }, 5)
    35  	if b(make([]byte, 13)) == nil {
    36  		t.Fatal("batched function should have returned error")
    37  	}
    38  }
    39  
    40  func TestBatchedEmpty(t *testing.T) {
    41  	b := batched(func(p []byte) error { return errors.New("") }, 5)
    42  	if err := b(make([]byte, 0)); err != nil {
    43  		t.Fatalf("empty slice should always return nil: %s", err)
    44  	}
    45  }
    46  

View as plain text