package lattice import ( "testing" "unsafe" ) func TestBlockSizes(t *testing.T) { if s := unsafe.Sizeof(Node{}); s != NodeSize { t.Fatalf("Node size = %d, want %d", s, NodeSize) } if s := unsafe.Sizeof(Record{}); s != RecordSize { t.Fatalf("Record size = %d, want %d", s, RecordSize) } if s := unsafe.Sizeof(MetaEntry{}); s != 32 { t.Fatalf("MetaEntry size = %d, want 32", s) } if s := unsafe.Sizeof(LexEntry{}); s != 32 { t.Fatalf("LexEntry size = %d, want 32", s) } if s := unsafe.Sizeof(AdjLink{}); s != 8 { t.Fatalf("AdjLink size = %d, want 8", s) } // PendingExp is 6 bytes in memory and 4 on disk: Branch, Depth, Factor. // The journal is written field by field, so the Go padding is never // serialised. A writer that wrote the struct raw would emit 6 bytes a row // and a reader would see a rising Factor. if s := unsafe.Sizeof(PendingExp{}); s != 6 { t.Fatalf("PendingExp size = %d, want 6 in memory", s) } } func TestMakeKey(t *testing.T) { key := MakeKey(LangEN, 0x00ABCDEF12345678) if KeyLang(key) != LangEN { t.Fatalf("KeyLang = %d, want %d", KeyLang(key), LangEN) } if uint64(key) != 0x02ABCDEF12345678 { t.Fatalf("MakeKey = %#x", uint64(key)) } if MakeKey(LangBG, 0xFFFFFFFFFFFFFFFF) != 0x01FFFFFFFFFFFFFF { t.Fatalf("MakeKey must mask to 56 bits") } } func TestCodeKey(t *testing.T) { k := MakeCodeKey(2, 0x00ABCDEF12345678) if KeyStage(k) != 2 || KeyHash(k) != 0x00ABCDEF12345678 { t.Fatalf("MakeCodeKey round trip: stage %d hash %#x", KeyStage(k), KeyHash(k)) } } func TestHashBytesIsFNV1aTruncated(t *testing.T) { // FNV-1a of "hello" is 0xa430d84680aabd0b; truncated by >> 8. if got := HashBytes([]byte("hello")); got != 0xa430d84680aabd { t.Fatalf("HashBytes(hello) = %#x", got) } } func TestNormalize(t *testing.T) { if got := string(Normalize(" Hello ")); got != "hello" { t.Fatalf("Normalize = %q", got) } if got := string(Normalize("")); got != "" { t.Fatalf("Normalize empty = %q", got) } }