github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/ranges/range_test.go (about) 1 package ranges 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestRevertRanges(t *testing.T) { 9 for _, testRange := range []struct { 10 start, end int 11 ranges []Range[int] 12 expected []Range[int] 13 }{ 14 { 15 start: 0, 16 end: 10, 17 ranges: []Range[int]{ 18 {0, 1}, 19 }, 20 expected: []Range[int]{ 21 {2, 10}, 22 }, 23 }, 24 { 25 start: 0, 26 end: 10, 27 ranges: []Range[int]{ 28 {9, 10}, 29 }, 30 expected: []Range[int]{ 31 {0, 8}, 32 }, 33 }, 34 { 35 start: 0, 36 end: 10, 37 ranges: []Range[int]{ 38 {0, 1}, 39 {9, 10}, 40 }, 41 expected: []Range[int]{ 42 {2, 8}, 43 }, 44 }, 45 { 46 start: 0, 47 end: 10, 48 ranges: []Range[int]{ 49 {2, 4}, 50 {6, 8}, 51 }, 52 expected: []Range[int]{ 53 {0, 1}, 54 {5, 5}, 55 {9, 10}, 56 }, 57 }, 58 { 59 start: 0, 60 end: 10, 61 ranges: []Range[int]{ 62 {2, 4}, 63 {8, 9}, 64 }, 65 expected: []Range[int]{ 66 {0, 1}, 67 {5, 7}, 68 {10, 10}, 69 }, 70 }, 71 } { 72 result := Revert(testRange.start, testRange.end, testRange.ranges) 73 if !reflect.DeepEqual(result, testRange.expected) { 74 t.Fatal("expected", testRange.expected, "\ngot", result) 75 } 76 } 77 } 78 79 func TestMergeRanges(t *testing.T) { 80 for _, testRange := range []struct { 81 ranges []Range[int] 82 expected []Range[int] 83 }{ 84 { 85 ranges: []Range[int]{ 86 {0, 1}, 87 {1, 2}, 88 }, 89 expected: []Range[int]{ 90 {0, 2}, 91 }, 92 }, 93 { 94 ranges: []Range[int]{ 95 {0, 3}, 96 {5, 7}, 97 {8, 9}, 98 {10, 10}, 99 }, 100 expected: []Range[int]{ 101 {0, 3}, 102 {5, 10}, 103 }, 104 }, 105 { 106 ranges: []Range[int]{ 107 {1, 3}, 108 {2, 6}, 109 {8, 10}, 110 {15, 18}, 111 }, 112 expected: []Range[int]{ 113 {1, 6}, 114 {8, 10}, 115 {15, 18}, 116 }, 117 }, 118 { 119 ranges: []Range[int]{ 120 {1, 3}, 121 {2, 7}, 122 {2, 6}, 123 }, 124 expected: []Range[int]{ 125 {1, 7}, 126 }, 127 }, 128 { 129 ranges: []Range[int]{ 130 {1, 3}, 131 {2, 6}, 132 {2, 7}, 133 }, 134 expected: []Range[int]{ 135 {1, 7}, 136 }, 137 }, 138 } { 139 result := Merge(testRange.ranges) 140 if !reflect.DeepEqual(result, testRange.expected) { 141 t.Fatal("input", testRange.ranges, "\nexpected", testRange.expected, "\ngot", result) 142 } 143 } 144 } 145 146 func TestExcludeRanges(t *testing.T) { 147 for _, testRange := range []struct { 148 ranges []Range[int] 149 exclude []Range[int] 150 expected []Range[int] 151 }{ 152 { 153 ranges: []Range[int]{ 154 {0, 100}, 155 }, 156 exclude: []Range[int]{ 157 {0, 10}, 158 {20, 30}, 159 {55, 55}, 160 }, 161 expected: []Range[int]{ 162 {11, 19}, 163 {31, 54}, 164 {56, 100}, 165 }, 166 }, 167 { 168 ranges: []Range[int]{ 169 {0, 100}, 170 {200, 300}, 171 }, 172 exclude: []Range[int]{ 173 {0, 10}, 174 {20, 30}, 175 {55, 55}, 176 {250, 250}, 177 {299, 299}, 178 }, 179 expected: []Range[int]{ 180 {11, 19}, 181 {31, 54}, 182 {56, 100}, 183 {200, 249}, 184 {251, 298}, 185 {300, 300}, 186 }, 187 }, 188 } { 189 result := Exclude(testRange.ranges, testRange.exclude) 190 if !reflect.DeepEqual(result, testRange.expected) { 191 t.Fatal("input", testRange.ranges, testRange.exclude, "\nexpected", testRange.expected, "\ngot", result) 192 } 193 } 194 }