You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
5.5 KiB

  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. package testutil
  7. import (
  8. "fmt"
  9. "math/rand"
  10. . "github.com/onsi/ginkgo"
  11. . "github.com/onsi/gomega"
  12. "github.com/syndtr/goleveldb/leveldb/errors"
  13. "github.com/syndtr/goleveldb/leveldb/util"
  14. )
  15. func TestFind(db Find, kv KeyValue) {
  16. ShuffledIndex(nil, kv.Len(), 1, func(i int) {
  17. key_, key, value := kv.IndexInexact(i)
  18. // Using exact key.
  19. rkey, rvalue, err := db.TestFind(key)
  20. Expect(err).ShouldNot(HaveOccurred(), "Error for exact key %q", key)
  21. Expect(rkey).Should(Equal(key), "Key")
  22. Expect(rvalue).Should(Equal(value), "Value for exact key %q", key)
  23. // Using inexact key.
  24. rkey, rvalue, err = db.TestFind(key_)
  25. Expect(err).ShouldNot(HaveOccurred(), "Error for inexact key %q (%q)", key_, key)
  26. Expect(rkey).Should(Equal(key), "Key for inexact key %q (%q)", key_, key)
  27. Expect(rvalue).Should(Equal(value), "Value for inexact key %q (%q)", key_, key)
  28. })
  29. }
  30. func TestFindAfterLast(db Find, kv KeyValue) {
  31. var key []byte
  32. if kv.Len() > 0 {
  33. key_, _ := kv.Index(kv.Len() - 1)
  34. key = BytesAfter(key_)
  35. }
  36. rkey, _, err := db.TestFind(key)
  37. Expect(err).Should(HaveOccurred(), "Find for key %q yield key %q", key, rkey)
  38. Expect(err).Should(Equal(errors.ErrNotFound))
  39. }
  40. func TestGet(db Get, kv KeyValue) {
  41. ShuffledIndex(nil, kv.Len(), 1, func(i int) {
  42. key_, key, value := kv.IndexInexact(i)
  43. // Using exact key.
  44. rvalue, err := db.TestGet(key)
  45. Expect(err).ShouldNot(HaveOccurred(), "Error for key %q", key)
  46. Expect(rvalue).Should(Equal(value), "Value for key %q", key)
  47. // Using inexact key.
  48. if len(key_) > 0 {
  49. _, err = db.TestGet(key_)
  50. Expect(err).Should(HaveOccurred(), "Error for key %q", key_)
  51. Expect(err).Should(Equal(errors.ErrNotFound))
  52. }
  53. })
  54. }
  55. func TestHas(db Has, kv KeyValue) {
  56. ShuffledIndex(nil, kv.Len(), 1, func(i int) {
  57. key_, key, _ := kv.IndexInexact(i)
  58. // Using exact key.
  59. ret, err := db.TestHas(key)
  60. Expect(err).ShouldNot(HaveOccurred(), "Error for key %q", key)
  61. Expect(ret).Should(BeTrue(), "False for key %q", key)
  62. // Using inexact key.
  63. if len(key_) > 0 {
  64. ret, err = db.TestHas(key_)
  65. Expect(err).ShouldNot(HaveOccurred(), "Error for key %q", key_)
  66. Expect(ret).ShouldNot(BeTrue(), "True for key %q", key)
  67. }
  68. })
  69. }
  70. func TestIter(db NewIterator, r *util.Range, kv KeyValue) {
  71. iter := db.TestNewIterator(r)
  72. Expect(iter.Error()).ShouldNot(HaveOccurred())
  73. t := IteratorTesting{
  74. KeyValue: kv,
  75. Iter: iter,
  76. }
  77. DoIteratorTesting(&t)
  78. iter.Release()
  79. }
  80. func KeyValueTesting(rnd *rand.Rand, kv KeyValue, p DB, setup func(KeyValue) DB, teardown func(DB)) {
  81. if rnd == nil {
  82. rnd = NewRand()
  83. }
  84. if p == nil {
  85. BeforeEach(func() {
  86. p = setup(kv)
  87. })
  88. if teardown != nil {
  89. AfterEach(func() {
  90. teardown(p)
  91. })
  92. }
  93. }
  94. It("Should find all keys with Find", func() {
  95. if db, ok := p.(Find); ok {
  96. TestFind(db, kv)
  97. }
  98. })
  99. It("Should return error if Find on key after the last", func() {
  100. if db, ok := p.(Find); ok {
  101. TestFindAfterLast(db, kv)
  102. }
  103. })
  104. It("Should only find exact key with Get", func() {
  105. if db, ok := p.(Get); ok {
  106. TestGet(db, kv)
  107. }
  108. })
  109. It("Should only find present key with Has", func() {
  110. if db, ok := p.(Has); ok {
  111. TestHas(db, kv)
  112. }
  113. })
  114. It("Should iterates and seeks correctly", func(done Done) {
  115. if db, ok := p.(NewIterator); ok {
  116. TestIter(db, nil, kv.Clone())
  117. }
  118. done <- true
  119. }, 30.0)
  120. It("Should iterates and seeks slice correctly", func(done Done) {
  121. if db, ok := p.(NewIterator); ok {
  122. RandomIndex(rnd, kv.Len(), Min(kv.Len(), 50), func(i int) {
  123. type slice struct {
  124. r *util.Range
  125. start, limit int
  126. }
  127. key_, _, _ := kv.IndexInexact(i)
  128. for _, x := range []slice{
  129. {&util.Range{Start: key_, Limit: nil}, i, kv.Len()},
  130. {&util.Range{Start: nil, Limit: key_}, 0, i},
  131. } {
  132. By(fmt.Sprintf("Random index of %d .. %d", x.start, x.limit), func() {
  133. TestIter(db, x.r, kv.Slice(x.start, x.limit))
  134. })
  135. }
  136. })
  137. }
  138. done <- true
  139. }, 200.0)
  140. It("Should iterates and seeks slice correctly", func(done Done) {
  141. if db, ok := p.(NewIterator); ok {
  142. RandomRange(rnd, kv.Len(), Min(kv.Len(), 50), func(start, limit int) {
  143. By(fmt.Sprintf("Random range of %d .. %d", start, limit), func() {
  144. r := kv.Range(start, limit)
  145. TestIter(db, &r, kv.Slice(start, limit))
  146. })
  147. })
  148. }
  149. done <- true
  150. }, 200.0)
  151. }
  152. func AllKeyValueTesting(rnd *rand.Rand, body, setup func(KeyValue) DB, teardown func(DB)) {
  153. Test := func(kv *KeyValue) func() {
  154. return func() {
  155. var p DB
  156. if setup != nil {
  157. Defer("setup", func() {
  158. p = setup(*kv)
  159. })
  160. }
  161. if teardown != nil {
  162. Defer("teardown", func() {
  163. teardown(p)
  164. })
  165. }
  166. if body != nil {
  167. p = body(*kv)
  168. }
  169. KeyValueTesting(rnd, *kv, p, func(KeyValue) DB {
  170. return p
  171. }, nil)
  172. }
  173. }
  174. Describe("with no key/value (empty)", Test(&KeyValue{}))
  175. Describe("with empty key", Test(KeyValue_EmptyKey()))
  176. Describe("with empty value", Test(KeyValue_EmptyValue()))
  177. Describe("with one key/value", Test(KeyValue_OneKeyValue()))
  178. Describe("with big value", Test(KeyValue_BigValue()))
  179. Describe("with special key", Test(KeyValue_SpecialKey()))
  180. Describe("with multiple key/value", Test(KeyValue_MultipleKeyValue()))
  181. Describe("with generated key/value 2-incr", Test(KeyValue_Generate(nil, 120, 2, 1, 50, 10, 120)))
  182. Describe("with generated key/value 3-incr", Test(KeyValue_Generate(nil, 120, 3, 1, 50, 10, 120)))
  183. }