pagedstoragefileheader_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 paging
  11. import (
  12. "testing"
  13. "devt.de/krotik/eliasdb/storage/file"
  14. )
  15. func TestPagedStorageFileHeader(t *testing.T) {
  16. record := file.NewRecord(5, make([]byte, 5, 5))
  17. testPagedStorageFileInitPanic1(t, record)
  18. record = file.NewRecord(5, make([]byte, 100, 100))
  19. testPagedStorageFileInitPanic2(t, record)
  20. NewPagedStorageFileHeader(record, true)
  21. psfh := NewPagedStorageFileHeader(record, true)
  22. if psfh.Roots() != 2 {
  23. t.Error("Unexpected number of roots:", psfh.Roots())
  24. }
  25. psfh.SetRoot(1, 0x42)
  26. if psfh.Root(1) != 0x42 {
  27. t.Error("Unexpected root value:", psfh.Root(1))
  28. }
  29. if psfh.Root(0) != 0x00 {
  30. t.Error("Unexpected root value:", psfh.Root(0))
  31. }
  32. psfh.SetFirstListElement(3, 5)
  33. if psfh.FirstListElement(3) != 5 {
  34. t.Error("Unexpected root value:", psfh.FirstListElement(3))
  35. }
  36. psfh.SetLastListElement(2, 5)
  37. if psfh.LastListElement(2) != 5 {
  38. t.Error("Unexpected root value:", psfh.LastListElement(3))
  39. }
  40. }
  41. func testPagedStorageFileInitPanic1(t *testing.T, r *file.Record) {
  42. defer func() {
  43. if r := recover(); r == nil {
  44. t.Error("Using a record which is too small did not cause a panic.")
  45. }
  46. }()
  47. NewPagedStorageFileHeader(r, true)
  48. }
  49. func testPagedStorageFileInitPanic2(t *testing.T, r *file.Record) {
  50. defer func() {
  51. if r := recover(); r == nil {
  52. t.Error("Using a record without header magic value did not cause a panic.")
  53. }
  54. }()
  55. NewPagedStorageFileHeader(r, false)
  56. }