1// Copyright 2020 The SwiftShader Authors. All Rights Reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package cov_test 16 17import ( 18 "reflect" 19 "testing" 20 21 cov "." 22) 23 24func TestSpanListAddNoMerge(t *testing.T) { 25 l := cov.SpanList{} 26 l.Add(span(3, 1, 3, 5)) 27 checkSpanList(t, l, span(3, 1, 3, 5)) 28 29 l.Add(span(4, 1, 4, 5)) 30 checkSpanList(t, l, span(3, 1, 3, 5), span(4, 1, 4, 5)) 31 32 l.Add(span(2, 1, 2, 5)) 33 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)) 34} 35 36func TestSpanListAddExpand(t *testing.T) { 37 l := cov.SpanList{span(1, 1, 1, 5), span(5, 4, 5, 7), span(9, 1, 9, 5)} 38 39 // Expand front (column) 40 l.Add(span(5, 1, 5, 5)) 41 checkSpanList(t, l, span(1, 1, 1, 5), span(5, 1, 5, 7), span(9, 1, 9, 5)) 42 43 // Expand back (column) 44 l.Add(span(5, 5, 5, 9)) 45 checkSpanList(t, l, span(1, 1, 1, 5), span(5, 1, 5, 9), span(9, 1, 9, 5)) 46 47 // Expand front (line) 48 l.Add(span(4, 3, 5, 2)) 49 checkSpanList(t, l, span(1, 1, 1, 5), span(4, 3, 5, 9), span(9, 1, 9, 5)) 50 51 // Expand back (line) 52 l.Add(span(5, 4, 6, 3)) 53 checkSpanList(t, l, span(1, 1, 1, 5), span(4, 3, 6, 3), span(9, 1, 9, 5)) 54 55 // Expand front (touching) 56 l.Add(span(4, 2, 4, 3)) 57 checkSpanList(t, l, span(1, 1, 1, 5), span(4, 2, 6, 3), span(9, 1, 9, 5)) 58 59 // Expand back (touching) 60 l.Add(span(6, 3, 6, 4)) 61 checkSpanList(t, l, span(1, 1, 1, 5), span(4, 2, 6, 4), span(9, 1, 9, 5)) 62} 63 64func TestSpanListAddMergeOverlap(t *testing.T) { 65 l := cov.SpanList{span(1, 1, 1, 5), span(5, 4, 5, 7), span(9, 1, 9, 5)} 66 67 l.Add(span(1, 3, 5, 6)) 68 checkSpanList(t, l, span(1, 1, 5, 7), span(9, 1, 9, 5)) 69 70 l.Add(span(5, 5, 9, 3)) 71 checkSpanList(t, l, span(1, 1, 9, 5)) 72} 73 74func TestSpanListAddMergeTouching(t *testing.T) { 75 l := cov.SpanList{span(1, 1, 1, 5), span(5, 4, 5, 7), span(9, 1, 9, 5)} 76 77 l.Add(span(1, 5, 9, 1)) 78 checkSpanList(t, l, span(1, 1, 9, 5)) 79} 80 81func TestSpanListRemoveNothing(t *testing.T) { 82 l := cov.SpanList{span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)} 83 84 l.Remove(span(1, 1, 2, 1)) 85 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)) 86 87 l.Remove(span(2, 5, 3, 1)) 88 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)) 89 90 l.Remove(span(3, 5, 4, 1)) 91 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)) 92 93 l.Remove(span(4, 5, 10, 10)) 94 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)) 95} 96 97func TestSpanListRemoveWhole(t *testing.T) { 98 l := cov.SpanList{span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)} 99 100 l.Remove(span(3, 1, 3, 5)) 101 checkSpanList(t, l, span(2, 1, 2, 5), span(4, 1, 4, 5)) 102 103 l.Remove(span(1, 1, 3, 3)) 104 checkSpanList(t, l, span(4, 1, 4, 5)) 105 106 l.Remove(span(3, 1, 4, 5)) 107 checkSpanList(t, l) 108} 109 110func TestSpanListRemoveZeroLength(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, 1)) 114 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)) 115 116 l.Remove(span(3, 5, 3, 5)) 117 checkSpanList(t, l, span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)) 118} 119 120func TestSpanListRemoveTrim(t *testing.T) { 121 l := cov.SpanList{span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)} 122 123 l.Remove(span(2, 1, 2, 2)) 124 checkSpanList(t, l, span(2, 2, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)) 125 126 l.Remove(span(2, 4, 2, 5)) 127 checkSpanList(t, l, span(2, 2, 2, 4), span(3, 1, 3, 5), span(4, 1, 4, 5)) 128 129 l.Remove(span(2, 5, 3, 2)) 130 checkSpanList(t, l, span(2, 2, 2, 4), span(3, 2, 3, 5), span(4, 1, 4, 5)) 131 132 l.Remove(span(3, 4, 3, 5)) 133 checkSpanList(t, l, span(2, 2, 2, 4), span(3, 2, 3, 4), span(4, 1, 4, 5)) 134 135 l.Remove(span(4, 1, 4, 2)) 136 checkSpanList(t, l, span(2, 2, 2, 4), span(3, 2, 3, 4), span(4, 2, 4, 5)) 137 138 l.Remove(span(4, 4, 4, 5)) 139 checkSpanList(t, l, span(2, 2, 2, 4), span(3, 2, 3, 4), span(4, 2, 4, 4)) 140} 141 142func TestSpanListRemoveSplit(t *testing.T) { 143 l := cov.SpanList{span(2, 1, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)} 144 145 l.Remove(span(2, 2, 2, 3)) 146 checkSpanList(t, l, span(2, 1, 2, 2), span(2, 3, 2, 5), span(3, 1, 3, 5), span(4, 1, 4, 5)) 147 148 l.Remove(span(3, 2, 3, 4)) 149 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)) 150 151 l.Remove(span(4, 2, 4, 2)) // zero length == no split 152 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)) 153} 154 155func span(startLine, startColumn, endLine, endColumn int) cov.Span { 156 return cov.Span{ 157 Start: cov.Location{Line: startLine, Column: startColumn}, 158 End: cov.Location{Line: endLine, Column: endColumn}, 159 } 160} 161 162func checkSpanList(t *testing.T, got cov.SpanList, expect ...cov.Span) { 163 if expect == nil { 164 expect = cov.SpanList{} 165 } 166 if !reflect.DeepEqual(got, cov.SpanList(expect)) { 167 t.Errorf("SpanList not as expected.\nGot:\n%v\nExpect:\n%v", got, cov.SpanList(expect)) 168 } 169} 170