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