github.com/MetalBlockchain/metalgo@v1.11.9/snow/engine/snowman/bootstrap/interval/interval_test.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package interval 5 6 import ( 7 "math" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestIntervalLess(t *testing.T) { 14 tests := []struct { 15 name string 16 left *Interval 17 right *Interval 18 expected bool 19 }{ 20 { 21 name: "less", 22 left: &Interval{ 23 LowerBound: 10, 24 UpperBound: 10, 25 }, 26 right: &Interval{ 27 LowerBound: 11, 28 UpperBound: 11, 29 }, 30 expected: true, 31 }, 32 { 33 name: "greater", 34 left: &Interval{ 35 LowerBound: 11, 36 UpperBound: 11, 37 }, 38 right: &Interval{ 39 LowerBound: 10, 40 UpperBound: 10, 41 }, 42 expected: false, 43 }, 44 { 45 name: "equal", 46 left: &Interval{ 47 LowerBound: 10, 48 UpperBound: 10, 49 }, 50 right: &Interval{ 51 LowerBound: 10, 52 UpperBound: 10, 53 }, 54 expected: false, 55 }, 56 } 57 for _, test := range tests { 58 t.Run(test.name, func(t *testing.T) { 59 less := test.left.Less(test.right) 60 require.Equal(t, test.expected, less) 61 }) 62 } 63 } 64 65 func TestIntervalContains(t *testing.T) { 66 tests := []struct { 67 name string 68 interval *Interval 69 height uint64 70 expected bool 71 }{ 72 { 73 name: "nil does not contain anything", 74 interval: nil, 75 height: 10, 76 expected: false, 77 }, 78 { 79 name: "too low", 80 interval: &Interval{ 81 LowerBound: 10, 82 UpperBound: 10, 83 }, 84 height: 9, 85 expected: false, 86 }, 87 { 88 name: "inside", 89 interval: &Interval{ 90 LowerBound: 9, 91 UpperBound: 11, 92 }, 93 height: 10, 94 expected: true, 95 }, 96 { 97 name: "equal", 98 interval: &Interval{ 99 LowerBound: 10, 100 UpperBound: 10, 101 }, 102 height: 10, 103 expected: true, 104 }, 105 { 106 name: "too high", 107 interval: &Interval{ 108 LowerBound: 10, 109 UpperBound: 10, 110 }, 111 height: 11, 112 expected: false, 113 }, 114 } 115 for _, test := range tests { 116 t.Run(test.name, func(t *testing.T) { 117 contains := test.interval.Contains(test.height) 118 require.Equal(t, test.expected, contains) 119 }) 120 } 121 } 122 123 func TestIntervalAdjacentToLowerBound(t *testing.T) { 124 tests := []struct { 125 name string 126 interval *Interval 127 height uint64 128 expected bool 129 }{ 130 { 131 name: "nil is not adjacent to anything", 132 interval: nil, 133 height: 10, 134 expected: false, 135 }, 136 { 137 name: "too low", 138 interval: &Interval{ 139 LowerBound: 10, 140 UpperBound: 10, 141 }, 142 height: 8, 143 expected: false, 144 }, 145 { 146 name: "equal", 147 interval: &Interval{ 148 LowerBound: 10, 149 UpperBound: 10, 150 }, 151 height: 10, 152 expected: false, 153 }, 154 { 155 name: "adjacent to both", 156 interval: &Interval{ 157 LowerBound: 10, 158 UpperBound: 10, 159 }, 160 height: 9, 161 expected: true, 162 }, 163 { 164 name: "adjacent to lower", 165 interval: &Interval{ 166 LowerBound: 10, 167 UpperBound: 11, 168 }, 169 height: 9, 170 expected: true, 171 }, 172 { 173 name: "check for overflow", 174 interval: &Interval{ 175 LowerBound: 0, 176 UpperBound: math.MaxUint64 - 1, 177 }, 178 height: math.MaxUint64, 179 expected: false, 180 }, 181 } 182 for _, test := range tests { 183 t.Run(test.name, func(t *testing.T) { 184 adjacent := test.interval.AdjacentToLowerBound(test.height) 185 require.Equal(t, test.expected, adjacent) 186 }) 187 } 188 } 189 190 func TestIntervalAdjacentToUpperBound(t *testing.T) { 191 tests := []struct { 192 name string 193 interval *Interval 194 height uint64 195 expected bool 196 }{ 197 { 198 name: "nil is not adjacent to anything", 199 interval: nil, 200 height: 10, 201 expected: false, 202 }, 203 { 204 name: "too low", 205 interval: &Interval{ 206 LowerBound: 10, 207 UpperBound: 10, 208 }, 209 height: 8, 210 expected: false, 211 }, 212 { 213 name: "equal", 214 interval: &Interval{ 215 LowerBound: 10, 216 UpperBound: 10, 217 }, 218 height: 10, 219 expected: false, 220 }, 221 { 222 name: "adjacent to both", 223 interval: &Interval{ 224 LowerBound: 10, 225 UpperBound: 10, 226 }, 227 height: 11, 228 expected: true, 229 }, 230 { 231 name: "adjacent to higher", 232 interval: &Interval{ 233 LowerBound: 9, 234 UpperBound: 10, 235 }, 236 height: 11, 237 expected: true, 238 }, 239 { 240 name: "check for overflow", 241 interval: &Interval{ 242 LowerBound: 1, 243 UpperBound: math.MaxUint64, 244 }, 245 height: 0, 246 expected: false, 247 }, 248 } 249 for _, test := range tests { 250 t.Run(test.name, func(t *testing.T) { 251 adjacent := test.interval.AdjacentToUpperBound(test.height) 252 require.Equal(t, test.expected, adjacent) 253 }) 254 } 255 }