Debian Codegraph @

inversion_tree_test.go

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