page_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package bbolt
  2. import (
  3. "reflect"
  4. "sort"
  5. "testing"
  6. "testing/quick"
  7. )
  8. // Ensure that the page type can be returned in human readable format.
  9. func TestPage_typ(t *testing.T) {
  10. if typ := (&page{flags: branchPageFlag}).typ(); typ != "branch" {
  11. t.Fatalf("exp=branch; got=%v", typ)
  12. }
  13. if typ := (&page{flags: leafPageFlag}).typ(); typ != "leaf" {
  14. t.Fatalf("exp=leaf; got=%v", typ)
  15. }
  16. if typ := (&page{flags: metaPageFlag}).typ(); typ != "meta" {
  17. t.Fatalf("exp=meta; got=%v", typ)
  18. }
  19. if typ := (&page{flags: freelistPageFlag}).typ(); typ != "freelist" {
  20. t.Fatalf("exp=freelist; got=%v", typ)
  21. }
  22. if typ := (&page{flags: 20000}).typ(); typ != "unknown<4e20>" {
  23. t.Fatalf("exp=unknown<4e20>; got=%v", typ)
  24. }
  25. }
  26. // Ensure that the hexdump debugging function doesn't blow up.
  27. func TestPage_dump(t *testing.T) {
  28. (&page{id: 256}).hexdump(16)
  29. }
  30. func TestPgids_merge(t *testing.T) {
  31. a := pgids{4, 5, 6, 10, 11, 12, 13, 27}
  32. b := pgids{1, 3, 8, 9, 25, 30}
  33. c := a.merge(b)
  34. if !reflect.DeepEqual(c, pgids{1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30}) {
  35. t.Errorf("mismatch: %v", c)
  36. }
  37. a = pgids{4, 5, 6, 10, 11, 12, 13, 27, 35, 36}
  38. b = pgids{8, 9, 25, 30}
  39. c = a.merge(b)
  40. if !reflect.DeepEqual(c, pgids{4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30, 35, 36}) {
  41. t.Errorf("mismatch: %v", c)
  42. }
  43. }
  44. func TestPgids_merge_quick(t *testing.T) {
  45. if err := quick.Check(func(a, b pgids) bool {
  46. // Sort incoming lists.
  47. sort.Sort(a)
  48. sort.Sort(b)
  49. // Merge the two lists together.
  50. got := a.merge(b)
  51. // The expected value should be the two lists combined and sorted.
  52. exp := append(a, b...)
  53. sort.Sort(exp)
  54. if !reflect.DeepEqual(exp, got) {
  55. t.Errorf("\nexp=%+v\ngot=%+v\n", exp, got)
  56. return false
  57. }
  58. return true
  59. }, nil); err != nil {
  60. t.Fatal(err)
  61. }
  62. }