github.com/grailbio/base@v0.0.11/writehash/writehash_test.go (about) 1 // Copyright 2019 GRAIL, Inc. All rights reserved. 2 // Use of this source code is governed by the Apache 2.0 3 // license that can be found in the LICENSE file. 4 5 package writehash_test 6 7 import ( 8 "bytes" 9 "io" 10 "testing" 11 12 "github.com/grailbio/base/writehash" 13 ) 14 15 type fakeHasher struct{ io.Writer } 16 17 func (fakeHasher) Sum([]byte) []byte { panic("sum") } 18 func (fakeHasher) Reset() { panic("reset") } 19 func (fakeHasher) Size() int { panic("size") } 20 func (fakeHasher) BlockSize() int { panic("blocksize") } 21 22 func TestWritehash(t *testing.T) { 23 b := new(bytes.Buffer) 24 var lastLen int 25 check := func(n int) { 26 t.Helper() 27 if got, want := b.Len()-lastLen, n; got != want { 28 t.Fatalf("got %v, want %v", got, want) 29 } 30 if bytes.Equal(b.Bytes()[lastLen:], make([]byte, n)) { 31 t.Error("wrote zeros") 32 } 33 lastLen = b.Len() 34 } 35 h := fakeHasher{b} 36 writehash.String(h, "hello world") 37 check(11) 38 writehash.Int(h, 1) 39 check(8) 40 writehash.Int16(h, 1) 41 check(2) 42 writehash.Int32(h, 1) 43 check(4) 44 writehash.Int64(h, 1) 45 check(8) 46 writehash.Uint(h, 1) 47 check(8) 48 writehash.Uint16(h, 1) 49 check(2) 50 writehash.Uint32(h, 1) 51 check(4) 52 writehash.Float32(h, 1) 53 check(4) 54 writehash.Float64(h, 1) 55 check(8) 56 writehash.Bool(h, true) 57 check(1) 58 writehash.Byte(h, 1) 59 check(1) 60 writehash.Rune(h, 'x') 61 check(8) 62 }