File: ftoa_test.go

package info (click to toggle)
golang-github-dustin-go-humanize 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 184 kB
  • sloc: makefile: 2
file content (125 lines) | stat: -rw-r--r-- 2,818 bytes parent folder | download | duplicates (2)
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
package humanize

import (
	"fmt"
	"math/rand"
	"reflect"
	"regexp"
	"strconv"
	"strings"
	"testing"
	"testing/quick"
)

func TestFtoa(t *testing.T) {
	testList{
		{"200", Ftoa(200), "200"},
		{"20", Ftoa(20.0), "20"},
		{"2", Ftoa(2), "2"},
		{"2.2", Ftoa(2.2), "2.2"},
		{"2.02", Ftoa(2.02), "2.02"},
		{"200.02", Ftoa(200.02), "200.02"},
	}.validate(t)
}

func TestFtoaWithDigits(t *testing.T) {
	testList{
		{"1.23, 0", FtoaWithDigits(1.23, 0), "1"},
		{"20, 0", FtoaWithDigits(20.0, 0), "20"},
		{"1.23, 1", FtoaWithDigits(1.23, 1), "1.2"},
		{"1.23, 2", FtoaWithDigits(1.23, 2), "1.23"},
		{"1.23, 3", FtoaWithDigits(1.23, 3), "1.23"},
	}.validate(t)
}

func TestStripTrailingDigits(t *testing.T) {
	err := quick.Check(func(s string, digits int) bool {
		stripped := stripTrailingDigits(s, digits)

		// A stripped string will always be a prefix of its original string
		if !strings.HasPrefix(s, stripped) {
			return false
		}

		if strings.ContainsRune(s, '.') {
			// If there is a dot, the part on the left of the dot will never change
			a := strings.Split(s, ".")
			b := strings.Split(stripped, ".")
			if a[0] != b[0] {
				return false
			}
		} else {
			// If there's no dot in the input, the output will always be the same as the input.
			if stripped != s {
				return false
			}
		}

		return true
	}, &quick.Config{
		MaxCount: 10000,
		Values: func(v []reflect.Value, r *rand.Rand) {
			rdigs := func(n int) string {
				digs := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
				var rv []rune
				for i := 0; i < n; i++ {
					rv = append(rv, digs[r.Intn(len(digs))])
				}
				return string(rv)
			}

			ls := r.Intn(20)
			rs := r.Intn(20)
			jc := "."
			if rs == 0 {
				jc = ""
			}
			s := rdigs(ls) + jc + rdigs(rs)
			digits := r.Intn(len(s) + 1)

			v[0] = reflect.ValueOf(s)
			v[1] = reflect.ValueOf(digits)
		},
	})

	if err != nil {
		t.Error(err)
	}
}

func BenchmarkFtoaRegexTrailing(b *testing.B) {
	trailingZerosRegex := regexp.MustCompile(`\.?0+$`)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		trailingZerosRegex.ReplaceAllString("2.00000", "")
		trailingZerosRegex.ReplaceAllString("2.0000", "")
		trailingZerosRegex.ReplaceAllString("2.000", "")
		trailingZerosRegex.ReplaceAllString("2.00", "")
		trailingZerosRegex.ReplaceAllString("2.0", "")
		trailingZerosRegex.ReplaceAllString("2", "")
	}
}

func BenchmarkFtoaFunc(b *testing.B) {
	for i := 0; i < b.N; i++ {
		stripTrailingZeros("2.00000")
		stripTrailingZeros("2.0000")
		stripTrailingZeros("2.000")
		stripTrailingZeros("2.00")
		stripTrailingZeros("2.0")
		stripTrailingZeros("2")
	}
}

func BenchmarkFmtF(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = fmt.Sprintf("%f", 2.03584)
	}
}

func BenchmarkStrconvF(b *testing.B) {
	for i := 0; i < b.N; i++ {
		strconv.FormatFloat(2.03584, 'f', 6, 64)
	}
}