diff options
author | Kevin Chabowski <kevin@kch42.de> | 2014-03-21 15:06:29 +0100 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2014-03-21 15:06:29 +0100 |
commit | ba25a3a15f617b6580523ccfb83d31764a50a97a (patch) | |
tree | e4448627bc83a16a3dd73d52f15ef3d36c6f9a24 | |
parent | 9f0a8395b31e30634296145219392770b2b0ffff (diff) | |
download | buzhash-ba25a3a15f617b6580523ccfb83d31764a50a97a.tar.gz buzhash-ba25a3a15f617b6580523ccfb83d31764a50a97a.tar.bz2 buzhash-ba25a3a15f617b6580523ccfb83d31764a50a97a.zip |
Moar tests
(Test coverage of 94% \o/)
-rw-r--r-- | hash_test.go | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/hash_test.go b/hash_test.go index 0f915c9..6b3993a 100644 --- a/hash_test.go +++ b/hash_test.go @@ -1,13 +1,13 @@ package buzhash import ( + "bytes" + "encoding/binary" "fmt" "testing" ) -// Test the rolling hash property of the buzhash -func TestRollingHash(t *testing.T) { - loremipsum1 := `Lorem ipsum dolor sit amet, consectetuer adipiscing elit. +var loremipsum1 = `Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis @@ -32,7 +32,7 @@ nonummy id, metus. Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia.` - loremipsum2 := `Nam pretium turpis et arcu. Duis arcu tortor, suscipit eget, +var loremipsum2 = `Nam pretium turpis et arcu. Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Sed aliquam ultrices mauris. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. @@ -51,6 +51,8 @@ placerat dolor lectus quis orci. Phasellus consectetuer vestibulum elit. Aenean tellus metus, bibendum sed, posuere ac, mattis non, nunc. Vestibulum fringilla pede sit amet augue. In turpis. Pellentesque posuere. Praesent turpis.` +// Test the rolling hash property of the buzhash +func TestRollingHash(t *testing.T) { phrase1 := "Aenean massa. Cum sociis natoque" phrase2 := "Phasellus leo dolor, tempus non, auctor et, hendrerit quis, nisi" @@ -90,3 +92,24 @@ pede sit amet augue. In turpis. Pellentesque posuere. Praesent turpis.` t.Errorf("Could not find '%s' by its checksum %08x.", phrase2, p2sum) } } + +func TestSum(t *testing.T) { + h := NewBuzHash(16) + fmt.Fprint(h, loremipsum1) + + sum32 := h.Sum32() + sumBytes := h.Sum(nil) + + if l := len(sumBytes); l != 4 { + t.Fatalf("h.Sum() returned slice of len %d, expected 4", l) + } + + var sumBytesAsNum uint32 + if err := binary.Read(bytes.NewBuffer(sumBytes), binary.LittleEndian, &sumBytesAsNum); err != nil { + t.Fatalf("Could not read binary number? %s", err) + } + + if sum32 != sumBytesAsNum { + t.Errorf("Sum32 (%08x) and Sum (%08x) returned different sums!", sum32, sumBytesAsNum) + } +} |