github.com/serversong/goreporter@v0.0.0-20200325104552-3cfaf44fd178/linters/copycheck/syntax/syntax_test.go (about) 1 package syntax 2 3 import "testing" 4 5 func TestSerialization(t *testing.T) { 6 n := genNodes(7) 7 n[0].AddChildren(n[1], n[2], n[3]) 8 n[1].AddChildren(n[4], n[5]) 9 n[2].AddChildren(n[6]) 10 m := genNodes(6) 11 m[0].AddChildren(m[1], m[2], m[3], m[4], m[5]) 12 testCases := []struct { 13 t *Node 14 expected []int 15 }{ 16 {n[0], []int{6, 2, 0, 0, 1, 0, 0}}, 17 {m[0], []int{5, 0, 0, 0, 0, 0}}, 18 } 19 20 for _, tc := range testCases { 21 compareSeries(t, Serialize(tc.t), tc.expected) 22 } 23 } 24 25 func genNodes(cnt int) []*Node { 26 nodes := make([]*Node, cnt) 27 for i := range nodes { 28 nodes[i] = NewNode() 29 } 30 return nodes 31 } 32 33 func compareSeries(t *testing.T, stream []*Node, owns []int) { 34 if len(stream) != len(owns) { 35 t.Errorf("series aren't the same length; got %d, want %d", len(stream), len(owns)) 36 return 37 } 38 for i, item := range stream { 39 if item.Owns != owns[i] { 40 t.Errorf("got %d, want %d", item.Owns, owns[i]) 41 } 42 } 43 } 44 45 func TestGetUnitsIndexes(t *testing.T) { 46 testCases := []struct { 47 seq string 48 threshold int 49 expected []int 50 }{ 51 {"a8 a0 a2 a0 a0", 3, []int{2}}, 52 {"a0 a8 a2 a0 a0", 1, []int{2}}, 53 {"a3 a0 a0 a0 a1", 3, []int{0}}, 54 {"a3 a0 a0 a0 a0", 1, []int{0, 4}}, 55 {"a1 a0 a1 a0 a0", 2, []int{0, 2}}, 56 } 57 58 Loop: 59 for _, tc := range testCases { 60 nodes := str2nodes(tc.seq) 61 indexes := getUnitsIndexes(nodes, tc.threshold) 62 for i := range tc.expected { 63 if i > len(indexes)-1 || tc.expected[i] != indexes[i] { 64 t.Errorf("for seq '%s', got %v, want %v", tc.seq, indexes, tc.expected) 65 } 66 continue Loop 67 } 68 } 69 } 70 71 func TestCyclicDupl(t *testing.T) { 72 testCases := []struct { 73 seq string 74 indexes []int 75 expected bool 76 }{ 77 {"a1 b0 a2 b0", []int{0, 2}, false}, 78 {"a1 b0 a1 b0", []int{0, 2}, true}, 79 {"a0 a0", []int{0, 1}, true}, 80 {"a1 b0 c1 b0 a1 b0 c1 b0", []int{0, 2, 4, 6}, true}, 81 {"a1 b0 c1 b0 a1 b0", []int{0, 2, 4}, false}, 82 {"a0 b0 a0 c0", []int{0, 1, 2, 3}, false}, 83 {"a0 b0 a0 b0 a0", []int{0, 1, 2}, false}, 84 {"a1 b0 a1 b0 c1 b0", []int{0, 2, 4}, false}, 85 {"a1 a1 a1 a1 a1 a1", []int{0, 4}, false}, 86 {"a2 b0 b0 a2 b0 b0 a2 b0 b0 a2 b0 b0 a2 b0 b0", []int{0, 3, 6, 9, 12}, true}, 87 } 88 89 for _, tc := range testCases { 90 nodes := str2nodes(tc.seq) 91 if tc.expected != isCyclic(tc.indexes, nodes) { 92 t.Errorf("for seq '%s', indexes %v, got %t, want %t", tc.seq, tc.indexes, !tc.expected, tc.expected) 93 } 94 } 95 } 96 97 // str2nodes converts strint to a sequence of *Node by following principle: 98 // - node is represented by 2 characters 99 // - first character is node type 100 // - second character is the number for Node.Owns. 101 func str2nodes(str string) []*Node { 102 chars := []rune(str) 103 nodes := make([]*Node, (len(chars)+1)/3) 104 for i := 0; i < len(chars)-1; i += 3 { 105 nodes[i/3] = &Node{Type: int(chars[i]), Owns: int(chars[i+1] - '0')} 106 } 107 return nodes 108 }