github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/internal/native/avx/fastfloat_amd64_test.go (about)

     1  // Code generated by Makefile, DO NOT EDIT.
     2  
     3  // Code generated by Makefile, DO NOT EDIT.
     4  
     5  /*
     6   * Copyright 2021 ByteDance Inc.
     7   *
     8   * Licensed under the Apache License, Version 2.0 (the "License");
     9   * you may not use this file except in compliance with the License.
    10   * You may obtain a copy of the License at
    11   *
    12   *     http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing, software
    15   * distributed under the License is distributed on an "AS IS" BASIS,
    16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17   * See the License for the specific language governing permissions and
    18   * limitations under the License.
    19   */
    20  
    21  package avx
    22  
    23  import (
    24  	"encoding/json"
    25  	"math"
    26  	"math/rand"
    27  	"strconv"
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestFastFloat_Encode(t *testing.T) {
    34  	var buf [64]byte
    35  	assert.Equal(t, "0", string(buf[:f64toa(&buf[0], 0)]))
    36  	assert.Equal(t, "-0", string(buf[:f64toa(&buf[0], math.Float64frombits(0x8000000000000000))]))
    37  	assert.Equal(t, "12340000000", string(buf[:f64toa(&buf[0], 1234e7)]))
    38  	assert.Equal(t, "12.34", string(buf[:f64toa(&buf[0], 1234e-2)]))
    39  	assert.Equal(t, "0.001234", string(buf[:f64toa(&buf[0], 1234e-6)]))
    40  	assert.Equal(t, "1e+30", string(buf[:f64toa(&buf[0], 1e30)]))
    41  	assert.Equal(t, "1.234e+33", string(buf[:f64toa(&buf[0], 1234e30)]))
    42  	assert.Equal(t, "1.234e+308", string(buf[:f64toa(&buf[0], 1234e305)]))
    43  	assert.Equal(t, "1.234e-317", string(buf[:f64toa(&buf[0], 1234e-320)]))
    44  	assert.Equal(t, "1.7976931348623157e+308", string(buf[:f64toa(&buf[0], 1.7976931348623157e308)]))
    45  	assert.Equal(t, "-12340000000", string(buf[:f64toa(&buf[0], -1234e7)]))
    46  	assert.Equal(t, "-12.34", string(buf[:f64toa(&buf[0], -1234e-2)]))
    47  	assert.Equal(t, "-0.001234", string(buf[:f64toa(&buf[0], -1234e-6)]))
    48  	assert.Equal(t, "-1e+30", string(buf[:f64toa(&buf[0], -1e30)]))
    49  	assert.Equal(t, "-1.234e+33", string(buf[:f64toa(&buf[0], -1234e30)]))
    50  	assert.Equal(t, "-1.234e+308", string(buf[:f64toa(&buf[0], -1234e305)]))
    51  	assert.Equal(t, "-1.234e-317", string(buf[:f64toa(&buf[0], -1234e-320)]))
    52  	assert.Equal(t, "-2.2250738585072014e-308", string(buf[:f64toa(&buf[0], -2.2250738585072014e-308)]))
    53  }
    54  
    55  func TestFastFloat_Random(t *testing.T) {
    56  	var buf [64]byte
    57  	N := 10000
    58  	for i := 0; i < N; i++ {
    59  		b64 := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
    60  		f64 := math.Float64frombits(b64)
    61  
    62  		jout, jerr := json.Marshal(f64)
    63  		n := f64toa(&buf[0], f64)
    64  		if jerr == nil {
    65  			assert.Equal(t, jout, buf[:n])
    66  		} else {
    67  			assert.True(t, n == 0)
    68  		}
    69  
    70  		f32 := math.Float32frombits(rand.Uint32())
    71  		jout, jerr = json.Marshal(f32)
    72  		n = f32toa(&buf[0], f32)
    73  		if jerr == nil {
    74  			assert.Equal(t, jout, buf[:n])
    75  		} else {
    76  			assert.True(t, n == 0)
    77  		}
    78  	}
    79  }
    80  
    81  func BenchmarkParseFloat64(b *testing.B) {
    82  	var f64toaBenches = []struct {
    83  		name  string
    84  		float float64
    85  	}{
    86  		{"Zero", 0},
    87  		{"Decimal", 33909},
    88  		{"Float", 339.7784},
    89  		{"Exp", -5.09e75},
    90  		{"NegExp", -5.11e-95},
    91  		{"LongExp", 1.234567890123456e-78},
    92  		{"Big", 123456789123456789123456789},
    93  	}
    94  	for _, c := range f64toaBenches {
    95  		f64bench := []struct {
    96  			name string
    97  			test func(*testing.B)
    98  		}{{
    99  			name: "StdLib",
   100  			test: func(b *testing.B) {
   101  				var buf [64]byte
   102  				for i := 0; i < b.N; i++ {
   103  					strconv.AppendFloat(buf[:0], c.float, 'g', -1, 64)
   104  				}
   105  			},
   106  		}, {
   107  			name: "FastFloat",
   108  			test: func(b *testing.B) {
   109  				var buf [64]byte
   110  				for i := 0; i < b.N; i++ {
   111  					f64toa(&buf[0], c.float)
   112  				}
   113  			},
   114  		}}
   115  		for _, bm := range f64bench {
   116  			name := bm.name + "_" + c.name
   117  			b.Run(name, bm.test)
   118  		}
   119  	}
   120  }
   121  
   122  func BenchmarkParseFloat32(b *testing.B) {
   123  	var f32toaBenches = []struct {
   124  		name  string
   125  		float float32
   126  	}{
   127  		{"Zero", 0},
   128  		{"Integer", 33909},
   129  		{"ExactFraction", 3.375},
   130  		{"Point", 339.7784},
   131  		{"Exp", -5.09e25},
   132  		{"NegExp", -5.11e-25},
   133  		{"Shortest", 1.234567e-8},
   134  	}
   135  	for _, c := range f32toaBenches {
   136  		bench := []struct {
   137  			name string
   138  			test func(*testing.B)
   139  		}{{
   140  			name: "StdLib32",
   141  			test: func(b *testing.B) {
   142  				var buf [64]byte
   143  				for i := 0; i < b.N; i++ {
   144  					strconv.AppendFloat(buf[:0], float64(c.float), 'g', -1, 32)
   145  				}
   146  			},
   147  		}, {
   148  			name: "FastFloat32",
   149  			test: func(b *testing.B) {
   150  				var buf [64]byte
   151  				for i := 0; i < b.N; i++ {
   152  					f32toa(&buf[0], c.float)
   153  				}
   154  			},
   155  		}}
   156  		for _, bm := range bench {
   157  			name := bm.name + "_" + c.name
   158  			b.Run(name, bm.test)
   159  		}
   160  	}
   161  }