@@ -300,3 +300,60 @@ func testKV[T any](t *testing.T, key string, val T, kv log.KeyValue) {
300
300
assert .False (t , kv .Value .Empty (), "value empty" )
301
301
assert .Equal (t , kv .Value .AsAny (), T (val ), "AsAny wrong value" )
302
302
}
303
+
304
+ func TestAllocationLimits (t * testing.T ) {
305
+ const (
306
+ runs = 5
307
+ key = "key"
308
+ )
309
+
310
+ // Assign testing results to external scope so the compiler doesn't
311
+ // optimize away the testing statements.
312
+ var (
313
+ i int64
314
+ f float64
315
+ b bool
316
+ by []byte
317
+ s string
318
+ slice []log.Value
319
+ m []log.KeyValue
320
+ )
321
+
322
+ assert .Equal (t , 0.0 , testing .AllocsPerRun (runs , func () {
323
+ b = log .Bool (key , true ).Value .AsBool ()
324
+ }), "Bool.AsBool" )
325
+
326
+ assert .Equal (t , 0.0 , testing .AllocsPerRun (runs , func () {
327
+ f = log .Float64 (key , 3.0 ).Value .AsFloat64 ()
328
+ }), "Float.AsFloat64" )
329
+
330
+ assert .Equal (t , 0.0 , testing .AllocsPerRun (runs , func () {
331
+ i = log .Int (key , 9 ).Value .AsInt64 ()
332
+ }), "Int.AsInt64" )
333
+
334
+ assert .Equal (t , 0.0 , testing .AllocsPerRun (runs , func () {
335
+ i = log .Int64 (key , 8 ).Value .AsInt64 ()
336
+ }), "Int64.AsInt64" )
337
+
338
+ assert .Equal (t , 0.0 , testing .AllocsPerRun (runs , func () {
339
+ s = log .String (key , "value" ).Value .AsString ()
340
+ }), "String.AsString" )
341
+
342
+ byteVal := []byte {1 , 3 , 4 }
343
+ assert .Equal (t , 0.0 , testing .AllocsPerRun (runs , func () {
344
+ by = log .Bytes (key , byteVal ).Value .AsBytes ()
345
+ }), "Byte.AsBytes" )
346
+
347
+ sliceVal := []log.Value {log .BoolValue (true ), log .IntValue (32 )}
348
+ assert .Equal (t , 0.0 , testing .AllocsPerRun (runs , func () {
349
+ slice = log .Slice (key , sliceVal ... ).Value .AsSlice ()
350
+ }), "Slice.AsSlice" )
351
+
352
+ mapVal := []log.KeyValue {log .Bool ("b" , true ), log .Int ("i" , 32 )}
353
+ assert .Equal (t , 0.0 , testing .AllocsPerRun (runs , func () {
354
+ m = log .Map (key , mapVal ... ).Value .AsMap ()
355
+ }), "Map.AsMap" )
356
+
357
+ // Convince the linter these values are used.
358
+ _ , _ , _ , _ , _ , _ , _ = i , f , b , by , s , slice , m
359
+ }
0 commit comments