pagecursor_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package paging
  11. import (
  12. "testing"
  13. "devt.de/krotik/eliasdb/storage/file"
  14. "devt.de/krotik/eliasdb/storage/paging/view"
  15. )
  16. func TestPageCursor(t *testing.T) {
  17. sf, err := file.NewDefaultStorageFile(DBDIR+"/test4", false)
  18. if err != nil {
  19. t.Error(err.Error())
  20. return
  21. }
  22. psf, err := NewPagedStorageFile(sf)
  23. if err != nil {
  24. t.Error(err)
  25. return
  26. }
  27. plist := make([]uint64, 0, 5)
  28. for i := 0; i < 5; i++ {
  29. p, err := psf.AllocatePage(view.TypeDataPage)
  30. if err != nil {
  31. t.Error(err)
  32. }
  33. plist = append(plist, p)
  34. }
  35. pc := NewPageCursor(psf, view.TypeDataPage, 0)
  36. checkPrev(t, pc, 0)
  37. if cur := pc.Current(); cur != 0 {
  38. t.Error("Unexpected current page", cur)
  39. }
  40. checkNext(t, pc, 1)
  41. if cur := pc.Current(); cur != 1 {
  42. t.Error("Unexpected current page", cur)
  43. }
  44. checkNext(t, pc, 2)
  45. checkPrev(t, pc, 1)
  46. checkPrev(t, pc, 0)
  47. checkPrev(t, pc, 0)
  48. // Once the first page was iterated we will not go back to 0
  49. if cur := pc.Current(); cur != 1 {
  50. t.Error("Unexpected current page", cur)
  51. }
  52. checkNext(t, pc, 2)
  53. checkNext(t, pc, 3)
  54. checkNext(t, pc, 4)
  55. checkNext(t, pc, 5)
  56. if cur := pc.Current(); cur != 5 {
  57. t.Error("Unexpected current page", cur)
  58. }
  59. checkNext(t, pc, 0)
  60. if cur := pc.Current(); cur != 5 {
  61. t.Error("Unexpected current page", cur)
  62. }
  63. checkPrev(t, pc, 4)
  64. // Test error cases of next / prev by putting records in use
  65. sf.Get(4)
  66. _, err = pc.Prev()
  67. if err != file.ErrAlreadyInUse {
  68. t.Error("Operation should fail as the required record is in use")
  69. return
  70. }
  71. _, err = pc.Next()
  72. if err != file.ErrAlreadyInUse {
  73. t.Error("Operation should fail as the required record is in use")
  74. return
  75. }
  76. sf.ReleaseInUseID(4, false)
  77. psf.Close()
  78. }
  79. func checkNext(t *testing.T, pc *PageCursor, expected uint64) {
  80. next, err := pc.Next()
  81. if err != nil {
  82. t.Error(err)
  83. return
  84. }
  85. if next != expected {
  86. t.Error("Unexpected next page", next, "expected", expected)
  87. }
  88. }
  89. func checkPrev(t *testing.T, pc *PageCursor, expected uint64) {
  90. prev, err := pc.Prev()
  91. if err != nil {
  92. t.Error(err)
  93. return
  94. }
  95. if prev != expected {
  96. t.Error("Unexpected previous page", prev, "expected", expected)
  97. }
  98. }