pageview_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 view
  11. import (
  12. "testing"
  13. "devt.de/krotik/eliasdb/storage/file"
  14. )
  15. func TestPageView(t *testing.T) {
  16. r := file.NewRecord(123, make([]byte, 20))
  17. pv := NewPageView(r, TypeDataPage)
  18. // Check that page type has been set
  19. if r.ReadInt16(0) != 0x1991 {
  20. t.Error("Unexpected header value")
  21. return
  22. }
  23. if r.PageView() != GetPageView(r) {
  24. t.Error("Unexpected page view on record")
  25. return
  26. }
  27. // Test corrupted page
  28. r.WriteSingleByte(0, 0x18)
  29. r.SetPageView(nil)
  30. testCheckMagicPanic(t, r)
  31. r.WriteSingleByte(0, 0x19)
  32. // Record should now contain the correct magic
  33. pv.checkMagic()
  34. if pv.Type() != TypeDataPage {
  35. t.Error("Wrong type for page view")
  36. return
  37. }
  38. if o := pv.String(); o != "PageView: 123 (type:1 previous page:0 next page:0)" {
  39. t.Error("Unexpected String output:", o)
  40. }
  41. // Check next/prev pointers - no particular error checking at this level
  42. if pv.NextPage() != 0 {
  43. t.Error("Unexpected next page")
  44. return
  45. }
  46. pv.SetNextPage(1)
  47. if pv.NextPage() != 1 {
  48. t.Error("Unexpected next page")
  49. return
  50. }
  51. if pv.PrevPage() != 0 {
  52. t.Error("Unexpected prev page")
  53. return
  54. }
  55. pv.SetPrevPage(1)
  56. if pv.PrevPage() != 1 {
  57. t.Error("Unexpected Prev page")
  58. return
  59. }
  60. }
  61. func testCheckMagicPanic(t *testing.T, r *file.Record) {
  62. defer func() {
  63. if r := recover(); r == nil {
  64. t.Error("Getting the page view from a corrupted record did not cause a panic.")
  65. }
  66. }()
  67. GetPageView(r)
  68. }