1 // Copyright (c) 2014, Suryandaru Triandana <syndtr@gmail.com>
2 // All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
10 . "github.com/onsi/ginkgo"
11 . "github.com/onsi/gomega"
13 "github.com/syndtr/goleveldb/leveldb/comparer"
14 "github.com/syndtr/goleveldb/leveldb/iterator"
15 "github.com/syndtr/goleveldb/leveldb/testutil"
16 "github.com/syndtr/goleveldb/leveldb/util"
19 func (p *DB) TestFindLT(key []byte) (rkey, value []byte, err error) {
21 if node := p.findLT(key); node != 0 {
23 m := n + p.nodeData[node+nKey]
25 value = p.kvData[m : m+p.nodeData[node+nVal]]
33 func (p *DB) TestFindLast() (rkey, value []byte, err error) {
35 if node := p.findLast(); node != 0 {
37 m := n + p.nodeData[node+nKey]
39 value = p.kvData[m : m+p.nodeData[node+nVal]]
47 func (p *DB) TestPut(key []byte, value []byte) error {
52 func (p *DB) TestDelete(key []byte) error {
57 func (p *DB) TestFind(key []byte) (rkey, rvalue []byte, err error) {
61 func (p *DB) TestGet(key []byte) (value []byte, err error) {
65 func (p *DB) TestNewIterator(slice *util.Range) iterator.Iterator {
66 return p.NewIterator(slice)
69 var _ = testutil.Defer(func() {
70 Describe("Memdb", func() {
71 Describe("write test", func() {
72 It("should do write correctly", func() {
73 db := New(comparer.DefaultComparer, 0)
74 t := testutil.DBTesting{
76 Deleted: testutil.KeyValue_Generate(nil, 1000, 1, 1, 30, 5, 5).Clone(),
77 PostFn: func(t *testutil.DBTesting) {
78 Expect(db.Len()).Should(Equal(t.Present.Len()))
79 Expect(db.Size()).Should(Equal(t.Present.Size()))
81 case testutil.DBPut, testutil.DBOverwrite:
82 Expect(db.Contains(t.ActKey)).Should(BeTrue())
84 Expect(db.Contains(t.ActKey)).Should(BeFalse())
88 testutil.DoDBTesting(&t)
92 Describe("read test", func() {
93 testutil.AllKeyValueTesting(nil, func(kv testutil.KeyValue) testutil.DB {
95 db := New(comparer.DefaultComparer, 0)
96 kv.IterateShuffled(nil, func(i int, key, value []byte) {
101 It("Should find correct keys with findLT", func() {
102 testutil.ShuffledIndex(nil, kv.Len()-1, 1, func(i int) {
103 key_, key, _ := kv.IndexInexact(i + 1)
104 expectedKey, expectedValue := kv.Index(i)
106 // Using key that exist.
107 rkey, rvalue, err := db.TestFindLT(key)
108 Expect(err).ShouldNot(HaveOccurred(), "Error for key %q -> %q", key, expectedKey)
109 Expect(rkey).Should(Equal(expectedKey), "Key")
110 Expect(rvalue).Should(Equal(expectedValue), "Value for key %q -> %q", key, expectedKey)
112 // Using key that doesn't exist.
113 rkey, rvalue, err = db.TestFindLT(key_)
114 Expect(err).ShouldNot(HaveOccurred(), "Error for key %q (%q) -> %q", key_, key, expectedKey)
115 Expect(rkey).Should(Equal(expectedKey))
116 Expect(rvalue).Should(Equal(expectedValue), "Value for key %q (%q) -> %q", key_, key, expectedKey)
122 It("Should find last key with findLast", func() {
123 key, value := kv.Index(kv.Len() - 1)
124 rkey, rvalue, err := db.TestFindLast()
125 Expect(err).ShouldNot(HaveOccurred())
126 Expect(rkey).Should(Equal(key))
127 Expect(rvalue).Should(Equal(value))