xor_test.go 3.53 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7
package keyspace

import (
	"bytes"
	"math/big"
	"testing"

8
	u "gitlab.dms3.io/dms3/go-dms3-util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11 12

func TestPrefixLen(t *testing.T) {
	cases := [][]byte{
rht's avatar
rht committed
13 14 15
		{0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00},
		{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
		{0x00, 0x58, 0xFF, 0x80, 0x00, 0x00, 0xF0},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
	}
	lens := []int{24, 56, 9}

	for i, c := range cases {
		r := ZeroPrefixLen(c)
		if r != lens[i] {
			t.Errorf("ZeroPrefixLen failed: %v != %v", r, lens[i])
		}
	}

}

func TestXorKeySpace(t *testing.T) {

	ids := [][]byte{
rht's avatar
rht committed
31 32 33
		{0xFF, 0xFF, 0xFF, 0xFF},
		{0x00, 0x00, 0x00, 0x00},
		{0xFF, 0xFF, 0xFF, 0xF0},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35 36
	}

	ks := [][2]Key{
rht's avatar
rht committed
37 38 39
		{XORKeySpace.Key(ids[0]), XORKeySpace.Key(ids[0])},
		{XORKeySpace.Key(ids[1]), XORKeySpace.Key(ids[1])},
		{XORKeySpace.Key(ids[2]), XORKeySpace.Key(ids[2])},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40 41 42 43 44 45 46
	}

	for i, set := range ks {
		if !set[0].Equal(set[1]) {
			t.Errorf("Key not eq. %v != %v", set[0], set[1])
		}

47 48
		if !bytes.Equal(set[0].Bytes, set[1].Bytes) {
			t.Errorf("Key gen failed. %v != %v", set[0].Bytes, set[1].Bytes)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49 50 51 52 53 54
		}

		if !bytes.Equal(set[0].Original, ids[i]) {
			t.Errorf("ptrs to original. %v != %v", set[0].Original, ids[i])
		}

55 56
		if len(set[0].Bytes) != 32 {
			t.Errorf("key length incorrect. 32 != %d", len(set[0].Bytes))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
		}
	}

	for i := 1; i < len(ks); i++ {
		if ks[i][0].Less(ks[i-1][0]) == ks[i-1][0].Less(ks[i][0]) {
			t.Errorf("less should be different.")
		}

		if ks[i][0].Distance(ks[i-1][0]).Cmp(ks[i-1][0].Distance(ks[i][0])) != 0 {
			t.Errorf("distance should be the same.")
		}

		if ks[i][0].Equal(ks[i-1][0]) {
			t.Errorf("Keys should not be eq. %v != %v", ks[i][0], ks[i-1][0])
		}
	}
}

func TestDistancesAndCenterSorting(t *testing.T) {

	adjs := [][]byte{
rht's avatar
rht committed
78 79 80 81 82 83
		{173, 149, 19, 27, 192, 183, 153, 192, 177, 175, 71, 127, 177, 79, 207, 38, 166, 169, 247, 96, 121, 228, 139, 240, 144, 172, 183, 232, 54, 123, 253, 14},
		{223, 63, 97, 152, 4, 169, 47, 219, 64, 87, 25, 45, 196, 61, 215, 72, 234, 119, 138, 220, 82, 188, 73, 140, 232, 5, 36, 192, 20, 184, 17, 25},
		{73, 176, 221, 176, 149, 143, 22, 42, 129, 124, 213, 114, 232, 95, 189, 154, 18, 3, 122, 132, 32, 199, 53, 185, 58, 157, 117, 78, 52, 146, 157, 127},
		{73, 176, 221, 176, 149, 143, 22, 42, 129, 124, 213, 114, 232, 95, 189, 154, 18, 3, 122, 132, 32, 199, 53, 185, 58, 157, 117, 78, 52, 146, 157, 127},
		{73, 176, 221, 176, 149, 143, 22, 42, 129, 124, 213, 114, 232, 95, 189, 154, 18, 3, 122, 132, 32, 199, 53, 185, 58, 157, 117, 78, 52, 146, 157, 126},
		{73, 0, 221, 176, 149, 143, 22, 42, 129, 124, 213, 114, 232, 95, 189, 154, 18, 3, 122, 132, 32, 199, 53, 185, 58, 157, 117, 78, 52, 146, 157, 127},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84 85 86 87
	}

	keys := make([]Key, len(adjs))
	for i, a := range adjs {
88
		keys[i] = Key{Space: XORKeySpace, Bytes: a}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
89 90
	}

91 92
	cmp := func(a int64, b *big.Int) int {
		return big.NewInt(a).Cmp(b)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93 94
	}

Marten Seemann's avatar
Marten Seemann committed
95
	if cmp(0, keys[2].Distance(keys[3])) != 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96 97 98
		t.Errorf("distance calculation wrong: %v", keys[2].Distance(keys[3]))
	}

Marten Seemann's avatar
Marten Seemann committed
99
	if cmp(1, keys[2].Distance(keys[4])) != 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100 101 102 103
		t.Errorf("distance calculation wrong: %v", keys[2].Distance(keys[4]))
	}

	d1 := keys[2].Distance(keys[5])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104
	d2 := u.XOR(keys[2].Bytes, keys[5].Bytes)
105
	d2 = d2[len(keys[2].Bytes)-len(d1.Bytes()):] // skip empty space for big
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
106 107 108 109 110 111 112 113 114 115 116
	if !bytes.Equal(d1.Bytes(), d2) {
		t.Errorf("bytes should be the same. %v == %v", d1.Bytes(), d2)
	}

	if -1 != cmp(2<<32, keys[2].Distance(keys[5])) {
		t.Errorf("2<<32 should be smaller")
	}

	keys2 := SortByDistance(XORKeySpace, keys[2], keys)
	order := []int{2, 3, 4, 5, 1, 0}
	for i, o := range order {
117
		if !bytes.Equal(keys[o].Bytes, keys2[i].Bytes) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
118 119 120 121 122
			t.Errorf("order is wrong. %d?? %v == %v", o, keys[o], keys2[i])
		}
	}

}