github.com/aergoio/aergo@v1.3.1/p2p/p2putil/ioutil_test.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2putil
     7  
     8  import (
     9  	"bytes"
    10  	"testing"
    11  )
    12  
    13  func TestReadToLen(t *testing.T) {
    14  	sample := []byte("0123456789ABCDEFGHIJabcdefghij")
    15  
    16  	type args struct {
    17  		bfLen int
    18  	}
    19  	tests := []struct {
    20  		name string
    21  
    22  		args   args
    23  		repeat int
    24  
    25  		want    int
    26  	}{
    27  		{"TExact",args{4},0, 4},
    28  		{"TBigBuf",args{8},0, 8},
    29  		{"TRepeat",args{4},4, 4},
    30  	}
    31  	for _, tt := range tests {
    32  		t.Run(tt.name, func(t *testing.T) {
    33  			rd := bytes.NewReader(sample)
    34  			bf := make([]byte, 100)
    35  			prev :=  make([]byte, 0, tt.args.bfLen)
    36  			for i := 0; i <= tt.repeat; i++ {
    37  				got, _ := ReadToLen(rd, bf[:tt.args.bfLen])
    38  				if got != tt.want {
    39  					t.Errorf("baseWireHandshaker.readToLen() = %v, want %v", got, tt.want)
    40  				}
    41  				if bytes.Equal(prev, bf) {
    42  					t.Errorf("baseWireHandshaker.readToLen() wrong, same as prev %v", bf)
    43  				}
    44  				copy(prev, bf)
    45  			}
    46  		})
    47  	}
    48  }