freelogicalslotpage_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 pageview
  11. import (
  12. "testing"
  13. "devt.de/krotik/eliasdb/storage/file"
  14. "devt.de/krotik/eliasdb/storage/paging/view"
  15. "devt.de/krotik/eliasdb/storage/util"
  16. )
  17. func TestFreeLogicalSlotPage(t *testing.T) {
  18. r := file.NewRecord(123, make([]byte, 44))
  19. testCheckFreeLogicalSlotPageMagicPanic(t, r)
  20. // Make sure the record has a correct magic
  21. view.NewPageView(r, view.TypeFreeLogicalSlotPage)
  22. flsp := NewFreeLogicalSlotPage(r)
  23. maxSlots := flsp.MaxSlots()
  24. if maxSlots != 3 {
  25. t.Error("Unexpected number of maxSlots:", maxSlots)
  26. return
  27. }
  28. slotinfoID := flsp.FirstFreeSlotInfo()
  29. if slotinfoID != 0 {
  30. t.Error("Unexpected first free slot:", slotinfoID)
  31. return
  32. }
  33. offset := flsp.AllocateSlotInfo(0)
  34. if !flsp.isAllocatedSlot(0) {
  35. t.Error("Slot 0 not allocated")
  36. return
  37. }
  38. flsp.SetSlotInfo(offset, 5, 0x22)
  39. if flsp.SlotInfoRecord(offset) != 5 {
  40. t.Error("Unexpected slotinfo record")
  41. return
  42. }
  43. if flsp.SlotInfoOffset(offset) != 0x22 {
  44. t.Error("Unexpected slotinfo offset")
  45. return
  46. }
  47. loc := flsp.SlotInfoLocation(0)
  48. if util.LocationRecord(loc) != 5 {
  49. t.Error("Unexpected slotinfo record")
  50. return
  51. }
  52. if util.LocationOffset(loc) != 0x22 {
  53. t.Error("Unexpected slotinfo offset")
  54. return
  55. }
  56. if !flsp.isAllocatedSlot(0) {
  57. t.Error("Slot 0 not allocated")
  58. return
  59. }
  60. if flsp.isAllocatedSlot(1) {
  61. t.Error("Slot 1 should not be allocated")
  62. return
  63. }
  64. if flsp.FirstFreeSlotInfo() != 1 {
  65. t.Error("Unexpected first free result", flsp.FirstFreeSlotInfo())
  66. return
  67. }
  68. flsp.AllocateSlotInfo(1)
  69. if fsi := flsp.FirstFreeSlotInfo(); fsi != 2 {
  70. t.Error("Unexpected first allocatable slot", fsi)
  71. return
  72. }
  73. flsp.AllocateSlotInfo(2)
  74. if flsp.FirstFreeSlotInfo() != -1 {
  75. t.Error("Unexpected first free result", flsp.FirstFreeSlotInfo())
  76. return
  77. }
  78. flsp.ReleaseSlotInfo(1)
  79. if flsp.FirstFreeSlotInfo() != 1 {
  80. t.Error("Unexpected first free result", flsp.FirstFreeSlotInfo())
  81. return
  82. }
  83. flsp.AllocateSlotInfo(1)
  84. if flsp.FirstFreeSlotInfo() != -1 {
  85. t.Error("Unexpected first free result", flsp.FirstFreeSlotInfo())
  86. return
  87. }
  88. flsp.AllocateSlotInfo(2)
  89. flsp.AllocateSlotInfo(1)
  90. flsp.ReleaseSlotInfo(0)
  91. if flsp.isAllocatedSlot(0) {
  92. t.Error("Slot 0 should no longer be allocated")
  93. return
  94. }
  95. if flsp.FirstAllocatedSlotInfo() != 1 {
  96. t.Error("Unexpected first allocated result", flsp.FirstFreeSlotInfo())
  97. return
  98. }
  99. if flsp.FirstFreeSlotInfo() != 0 {
  100. t.Error("Unexpected first free result", flsp.FirstFreeSlotInfo())
  101. return
  102. }
  103. if flsp.prevFoundAllocatedSlot != 1 {
  104. t.Error("Unexpected to previous found allocated slot:",
  105. flsp.prevFoundAllocatedSlot)
  106. }
  107. flsp.AllocateSlotInfo(0)
  108. if flsp.prevFoundAllocatedSlot != 0 {
  109. t.Error("Unexpected to previous found allocated slot:",
  110. flsp.prevFoundAllocatedSlot)
  111. }
  112. flsp.ReleaseSlotInfo(0)
  113. flsp.ReleaseSlotInfo(1)
  114. flsp.ReleaseSlotInfo(2)
  115. if flsp.FirstAllocatedSlotInfo() != -1 {
  116. t.Error("Unexpected first allocated result", flsp.FirstFreeSlotInfo())
  117. return
  118. }
  119. }
  120. func testCheckFreeLogicalSlotPageMagicPanic(t *testing.T, r *file.Record) {
  121. defer func() {
  122. if r := recover(); r == nil {
  123. t.Error("Checking magic should fail.")
  124. }
  125. }()
  126. checkFreeLogicalSlotPageMagic(r)
  127. }