blob: 5de475f3368a5118c4b7d1d622ecab609d5c76b5 [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2022 The Dawn & Tint Authors
Ben Claytonf8e0aac2022-12-12 21:49:02 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Ben Claytonf8e0aac2022-12-12 21:49:02 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Ben Claytonf8e0aac2022-12-12 21:49:02 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ben Claytonf8e0aac2022-12-12 21:49:02 +000027
28package cov_test
29
30import (
31 "reflect"
32 "testing"
33
34 "dawn.googlesource.com/dawn/tools/src/cov"
35)
36
37func TestSpanListAddNoMerge(t *testing.T) {
38 l := cov.SpanList{}
39 l.Add(span(3, 1, 3, 5))
40 checkSpanList(t, l, span(3, 1, 3, 5))
41
42 l.Add(span(4, 1, 4, 5))
43 checkSpanList(t, l, span(3, 1, 3, 5), span(4, 1, 4, 5))
44
45 l.Add(span(2, 1, 2, 5))
46 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5))
47}
48
49func TestSpanListAddExpand(t *testing.T) {
50 l := cov.SpanList{span(1, 1, 1, 5), span(5, 4, 5, 7), span(9, 1, 9, 5)}
51
52 // Expand front (column)
53 l.Add(span(5, 1, 5, 5))
54 checkSpanList(t, l, span(1, 1, 1, 5), span(5, 1, 5, 7), span(9, 1, 9, 5))
55
56 // Expand back (column)
57 l.Add(span(5, 5, 5, 9))
58 checkSpanList(t, l, span(1, 1, 1, 5), span(5, 1, 5, 9), span(9, 1, 9, 5))
59
60 // Expand front (line)
61 l.Add(span(4, 3, 5, 2))
62 checkSpanList(t, l, span(1, 1, 1, 5), span(4, 3, 5, 9), span(9, 1, 9, 5))
63
64 // Expand back (line)
65 l.Add(span(5, 4, 6, 3))
66 checkSpanList(t, l, span(1, 1, 1, 5), span(4, 3, 6, 3), span(9, 1, 9, 5))
67
68 // Expand front (touching)
69 l.Add(span(4, 2, 4, 3))
70 checkSpanList(t, l, span(1, 1, 1, 5), span(4, 2, 6, 3), span(9, 1, 9, 5))
71
72 // Expand back (touching)
73 l.Add(span(6, 3, 6, 4))
74 checkSpanList(t, l, span(1, 1, 1, 5), span(4, 2, 6, 4), span(9, 1, 9, 5))
75}
76
77func TestSpanListAddMergeOverlap(t *testing.T) {
78 l := cov.SpanList{span(1, 1, 1, 5), span(5, 4, 5, 7), span(9, 1, 9, 5)}
79
80 l.Add(span(1, 3, 5, 6))
81 checkSpanList(t, l, span(1, 1, 5, 7), span(9, 1, 9, 5))
82
83 l.Add(span(5, 5, 9, 3))
84 checkSpanList(t, l, span(1, 1, 9, 5))
85}
86
87func TestSpanListAddMergeTouching(t *testing.T) {
88 l := cov.SpanList{span(1, 1, 1, 5), span(5, 4, 5, 7), span(9, 1, 9, 5)}
89
90 l.Add(span(1, 5, 9, 1))
91 checkSpanList(t, l, span(1, 1, 9, 5))
92}
93
94func TestSpanListRemoveNothing(t *testing.T) {
95 l := cov.SpanList{span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)}
96
97 l.Remove(span(1, 1, 2, 1))
98 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5))
99
100 l.Remove(span(2, 5, 3, 1))
101 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5))
102
103 l.Remove(span(3, 5, 4, 1))
104 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5))
105
106 l.Remove(span(4, 5, 10, 10))
107 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5))
108}
109
110func TestSpanListRemoveWhole(t *testing.T) {
111 l := cov.SpanList{span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)}
112
113 l.Remove(span(3, 1, 3, 5))
114 checkSpanList(t, l, span(2, 1, 2, 5), span(4, 1, 4, 5))
115
116 l.Remove(span(1, 1, 3, 3))
117 checkSpanList(t, l, span(4, 1, 4, 5))
118
119 l.Remove(span(3, 1, 4, 5))
120 checkSpanList(t, l)
121}
122
123func TestSpanListRemoveZeroLength(t *testing.T) {
124 l := cov.SpanList{span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)}
125
126 l.Remove(span(3, 1, 3, 1))
127 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5))
128
129 l.Remove(span(3, 5, 3, 5))
130 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5))
131}
132
133func TestSpanListRemoveTrim(t *testing.T) {
134 l := cov.SpanList{span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)}
135
136 l.Remove(span(2, 1, 2, 2))
137 checkSpanList(t, l, span(2, 2, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5))
138
139 l.Remove(span(2, 4, 2, 5))
140 checkSpanList(t, l, span(2, 2, 2, 4), span(3, 1, 3, 5), span(4, 1, 4, 5))
141
142 l.Remove(span(2, 5, 3, 2))
143 checkSpanList(t, l, span(2, 2, 2, 4), span(3, 2, 3, 5), span(4, 1, 4, 5))
144
145 l.Remove(span(3, 4, 3, 5))
146 checkSpanList(t, l, span(2, 2, 2, 4), span(3, 2, 3, 4), span(4, 1, 4, 5))
147
148 l.Remove(span(4, 1, 4, 2))
149 checkSpanList(t, l, span(2, 2, 2, 4), span(3, 2, 3, 4), span(4, 2, 4, 5))
150
151 l.Remove(span(4, 4, 4, 5))
152 checkSpanList(t, l, span(2, 2, 2, 4), span(3, 2, 3, 4), span(4, 2, 4, 4))
153}
154
155func TestSpanListRemoveSplit(t *testing.T) {
156 l := cov.SpanList{span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)}
157
158 l.Remove(span(2, 2, 2, 3))
159 checkSpanList(t, l, span(2, 1, 2, 2), span(2, 3, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5))
160
161 l.Remove(span(3, 2, 3, 4))
162 checkSpanList(t, l, span(2, 1, 2, 2), span(2, 3, 2, 5), span(3, 1, 3, 2), span(3, 4, 3, 5), span(4, 1, 4, 5))
163
164 l.Remove(span(4, 2, 4, 2)) // zero length == no split
165 checkSpanList(t, l, span(2, 1, 2, 2), span(2, 3, 2, 5), span(3, 1, 3, 2), span(3, 4, 3, 5), span(4, 1, 4, 5))
166}
167
168func span(startLine, startColumn, endLine, endColumn int) cov.Span {
169 return cov.Span{
170 Start: cov.Location{Line: startLine, Column: startColumn},
171 End: cov.Location{Line: endLine, Column: endColumn},
172 }
173}
174
175func checkSpanList(t *testing.T, got cov.SpanList, expect ...cov.Span) {
176 if expect == nil {
177 expect = cov.SpanList{}
178 }
179 if !reflect.DeepEqual(got, cov.SpanList(expect)) {
180 t.Errorf("SpanList not as expected.\nGot:\n%v\nExpect:\n%v", got, cov.SpanList(expect))
181 }
182}