Source file src/runtime/print_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime_test
     6  
     7  import (
     8  	"math"
     9  	"runtime"
    10  	"testing"
    11  )
    12  
    13  func FuzzPrintFloat64(f *testing.F) {
    14  	f.Add(math.SmallestNonzeroFloat64)
    15  	f.Add(math.MaxFloat64)
    16  	f.Add(-1.7976931348623157e+308) // requires 24 digits
    17  
    18  	f.Fuzz(func(t *testing.T, v float64) {
    19  		s := runtime.DumpPrint(v)
    20  		if len(s) > runtime.Float64Bytes {
    21  			t.Errorf("print(%f) got %s (len %d) want len <= %d", v, s, len(s), runtime.Float64Bytes)
    22  		}
    23  	})
    24  }
    25  
    26  func FuzzPrintFloat32(f *testing.F) {
    27  	f.Add(float32(math.SmallestNonzeroFloat32))
    28  	f.Add(float32(math.MaxFloat32))
    29  	f.Add(float32(-1.06338233e+37)) // requires 15 digits
    30  
    31  	f.Fuzz(func(t *testing.T, v float32) {
    32  		s := runtime.DumpPrint(v)
    33  		if len(s) > runtime.Float32Bytes {
    34  			t.Errorf("print(%f) got %s (len %d) want len <= %d", v, s, len(s), runtime.Float32Bytes)
    35  		}
    36  	})
    37  }
    38  
    39  func FuzzPrintComplex128(f *testing.F) {
    40  	f.Add(math.SmallestNonzeroFloat64, math.SmallestNonzeroFloat64)
    41  	f.Add(math.MaxFloat64, math.MaxFloat64)
    42  	f.Add(-1.7976931348623157e+308, -1.7976931348623157e+308) // requires 51 digits
    43  
    44  	f.Fuzz(func(t *testing.T, r, i float64) {
    45  		v := complex(r, i)
    46  		s := runtime.DumpPrint(v)
    47  		if len(s) > runtime.Complex128Bytes {
    48  			t.Errorf("print(%f) got %s (len %d) want len <= %d", v, s, len(s), runtime.Complex128Bytes)
    49  		}
    50  	})
    51  }
    52  
    53  func FuzzPrintComplex64(f *testing.F) {
    54  	f.Add(float32(math.SmallestNonzeroFloat32), float32(math.SmallestNonzeroFloat32))
    55  	f.Add(float32(math.MaxFloat32), float32(math.MaxFloat32))
    56  	f.Add(float32(-1.06338233e+37), float32(-1.06338233e+37)) // requires 33 digits
    57  
    58  	f.Fuzz(func(t *testing.T, r, i float32) {
    59  		v := complex(r, i)
    60  		s := runtime.DumpPrint(v)
    61  		if len(s) > runtime.Complex64Bytes {
    62  			t.Errorf("print(%f) got %s (len %d) want len <= %d", v, s, len(s), runtime.Complex64Bytes)
    63  		}
    64  	})
    65  }
    66  

View as plain text