freephysicalslotpage_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 TestFreePhysicalSlotPage(t *testing.T) {
  18. r := file.NewRecord(123, make([]byte, 44))
  19. testCheckFreePhysicalSlotPageMagicPanic(t, r)
  20. // Make sure the record has a correct magic
  21. view.NewPageView(r, view.TypeFreePhysicalSlotPage)
  22. fpsp := NewFreePhysicalSlotPage(r)
  23. maxSlots := fpsp.MaxSlots()
  24. if maxSlots != 2 {
  25. t.Error("Unexpected number of maxSlots:", maxSlots)
  26. return
  27. }
  28. slotinfoID := fpsp.FirstFreeSlotInfo()
  29. if slotinfoID != 0 {
  30. t.Error("Unexpected first free slot:", slotinfoID)
  31. return
  32. }
  33. offset := fpsp.AllocateSlotInfo(0)
  34. if !fpsp.isAllocatedSlot(0) {
  35. t.Error("Slot 0 not allocated")
  36. return
  37. }
  38. fpsp.SetSlotInfo(offset, 5, 0x22)
  39. fpsp.SetFreeSlotSize(offset, 0x123)
  40. if fpsp.SlotInfoRecord(offset) != 5 {
  41. t.Error("Unexpected slotinfo record")
  42. return
  43. }
  44. if fpsp.SlotInfoOffset(offset) != 0x22 {
  45. t.Error("Unexpected slotinfo offset")
  46. return
  47. }
  48. loc := fpsp.SlotInfoLocation(0)
  49. if util.LocationRecord(loc) != 5 {
  50. t.Error("Unexpected slotinfo record")
  51. return
  52. }
  53. if util.LocationOffset(loc) != 0x22 {
  54. t.Error("Unexpected slotinfo offset")
  55. return
  56. }
  57. if fpsp.FreeSlotSize(offset) != 0x123 {
  58. t.Error("Unexpected slot size")
  59. return
  60. }
  61. if !fpsp.isAllocatedSlot(0) {
  62. t.Error("Slot 0 not allocated")
  63. return
  64. }
  65. if fpsp.isAllocatedSlot(1) {
  66. t.Error("Slot 1 should not be allocated")
  67. return
  68. }
  69. if fpsp.FirstFreeSlotInfo() != 1 {
  70. t.Error("Unexpected first free result", fpsp.FirstFreeSlotInfo())
  71. return
  72. }
  73. fpsp.AllocateSlotInfo(1)
  74. if fpsp.FirstFreeSlotInfo() != -1 {
  75. t.Error("Unexpected first free result", fpsp.FirstFreeSlotInfo())
  76. return
  77. }
  78. fpsp.ReleaseSlotInfo(1)
  79. fpsp.ReleaseSlotInfo(0)
  80. if fpsp.isAllocatedSlot(0) {
  81. t.Error("Slot 0 should no longer be allocated")
  82. return
  83. }
  84. if fpsp.FirstFreeSlotInfo() != 0 {
  85. t.Error("Unexpected first free result", fpsp.FirstFreeSlotInfo())
  86. return
  87. }
  88. }
  89. func TestFreePhysicalSlotPageAllocation(t *testing.T) {
  90. r := file.NewRecord(123, make([]byte, 4096))
  91. view.NewPageView(r, view.TypeFreePhysicalSlotPage)
  92. fpsp := NewFreePhysicalSlotPage(r)
  93. maxSlots := fpsp.MaxSlots()
  94. if maxSlots != 339 {
  95. t.Error("Unexpected number of maxSlots:", maxSlots)
  96. return
  97. }
  98. if fpsp.maxAcceptableWaste != 1024 {
  99. t.Error("Unexpected max accpectable waste:", fpsp.maxAcceptableWaste)
  100. return
  101. }
  102. // Allocate some free physical flats
  103. offset := fpsp.AllocateSlotInfo(3)
  104. fpsp.SetSlotInfo(offset, 0x22, 0x22)
  105. fpsp.SetFreeSlotSize(offset, 100)
  106. offset = fpsp.AllocateSlotInfo(5)
  107. fpsp.SetSlotInfo(offset, 0x22, 0x22)
  108. fpsp.SetFreeSlotSize(offset, 5024)
  109. offset = fpsp.AllocateSlotInfo(7)
  110. fpsp.SetSlotInfo(offset, 0x22, 0x22)
  111. fpsp.SetFreeSlotSize(offset, 2000)
  112. offset = fpsp.AllocateSlotInfo(9)
  113. fpsp.SetSlotInfo(offset, 0x22, 0x22)
  114. fpsp.SetFreeSlotSize(offset, 500)
  115. offset = fpsp.AllocateSlotInfo(19)
  116. fpsp.SetSlotInfo(offset, 0x22, 0x22)
  117. fpsp.SetFreeSlotSize(offset, 50000)
  118. if slot := fpsp.FindSlot(500); slot != 9 {
  119. t.Error("Unexpected found slot:", slot)
  120. return
  121. }
  122. if slot := fpsp.FindSlot(499); slot != 9 {
  123. t.Error("Unexpected found slot:", slot)
  124. return
  125. }
  126. // Test not found slots
  127. if slot := fpsp.FindSlot(4000); slot != -50000 {
  128. t.Error("Unexpected found slot:", slot)
  129. return
  130. }
  131. if slot := fpsp.FindSlot(4001); slot != 5 {
  132. t.Error("Unexpected found slot:", slot)
  133. return
  134. }
  135. }
  136. func testCheckFreePhysicalSlotPageMagicPanic(t *testing.T, r *file.Record) {
  137. defer func() {
  138. if r := recover(); r == nil {
  139. t.Error("Checking magic should fail.")
  140. }
  141. }()
  142. checkFreePhysicalSlotPageMagic(r)
  143. }