smallindex_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (C) 2018 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package db
  7. import "testing"
  8. func TestSmallIndex(t *testing.T) {
  9. db := OpenMemory()
  10. idx := newSmallIndex(db.DB, []byte{12, 34})
  11. // ID zero should be unallocated
  12. if val, ok := idx.Val(0); ok || val != nil {
  13. t.Fatal("Unexpected return for nonexistent ID 0")
  14. }
  15. // A new key should get ID zero
  16. if id := idx.ID([]byte("hello")); id != 0 {
  17. t.Fatal("Expected 0, not", id)
  18. }
  19. // Looking up ID zero should work
  20. if val, ok := idx.Val(0); !ok || string(val) != "hello" {
  21. t.Fatalf(`Expected true, "hello", not %v, %q`, ok, val)
  22. }
  23. // Delete the key
  24. idx.Delete([]byte("hello"))
  25. // Next ID should be one
  26. if id := idx.ID([]byte("key2")); id != 1 {
  27. t.Fatal("Expected 1, not", id)
  28. }
  29. // Now lets create a new index instance based on what's actually serialized to the database.
  30. idx = newSmallIndex(db.DB, []byte{12, 34})
  31. // Status should be about the same as before.
  32. if val, ok := idx.Val(0); ok || val != nil {
  33. t.Fatal("Unexpected return for deleted ID 0")
  34. }
  35. if id := idx.ID([]byte("key2")); id != 1 {
  36. t.Fatal("Expected 1, not", id)
  37. }
  38. // Setting "hello" again should get us ID 2, not 0 as it was originally.
  39. if id := idx.ID([]byte("hello")); id != 2 {
  40. t.Fatal("Expected 2, not", id)
  41. }
  42. }