slotsize_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 util
  11. import (
  12. "testing"
  13. "devt.de/krotik/eliasdb/storage/file"
  14. )
  15. func TestSlotSize(t *testing.T) {
  16. var i, mxSlotSize uint32
  17. mxSlotSize = decodeSize(UnsignedShortMax)
  18. r := file.NewRecord(123, make([]byte, 20))
  19. if AvailableSize(r, 2) != 0 {
  20. t.Error("Unexpected size")
  21. return
  22. }
  23. if CurrentSize(r, 2) != 0 {
  24. t.Error("Unexpected initial size")
  25. return
  26. }
  27. for i = 0; i < mxSlotSize; i = i + 1000 {
  28. round := NormalizeSlotSize(i)
  29. if uint32(round) < i {
  30. t.Error("Normalized size ", round, " is smaller than actual size:", i)
  31. return
  32. }
  33. if i < 17000 { // Up to 17 kilobyte
  34. // the rouding result should be exact.
  35. if round-i != 0 {
  36. t.Error("Unexpected rounding result.")
  37. return
  38. }
  39. } else if i < 278495 { // Up to 271.966797 kilobyte
  40. // we are no more then 15 bytes off
  41. if round-i >= 16 {
  42. t.Error("Unexpected rounding result.")
  43. return
  44. }
  45. testNormalizationPanic(t, r, i)
  46. } else if i < 4472287 { // Up to 4.26510429 megabyte
  47. // we are no more then 255 bytes off
  48. if round-i >= 256 {
  49. t.Error("Unexpected rounding result.")
  50. return
  51. }
  52. testNormalizationPanic(t, r, i)
  53. } else { // In all other cases
  54. // we are never more then 8191 bytes off
  55. if round-i >= 8192 {
  56. t.Error("Unexpected rounding result.")
  57. return
  58. }
  59. testNormalizationPanic(t, r, i)
  60. }
  61. SetAvailableSize(r, 2, round)
  62. if r.ReadSingleByte(1) != 0 {
  63. t.Error("Unexpected record data on byte 1:", r)
  64. return
  65. }
  66. if r.ReadSingleByte(6) != 0 {
  67. t.Error("Unexpected record data on byte 6:", r)
  68. return
  69. }
  70. }
  71. if r.ReadUInt16(2) != 0xFFFF {
  72. t.Error("Unexpected written size value")
  73. return
  74. }
  75. testNormalizationPanic(t, r, mxSlotSize+1)
  76. SetCurrentSize(r, 2, mxSlotSize-MaxAvailableSizeDifference)
  77. if CurrentSize(r, 2) != mxSlotSize-MaxAvailableSizeDifference {
  78. t.Error("Unexpected current size on extreme values")
  79. }
  80. r = file.NewRecord(123, make([]byte, 20))
  81. // Test a growing slot
  82. SetAvailableSize(r, 2, 100)
  83. SetCurrentSize(r, 2, 1)
  84. SetCurrentSize(r, 2, 10)
  85. SetCurrentSize(r, 2, 100)
  86. // Test panic if the slot grows too much
  87. testCurrentSizePanic(t, r, 101)
  88. }
  89. func testNormalizationPanic(t *testing.T, record *file.Record, i uint32) {
  90. defer func() {
  91. if r := recover(); r == nil {
  92. t.Error("Using not normalized sized values should cause a panic.")
  93. }
  94. }()
  95. SetAvailableSize(record, 2, i)
  96. }
  97. func testCurrentSizePanic(t *testing.T, record *file.Record, i uint32) {
  98. defer func() {
  99. if r := recover(); r == nil {
  100. t.Error("Using not normalized sized values should cause a panic.")
  101. }
  102. }()
  103. SetCurrentSize(record, 2, i)
  104. }