github.com/matrixorigin/matrixone@v1.2.0/pkg/incrservice/range_cache_test.go (about) 1 // Copyright 2023 Matrix Origin 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 15 package incrservice 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 "github.com/stretchr/testify/require" 22 ) 23 24 func TestRangeCount(t *testing.T) { 25 r := &ranges{step: 1, values: []uint64{1, 2, 2, 3, 3, 4}} 26 assert.Equal(t, 3, r.rangeCount()) 27 28 r = &ranges{step: 2, values: []uint64{1, 3, 3, 5, 5, 7}} 29 assert.Equal(t, 3, r.rangeCount()) 30 } 31 32 func TestRangeEmpty(t *testing.T) { 33 r := &ranges{} 34 assert.True(t, r.empty()) 35 36 r = &ranges{step: 2, values: []uint64{3, 3, 5, 5, 7, 7}} 37 assert.True(t, r.empty()) 38 39 r = &ranges{step: 2, values: []uint64{1, 3}} 40 assert.False(t, r.empty()) 41 42 r = &ranges{step: 2, values: []uint64{3, 3, 5, 5, 7, 9}} 43 assert.False(t, r.empty()) 44 } 45 46 func TestRangeNext(t *testing.T) { 47 r := &ranges{} 48 assert.Equal(t, uint64(0), r.next()) 49 50 r = &ranges{step: 1, values: []uint64{2, 2}} 51 assert.Equal(t, uint64(0), r.next()) 52 53 r = &ranges{step: 1, values: []uint64{1, 2}} 54 assert.Equal(t, uint64(1), r.next()) 55 assert.Equal(t, 0, len(r.values)) 56 57 r = &ranges{step: 1, values: []uint64{2, 2, 2, 3}} 58 assert.Equal(t, uint64(2), r.next()) 59 assert.Equal(t, 0, len(r.values)) 60 61 r = &ranges{step: 1, values: []uint64{1000, 1001}} 62 assert.Equal(t, uint64(1000), r.next()) 63 assert.Equal(t, 0, len(r.values)) 64 } 65 66 func TestRangeLeft(t *testing.T) { 67 r := &ranges{} 68 assert.Equal(t, 0, r.left()) 69 70 r = &ranges{step: 2, values: []uint64{3, 3, 5, 5, 7, 7}} 71 assert.Equal(t, 0, r.left()) 72 73 r = &ranges{step: 2, values: []uint64{1, 3}} 74 assert.Equal(t, 1, r.left()) 75 76 r = &ranges{step: 2, values: []uint64{3, 3, 5, 7, 7, 9}} 77 assert.Equal(t, 2, r.left()) 78 } 79 80 func TestRangeAdd(t *testing.T) { 81 r := &ranges{step: 1} 82 r.add(1, 2) 83 assert.Equal(t, 1, r.left()) 84 assert.Equal(t, 2, len(r.values)) 85 86 r.add(2, 3) 87 assert.Equal(t, 2, r.left()) 88 assert.Equal(t, 4, len(r.values)) 89 90 r.add(1, 2) 91 assert.Equal(t, 2, r.left()) 92 assert.Equal(t, 4, len(r.values)) 93 } 94 95 func TestUpdateTo(t *testing.T) { 96 cases := []struct { 97 values []uint64 98 updateTo uint64 99 expected []uint64 100 contains bool 101 }{ 102 { 103 values: []uint64{}, 104 updateTo: 1, 105 contains: false, 106 expected: []uint64{}, 107 }, 108 { 109 values: []uint64{1, 2}, 110 updateTo: 1, 111 contains: true, 112 expected: []uint64{1, 2}, 113 }, 114 { 115 values: []uint64{1, 3}, 116 updateTo: 2, 117 contains: true, 118 expected: []uint64{2, 3}, 119 }, 120 { 121 values: []uint64{1, 3}, 122 updateTo: 3, 123 contains: false, 124 expected: []uint64{}, 125 }, 126 { 127 values: []uint64{1, 2, 2, 4}, 128 updateTo: 2, 129 contains: true, 130 expected: []uint64{2, 4}, 131 }, 132 { 133 values: []uint64{1, 2, 2, 4}, 134 updateTo: 3, 135 contains: true, 136 expected: []uint64{3, 4}, 137 }, 138 { 139 values: []uint64{1, 2, 2, 4}, 140 updateTo: 4, 141 contains: false, 142 expected: []uint64{}, 143 }, 144 { 145 values: []uint64{101, 1001}, 146 updateTo: 100, 147 contains: true, 148 expected: []uint64{101, 1001}, 149 }, 150 } 151 152 for _, c := range cases { 153 r := &ranges{values: c.values, step: 1} 154 require.Equal(t, c.contains, r.updateTo(c.updateTo)) 155 assert.Equal(t, c.expected, r.values) 156 } 157 } 158 159 func TestSetManual(t *testing.T) { 160 cases := []struct { 161 values []uint64 162 manual uint64 163 expectedSkipped []uint64 164 expectLeft []uint64 165 }{ 166 { 167 values: []uint64{}, 168 manual: 1, 169 expectedSkipped: []uint64(nil), 170 expectLeft: []uint64{}, 171 }, 172 { 173 values: []uint64{2, 3}, 174 manual: 1, 175 expectedSkipped: []uint64(nil), 176 expectLeft: []uint64{2, 3}, 177 }, 178 { 179 values: []uint64{2, 3}, 180 manual: 2, 181 expectedSkipped: []uint64(nil), 182 expectLeft: []uint64{}, 183 }, 184 { 185 values: []uint64{1, 3}, 186 manual: 2, 187 expectedSkipped: []uint64{1, 2}, 188 expectLeft: []uint64{}, 189 }, 190 { 191 values: []uint64{1, 4}, 192 manual: 2, 193 expectedSkipped: []uint64{1, 2}, 194 expectLeft: []uint64{3, 4}, 195 }, 196 { 197 values: []uint64{1, 4}, 198 manual: 3, 199 expectedSkipped: []uint64{1, 3}, 200 expectLeft: []uint64{}, 201 }, 202 { 203 values: []uint64{1, 4}, 204 manual: 4, 205 expectedSkipped: []uint64{1, 4}, 206 expectLeft: []uint64{}, 207 }, 208 { 209 values: []uint64{1, 3}, 210 manual: 4, 211 expectedSkipped: []uint64{1, 3}, 212 expectLeft: []uint64{}, 213 }, 214 } 215 216 for _, c := range cases { 217 r := &ranges{values: c.values, step: 1} 218 skipped := &ranges{} 219 r.setManual(c.manual, skipped) 220 require.Equal(t, c.expectedSkipped, skipped.values) 221 assert.Equal(t, c.expectLeft, r.values) 222 } 223 }