datapage_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. )
  16. func TestDataPage(t *testing.T) {
  17. r := file.NewRecord(123, make([]byte, 44))
  18. testCheckDataPageMagicPanic(t, r)
  19. // Make sure the record has a correct magic
  20. view.NewPageView(r, view.TypeDataPage)
  21. dp := NewDataPage(r)
  22. if ds := dp.DataSpace(); ds != 24 {
  23. t.Error("Unexpected data space", ds)
  24. }
  25. testCheckDataPageOffsetFirstPanic(t, dp)
  26. if of := dp.OffsetFirst(); of != 0 {
  27. t.Error("Unexpected first offset", of)
  28. return
  29. }
  30. dp.SetOffsetFirst(20)
  31. if of := dp.OffsetFirst(); of != 20 {
  32. t.Error("Unexpected first offset", of)
  33. return
  34. }
  35. }
  36. func testCheckDataPageMagicPanic(t *testing.T, r *file.Record) {
  37. defer func() {
  38. if r := recover(); r == nil {
  39. t.Error("Checking magic should fail.")
  40. }
  41. }()
  42. checkDataPageMagic(r)
  43. }
  44. func testCheckDataPageOffsetFirstPanic(t *testing.T, dp *DataPage) {
  45. defer func() {
  46. if r := recover(); r == nil {
  47. t.Error("Setting offset which is too small should fail.")
  48. }
  49. }()
  50. dp.SetOffsetFirst(OffsetData - 1)
  51. }