reshetka_test.go raw

   1  package lattice
   2  
   3  import (
   4  	"testing"
   5  	"unsafe"
   6  )
   7  
   8  func TestBlockSizes(t *testing.T) {
   9  	if s := unsafe.Sizeof(Node{}); s != NodeSize {
  10  		t.Fatalf("Node size = %d, want %d", s, NodeSize)
  11  	}
  12  	if s := unsafe.Sizeof(Record{}); s != RecordSize {
  13  		t.Fatalf("Record size = %d, want %d", s, RecordSize)
  14  	}
  15  	if s := unsafe.Sizeof(MetaEntry{}); s != 32 {
  16  		t.Fatalf("MetaEntry size = %d, want 32", s)
  17  	}
  18  	if s := unsafe.Sizeof(LexEntry{}); s != 32 {
  19  		t.Fatalf("LexEntry size = %d, want 32", s)
  20  	}
  21  	if s := unsafe.Sizeof(AdjLink{}); s != 8 {
  22  		t.Fatalf("AdjLink size = %d, want 8", s)
  23  	}
  24  	// PendingExp is 6 bytes in memory and 4 on disk: Branch, Depth, Factor.
  25  	// The journal is written field by field, so the Go padding is never
  26  	// serialised. A writer that wrote the struct raw would emit 6 bytes a row
  27  	// and a reader would see a rising Factor.
  28  	if s := unsafe.Sizeof(PendingExp{}); s != 6 {
  29  		t.Fatalf("PendingExp size = %d, want 6 in memory", s)
  30  	}
  31  }
  32  
  33  func TestMakeKey(t *testing.T) {
  34  	key := MakeKey(LangEN, 0x00ABCDEF12345678)
  35  	if KeyLang(key) != LangEN {
  36  		t.Fatalf("KeyLang = %d, want %d", KeyLang(key), LangEN)
  37  	}
  38  	if uint64(key) != 0x02ABCDEF12345678 {
  39  		t.Fatalf("MakeKey = %#x", uint64(key))
  40  	}
  41  	if MakeKey(LangBG, 0xFFFFFFFFFFFFFFFF) != 0x01FFFFFFFFFFFFFF {
  42  		t.Fatalf("MakeKey must mask to 56 bits")
  43  	}
  44  }
  45  
  46  func TestCodeKey(t *testing.T) {
  47  	k := MakeCodeKey(2, 0x00ABCDEF12345678)
  48  	if KeyStage(k) != 2 || KeyHash(k) != 0x00ABCDEF12345678 {
  49  		t.Fatalf("MakeCodeKey round trip: stage %d hash %#x", KeyStage(k), KeyHash(k))
  50  	}
  51  }
  52  
  53  func TestHashBytesIsFNV1aTruncated(t *testing.T) {
  54  	// FNV-1a of "hello" is 0xa430d84680aabd0b; truncated by >> 8.
  55  	if got := HashBytes([]byte("hello")); got != 0xa430d84680aabd {
  56  		t.Fatalf("HashBytes(hello) = %#x", got)
  57  	}
  58  }
  59  
  60  func TestNormalize(t *testing.T) {
  61  	if got := string(Normalize("  Hello  ")); got != "hello" {
  62  		t.Fatalf("Normalize = %q", got)
  63  	}
  64  	if got := string(Normalize("")); got != "" {
  65  		t.Fatalf("Normalize empty = %q", got)
  66  	}
  67  }
  68