github.com/m3db/m3@v1.5.0/src/m3ninx/index/segment/fst/encoding/encoding_test.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package encoding
    22  
    23  import (
    24  	"fmt"
    25  	"math"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestUint32(t *testing.T) {
    32  	tests := []struct {
    33  		x uint32
    34  	}{
    35  		{
    36  			x: 0,
    37  		},
    38  		{
    39  			x: 42,
    40  		},
    41  		{
    42  			x: math.MaxUint32,
    43  		},
    44  	}
    45  
    46  	for _, test := range tests {
    47  		name := fmt.Sprintf("Encode and Decode %d", test.x)
    48  		t.Run(name, func(t *testing.T) {
    49  			enc := NewEncoder(1024)
    50  			n := enc.PutUint32(test.x)
    51  			require.Equal(t, 4, n)
    52  
    53  			dec := NewDecoder(enc.Bytes())
    54  			actual, err := dec.Uint32()
    55  
    56  			require.NoError(t, err)
    57  			require.Equal(t, test.x, actual)
    58  		})
    59  	}
    60  }
    61  
    62  func TestUint64(t *testing.T) {
    63  	tests := []struct {
    64  		x uint64
    65  	}{
    66  		{
    67  			x: 0,
    68  		},
    69  		{
    70  			x: 42,
    71  		},
    72  		{
    73  			x: math.MaxUint64,
    74  		},
    75  	}
    76  
    77  	for _, test := range tests {
    78  		name := fmt.Sprintf("Encode and Decode %d", test.x)
    79  		t.Run(name, func(t *testing.T) {
    80  			enc := NewEncoder(1024)
    81  			n := enc.PutUint64(test.x)
    82  			require.Equal(t, 8, n)
    83  
    84  			dec := NewDecoder(enc.Bytes())
    85  			actual, err := dec.Uint64()
    86  
    87  			require.NoError(t, err)
    88  			require.Equal(t, test.x, actual)
    89  		})
    90  	}
    91  }
    92  
    93  func TestUvarint(t *testing.T) {
    94  	tests := []struct {
    95  		x uint64
    96  		n int
    97  	}{
    98  		{
    99  			x: 0,
   100  			n: 1,
   101  		},
   102  		{
   103  			x: 42,
   104  			n: 1,
   105  		},
   106  		{
   107  			x: math.MaxUint64,
   108  			n: 10,
   109  		},
   110  	}
   111  
   112  	for _, test := range tests {
   113  		name := fmt.Sprintf("Encode and Decode %d", test.n)
   114  		t.Run(name, func(t *testing.T) {
   115  			enc := NewEncoder(1024)
   116  			n := enc.PutUvarint(test.x)
   117  			require.Equal(t, test.n, n)
   118  
   119  			dec := NewDecoder(enc.Bytes())
   120  			actual, err := dec.Uvarint()
   121  
   122  			require.NoError(t, err)
   123  			require.Equal(t, test.x, actual)
   124  		})
   125  	}
   126  }
   127  
   128  func TestBytes(t *testing.T) {
   129  	tests := []struct {
   130  		name string
   131  		b    []byte
   132  		n    int
   133  	}{
   134  		{
   135  			name: "Encode and Decode Empty Byte Slice",
   136  			b:    []byte(""),
   137  			n:    1,
   138  		},
   139  		{
   140  			name: "Encode and Decode Non-Empty Byte Slice",
   141  			b:    []byte("foo bar baz"),
   142  			n:    12,
   143  		},
   144  	}
   145  
   146  	for _, test := range tests {
   147  		t.Run(test.name, func(t *testing.T) {
   148  			enc := NewEncoder(1024)
   149  			n := enc.PutBytes(test.b)
   150  			require.Equal(t, test.n, n)
   151  
   152  			dec := NewDecoder(enc.Bytes())
   153  			actual, err := dec.Bytes()
   154  
   155  			require.NoError(t, err)
   156  			require.Equal(t, test.b, actual)
   157  		})
   158  	}
   159  }
   160  
   161  func TestEncoderLen(t *testing.T) {
   162  	enc := NewEncoder(1024)
   163  
   164  	enc.PutUint32(42)
   165  	require.Equal(t, 4, enc.Len())
   166  
   167  	enc.PutUint64(42)
   168  	require.Equal(t, 4+8, enc.Len())
   169  
   170  	enc.PutUvarint(42)
   171  	require.Equal(t, 4+8+1, enc.Len())
   172  
   173  	enc.PutBytes([]byte("42"))
   174  	require.Equal(t, 4+8+1+3, enc.Len())
   175  }
   176  
   177  func TestEncoderReset(t *testing.T) {
   178  	enc := NewEncoder(1024)
   179  	enc.PutUint32(42)
   180  	enc.Reset()
   181  
   182  	b := enc.Bytes()
   183  	require.Equal(t, 0, len(b))
   184  }
   185  
   186  func TestDecoderReset(t *testing.T) {
   187  	enc := NewEncoder(1024)
   188  	enc.PutUint32(42)
   189  	b := enc.Bytes()
   190  
   191  	dec := NewDecoder(nil)
   192  	dec.Reset(b)
   193  
   194  	require.Equal(t, b, dec.buf)
   195  }