github.com/mitranim/gg@v0.1.17/misc_test.go (about) 1 package gg_test 2 3 import ( 4 "context" 5 "fmt" 6 "math" 7 r "reflect" 8 "testing" 9 10 "github.com/mitranim/gg" 11 "github.com/mitranim/gg/gtest" 12 ) 13 14 func TestZero(t *testing.T) { 15 defer gtest.Catch(t) 16 17 gtest.Equal(gg.Zero[int](), 0) 18 gtest.Equal(gg.Zero[string](), ``) 19 gtest.Equal(gg.Zero[struct{}](), struct{}{}) 20 gtest.Equal(gg.Zero[[]string](), nil) 21 gtest.Equal(gg.Zero[func()](), nil) 22 } 23 24 func Benchmark_reflect_Zero(b *testing.B) { 25 typ := r.TypeOf(SomeModel{}) 26 27 for ind := 0; ind < b.N; ind++ { 28 gg.Nop1(r.Zero(typ)) 29 } 30 } 31 32 func BenchmarkZero(b *testing.B) { 33 for ind := 0; ind < b.N; ind++ { 34 gg.Nop1(gg.Zero[SomeModel]()) 35 } 36 } 37 38 func TestAnyIs(t *testing.T) { 39 t.Run(`nil`, func(t *testing.T) { 40 defer gtest.Catch(t) 41 42 gtest.False(gg.AnyIs[any](nil)) 43 gtest.False(gg.AnyIs[int](nil)) 44 gtest.False(gg.AnyIs[string](nil)) 45 gtest.False(gg.AnyIs[*string](nil)) 46 gtest.False(gg.AnyIs[fmt.Stringer](nil)) 47 }) 48 49 t.Run(`mismatch`, func(t *testing.T) { 50 defer gtest.Catch(t) 51 52 gtest.False(gg.AnyIs[int](`str`)) 53 gtest.False(gg.AnyIs[string](10)) 54 gtest.False(gg.AnyIs[*string](`str`)) 55 gtest.False(gg.AnyIs[fmt.Stringer](`str`)) 56 }) 57 58 t.Run(`match`, func(t *testing.T) { 59 defer gtest.Catch(t) 60 61 gtest.True(gg.AnyIs[any](`str`)) 62 gtest.True(gg.AnyIs[int](10)) 63 gtest.True(gg.AnyIs[string](`str`)) 64 gtest.True(gg.AnyIs[*string]((*string)(nil))) 65 gtest.True(gg.AnyIs[*string](gg.Ptr(`str`))) 66 gtest.True(gg.AnyIs[fmt.Stringer](gg.ErrStr(`str`))) 67 }) 68 } 69 70 func TestAnyAs(t *testing.T) { 71 t.Run(`nil`, func(t *testing.T) { 72 defer gtest.Catch(t) 73 74 gtest.Zero(gg.AnyAs[any](nil)) 75 gtest.Zero(gg.AnyAs[int](nil)) 76 gtest.Zero(gg.AnyAs[string](nil)) 77 gtest.Zero(gg.AnyAs[*string](nil)) 78 gtest.Zero(gg.AnyAs[fmt.Stringer](nil)) 79 }) 80 81 t.Run(`mismatch`, func(t *testing.T) { 82 defer gtest.Catch(t) 83 84 gtest.Zero(gg.AnyAs[int](`str`)) 85 gtest.Zero(gg.AnyAs[string](10)) 86 gtest.Zero(gg.AnyAs[*string](`str`)) 87 gtest.Zero(gg.AnyAs[fmt.Stringer](`str`)) 88 }) 89 90 t.Run(`match`, func(t *testing.T) { 91 defer gtest.Catch(t) 92 93 gtest.Equal(gg.AnyAs[any](`str`), `str`) 94 gtest.Eq(gg.AnyAs[int](10), 10) 95 gtest.Eq(gg.AnyAs[string](`str`), `str`) 96 gtest.Equal(gg.AnyAs[*string](gg.Ptr(`str`)), gg.Ptr(`str`)) 97 98 gtest.Equal( 99 gg.AnyAs[fmt.Stringer](gg.ErrStr(`str`)), 100 fmt.Stringer(gg.ErrStr(`str`)), 101 ) 102 }) 103 } 104 func BenchmarkAnyAs_concrete_miss(b *testing.B) { 105 for ind := 0; ind < b.N; ind++ { 106 gg.Nop1(gg.AnyAs[gg.ErrStr](0)) 107 } 108 } 109 110 func BenchmarkAnyAs_iface_miss(b *testing.B) { 111 var tar []string 112 113 for ind := 0; ind < b.N; ind++ { 114 gg.Nop1(gg.AnyAs[fmt.Stringer](tar)) 115 } 116 } 117 118 func BenchmarkAnyAs_concrete_hit(b *testing.B) { 119 var tar gg.Err 120 121 for ind := 0; ind < b.N; ind++ { 122 gg.Nop1(gg.AnyAs[gg.Err](tar)) 123 } 124 } 125 126 func BenchmarkAnyAs_iface_hit(b *testing.B) { 127 var tar gg.Err 128 129 for ind := 0; ind < b.N; ind++ { 130 gg.Nop1(gg.AnyAs[error](tar)) 131 } 132 } 133 134 func BenchmarkAnyAs_analog_native_miss(b *testing.B) { 135 var tar []string 136 137 for ind := 0; ind < b.N; ind++ { 138 impl, ok := any(tar).(fmt.Stringer) 139 gg.Nop2(impl, ok) 140 } 141 } 142 143 func BenchmarkAnyAs_analog_native_hit(b *testing.B) { 144 var tar gg.Err 145 146 for ind := 0; ind < b.N; ind++ { 147 impl, ok := any(tar).(fmt.Stringer) 148 gg.Nop2(impl, ok) 149 } 150 } 151 152 func TestCtxSet(t *testing.T) { 153 defer gtest.Catch(t) 154 155 ctx := context.Background() 156 157 gtest.Zero(ctx.Value((*string)(nil))) 158 159 gtest.Equal( 160 gg.CtxSet(ctx, `str`).Value((*string)(nil)), 161 any(`str`), 162 ) 163 } 164 165 func TestCtxGet(t *testing.T) { 166 defer gtest.Catch(t) 167 168 ctx := context.Background() 169 170 gtest.Zero(gg.CtxGet[string](ctx)) 171 172 gtest.Eq( 173 gg.CtxGet[string](gg.CtxSet(ctx, `str`)), 174 `str`, 175 ) 176 177 gtest.Eq( 178 gg.CtxGet[string](context.WithValue(gg.CtxSet(ctx, `str`), (*int)(nil), 10)), 179 `str`, 180 ) 181 } 182 183 func TestRange(t *testing.T) { 184 defer gtest.Catch(t) 185 186 rangeU := gg.Range[uint8] 187 rangeS := gg.Range[int8] 188 189 gtest.Equal( 190 rangeU(0, math.MaxUint8), 191 []uint8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254}, 192 ) 193 194 gtest.Equal( 195 rangeS(math.MinInt8, math.MaxInt8), 196 []int8{-128, -127, -126, -125, -124, -123, -122, -121, -120, -119, -118, -117, -116, -115, -114, -113, -112, -111, -110, -109, -108, -107, -106, -105, -104, -103, -102, -101, -100, -99, -98, -97, -96, -95, -94, -93, -92, -91, -90, -89, -88, -87, -86, -85, -84, -83, -82, -81, -80, -79, -78, -77, -76, -75, -74, -73, -72, -71, -70, -69, -68, -67, -66, -65, -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53, -52, -51, -50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126}, 197 ) 198 199 gtest.Equal(rangeU(math.MaxUint8, 0), nil) 200 gtest.Equal(rangeU(math.MaxUint8, 1), nil) 201 gtest.Equal(rangeU(math.MaxUint8, math.MaxUint8), nil) 202 gtest.Equal(rangeU(math.MaxUint8/2, 1), nil) 203 gtest.Equal(rangeU(math.MaxUint8/2, math.MaxUint8/4), nil) 204 gtest.Equal(rangeU(0, 0), nil) 205 gtest.Equal(rangeU(1, 1), nil) 206 gtest.Equal(rangeU(2, 2), nil) 207 208 gtest.Equal(rangeS(math.MaxInt8, 0), nil) 209 gtest.Equal(rangeS(math.MaxInt8, 1), nil) 210 gtest.Equal(rangeS(math.MaxInt8, math.MaxInt8), nil) 211 gtest.Equal(rangeS(0, math.MinInt8), nil) 212 gtest.Equal(rangeS(1, math.MinInt8), nil) 213 gtest.Equal(rangeS(math.MinInt8, math.MinInt8), nil) 214 gtest.Equal(rangeS(math.MaxInt8, math.MinInt8), nil) 215 gtest.Equal(rangeS(math.MaxInt8/2, math.MinInt8/2), nil) 216 gtest.Equal(rangeS(-2, -2), nil) 217 gtest.Equal(rangeS(-1, -1), nil) 218 gtest.Equal(rangeS(0, 0), nil) 219 gtest.Equal(rangeS(1, 1), nil) 220 gtest.Equal(rangeS(2, 2), nil) 221 222 gtest.Equal(rangeS(-3, -2), []int8{-3}) 223 gtest.Equal(rangeS(-3, -1), []int8{-3, -2}) 224 gtest.Equal(rangeS(-3, 0), []int8{-3, -2, -1}) 225 gtest.Equal(rangeS(-3, 1), []int8{-3, -2, -1, 0}) 226 gtest.Equal(rangeS(-3, 2), []int8{-3, -2, -1, 0, 1}) 227 gtest.Equal(rangeS(-3, 3), []int8{-3, -2, -1, 0, 1, 2}) 228 229 gtest.Equal(rangeS(0, 1), []int8{0}) 230 gtest.Equal(rangeS(0, 2), []int8{0, 1}) 231 gtest.Equal(rangeS(0, 3), []int8{0, 1, 2}) 232 233 gtest.Equal(rangeS(3, 4), []int8{3}) 234 gtest.Equal(rangeS(3, 5), []int8{3, 4}) 235 gtest.Equal(rangeS(3, 6), []int8{3, 4, 5}) 236 237 gtest.Equal(gg.Range(math.MinInt, math.MinInt+1), []int{math.MinInt}) 238 gtest.Equal(gg.Range(math.MaxInt-1, math.MaxInt), []int{math.MaxInt - 1}) 239 240 gtest.PanicStr(`unable to safely convert uint 18446744073709551614 to int -2`, func() { 241 gg.Range[uint](math.MaxUint-1, math.MaxUint) 242 }) 243 } 244 245 func TestRangeIncl(t *testing.T) { 246 defer gtest.Catch(t) 247 248 rangeU := gg.RangeIncl[uint8] 249 rangeS := gg.RangeIncl[int8] 250 251 gtest.Equal( 252 rangeU(0, math.MaxUint8), 253 []uint8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255}, 254 ) 255 256 gtest.Equal( 257 rangeS(math.MinInt8, math.MaxInt8), 258 []int8{-128, -127, -126, -125, -124, -123, -122, -121, -120, -119, -118, -117, -116, -115, -114, -113, -112, -111, -110, -109, -108, -107, -106, -105, -104, -103, -102, -101, -100, -99, -98, -97, -96, -95, -94, -93, -92, -91, -90, -89, -88, -87, -86, -85, -84, -83, -82, -81, -80, -79, -78, -77, -76, -75, -74, -73, -72, -71, -70, -69, -68, -67, -66, -65, -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53, -52, -51, -50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127}, 259 ) 260 261 gtest.Equal(rangeU(math.MaxUint8, 0), nil) 262 gtest.Equal(rangeU(math.MaxUint8, 1), nil) 263 gtest.Equal(rangeU(math.MaxUint8/2, 1), nil) 264 gtest.Equal(rangeU(math.MaxUint8/2, math.MaxUint8/4), nil) 265 266 gtest.Equal(rangeS(math.MaxInt8, 0), nil) 267 gtest.Equal(rangeS(math.MaxInt8, 1), nil) 268 gtest.Equal(rangeS(0, math.MinInt8), nil) 269 gtest.Equal(rangeS(1, math.MinInt8), nil) 270 gtest.Equal(rangeS(math.MaxInt8, math.MinInt8), nil) 271 gtest.Equal(rangeS(math.MaxInt8/2, math.MinInt8/2), nil) 272 273 gtest.Equal(rangeU(math.MaxUint8, math.MaxUint8), []uint8{math.MaxUint8}) 274 gtest.Equal(rangeS(math.MaxInt8, math.MaxInt8), []int8{math.MaxInt8}) 275 gtest.Equal(rangeS(math.MinInt8, math.MinInt8), []int8{math.MinInt8}) 276 277 gtest.Equal(rangeU(0, 0), []uint8{0}) 278 gtest.Equal(rangeU(1, 1), []uint8{1}) 279 gtest.Equal(rangeU(2, 2), []uint8{2}) 280 281 gtest.Equal(rangeS(-2, -2), []int8{-2}) 282 gtest.Equal(rangeS(-1, -1), []int8{-1}) 283 gtest.Equal(rangeS(0, 0), []int8{0}) 284 gtest.Equal(rangeS(1, 1), []int8{1}) 285 gtest.Equal(rangeS(2, 2), []int8{2}) 286 287 gtest.Equal(rangeS(-3, -2), []int8{-3, -2}) 288 gtest.Equal(rangeS(-3, -1), []int8{-3, -2, -1}) 289 gtest.Equal(rangeS(-3, 0), []int8{-3, -2, -1, 0}) 290 gtest.Equal(rangeS(-3, 1), []int8{-3, -2, -1, 0, 1}) 291 gtest.Equal(rangeS(-3, 2), []int8{-3, -2, -1, 0, 1, 2}) 292 gtest.Equal(rangeS(-3, 3), []int8{-3, -2, -1, 0, 1, 2, 3}) 293 294 gtest.Equal(rangeS(0, 1), []int8{0, 1}) 295 gtest.Equal(rangeS(0, 2), []int8{0, 1, 2}) 296 gtest.Equal(rangeS(0, 3), []int8{0, 1, 2, 3}) 297 298 gtest.Equal(rangeS(3, 4), []int8{3, 4}) 299 gtest.Equal(rangeS(3, 5), []int8{3, 4, 5}) 300 gtest.Equal(rangeS(3, 6), []int8{3, 4, 5, 6}) 301 302 gtest.Equal(gg.RangeIncl(math.MinInt, math.MinInt+1), []int{math.MinInt, math.MinInt + 1}) 303 gtest.Equal(gg.RangeIncl(math.MaxInt-1, math.MaxInt), []int{math.MaxInt - 1, math.MaxInt}) 304 305 gtest.PanicStr(`unable to safely convert uint 18446744073709551614 to int -2`, func() { 306 gg.RangeIncl[uint](math.MaxUint-1, math.MaxUint) 307 }) 308 } 309 310 func TestSpan(t *testing.T) { 311 defer gtest.Catch(t) 312 313 gtest.Equal(gg.Span(0), nil) 314 gtest.Equal(gg.Span(1), []int{0}) 315 gtest.Equal(gg.Span(2), []int{0, 1}) 316 gtest.Equal(gg.Span(3), []int{0, 1, 2}) 317 } 318 319 func TestPlus2(t *testing.T) { 320 defer gtest.Catch(t) 321 322 gtest.Eq(gg.Plus2(10, 20), 30) 323 gtest.Eq(gg.Plus2(`10`, `20`), `1020`) 324 } 325 326 func TestPlus(t *testing.T) { 327 defer gtest.Catch(t) 328 329 t.Run(`Num`, func(t *testing.T) { 330 defer gtest.Catch(t) 331 332 gtest.Eq(gg.Plus[int](), 0) 333 gtest.Eq(gg.Plus(0), 0) 334 gtest.Eq(gg.Plus(10), 10) 335 gtest.Eq(gg.Plus(10, 20), 30) 336 gtest.Eq(gg.Plus(-10, 0), -10) 337 gtest.Eq(gg.Plus(-10, 0, 10), 0) 338 gtest.Eq(gg.Plus(-10, 0, 10, 20), 20) 339 gtest.Eq(gg.Plus(-10, 0, 10, 20, 30), 50) 340 }) 341 342 t.Run(`string`, func(t *testing.T) { 343 defer gtest.Catch(t) 344 345 gtest.Eq(gg.Plus[string](), ``) 346 gtest.Eq(gg.Plus(``), ``) 347 gtest.Eq(gg.Plus(`one`), `one`) 348 gtest.Eq(gg.Plus(`one`, ``), `one`) 349 gtest.Eq(gg.Plus(``, `two`), `two`) 350 gtest.Eq(gg.Plus(`one`, `two`), `onetwo`) 351 gtest.Eq(gg.Plus(`one`, `two`, `three`), `onetwothree`) 352 }) 353 } 354 355 func BenchmarkPlus(b *testing.B) { 356 for ind := 0; ind < b.N; ind++ { 357 gg.Nop1(gg.Plus(10, 20, 30, 40, 50, 60, 70, 80, 90)) 358 } 359 } 360 361 func TestSnapSlice(t *testing.T) { 362 defer gtest.Catch(t) 363 364 gtest.Zero(gg.SnapSlice((*[]int)(nil))) 365 366 tar := []int{10, 20} 367 snap := gg.SnapSlice(&tar) 368 369 gtest.Equal(snap, gg.SliceSnapshot[[]int, int]{&tar, 2}) 370 gtest.Eq(cap(tar), 2) 371 372 tar = []int{10, 20, 30, 40} 373 gtest.Equal(tar, []int{10, 20, 30, 40}) 374 gtest.Eq(cap(tar), 4) 375 376 snap.Done() 377 gtest.Equal(tar, []int{10, 20}) 378 gtest.Eq(cap(tar), 4) 379 }