| 1 |
package bolt |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"sort" |
| 6 |
"testing" |
| 7 |
"testing/quick" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
func TestPage_typ(t *testing.T) { |
| 12 |
if typ := (&page{flags: branchPageFlag}).typ(); typ != "branch" { |
| 13 |
t.Fatalf("exp=branch; got=%v", typ) |
| 14 |
} |
| 15 |
if typ := (&page{flags: leafPageFlag}).typ(); typ != "leaf" { |
| 16 |
t.Fatalf("exp=leaf; got=%v", typ) |
| 17 |
} |
| 18 |
if typ := (&page{flags: metaPageFlag}).typ(); typ != "meta" { |
| 19 |
t.Fatalf("exp=meta; got=%v", typ) |
| 20 |
} |
| 21 |
if typ := (&page{flags: freelistPageFlag}).typ(); typ != "freelist" { |
| 22 |
t.Fatalf("exp=freelist; got=%v", typ) |
| 23 |
} |
| 24 |
if typ := (&page{flags: 20000}).typ(); typ != "unknown<4e20>" { |
| 25 |
t.Fatalf("exp=unknown<4e20>; got=%v", typ) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func TestPage_dump(t *testing.T) { |
| 31 |
(&page{id: 256}).hexdump(16) |
| 32 |
} |
| 33 |
|
| 34 |
func TestPgids_merge(t *testing.T) { |
| 35 |
a := pgids{4, 5, 6, 10, 11, 12, 13, 27} |
| 36 |
b := pgids{1, 3, 8, 9, 25, 30} |
| 37 |
c := a.merge(b) |
| 38 |
if !reflect.DeepEqual(c, pgids{1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30}) { |
| 39 |
t.Errorf("mismatch: %v", c) |
| 40 |
} |
| 41 |
|
| 42 |
a = pgids{4, 5, 6, 10, 11, 12, 13, 27, 35, 36} |
| 43 |
b = pgids{8, 9, 25, 30} |
| 44 |
c = a.merge(b) |
| 45 |
if !reflect.DeepEqual(c, pgids{4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30, 35, 36}) { |
| 46 |
t.Errorf("mismatch: %v", c) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func TestPgids_merge_quick(t *testing.T) { |
| 51 |
if err := quick.Check(func(a, b pgids) bool { |
| 52 |
|
| 53 |
sort.Sort(a) |
| 54 |
sort.Sort(b) |
| 55 |
|
| 56 |
|
| 57 |
got := a.merge(b) |
| 58 |
|
| 59 |
|
| 60 |
exp := append(a, b...) |
| 61 |
sort.Sort(exp) |
| 62 |
|
| 63 |
if !reflect.DeepEqual(exp, got) { |
| 64 |
t.Errorf("\nexp=%+v\ngot=%+v\n", exp, got) |
| 65 |
return false |
| 66 |
} |
| 67 |
|
| 68 |
return true |
| 69 |
}, nil); err != nil { |
| 70 |
t.Fatal(err) |
| 71 |
} |
| 72 |
} |