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.

1441 lines
29 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package bptree
  2. import (
  3. "encoding/hex"
  4. "runtime/debug"
  5. "sort"
  6. "sync"
  7. "testing"
  8. crand "crypto/rand"
  9. "encoding/binary"
  10. mrand "math/rand"
  11. )
  12. var rand *mrand.Rand
  13. func init() {
  14. seed := make([]byte, 8)
  15. if _, err := crand.Read(seed); err == nil {
  16. rand = ThreadSafeRand(int64(binary.BigEndian.Uint64(seed)))
  17. } else {
  18. panic(err)
  19. }
  20. }
  21. func randslice(length int) []byte {
  22. return RandSlice(length)
  23. }
  24. func randstr(length int) String {
  25. return String(RandStr(length))
  26. }
  27. type Strings []String
  28. func (self Strings) Len() int {
  29. return len(self)
  30. }
  31. func (self Strings) Less(i, j int) bool {
  32. return self[i].Less(self[j])
  33. }
  34. func (self Strings) Swap(i, j int) {
  35. self[i], self[j] = self[j], self[i]
  36. }
  37. type record struct {
  38. key String
  39. value String
  40. }
  41. type records []*record
  42. func (self records) Len() int {
  43. return len(self)
  44. }
  45. func (self records) Less(i, j int) bool {
  46. return self[i].key.Less(self[j].key)
  47. }
  48. func (self records) Swap(i, j int) {
  49. self[i], self[j] = self[j], self[i]
  50. }
  51. func BenchmarkBpTree(b *testing.B) {
  52. b.StopTimer()
  53. recs := make(records, 100)
  54. ranrec := func() *record {
  55. return &record{randstr(20), randstr(20)}
  56. }
  57. for i := range recs {
  58. recs[i] = ranrec()
  59. }
  60. b.StartTimer()
  61. for i := 0; i < b.N; i++ {
  62. t := NewBpTree(23)
  63. for _, r := range recs {
  64. t.Add(r.key, r.value)
  65. }
  66. for _, r := range recs {
  67. t.RemoveWhere(r.key, func(value interface{}) bool { return true })
  68. }
  69. }
  70. }
  71. func TestAddHasCountFindIterateRemove(t *testing.T) {
  72. ranrec := func() *record {
  73. return &record{
  74. randstr(12),
  75. randstr(12),
  76. }
  77. }
  78. test := func(bpt *BpTree) {
  79. var err error
  80. recs := make(records, 128)
  81. new_recs := make(records, 128)
  82. for i := range recs {
  83. r := ranrec()
  84. recs[i] = r
  85. new_recs[i] = &record{r.key, randstr(12)}
  86. err = bpt.Add(r.key, r.value)
  87. if err != nil {
  88. t.Error(err)
  89. }
  90. }
  91. for i, r := range recs {
  92. if has := bpt.Has(r.key); !has {
  93. t.Error(bpt, "Missing key")
  94. }
  95. if has := bpt.Has(randstr(10)); has {
  96. t.Error("Table has extra key")
  97. }
  98. for k, v, next := bpt.Find(r.key)(); next != nil; k, v, next = next() {
  99. if !k.Equals(r.key) {
  100. t.Error(bpt, "Find Failed Key Error")
  101. }
  102. if !v.(String).Equals(r.value) {
  103. t.Error(bpt, "Find Failed Value Error")
  104. }
  105. }
  106. err = bpt.Replace(r.key, func(value interface{}) bool { return true }, new_recs[i].value)
  107. if err != nil {
  108. t.Error(err)
  109. }
  110. }
  111. sort.Sort(recs)
  112. sort.Sort(new_recs)
  113. i := 0
  114. for k, v, next := bpt.Iterate()(); next != nil; k, v, next = next() {
  115. if !recs[i].key.Equals(k) {
  116. t.Error("iterate error wrong key")
  117. }
  118. if !new_recs[i].value.Equals(v.(String)) {
  119. t.Error("iterate error wrong value")
  120. }
  121. i++
  122. }
  123. i = len(recs) - 1
  124. for k, v, next := bpt.Backward()(); next != nil; k, v, next = next() {
  125. if !recs[i].key.Equals(k) {
  126. t.Error("iterate error wrong key")
  127. }
  128. if !new_recs[i].value.Equals(v.(String)) {
  129. t.Error("iterate error wrong value")
  130. }
  131. i--
  132. }
  133. i = 0
  134. for k, next := bpt.Keys()(); next != nil; k, next = next() {
  135. if !recs[i].key.Equals(k) {
  136. t.Error("iterate error wrong key")
  137. }
  138. i++
  139. }
  140. i = 7
  141. for k, v, next := bpt.Range(recs[i].key, recs[i+(len(recs)/2)].key)(); next != nil; k, v, next = next() {
  142. if !recs[i].key.Equals(k) {
  143. t.Error("iterate error wrong key")
  144. }
  145. if !new_recs[i].value.Equals(v.(String)) {
  146. t.Error("iterate error wrong value")
  147. }
  148. i++
  149. }
  150. for k, v, next := bpt.Range(recs[i].key, recs[7].key)(); next != nil; k, v, next = next() {
  151. if !recs[i].key.Equals(k) {
  152. t.Error("iterate error wrong key")
  153. }
  154. if !new_recs[i].value.Equals(v.(String)) {
  155. t.Error("iterate error wrong value", k, v, recs[i].value, new_recs[i].value)
  156. }
  157. i--
  158. }
  159. for i, r := range recs {
  160. if has := bpt.Has(r.key); !has {
  161. t.Error(bpt, "Missing key")
  162. }
  163. if err := bpt.RemoveWhere(r.key, func(value interface{}) bool { return true }); err != nil {
  164. t.Fatal(bpt, err)
  165. }
  166. if has := bpt.Has(r.key); has {
  167. t.Error("Table has extra key")
  168. }
  169. for _, x := range recs[i+1:] {
  170. if has := bpt.Has(x.key); !has {
  171. t.Error(bpt, "Missing key", x.key)
  172. }
  173. }
  174. }
  175. }
  176. for i := 2; i < 64; i++ {
  177. test(NewBpTree(i))
  178. }
  179. }
  180. func TestBpMap(t *testing.T) {
  181. ranrec := func() *record {
  182. return &record{
  183. randstr(12),
  184. randstr(12),
  185. }
  186. }
  187. test := func(table MapOperable) {
  188. recs := make(records, 400)
  189. for i := range recs {
  190. r := ranrec()
  191. recs[i] = r
  192. err := table.Put(r.key, String(""))
  193. if err != nil {
  194. t.Error(err)
  195. }
  196. err = table.Put(r.key, r.value)
  197. if err != nil {
  198. t.Error(err)
  199. }
  200. }
  201. for _, r := range recs {
  202. if has := table.Has(r.key); !has {
  203. t.Error(table, "Missing key")
  204. }
  205. if has := table.Has(randstr(12)); has {
  206. t.Error("Table has extra key")
  207. }
  208. if val, err := table.Get(r.key); err != nil {
  209. t.Error(err)
  210. } else if !(val.(String)).Equals(r.value) {
  211. t.Error("wrong value")
  212. }
  213. }
  214. for i, x := range recs {
  215. if val, err := table.Remove(x.key); err != nil {
  216. t.Error(err)
  217. } else if !(val.(String)).Equals(x.value) {
  218. t.Error("wrong value")
  219. }
  220. for _, r := range recs[i+1:] {
  221. if has := table.Has(r.key); !has {
  222. t.Error("Missing key")
  223. }
  224. if has := table.Has(randstr(12)); has {
  225. t.Error("Table has extra key")
  226. }
  227. if val, err := table.Get(r.key); err != nil {
  228. t.Error(err)
  229. } else if !(val.(String)).Equals(r.value) {
  230. t.Error("wrong value")
  231. }
  232. }
  233. }
  234. }
  235. test(NewBpMap(23))
  236. }
  237. func Test_get_start(t *testing.T) {
  238. root := NewLeaf(2)
  239. root, err := root.put(Int(1), 1)
  240. if err != nil {
  241. t.Error(err)
  242. }
  243. root, err = root.put(Int(5), 3)
  244. if err != nil {
  245. t.Error(err)
  246. }
  247. root, err = root.put(Int(3), 2)
  248. if err != nil {
  249. t.Error(err)
  250. }
  251. t.Log(root)
  252. t.Log(root.pointers[0])
  253. t.Log(root.pointers[1])
  254. i, n := root.get_start(Int(1))
  255. if n != root.pointers[0] {
  256. t.Error("wrong node from get_start")
  257. }
  258. if i != 0 {
  259. t.Error("wrong index from get_start")
  260. }
  261. i, n = root.get_start(Int(3))
  262. if n != root.pointers[0] {
  263. t.Error("wrong node from get_start")
  264. }
  265. if i != 1 {
  266. t.Error("wrong index from get_start")
  267. }
  268. i, n = root.get_start(Int(5))
  269. if n != root.pointers[1] {
  270. t.Error("wrong node from get_start")
  271. }
  272. if i != 0 {
  273. t.Error("wrong index from get_start")
  274. }
  275. i, n = root.get_start(Int(2))
  276. if n != root.pointers[0] {
  277. t.Error("wrong node from get_start")
  278. }
  279. if i != 1 {
  280. t.Error("wrong index from get_start")
  281. }
  282. i, n = root.get_start(Int(4))
  283. t.Log(n)
  284. if n != root.pointers[1] {
  285. t.Error("wrong node from get_start")
  286. }
  287. if i != 0 {
  288. t.Error("wrong index from get_start")
  289. }
  290. i, n = root.get_start(Int(0))
  291. if n != root.pointers[0] {
  292. t.Error("wrong node from get_start")
  293. }
  294. if i != 0 {
  295. t.Error("wrong index from get_start")
  296. }
  297. i, n = root.get_start(Int(5))
  298. if n != root.pointers[1] {
  299. t.Error("wrong node from get_start")
  300. }
  301. if i != 0 {
  302. t.Error("wrong index from get_start")
  303. }
  304. }
  305. func Test_get_end(t *testing.T) {
  306. root := NewLeaf(3)
  307. root, err := root.put(Int(1), -1)
  308. if err != nil {
  309. t.Fatal(err)
  310. }
  311. root, err = root.put(Int(4), -1)
  312. if err != nil {
  313. t.Fatal(err)
  314. }
  315. root, err = root.put(Int(3), 1)
  316. if err != nil {
  317. t.Fatal(err)
  318. }
  319. root, err = root.put(Int(3), 2)
  320. if err != nil {
  321. t.Fatal(err)
  322. }
  323. root, err = root.put(Int(3), 3)
  324. if err != nil {
  325. t.Fatal(err)
  326. }
  327. root, err = root.put(Int(3), 4)
  328. if err != nil {
  329. t.Fatal(err)
  330. }
  331. root, err = root.put(Int(3), 5)
  332. if err != nil {
  333. t.Fatal(err)
  334. }
  335. t.Log(root)
  336. t.Log(root.pointers[0])
  337. t.Log(root.pointers[1])
  338. t.Log(root.pointers[2])
  339. i, n := root.get_start(Int(3))
  340. t.Log(n)
  341. if n != root.pointers[1] {
  342. t.Error("wrong node from get_start")
  343. }
  344. if i != 0 {
  345. t.Error("wrong index from get_start")
  346. }
  347. i, n = root.get_end(Int(3))
  348. t.Log(n)
  349. if n != root.pointers[1].getNext() {
  350. t.Error("wrong node from get_end")
  351. }
  352. if i != 1 {
  353. t.Error("wrong index from get_end")
  354. }
  355. i, n = root.get_end(Int(1))
  356. t.Log(n)
  357. if n != root.pointers[0] {
  358. t.Error("wrong node from get_end")
  359. }
  360. if i != 0 {
  361. t.Error("wrong index from get_end")
  362. }
  363. i, n = root.get_end(Int(4))
  364. t.Log(n)
  365. if n != root.pointers[2] {
  366. t.Error("wrong node from get_end")
  367. }
  368. if i != 0 {
  369. t.Error("wrong index from get_end")
  370. }
  371. i, n = root.get_end(Int(0))
  372. t.Log(n)
  373. if n != root.pointers[0] {
  374. t.Error("wrong node from get_end")
  375. }
  376. if i != 0 {
  377. t.Error("wrong index from get_end")
  378. }
  379. i, n = root.get_end(Int(5))
  380. t.Log(n)
  381. if n != root.pointers[2] {
  382. t.Error("wrong node from get_end")
  383. }
  384. if i != 0 {
  385. t.Error("wrong index from get_end")
  386. }
  387. i, n = root.get_end(Int(2))
  388. t.Log(n)
  389. if n != root.pointers[1] {
  390. t.Error("wrong node from get_end")
  391. }
  392. if i != 0 {
  393. t.Error("wrong index from get_end")
  394. }
  395. }
  396. func Test_put_no_root_split(t *testing.T) {
  397. a := NewLeaf(2)
  398. if err := a.put_kv(Int(1), 1); err != nil {
  399. t.Error(err)
  400. }
  401. p, err := a.put(Int(1), 2)
  402. if err != nil {
  403. t.Error(err)
  404. } else {
  405. if p != a {
  406. t.Errorf("p != a")
  407. }
  408. if !p.has(Int(1)) {
  409. t.Error("p didn't have the right keys", p)
  410. }
  411. }
  412. p, err = a.put(Int(1), 3)
  413. if err != nil {
  414. t.Error(err)
  415. } else {
  416. if p != a {
  417. t.Errorf("p != a")
  418. }
  419. if !p.has(Int(1)) {
  420. t.Error("p didn't have the right keys", p)
  421. }
  422. if p.getNext() == nil {
  423. t.Error("p.next should not be nil")
  424. }
  425. t.Log(p)
  426. t.Log(p.getNext())
  427. }
  428. }
  429. func Test_put_root_split(t *testing.T) {
  430. a := NewLeaf(2)
  431. p, err := a.put(Int(1), 1)
  432. if err != nil {
  433. t.Error(err)
  434. } else {
  435. if p != a {
  436. t.Errorf("p != a")
  437. }
  438. if !p.has(Int(1)) {
  439. t.Error("p didn't have the right keys", p)
  440. }
  441. }
  442. p, err = a.put(Int(3), 3)
  443. if err != nil {
  444. t.Error(err)
  445. } else {
  446. if p != a {
  447. t.Errorf("p != a")
  448. }
  449. if !p.has(Int(1)) || !p.has(Int(3)) {
  450. t.Error("p didn't have the right keys", p)
  451. }
  452. }
  453. p, err = a.put(Int(2), 2)
  454. if err != nil {
  455. t.Error(err)
  456. } else {
  457. if p == a {
  458. t.Errorf("p == a")
  459. }
  460. if !p.has(Int(1)) || !p.has(Int(3)) {
  461. t.Error("p didn't have the right keys", p)
  462. }
  463. if len(p.pointers) != 2 {
  464. t.Error("p didn't have right number of pointers", p)
  465. }
  466. if !p.pointers[0].has(Int(1)) || !p.pointers[0].has(Int(2)) {
  467. t.Error("p.pointers[0] didn't have the right keys", p.pointers[0])
  468. }
  469. if !p.pointers[1].has(Int(3)) {
  470. t.Error("p.pointers[1] didn't have the right keys", p.pointers[1])
  471. }
  472. t.Log(p)
  473. t.Log(p.pointers[0])
  474. t.Log(p.pointers[1])
  475. }
  476. }
  477. func Test_internal_insert_no_split(t *testing.T) {
  478. a := NewInternal(3)
  479. leaf := NewLeaf(1)
  480. if err := leaf.put_kv(Int(1), 1); err != nil {
  481. t.Error(err)
  482. }
  483. if err := a.put_kp(Int(1), leaf); err != nil {
  484. t.Error(err)
  485. }
  486. if err := a.put_kp(Int(5), nil); err != nil {
  487. t.Error(err)
  488. }
  489. p, q, err := a.internal_insert(Int(2), nil)
  490. if err != nil {
  491. t.Error(err)
  492. } else {
  493. if p != a {
  494. t.Errorf("p != a")
  495. }
  496. if q != nil {
  497. t.Errorf("q != nil")
  498. }
  499. if !p.has(Int(1)) || !p.has(Int(2)) || !p.has(Int(5)) {
  500. t.Error("p didn't have the right keys", p)
  501. }
  502. }
  503. }
  504. func Test_internal_insert_split_less(t *testing.T) {
  505. a := NewInternal(3)
  506. leaf := NewLeaf(1)
  507. if err := leaf.put_kv(Int(1), 1); err != nil {
  508. t.Error(err)
  509. }
  510. if err := a.put_kp(Int(1), leaf); err != nil {
  511. t.Error(err)
  512. }
  513. if err := a.put_kp(Int(3), nil); err != nil {
  514. t.Error(err)
  515. }
  516. if err := a.put_kp(Int(5), nil); err != nil {
  517. t.Error(err)
  518. }
  519. p, q, err := a.internal_insert(Int(2), nil)
  520. if err != nil {
  521. t.Error(err)
  522. } else {
  523. if p != a {
  524. t.Errorf("p != a")
  525. }
  526. if q == nil {
  527. t.Errorf("q == nil")
  528. }
  529. if !p.has(Int(1)) || !p.has(Int(2)) {
  530. t.Error("p didn't have the right keys", p)
  531. }
  532. if !q.has(Int(3)) || !q.has(Int(5)) {
  533. t.Error("q didn't have the right keys", q)
  534. }
  535. }
  536. }
  537. func Test_internal_split_less(t *testing.T) {
  538. a := NewInternal(3)
  539. if err := a.put_kp(Int(1), nil); err != nil {
  540. t.Error(err)
  541. }
  542. if err := a.put_kp(Int(3), nil); err != nil {
  543. t.Error(err)
  544. }
  545. if err := a.put_kp(Int(5), nil); err != nil {
  546. t.Error(err)
  547. }
  548. p, q, err := a.internal_split(Int(2), nil)
  549. if err != nil {
  550. t.Error(err)
  551. } else {
  552. if p != a {
  553. t.Errorf("p != a")
  554. }
  555. if q == nil {
  556. t.Errorf("q == nil")
  557. }
  558. if !p.has(Int(1)) || !p.has(Int(2)) {
  559. t.Error("p didn't have the right keys", p)
  560. }
  561. if !q.has(Int(3)) || !q.has(Int(5)) {
  562. t.Error("q didn't have the right keys", q)
  563. }
  564. }
  565. }
  566. func Test_internal_split_equal(t *testing.T) {
  567. a := NewInternal(3)
  568. if err := a.put_kp(Int(1), nil); err != nil {
  569. t.Error(err)
  570. }
  571. if err := a.put_kp(Int(3), nil); err != nil {
  572. t.Error(err)
  573. }
  574. if err := a.put_kp(Int(5), nil); err != nil {
  575. t.Error(err)
  576. }
  577. p, q, err := a.internal_split(Int(3), nil)
  578. if err == nil {
  579. t.Error("split succeeded should have failed", p, q)
  580. }
  581. }
  582. func Test_internal_split_greater(t *testing.T) {
  583. a := NewInternal(3)
  584. if err := a.put_kp(Int(1), nil); err != nil {
  585. t.Error(err)
  586. }
  587. if err := a.put_kp(Int(3), nil); err != nil {
  588. t.Error(err)
  589. }
  590. if err := a.put_kp(Int(5), nil); err != nil {
  591. t.Error(err)
  592. }
  593. p, q, err := a.internal_split(Int(4), nil)
  594. if err != nil {
  595. t.Error(err)
  596. } else {
  597. if p != a {
  598. t.Errorf("p != a")
  599. }
  600. if q == nil {
  601. t.Errorf("q == nil")
  602. }
  603. if !p.has(Int(1)) {
  604. t.Error("p didn't have the right keys", p)
  605. }
  606. if !q.has(Int(3)) || !q.has(Int(4)) || !q.has(Int(5)) {
  607. t.Error("q didn't have the right keys", q)
  608. }
  609. }
  610. }
  611. func Test_leaf_insert_no_split(t *testing.T) {
  612. a := NewLeaf(3)
  613. insert_linked_list_node(a, nil, nil)
  614. if err := a.put_kv(Int(1), 1); err != nil {
  615. t.Error(err)
  616. }
  617. if err := a.put_kv(Int(3), 3); err != nil {
  618. t.Error(err)
  619. }
  620. p, q, err := a.leaf_insert(Int(2), 2)
  621. if err != nil {
  622. t.Error(err)
  623. } else {
  624. if p != a {
  625. t.Errorf("p != a")
  626. }
  627. if q != nil {
  628. t.Errorf("q != nil")
  629. }
  630. if !p.has(Int(1)) || !p.has(Int(2)) || !p.has(Int(3)) {
  631. t.Error("p didn't have the right keys", p)
  632. }
  633. }
  634. }
  635. // tests the defer to split logic
  636. func Test_leaf_insert_split_less(t *testing.T) {
  637. a := NewLeaf(3)
  638. insert_linked_list_node(a, nil, nil)
  639. if err := a.put_kv(Int(1), 1); err != nil {
  640. t.Error(err)
  641. }
  642. if err := a.put_kv(Int(3), 3); err != nil {
  643. t.Error(err)
  644. }
  645. if err := a.put_kv(Int(5), 5); err != nil {
  646. t.Error(err)
  647. }
  648. p, q, err := a.leaf_insert(Int(2), 2)
  649. if err != nil {
  650. t.Error(err)
  651. } else {
  652. if p != a {
  653. t.Errorf("p != a")
  654. }
  655. if q == nil {
  656. t.Errorf("q == nil")
  657. }
  658. if !p.has(Int(1)) || !p.has(Int(2)) {
  659. t.Error("p didn't have the right keys", p)
  660. }
  661. if !q.has(Int(3)) || !q.has(Int(5)) {
  662. t.Error("q didn't have the right keys", q)
  663. }
  664. }
  665. }
  666. func Test_leaf_split_less(t *testing.T) {
  667. a := NewLeaf(3)
  668. insert_linked_list_node(a, nil, nil)
  669. if err := a.put_kv(Int(1), 1); err != nil {
  670. t.Error(err)
  671. }
  672. if err := a.put_kv(Int(3), 3); err != nil {
  673. t.Error(err)
  674. }
  675. if err := a.put_kv(Int(5), 5); err != nil {
  676. t.Error(err)
  677. }
  678. p, q, err := a.leaf_split(Int(2), 2)
  679. if err != nil {
  680. t.Error(err)
  681. } else {
  682. if p != a {
  683. t.Errorf("p != a")
  684. }
  685. if q == nil {
  686. t.Errorf("q == nil")
  687. }
  688. if !p.has(Int(1)) || !p.has(Int(2)) {
  689. t.Error("p didn't have the right keys", p)
  690. }
  691. if !q.has(Int(3)) || !q.has(Int(5)) {
  692. t.Error("q didn't have the right keys", q)
  693. }
  694. }
  695. }
  696. func Test_leaf_split_equal(t *testing.T) {
  697. a := NewLeaf(3)
  698. insert_linked_list_node(a, nil, nil)
  699. if err := a.put_kv(Int(1), 1); err != nil {
  700. t.Error(err)
  701. }
  702. if err := a.put_kv(Int(3), 3); err != nil {
  703. t.Error(err)
  704. }
  705. if err := a.put_kv(Int(5), 5); err != nil {
  706. t.Error(err)
  707. }
  708. p, q, err := a.leaf_split(Int(3), 2)
  709. if err != nil {
  710. t.Error(err)
  711. } else {
  712. if p != a {
  713. t.Errorf("p != a")
  714. }
  715. if q == nil {
  716. t.Errorf("q == nil")
  717. }
  718. if !p.has(Int(1)) {
  719. t.Error("p didn't have the right keys", p)
  720. }
  721. if !q.has(Int(3)) || q.count(Int(3)) != 2 || !q.has(Int(5)) {
  722. t.Error("q didn't have the right keys", q, q.count(Int(3)))
  723. }
  724. }
  725. }
  726. func Test_leaf_split_greater(t *testing.T) {
  727. a := NewLeaf(3)
  728. insert_linked_list_node(a, nil, nil)
  729. if err := a.put_kv(Int(1), 1); err != nil {
  730. t.Error(err)
  731. }
  732. if err := a.put_kv(Int(3), 3); err != nil {
  733. t.Error(err)
  734. }
  735. if err := a.put_kv(Int(5), 5); err != nil {
  736. t.Error(err)
  737. }
  738. p, q, err := a.leaf_split(Int(4), 2)
  739. if err != nil {
  740. t.Error(err)
  741. } else {
  742. if p != a {
  743. t.Errorf("p != a")
  744. }
  745. if q == nil {
  746. t.Errorf("q == nil")
  747. }
  748. if !p.has(Int(1)) {
  749. t.Error("p didn't have the right keys", p)
  750. }
  751. if !q.has(Int(3)) || !q.has(Int(4)) || !q.has(Int(5)) {
  752. t.Error("q didn't have the right keys", q)
  753. }
  754. }
  755. }
  756. // tests the defer logic
  757. func Test_pure_leaf_insert_split_less(t *testing.T) {
  758. a := NewLeaf(2)
  759. insert_linked_list_node(a, nil, nil)
  760. b := NewLeaf(2)
  761. insert_linked_list_node(b, a, nil)
  762. c := NewLeaf(2)
  763. insert_linked_list_node(c, b, nil)
  764. d := NewLeaf(2)
  765. insert_linked_list_node(d, c, nil)
  766. if err := a.put_kv(Int(3), 1); err != nil {
  767. t.Error(err)
  768. }
  769. if err := a.put_kv(Int(3), 2); err != nil {
  770. t.Error(err)
  771. }
  772. if err := b.put_kv(Int(3), 3); err != nil {
  773. t.Error(err)
  774. }
  775. if err := b.put_kv(Int(3), 4); err != nil {
  776. t.Error(err)
  777. }
  778. if err := c.put_kv(Int(3), 5); err != nil {
  779. t.Error(err)
  780. }
  781. if err := c.put_kv(Int(3), 6); err != nil {
  782. t.Error(err)
  783. }
  784. if err := d.put_kv(Int(4), 6); err != nil {
  785. t.Error(err)
  786. }
  787. p, q, err := a.leaf_insert(Int(2), 1)
  788. if err != nil {
  789. t.Error(err)
  790. } else {
  791. if q != a {
  792. t.Errorf("q != a")
  793. }
  794. if p == nil || len(p.keys) != 1 || !p.keys[0].Equals(Int(2)) {
  795. t.Errorf("p did not contain the right key")
  796. }
  797. if p.getPrev() != nil {
  798. t.Errorf("expected p.prev == nil")
  799. }
  800. if p.getNext() != a {
  801. t.Errorf("expected p.next == a")
  802. }
  803. if a.getPrev() != p {
  804. t.Errorf("expected a.prev == p")
  805. }
  806. if a.getNext() != b {
  807. t.Errorf("expected a.next == b")
  808. }
  809. if b.getPrev() != a {
  810. t.Errorf("expected b.prev == a")
  811. }
  812. if b.getNext() != c {
  813. t.Errorf("expected b.next == c")
  814. }
  815. if c.getPrev() != b {
  816. t.Errorf("expected c.prev == b")
  817. }
  818. if c.getNext() != d {
  819. t.Errorf("expected c.next == d")
  820. }
  821. if d.getPrev() != c {
  822. t.Errorf("expected d.prev == c")
  823. }
  824. if d.getNext() != nil {
  825. t.Errorf("expected d.next == nil")
  826. }
  827. }
  828. }
  829. func Test_pure_leaf_split_less(t *testing.T) {
  830. a := NewLeaf(2)
  831. insert_linked_list_node(a, nil, nil)
  832. b := NewLeaf(2)
  833. insert_linked_list_node(b, a, nil)
  834. c := NewLeaf(2)
  835. insert_linked_list_node(c, b, nil)
  836. d := NewLeaf(2)
  837. insert_linked_list_node(d, c, nil)
  838. if err := a.put_kv(Int(3), 1); err != nil {
  839. t.Error(err)
  840. }
  841. if err := a.put_kv(Int(3), 2); err != nil {
  842. t.Error(err)
  843. }
  844. if err := b.put_kv(Int(3), 3); err != nil {
  845. t.Error(err)
  846. }
  847. if err := b.put_kv(Int(3), 4); err != nil {
  848. t.Error(err)
  849. }
  850. if err := c.put_kv(Int(3), 5); err != nil {
  851. t.Error(err)
  852. }
  853. if err := c.put_kv(Int(3), 6); err != nil {
  854. t.Error(err)
  855. }
  856. if err := d.put_kv(Int(4), 6); err != nil {
  857. t.Error(err)
  858. }
  859. p, q, err := a.pure_leaf_split(Int(2), 1)
  860. if err != nil {
  861. t.Error(err)
  862. } else {
  863. if q != a {
  864. t.Errorf("q != a")
  865. }
  866. if p == nil || len(p.keys) != 1 || !p.keys[0].Equals(Int(2)) {
  867. t.Errorf("p did not contain the right key")
  868. }
  869. if p.getPrev() != nil {
  870. t.Errorf("expected p.prev == nil")
  871. }
  872. if p.getNext() != a {
  873. t.Errorf("expected p.next == a")
  874. }
  875. if a.getPrev() != p {
  876. t.Errorf("expected a.prev == p")
  877. }
  878. if a.getNext() != b {
  879. t.Errorf("expected a.next == b")
  880. }
  881. if b.getPrev() != a {
  882. t.Errorf("expected b.prev == a")
  883. }
  884. if b.getNext() != c {
  885. t.Errorf("expected b.next == c")
  886. }
  887. if c.getPrev() != b {
  888. t.Errorf("expected c.prev == b")
  889. }
  890. if c.getNext() != d {
  891. t.Errorf("expected c.next == d")
  892. }
  893. if d.getPrev() != c {
  894. t.Errorf("expected d.prev == c")
  895. }
  896. if d.getNext() != nil {
  897. t.Errorf("expected d.next == nil")
  898. }
  899. }
  900. }
  901. func Test_pure_leaf_split_equal(t *testing.T) {
  902. a := NewLeaf(2)
  903. insert_linked_list_node(a, nil, nil)
  904. b := NewLeaf(2)
  905. insert_linked_list_node(b, a, nil)
  906. c := NewLeaf(2)
  907. insert_linked_list_node(c, b, nil)
  908. d := NewLeaf(2)
  909. insert_linked_list_node(d, c, nil)
  910. if err := a.put_kv(Int(3), 1); err != nil {
  911. t.Error(err)
  912. }
  913. if err := a.put_kv(Int(3), 2); err != nil {
  914. t.Error(err)
  915. }
  916. if err := b.put_kv(Int(3), 3); err != nil {
  917. t.Error(err)
  918. }
  919. if err := b.put_kv(Int(3), 4); err != nil {
  920. t.Error(err)
  921. }
  922. if err := c.put_kv(Int(3), 5); err != nil {
  923. t.Error(err)
  924. }
  925. if err := d.put_kv(Int(4), 6); err != nil {
  926. t.Error(err)
  927. }
  928. p, q, err := a.pure_leaf_split(Int(3), 1)
  929. if err != nil {
  930. t.Error(err)
  931. } else {
  932. if p != a {
  933. t.Errorf("p != a")
  934. }
  935. if q != nil {
  936. t.Errorf("q != nil")
  937. }
  938. if a.getPrev() != nil {
  939. t.Errorf("expected a.prev == nil")
  940. }
  941. if a.getNext() != b {
  942. t.Errorf("expected a.next == b")
  943. }
  944. if b.getPrev() != a {
  945. t.Errorf("expected b.prev == a")
  946. }
  947. if b.getNext() != c {
  948. t.Errorf("expected b.next == c")
  949. }
  950. if c.getPrev() != b {
  951. t.Errorf("expected c.prev == b")
  952. }
  953. if c.getNext() != d {
  954. t.Errorf("expected c.next == d")
  955. }
  956. if d.getPrev() != c {
  957. t.Errorf("expected d.prev == c")
  958. }
  959. if d.getNext() != nil {
  960. t.Errorf("expected d.next == nil")
  961. }
  962. }
  963. }
  964. func Test_pure_leaf_split_greater(t *testing.T) {
  965. a := NewLeaf(2)
  966. insert_linked_list_node(a, nil, nil)
  967. b := NewLeaf(2)
  968. insert_linked_list_node(b, a, nil)
  969. c := NewLeaf(2)
  970. insert_linked_list_node(c, b, nil)
  971. d := NewLeaf(2)
  972. insert_linked_list_node(d, c, nil)
  973. if err := a.put_kv(Int(3), 1); err != nil {
  974. t.Error(err)
  975. }
  976. if err := a.put_kv(Int(3), 2); err != nil {
  977. t.Error(err)
  978. }
  979. if err := b.put_kv(Int(3), 3); err != nil {
  980. t.Error(err)
  981. }
  982. if err := b.put_kv(Int(3), 4); err != nil {
  983. t.Error(err)
  984. }
  985. if err := c.put_kv(Int(3), 5); err != nil {
  986. t.Error(err)
  987. }
  988. if err := d.put_kv(Int(5), 6); err != nil {
  989. t.Error(err)
  990. }
  991. p, q, err := a.pure_leaf_split(Int(4), 1)
  992. if err != nil {
  993. t.Error(err)
  994. } else {
  995. if p != a {
  996. t.Errorf("p != a")
  997. }
  998. if q == nil || len(q.keys) != 1 || !q.keys[0].Equals(Int(4)) {
  999. t.Errorf("q != nil")
  1000. }
  1001. if a.getPrev() != nil {
  1002. t.Errorf("expected a.prev == nil")
  1003. }
  1004. if a.getNext() != b {
  1005. t.Errorf("expected a.next == b")
  1006. }
  1007. if b.getPrev() != a {
  1008. t.Errorf("expected b.prev == a")
  1009. }
  1010. if b.getNext() != c {
  1011. t.Errorf("expected b.next == c")
  1012. }
  1013. if c.getPrev() != b {
  1014. t.Errorf("expected c.prev == b")
  1015. }
  1016. if c.getNext() != q {
  1017. t.Errorf("expected c.next == q")
  1018. }
  1019. if q.getPrev() != c {
  1020. t.Errorf("expected q.prev == c")
  1021. }
  1022. if q.getNext() != d {
  1023. t.Errorf("expected q.next == d")
  1024. }
  1025. if d.getPrev() != q {
  1026. t.Errorf("expected d.prev == q")
  1027. }
  1028. if d.getNext() != nil {
  1029. t.Errorf("expected d.next == nil")
  1030. }
  1031. }
  1032. }
  1033. func Test_find_end_of_pure_run(t *testing.T) {
  1034. a := NewLeaf(2)
  1035. insert_linked_list_node(a, nil, nil)
  1036. b := NewLeaf(2)
  1037. insert_linked_list_node(b, a, nil)
  1038. c := NewLeaf(2)
  1039. insert_linked_list_node(c, b, nil)
  1040. d := NewLeaf(2)
  1041. insert_linked_list_node(d, c, nil)
  1042. if err := a.put_kv(Int(3), 1); err != nil {
  1043. t.Error(err)
  1044. }
  1045. if err := a.put_kv(Int(3), 2); err != nil {
  1046. t.Error(err)
  1047. }
  1048. if err := b.put_kv(Int(3), 3); err != nil {
  1049. t.Error(err)
  1050. }
  1051. if err := b.put_kv(Int(3), 4); err != nil {
  1052. t.Error(err)
  1053. }
  1054. if err := c.put_kv(Int(3), 5); err != nil {
  1055. t.Error(err)
  1056. }
  1057. if err := c.put_kv(Int(3), 6); err != nil {
  1058. t.Error(err)
  1059. }
  1060. if err := d.put_kv(Int(4), 6); err != nil {
  1061. t.Error(err)
  1062. }
  1063. e := a.find_end_of_pure_run()
  1064. if e != c {
  1065. t.Errorf("end of run should have been block c %v %v", e, c)
  1066. }
  1067. }
  1068. func Test_insert_linked_list_node(t *testing.T) {
  1069. a := NewLeaf(1)
  1070. insert_linked_list_node(a, nil, nil)
  1071. b := NewLeaf(2)
  1072. insert_linked_list_node(b, a, nil)
  1073. c := NewLeaf(3)
  1074. insert_linked_list_node(c, b, nil)
  1075. d := NewLeaf(4)
  1076. insert_linked_list_node(d, a, b)
  1077. if a.getPrev() != nil {
  1078. t.Errorf("expected a.prev == nil")
  1079. }
  1080. if a.getNext() != d {
  1081. t.Errorf("expected a.next == d")
  1082. }
  1083. if d.getPrev() != a {
  1084. t.Errorf("expected d.prev == a")
  1085. }
  1086. if d.getNext() != b {
  1087. t.Errorf("expected d.next == b")
  1088. }
  1089. if b.getPrev() != d {
  1090. t.Errorf("expected b.prev == d")
  1091. }
  1092. if b.getNext() != c {
  1093. t.Errorf("expected b.next == c")
  1094. }
  1095. if c.getPrev() != b {
  1096. t.Errorf("expected c.prev == b")
  1097. }
  1098. if c.getNext() != nil {
  1099. t.Errorf("expected c.next == nil")
  1100. }
  1101. }
  1102. func Test_remove_linked_list_node(t *testing.T) {
  1103. a := NewLeaf(1)
  1104. insert_linked_list_node(a, nil, nil)
  1105. b := NewLeaf(2)
  1106. insert_linked_list_node(b, a, nil)
  1107. c := NewLeaf(3)
  1108. insert_linked_list_node(c, b, nil)
  1109. d := NewLeaf(4)
  1110. insert_linked_list_node(d, a, b)
  1111. if a.getPrev() != nil {
  1112. t.Errorf("expected a.prev == nil")
  1113. }
  1114. if a.getNext() != d {
  1115. t.Errorf("expected a.next == d")
  1116. }
  1117. if d.getPrev() != a {
  1118. t.Errorf("expected d.prev == a")
  1119. }
  1120. if d.getNext() != b {
  1121. t.Errorf("expected d.next == b")
  1122. }
  1123. if b.getPrev() != d {
  1124. t.Errorf("expected b.prev == d")
  1125. }
  1126. if b.getNext() != c {
  1127. t.Errorf("expected b.next == c")
  1128. }
  1129. if c.getPrev() != b {
  1130. t.Errorf("expected c.prev == b")
  1131. }
  1132. if c.getNext() != nil {
  1133. t.Errorf("expected c.next == nil")
  1134. }
  1135. remove_linked_list_node(d)
  1136. if a.getPrev() != nil {
  1137. t.Errorf("expected a.prev == nil")
  1138. }
  1139. if a.getNext() != b {
  1140. t.Errorf("expected a.next == b")
  1141. }
  1142. if b.getPrev() != a {
  1143. t.Errorf("expected b.prev == a")
  1144. }
  1145. if b.getNext() != c {
  1146. t.Errorf("expected b.next == c")
  1147. }
  1148. if c.getPrev() != b {
  1149. t.Errorf("expected c.prev == b")
  1150. }
  1151. if c.getNext() != nil {
  1152. t.Errorf("expected c.next == nil")
  1153. }
  1154. remove_linked_list_node(a)
  1155. if b.getPrev() != nil {
  1156. t.Errorf("expected b.prev == nil")
  1157. }
  1158. if b.getNext() != c {
  1159. t.Errorf("expected b.next == c")
  1160. }
  1161. if c.getPrev() != b {
  1162. t.Errorf("expected c.prev == b")
  1163. }
  1164. if c.getNext() != nil {
  1165. t.Errorf("expected c.next == nil")
  1166. }
  1167. remove_linked_list_node(c)
  1168. if b.getPrev() != nil {
  1169. t.Errorf("expected b.prev == nil")
  1170. }
  1171. if b.getNext() != nil {
  1172. t.Errorf("expected b.next == nil")
  1173. }
  1174. remove_linked_list_node(b)
  1175. }
  1176. func Test_balance_leaf_nodes_with_dup(t *testing.T) {
  1177. a := NewLeaf(3)
  1178. b := NewLeaf(3)
  1179. if err := a.put_kv(Int(1), 1); err != nil {
  1180. t.Error(err)
  1181. }
  1182. if err := a.put_kv(Int(1), 1); err != nil {
  1183. t.Error(err)
  1184. }
  1185. if err := a.put_kv(Int(2), 1); err != nil {
  1186. t.Error(err)
  1187. }
  1188. balance_nodes(a, b)
  1189. if !a.has(Int(1)) || a.count(Int(1)) != 2 || a.has(Int(2)) {
  1190. t.Error("a had wrong items", a)
  1191. }
  1192. if !b.has(Int(2)) || b.count(Int(2)) != 1 || b.has(Int(1)) {
  1193. t.Error("a had wrong items", b)
  1194. }
  1195. }
  1196. func Test_balance_leaf_nodes(t *testing.T) {
  1197. a := NewLeaf(7)
  1198. b := NewLeaf(7)
  1199. if err := a.put_kv(Int(1), 1); err != nil {
  1200. t.Error(err)
  1201. }
  1202. if err := a.put_kv(Int(2), 2); err != nil {
  1203. t.Error(err)
  1204. }
  1205. if err := a.put_kv(Int(3), 3); err != nil {
  1206. t.Error(err)
  1207. }
  1208. if err := a.put_kv(Int(4), 4); err != nil {
  1209. t.Error(err)
  1210. }
  1211. if err := a.put_kv(Int(5), 5); err != nil {
  1212. t.Error(err)
  1213. }
  1214. if err := a.put_kv(Int(6), 6); err != nil {
  1215. t.Error(err)
  1216. }
  1217. if err := a.put_kv(Int(7), 7); err != nil {
  1218. t.Error(err)
  1219. }
  1220. balance_nodes(a, b)
  1221. for i, k := range a.keys {
  1222. if int(k.(Int)) != i+1 {
  1223. t.Errorf("k != %d", i+1)
  1224. }
  1225. }
  1226. for i, k := range b.keys {
  1227. if int(k.(Int)) != 3+i+1 {
  1228. t.Errorf("k != %d", 3+i+1)
  1229. }
  1230. }
  1231. for i, v := range a.values {
  1232. if v.(int) != i+1 {
  1233. t.Errorf("k != %d", i+1)
  1234. }
  1235. }
  1236. for i, v := range b.values {
  1237. if v.(int) != 3+i+1 {
  1238. t.Errorf("v != %d", 3+i+1)
  1239. }
  1240. }
  1241. t.Log(a)
  1242. t.Log(b)
  1243. }
  1244. func Test_balance_internal_nodes(t *testing.T) {
  1245. a := NewInternal(6)
  1246. b := NewInternal(6)
  1247. if err := a.put_kp(Int(1), nil); err != nil {
  1248. t.Error(err)
  1249. }
  1250. if err := a.put_kp(Int(2), nil); err != nil {
  1251. t.Error(err)
  1252. }
  1253. if err := a.put_kp(Int(3), nil); err != nil {
  1254. t.Error(err)
  1255. }
  1256. if err := a.put_kp(Int(4), nil); err != nil {
  1257. t.Error(err)
  1258. }
  1259. if err := a.put_kp(Int(5), nil); err != nil {
  1260. t.Error(err)
  1261. }
  1262. if err := a.put_kp(Int(6), nil); err != nil {
  1263. t.Error(err)
  1264. }
  1265. balance_nodes(a, b)
  1266. for i, k := range a.keys {
  1267. if int(k.(Int)) != i+1 {
  1268. t.Errorf("k != %d", i+1)
  1269. }
  1270. }
  1271. for i, k := range b.keys {
  1272. if int(k.(Int)) != 3+i+1 {
  1273. t.Errorf("k != %d", 3+i+1)
  1274. }
  1275. }
  1276. t.Log(a)
  1277. t.Log(b)
  1278. }
  1279. // copied from
  1280. // ThreadSafeRand provides a thread safe version of math/rand.Rand using
  1281. // the same technique used in the math/rand package to make the top level
  1282. // functions thread safe.
  1283. func ThreadSafeRand(seed int64) *mrand.Rand {
  1284. return mrand.New(&lockedSource{src: mrand.NewSource(seed).(mrand.Source64)})
  1285. }
  1286. // from: https://golang.org/src/math/rand/rand.go?s=8161:8175#L317
  1287. type lockedSource struct {
  1288. lk sync.Mutex
  1289. src mrand.Source64
  1290. }
  1291. func (r *lockedSource) Int63() (n int64) {
  1292. r.lk.Lock()
  1293. n = r.src.Int63()
  1294. r.lk.Unlock()
  1295. return
  1296. }
  1297. func (r *lockedSource) Uint64() (n uint64) {
  1298. r.lk.Lock()
  1299. n = r.src.Uint64()
  1300. r.lk.Unlock()
  1301. return
  1302. }
  1303. func (r *lockedSource) Seed(seed int64) {
  1304. r.lk.Lock()
  1305. r.src.Seed(seed)
  1306. r.lk.Unlock()
  1307. }
  1308. // seedPos implements Seed for a lockedSource without a race condiiton.
  1309. func (r *lockedSource) seedPos(seed int64, readPos *int8) {
  1310. r.lk.Lock()
  1311. r.src.Seed(seed)
  1312. *readPos = 0
  1313. r.lk.Unlock()
  1314. }
  1315. // read implements Read for a lockedSource without a race condition.
  1316. func (r *lockedSource) read(p []byte, readVal *int64, readPos *int8) (n int, err error) {
  1317. r.lk.Lock()
  1318. n, err = read(p, r.src.Int63, readVal, readPos)
  1319. r.lk.Unlock()
  1320. return
  1321. }
  1322. func read(p []byte, int63 func() int64, readVal *int64, readPos *int8) (n int, err error) {
  1323. pos := *readPos
  1324. val := *readVal
  1325. for n = 0; n < len(p); n++ {
  1326. if pos == 0 {
  1327. val = int63()
  1328. pos = 7
  1329. }
  1330. p[n] = byte(val)
  1331. val >>= 8
  1332. pos--
  1333. }
  1334. *readPos = pos
  1335. *readVal = val
  1336. return
  1337. }
  1338. // copied from https://sourcegraph.com/github.com/timtadh/data-structures@master/-/blob/test/support.go
  1339. type T testing.T
  1340. func (t *T) Assert(ok bool, msg string, vars ...interface{}) {
  1341. if !ok {
  1342. t.Log("\n" + string(debug.Stack()))
  1343. t.Fatalf(msg, vars...)
  1344. }
  1345. }
  1346. func (t *T) AssertNil(errors ...error) {
  1347. any := false
  1348. for _, err := range errors {
  1349. if err != nil {
  1350. any = true
  1351. t.Log("\n" + string(debug.Stack()))
  1352. t.Error(err)
  1353. }
  1354. }
  1355. if any {
  1356. t.Fatal("assert failed")
  1357. }
  1358. }
  1359. func RandSlice(length int) []byte {
  1360. slice := make([]byte, length)
  1361. if _, err := crand.Read(slice); err != nil {
  1362. panic(err)
  1363. }
  1364. return slice
  1365. }
  1366. func RandHex(length int) string {
  1367. return hex.EncodeToString(RandSlice(length / 2))
  1368. }
  1369. func RandStr(length int) string {
  1370. return string(RandSlice(length))
  1371. }