github.com/m3db/m3@v1.5.0/src/dbnode/digest/buffer_test.go (about)

     1  // Copyright (c) 2016 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 digest
    22  
    23  import (
    24  	"io/ioutil"
    25  	"os"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestWriteDigest(t *testing.T) {
    32  	buf := NewBuffer()
    33  	buf.WriteDigest(2)
    34  	require.Equal(t, []byte{0x2, 0x0, 0x0, 0x0}, []byte(buf))
    35  }
    36  
    37  func TestWriteDigestToFile(t *testing.T) {
    38  	fd := createTempFile(t)
    39  	filePath := fd.Name()
    40  	defer os.Remove(filePath)
    41  
    42  	buf := NewBuffer()
    43  	require.NoError(t, buf.WriteDigestToFile(fd, 20))
    44  	fd.Close()
    45  
    46  	fd, err := os.Open(filePath)
    47  	require.NoError(t, err)
    48  	b, err := ioutil.ReadAll(fd)
    49  	require.NoError(t, err)
    50  	fd.Close()
    51  	require.Equal(t, []byte{0x14, 0x0, 0x0, 0x0}, b)
    52  }
    53  
    54  func TestReadDigest(t *testing.T) {
    55  	buf := ToBuffer([]byte{0x0, 0x1, 0x0, 0x1, 0x0, 0x1})
    56  	require.Equal(t, uint32(0x1000100), buf.ReadDigest())
    57  }
    58  
    59  func TestReadDigestFromFile(t *testing.T) {
    60  	fd := createTempFile(t)
    61  	defer func() {
    62  		fd.Close()
    63  		os.Remove(fd.Name())
    64  	}()
    65  
    66  	b := []byte{0xa, 0x0, 0x0, 0x0}
    67  	fd.Write(b)
    68  	fd.Seek(0, 0)
    69  
    70  	buf := NewBuffer()
    71  	res, err := buf.ReadDigestFromFile(fd)
    72  	require.NoError(t, err)
    73  	require.Equal(t, uint32(10), res)
    74  }