inversion_tree_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Unit tests for inversion tree.
  3. *
  4. * Copyright 2016, Peter Collins
  5. */
  6. package reedsolomon
  7. import (
  8. "testing"
  9. )
  10. func TestNewInversionTree(t *testing.T) {
  11. tree := newInversionTree(3, 2)
  12. children := len(tree.root.children)
  13. if children != 5 {
  14. t.Fatal("Root node children list length", children, "!=", 5)
  15. }
  16. str := tree.root.matrix.String()
  17. expect := "[[1, 0, 0], [0, 1, 0], [0, 0, 1]]"
  18. if str != expect {
  19. t.Fatal(str, "!=", expect)
  20. }
  21. }
  22. func TestGetInvertedMatrix(t *testing.T) {
  23. tree := newInversionTree(3, 2)
  24. matrix := tree.GetInvertedMatrix([]int{})
  25. str := matrix.String()
  26. expect := "[[1, 0, 0], [0, 1, 0], [0, 0, 1]]"
  27. if str != expect {
  28. t.Fatal(str, "!=", expect)
  29. }
  30. matrix = tree.GetInvertedMatrix([]int{1})
  31. if matrix != nil {
  32. t.Fatal(matrix, "!= nil")
  33. }
  34. matrix = tree.GetInvertedMatrix([]int{1, 2})
  35. if matrix != nil {
  36. t.Fatal(matrix, "!= nil")
  37. }
  38. matrix, err := newMatrix(3, 3)
  39. if err != nil {
  40. t.Fatalf("Failed initializing new Matrix : %s", err)
  41. }
  42. err = tree.InsertInvertedMatrix([]int{1}, matrix, 5)
  43. if err != nil {
  44. t.Fatalf("Failed inserting new Matrix : %s", err)
  45. }
  46. cachedMatrix := tree.GetInvertedMatrix([]int{1})
  47. if cachedMatrix == nil {
  48. t.Fatal(cachedMatrix, "== nil")
  49. }
  50. if matrix.String() != cachedMatrix.String() {
  51. t.Fatal(matrix.String(), "!=", cachedMatrix.String())
  52. }
  53. }
  54. func TestInsertInvertedMatrix(t *testing.T) {
  55. tree := newInversionTree(3, 2)
  56. matrix, err := newMatrix(3, 3)
  57. if err != nil {
  58. t.Fatalf("Failed initializing new Matrix : %s", err)
  59. }
  60. err = tree.InsertInvertedMatrix([]int{1}, matrix, 5)
  61. if err != nil {
  62. t.Fatalf("Failed inserting new Matrix : %s", err)
  63. }
  64. err = tree.InsertInvertedMatrix([]int{}, matrix, 5)
  65. if err == nil {
  66. t.Fatal("Should have failed inserting the root node matrix", matrix)
  67. }
  68. matrix, err = newMatrix(3, 2)
  69. if err != nil {
  70. t.Fatalf("Failed initializing new Matrix : %s", err)
  71. }
  72. err = tree.InsertInvertedMatrix([]int{2}, matrix, 5)
  73. if err == nil {
  74. t.Fatal("Should have failed inserting a non-square matrix", matrix)
  75. }
  76. matrix, err = newMatrix(3, 3)
  77. if err != nil {
  78. t.Fatalf("Failed initializing new Matrix : %s", err)
  79. }
  80. err = tree.InsertInvertedMatrix([]int{0, 1}, matrix, 5)
  81. if err != nil {
  82. t.Fatalf("Failed inserting new Matrix : %s", err)
  83. }
  84. }
  85. func TestDoubleInsertInvertedMatrix(t *testing.T) {
  86. tree := newInversionTree(3, 2)
  87. matrix, err := newMatrix(3, 3)
  88. if err != nil {
  89. t.Fatalf("Failed initializing new Matrix : %s", err)
  90. }
  91. err = tree.InsertInvertedMatrix([]int{1}, matrix, 5)
  92. if err != nil {
  93. t.Fatalf("Failed inserting new Matrix : %s", err)
  94. }
  95. err = tree.InsertInvertedMatrix([]int{1}, matrix, 5)
  96. if err != nil {
  97. t.Fatalf("Failed inserting new Matrix : %s", err)
  98. }
  99. cachedMatrix := tree.GetInvertedMatrix([]int{1})
  100. if cachedMatrix == nil {
  101. t.Fatal(cachedMatrix, "== nil")
  102. }
  103. if matrix.String() != cachedMatrix.String() {
  104. t.Fatal(matrix.String(), "!=", cachedMatrix.String())
  105. }
  106. }