github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/integers_test.go (about)

     1  // Copyright 2020 DataStax
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package primitive
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestReadByte(t *testing.T) {
    27  	tests := []struct {
    28  		name     string
    29  		source   []byte
    30  		expected byte
    31  		err      error
    32  	}{
    33  		{"simple byte", []byte{5}, byte(5), nil},
    34  		{"zero byte", []byte{0}, byte(0), nil},
    35  		{"byte with remaining", []byte{5, 1, 2, 3, 4}, byte(5), nil},
    36  		{"cannot read byte", []byte{}, byte(0), fmt.Errorf("cannot read [byte]: %w", errors.New("EOF"))},
    37  	}
    38  	for _, tt := range tests {
    39  		t.Run(tt.name, func(t *testing.T) {
    40  			buf := bytes.NewBuffer(tt.source)
    41  			actual, err := ReadByte(buf)
    42  			assert.Equal(t, tt.expected, actual)
    43  			assert.Equal(t, tt.err, err)
    44  		})
    45  	}
    46  }
    47  
    48  func TestWriteByte(t *testing.T) {
    49  	tests := []struct {
    50  		name     string
    51  		input    byte
    52  		expected []byte
    53  		err      error
    54  	}{
    55  		{"simple byte", byte(5), []byte{5}, nil},
    56  	}
    57  	for _, tt := range tests {
    58  		t.Run(tt.name, func(t *testing.T) {
    59  			buf := &bytes.Buffer{}
    60  			err := WriteByte(tt.input, buf)
    61  			assert.Equal(t, tt.expected, buf.Bytes())
    62  			assert.Equal(t, tt.err, err)
    63  		})
    64  	}
    65  }
    66  
    67  func TestReadShort(t *testing.T) {
    68  	tests := []struct {
    69  		name      string
    70  		source    []byte
    71  		expected  uint16
    72  		remaining []byte
    73  		err       error
    74  	}{
    75  		{"simple short", []byte{0, 5}, uint16(5), []byte{}, nil},
    76  		{"zero short", []byte{0, 0}, uint16(0), []byte{}, nil},
    77  		{"short with remaining", []byte{0, 5, 1, 2, 3, 4}, uint16(5), []byte{1, 2, 3, 4}, nil},
    78  		{"cannot read short", []byte{0}, uint16(0), []byte{}, fmt.Errorf("cannot read [short]: %w", errors.New("unexpected EOF"))},
    79  	}
    80  	for _, tt := range tests {
    81  		t.Run(tt.name, func(t *testing.T) {
    82  			buf := bytes.NewBuffer(tt.source)
    83  			actual, err := ReadShort(buf)
    84  			assert.Equal(t, tt.expected, actual)
    85  			assert.Equal(t, tt.remaining, buf.Bytes())
    86  			assert.Equal(t, tt.err, err)
    87  		})
    88  	}
    89  }
    90  
    91  func TestWriteShort(t *testing.T) {
    92  	tests := []struct {
    93  		name     string
    94  		input    uint16
    95  		expected []byte
    96  		err      error
    97  	}{
    98  		{"simple short", uint16(5), []byte{0, 5}, nil},
    99  	}
   100  	for _, tt := range tests {
   101  		t.Run(tt.name, func(t *testing.T) {
   102  			buf := &bytes.Buffer{}
   103  			err := WriteShort(tt.input, buf)
   104  			assert.Equal(t, tt.expected, buf.Bytes())
   105  			assert.Equal(t, tt.err, err)
   106  		})
   107  	}
   108  }
   109  
   110  func TestReadInt(t *testing.T) {
   111  	tests := []struct {
   112  		name      string
   113  		source    []byte
   114  		expected  int32
   115  		remaining []byte
   116  		err       error
   117  	}{
   118  		{"simple int", []byte{0, 0, 0, 5}, int32(5), []byte{}, nil},
   119  		{"zero int", []byte{0, 0, 0, 0}, int32(0), []byte{}, nil},
   120  		{"negative int", []byte{0xff, 0xff, 0xff, 0xff & -5}, int32(-5), []byte{}, nil},
   121  		{"int with remaining", []byte{0, 0, 0, 5, 1, 2, 3, 4}, int32(5), []byte{1, 2, 3, 4}, nil},
   122  		{"cannot read int", []byte{0, 0, 0}, int32(0), []byte{}, fmt.Errorf("cannot read [int]: %w", errors.New("unexpected EOF"))},
   123  	}
   124  	for _, tt := range tests {
   125  		t.Run(tt.name, func(t *testing.T) {
   126  			buf := bytes.NewBuffer(tt.source)
   127  			actual, err := ReadInt(buf)
   128  			assert.Equal(t, tt.expected, actual)
   129  			assert.Equal(t, tt.remaining, buf.Bytes())
   130  			assert.Equal(t, tt.err, err)
   131  		})
   132  	}
   133  }
   134  
   135  func TestWriteInt(t *testing.T) {
   136  	tests := []struct {
   137  		name     string
   138  		input    int32
   139  		expected []byte
   140  		err      error
   141  	}{
   142  		{"simple int", int32(5), []byte{0, 0, 0, 5}, nil},
   143  		{"negative int", int32(-5), []byte{0xff, 0xff, 0xff, 0xff & -5}, nil},
   144  	}
   145  	for _, tt := range tests {
   146  		t.Run(tt.name, func(t *testing.T) {
   147  			buf := &bytes.Buffer{}
   148  			err := WriteInt(tt.input, buf)
   149  			assert.Equal(t, tt.expected, buf.Bytes())
   150  			assert.Equal(t, tt.err, err)
   151  		})
   152  	}
   153  }
   154  
   155  func TestReadLong(t *testing.T) {
   156  	tests := []struct {
   157  		name      string
   158  		source    []byte
   159  		expected  int64
   160  		remaining []byte
   161  		err       error
   162  	}{
   163  		{"simple long", []byte{0, 0, 0, 0, 0, 0, 0, 5}, int64(5), []byte{}, nil},
   164  		{"zero long", []byte{0, 0, 0, 0, 0, 0, 0, 0}, int64(0), []byte{}, nil},
   165  		{"negative long", []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff & -5}, int64(-5), []byte{}, nil},
   166  		{"long with remaining", []byte{0, 0, 0, 0, 0, 0, 0, 5, 1, 2, 3, 4}, int64(5), []byte{1, 2, 3, 4}, nil},
   167  		{"cannot read long", []byte{0, 0, 0, 0, 0, 0, 0}, int64(0), []byte{}, fmt.Errorf("cannot read [long]: %w", errors.New("unexpected EOF"))},
   168  	}
   169  	for _, tt := range tests {
   170  		t.Run(tt.name, func(t *testing.T) {
   171  			buf := bytes.NewBuffer(tt.source)
   172  			actual, err := ReadLong(buf)
   173  			assert.Equal(t, tt.expected, actual)
   174  			assert.Equal(t, tt.remaining, buf.Bytes())
   175  			assert.Equal(t, tt.err, err)
   176  		})
   177  	}
   178  }
   179  
   180  func TestWriteLong(t *testing.T) {
   181  	tests := []struct {
   182  		name     string
   183  		input    int64
   184  		expected []byte
   185  		err      error
   186  	}{
   187  		{"simple long", int64(5), []byte{0, 0, 0, 0, 0, 0, 0, 5}, nil},
   188  		{"negative long", int64(-5), []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff & -5}, nil},
   189  	}
   190  	for _, tt := range tests {
   191  		t.Run(tt.name, func(t *testing.T) {
   192  			buf := &bytes.Buffer{}
   193  			err := WriteLong(tt.input, buf)
   194  			assert.Equal(t, tt.expected, buf.Bytes())
   195  			assert.Equal(t, tt.err, err)
   196  		})
   197  	}
   198  }