| 1 | package lmdb |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "reflect" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestMultiVal(t *testing.T) { |
| 10 | data := []byte("abcdef") |
| 11 | m := WrapMulti(data, 2) |
| 12 | vals := m.Vals() |
| 13 | if !reflect.DeepEqual(vals, [][]byte{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}) { |
| 14 | t.Errorf("unexpected vals: %q", vals) |
| 15 | } |
| 16 | size := m.Size() |
| 17 | if size != 6 { |
| 18 | t.Errorf("unexpected size: %v (!= %v)", size, 6) |
| 19 | } |
| 20 | length := m.Len() |
| 21 | if length != 3 { |
| 22 | t.Errorf("unexpected length: %v (!= %v)", length, 3) |
| 23 | } |
| 24 | stride := m.Stride() |
| 25 | if stride != 2 { |
| 26 | t.Errorf("unexpected stride: %v (!= %v)", stride, 2) |
| 27 | } |
| 28 | page := m.Page() |
| 29 | if !bytes.Equal(page, data) { |
| 30 | t.Errorf("unexpected page: %v (!= %v)", page, data) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestMultiVal_panic(t *testing.T) { |
| 35 | var p bool |
| 36 | defer func() { |
| 37 | if e := recover(); e != nil { |
| 38 | p = true |
| 39 | } |
| 40 | if !p { |
| 41 | t.Errorf("expected a panic") |
| 42 | } |
| 43 | }() |
| 44 | WrapMulti([]byte("123"), 2) |
| 45 | } |
| 46 | |
| 47 | func TestValBytes(t *testing.T) { |
| 48 | ptr, n := valBytes(nil) |
| 49 | if len(ptr) == 0 { |
| 50 | t.Errorf("unexpected unaddressable slice") |
| 51 | } |
| 52 | if n != 0 { |
| 53 | t.Errorf("unexpected length: %d (expected 0)", n) |
| 54 | } |
| 55 | |
| 56 | b := []byte("abc") |
| 57 | ptr, n = valBytes(b) |
| 58 | if len(ptr) == 0 { |
| 59 | t.Errorf("unexpected unaddressable slice") |
| 60 | } |
| 61 | if n != 3 { |
| 62 | t.Errorf("unexpected length: %d (expected %d)", n, len(b)) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestVal(t *testing.T) { |
| 67 | orig := []byte("hey hey") |
| 68 | val := wrapVal(orig) |
| 69 | |
| 70 | p := getBytes(val) |
| 71 | if !bytes.Equal(p, orig) { |
| 72 | t.Errorf("getBytes() not the same as original data: %q", p) |
| 73 | } |
| 74 | if &p[0] != &orig[0] { |
| 75 | t.Errorf("getBytes() is not the same slice as original") |
| 76 | } |
| 77 | |
| 78 | p = getBytesCopy(val) |
| 79 | if !bytes.Equal(p, orig) { |
| 80 | t.Errorf("getBytesCopy() not the same as original data: %q", p) |
| 81 | } |
| 82 | if &p[0] == &orig[0] { |
| 83 | t.Errorf("getBytesCopy() overlaps with orignal slice") |
| 84 | } |
| 85 | } |