util_test.go 1.38 KB
Newer Older
Juan Batiz-Benet's avatar
key  
Juan Batiz-Benet committed
1 2 3
package util

import (
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
4 5
	"bytes"
	"testing"
6

7
	mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
Juan Batiz-Benet's avatar
key  
Juan Batiz-Benet committed
8 9 10 11
)

func TestKey(t *testing.T) {

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
12 13 14 15
	h1, err := mh.Sum([]byte("beep boop"), mh.SHA2_256, -1)
	if err != nil {
		t.Error(err)
	}
Juan Batiz-Benet's avatar
key  
Juan Batiz-Benet committed
16

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
17 18 19
	k1 := Key(h1)
	h2 := mh.Multihash(k1)
	k2 := Key(h2)
Juan Batiz-Benet's avatar
key  
Juan Batiz-Benet committed
20

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
21 22 23
	if !bytes.Equal(h1, h2) {
		t.Error("Multihashes not equal.")
	}
Juan Batiz-Benet's avatar
key  
Juan Batiz-Benet committed
24

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
25 26 27
	if k1 != k2 {
		t.Error("Keys not equal.")
	}
Juan Batiz-Benet's avatar
key  
Juan Batiz-Benet committed
28
}
29

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31
func TestXOR(t *testing.T) {
	cases := [][3][]byte{
rht's avatar
rht committed
32 33 34 35
		{
			{0xFF, 0xFF, 0xFF},
			{0xFF, 0xFF, 0xFF},
			{0x00, 0x00, 0x00},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36
		},
rht's avatar
rht committed
37 38 39 40
		{
			{0x00, 0xFF, 0x00},
			{0xFF, 0xFF, 0xFF},
			{0xFF, 0x00, 0xFF},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
		},
rht's avatar
rht committed
42 43 44 45
		{
			{0x55, 0x55, 0x55},
			{0x55, 0xFF, 0xAA},
			{0x00, 0xAA, 0xFF},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46 47 48 49 50 51 52 53 54 55
		},
	}

	for _, c := range cases {
		r := XOR(c[0], c[1])
		if !bytes.Equal(r, c[2]) {
			t.Error("XOR failed")
		}
	}
}
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

func BenchmarkHash256K(b *testing.B) {
	buf := make([]byte, 256*1024)
	NewTimeSeededRand().Read(buf)
	b.SetBytes(int64(256 * 1024))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Hash(buf)
	}
}

func BenchmarkHash512K(b *testing.B) {
	buf := make([]byte, 512*1024)
	NewTimeSeededRand().Read(buf)
	b.SetBytes(int64(512 * 1024))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Hash(buf)
	}
}

func BenchmarkHash1M(b *testing.B) {
	buf := make([]byte, 1024*1024)
	NewTimeSeededRand().Read(buf)
	b.SetBytes(int64(1024 * 1024))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Hash(buf)
	}
}