File: val_test.go

package info (click to toggle)
golang-github-bmatsuo-lmdb-go 1.8.0%2Bgit20170215.a14b5a3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 888 kB
  • sloc: ansic: 8,247; makefile: 19
file content (85 lines) | stat: -rw-r--r-- 1,740 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package lmdb

import (
	"bytes"
	"reflect"
	"testing"
)

func TestMultiVal(t *testing.T) {
	data := []byte("abcdef")
	m := WrapMulti(data, 2)
	vals := m.Vals()
	if !reflect.DeepEqual(vals, [][]byte{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}) {
		t.Errorf("unexpected vals: %q", vals)
	}
	size := m.Size()
	if size != 6 {
		t.Errorf("unexpected size: %v (!= %v)", size, 6)
	}
	length := m.Len()
	if length != 3 {
		t.Errorf("unexpected length: %v (!= %v)", length, 3)
	}
	stride := m.Stride()
	if stride != 2 {
		t.Errorf("unexpected stride: %v (!= %v)", stride, 2)
	}
	page := m.Page()
	if !bytes.Equal(page, data) {
		t.Errorf("unexpected page: %v (!= %v)", page, data)
	}
}

func TestMultiVal_panic(t *testing.T) {
	var p bool
	defer func() {
		if e := recover(); e != nil {
			p = true
		}
		if !p {
			t.Errorf("expected a panic")
		}
	}()
	WrapMulti([]byte("123"), 2)
}

func TestValBytes(t *testing.T) {
	ptr, n := valBytes(nil)
	if len(ptr) == 0 {
		t.Errorf("unexpected unaddressable slice")
	}
	if n != 0 {
		t.Errorf("unexpected length: %d (expected 0)", n)
	}

	b := []byte("abc")
	ptr, n = valBytes(b)
	if len(ptr) == 0 {
		t.Errorf("unexpected unaddressable slice")
	}
	if n != 3 {
		t.Errorf("unexpected length: %d (expected %d)", n, len(b))
	}
}

func TestVal(t *testing.T) {
	orig := []byte("hey hey")
	val := wrapVal(orig)

	p := getBytes(val)
	if !bytes.Equal(p, orig) {
		t.Errorf("getBytes() not the same as original data: %q", p)
	}
	if &p[0] != &orig[0] {
		t.Errorf("getBytes() is not the same slice as original")
	}

	p = getBytesCopy(val)
	if !bytes.Equal(p, orig) {
		t.Errorf("getBytesCopy() not the same as original data: %q", p)
	}
	if &p[0] == &orig[0] {
		t.Errorf("getBytesCopy() overlaps with orignal slice")
	}
}