github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/syndtr/goleveldb/leveldb/memdb/memdb_test.go (about) 1 // Copyright (c) 2014, Suryandaru Triandana <syndtr@gmail.com> 2 // All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 package memdb 8 9 import ( 10 . "github.com/insionng/yougam/libraries/onsi/ginkgo" 11 . "github.com/insionng/yougam/libraries/onsi/gomega" 12 13 "github.com/insionng/yougam/libraries/syndtr/goleveldb/leveldb/comparer" 14 "github.com/insionng/yougam/libraries/syndtr/goleveldb/leveldb/iterator" 15 "github.com/insionng/yougam/libraries/syndtr/goleveldb/leveldb/testutil" 16 "github.com/insionng/yougam/libraries/syndtr/goleveldb/leveldb/util" 17 ) 18 19 func (p *DB) TestFindLT(key []byte) (rkey, value []byte, err error) { 20 p.mu.RLock() 21 if node := p.findLT(key); node != 0 { 22 n := p.nodeData[node] 23 m := n + p.nodeData[node+nKey] 24 rkey = p.kvData[n:m] 25 value = p.kvData[m : m+p.nodeData[node+nVal]] 26 } else { 27 err = ErrNotFound 28 } 29 p.mu.RUnlock() 30 return 31 } 32 33 func (p *DB) TestFindLast() (rkey, value []byte, err error) { 34 p.mu.RLock() 35 if node := p.findLast(); node != 0 { 36 n := p.nodeData[node] 37 m := n + p.nodeData[node+nKey] 38 rkey = p.kvData[n:m] 39 value = p.kvData[m : m+p.nodeData[node+nVal]] 40 } else { 41 err = ErrNotFound 42 } 43 p.mu.RUnlock() 44 return 45 } 46 47 func (p *DB) TestPut(key []byte, value []byte) error { 48 p.Put(key, value) 49 return nil 50 } 51 52 func (p *DB) TestDelete(key []byte) error { 53 p.Delete(key) 54 return nil 55 } 56 57 func (p *DB) TestFind(key []byte) (rkey, rvalue []byte, err error) { 58 return p.Find(key) 59 } 60 61 func (p *DB) TestGet(key []byte) (value []byte, err error) { 62 return p.Get(key) 63 } 64 65 func (p *DB) TestNewIterator(slice *util.Range) iterator.Iterator { 66 return p.NewIterator(slice) 67 } 68 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{ 75 DB: db, 76 Deleted: testutil.KeyValue_Generate(nil, 1000, 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())) 80 switch t.Act { 81 case testutil.DBPut, testutil.DBOverwrite: 82 Expect(db.Contains(t.ActKey)).Should(BeTrue()) 83 default: 84 Expect(db.Contains(t.ActKey)).Should(BeFalse()) 85 } 86 }, 87 } 88 testutil.DoDBTesting(&t) 89 }) 90 }) 91 92 Describe("read test", func() { 93 testutil.AllKeyValueTesting(nil, func(kv testutil.KeyValue) testutil.DB { 94 // Building the DB. 95 db := New(comparer.DefaultComparer, 0) 96 kv.IterateShuffled(nil, func(i int, key, value []byte) { 97 db.Put(key, value) 98 }) 99 100 if kv.Len() > 1 { 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) 105 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) 111 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) 117 }) 118 }) 119 } 120 121 if kv.Len() > 0 { 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)) 128 }) 129 } 130 131 return db 132 }, nil, nil) 133 }) 134 }) 135 })