github.com/m3db/m3@v1.5.0/src/query/util/writer/int_writer_test.go (about)

     1  // Copyright (c) 2019 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 writer
    22  
    23  import (
    24  	"fmt"
    25  	"math"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestIntegerLength(t *testing.T) {
    33  	for i := 0; i < 18; i++ {
    34  		small := int(math.Pow(10, float64(i)))
    35  		large := small*10 - 1
    36  		expected := i + 1
    37  		assert.Equal(t, expected, IntLength(small))
    38  		assert.Equal(t, expected, IntLength(large))
    39  	}
    40  
    41  	assert.Equal(t, 19, IntLength(1000000000000000000))
    42  }
    43  
    44  func TestWriteInteger(t *testing.T) {
    45  	ints := make([]int, 105)
    46  	for i := range ints {
    47  		l := IntLength(i)
    48  		buf := make([]byte, l)
    49  		idx := WriteInteger(buf, i, 0)
    50  		assert.Equal(t, l, idx)
    51  		assert.Equal(t, []byte(fmt.Sprint(i)), buf)
    52  	}
    53  }
    54  
    55  func TestWriteIntegersAtIndex(t *testing.T) {
    56  	l := IntLength(345) + IntLength(12)
    57  	buf := make([]byte, l)
    58  	idx := WriteInteger(buf, 12, 0)
    59  	idx = WriteInteger(buf, 345, idx)
    60  	assert.Equal(t, 5, idx)
    61  	assert.Equal(t, []byte("12345"), buf)
    62  }
    63  
    64  func TestWriteIntegersSingle(t *testing.T) {
    65  	sep := byte('!')
    66  	ints := []int{1}
    67  	l := IntsLength(ints)
    68  	assert.Equal(t, 1, l)
    69  
    70  	buf := make([]byte, l)
    71  	idx := WriteIntegers(buf, ints, sep, 0)
    72  	assert.Equal(t, l, idx)
    73  	expected := []byte("1")
    74  	assert.Equal(t, expected, buf)
    75  
    76  	ints = []int{10}
    77  	l = IntsLength(ints)
    78  	assert.Equal(t, 2, l)
    79  	buf = make([]byte, l)
    80  	idx = WriteIntegers(buf, ints, sep, 0)
    81  	assert.Equal(t, l, idx)
    82  	expected = []byte("10")
    83  	assert.Equal(t, expected, buf)
    84  }
    85  
    86  func TestWriteIntegersSingleAtIndex(t *testing.T) {
    87  	sep := byte('!')
    88  	ints := []int{1}
    89  	buf := make([]byte, 2)
    90  	buf[0] = byte('?')
    91  	idx := WriteIntegers(buf, ints, sep, 1)
    92  	assert.Equal(t, 2, idx)
    93  	expected := []byte("?1")
    94  	assert.Equal(t, expected, buf)
    95  
    96  	idx = 0
    97  	idx = WriteIntegers(buf, ints, sep, idx)
    98  	idx = WriteIntegers(buf, ints, sep, idx)
    99  	assert.Equal(t, 2, idx)
   100  	expected = []byte("11")
   101  	assert.Equal(t, expected, buf)
   102  }
   103  
   104  func TestWriteIntegersMultiple(t *testing.T) {
   105  	sep := byte('!')
   106  	ints := []int{1, 2}
   107  	l := IntsLength(ints)
   108  	assert.Equal(t, 3, l)
   109  
   110  	buf := make([]byte, l)
   111  	idx := WriteIntegers(buf, ints, sep, 0)
   112  	assert.Equal(t, l, idx)
   113  	expected := []byte("1!2")
   114  	require.Equal(t, expected, buf)
   115  
   116  	ints = []int{10, 20}
   117  	l = IntsLength(ints)
   118  	assert.Equal(t, 5, l)
   119  	buf = make([]byte, l)
   120  	idx = WriteIntegers(buf, ints, sep, 0)
   121  	assert.Equal(t, l, idx)
   122  	expected = []byte("10!20")
   123  	assert.Equal(t, expected, buf)
   124  }
   125  
   126  func TestWriteIntegersMultipleAtIndex(t *testing.T) {
   127  	sep := byte('!')
   128  	ints := []int{1, 20, 300, 4000, 50000}
   129  	l := IntsLength(ints)
   130  	assert.Equal(t, 19, l)
   131  
   132  	buf := make([]byte, l+2)
   133  	buf[0] = '?'
   134  	buf[l+1] = '?'
   135  	idx := WriteIntegers(buf, ints, sep, 1)
   136  	assert.Equal(t, l+1, idx)
   137  	expected := []byte("?1!20!300!4000!50000?")
   138  	require.Equal(t, expected, buf)
   139  }