File: int_test.go

package info (click to toggle)
golang-github-tdewolff-parse 2.7.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 836 kB
  • sloc: makefile: 2
file content (177 lines) | stat: -rw-r--r-- 3,298 bytes parent folder | download
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package strconv

import (
	"fmt"
	"math"
	"math/rand"
	"testing"

	"github.com/tdewolff/test"
)

func TestParseInt(t *testing.T) {
	intTests := []struct {
		i        string
		expected int64
	}{
		{"5", 5},
		{"99", 99},
		{"999", 999},
		{"-5", -5},
		{"+5", 5},
		{"9223372036854775807", 9223372036854775807},
		{"-9223372036854775807", -9223372036854775807},
		{"-9223372036854775808", -9223372036854775808},
	}
	for _, tt := range intTests {
		t.Run(fmt.Sprint(tt.i), func(t *testing.T) {
			i, n := ParseInt([]byte(tt.i))
			test.T(t, n, len(tt.i))
			test.T(t, i, tt.expected)
		})
	}
}

func TestParseIntError(t *testing.T) {
	intTests := []struct {
		i        string
		n        int
		expected int64
	}{
		{"a", 0, 0},
		{"+", 0, 0},
		{"9223372036854775808", 0, 0},
		{"-9223372036854775809", 0, 0},
		{"18446744073709551620", 0, 0},
	}
	for _, tt := range intTests {
		t.Run(fmt.Sprint(tt.i), func(t *testing.T) {
			i, n := ParseInt([]byte(tt.i))
			test.T(t, n, tt.n)
			test.T(t, i, tt.expected)
		})
	}
}

func FuzzParseInt(f *testing.F) {
	f.Add("5")
	f.Add("-99")
	f.Add("9223372036854775807")
	f.Add("-9223372036854775808")
	f.Fuzz(func(t *testing.T, s string) {
		ParseInt([]byte(s))
	})
}

func TestParseUint(t *testing.T) {
	intTests := []struct {
		i        string
		expected uint64
	}{
		{"5", 5},
		{"99", 99},
		{"999", 999},
		{"18446744073709551615", 18446744073709551615},
	}
	for _, tt := range intTests {
		t.Run(fmt.Sprint(tt.i), func(t *testing.T) {
			i, n := ParseUint([]byte(tt.i))
			test.T(t, n, len(tt.i))
			test.T(t, i, tt.expected)
		})
	}
}

func TestParseUintError(t *testing.T) {
	intTests := []struct {
		i        string
		n        int
		expected uint64
	}{
		{"a", 0, 0},
		{"18446744073709551616", 0, 0},
		{"-1", 0, 0},
	}
	for _, tt := range intTests {
		t.Run(fmt.Sprint(tt.i), func(t *testing.T) {
			i, n := ParseUint([]byte(tt.i))
			test.T(t, n, tt.n)
			test.T(t, i, tt.expected)
		})
	}
}

func FuzzParseUint(f *testing.F) {
	f.Add("5")
	f.Add("99")
	f.Add("18446744073709551615")
	f.Fuzz(func(t *testing.T, s string) {
		ParseUint([]byte(s))
	})
}

func TestLenInt(t *testing.T) {
	lenIntTests := []struct {
		i        int64
		expected int
	}{
		{0, 1},
		{1, 1},
		{10, 2},
		{99, 2},
		{9223372036854775807, 19},
		{-9223372036854775808, 19},

		// coverage
		{100, 3},
		{1000, 4},
		{10000, 5},
		{100000, 6},
		{1000000, 7},
		{10000000, 8},
		{100000000, 9},
		{1000000000, 10},
		{10000000000, 11},
		{100000000000, 12},
		{1000000000000, 13},
		{10000000000000, 14},
		{100000000000000, 15},
		{1000000000000000, 16},
		{10000000000000000, 17},
		{100000000000000000, 18},
		{1000000000000000000, 19},
	}
	for _, tt := range lenIntTests {
		t.Run(fmt.Sprint(tt.i), func(t *testing.T) {
			test.T(t, LenInt(tt.i), tt.expected)
		})
	}
}

////////////////////////////////////////////////////////////////

var num []int64

func TestMain(t *testing.T) {
	for j := 0; j < 1000; j++ {
		num = append(num, rand.Int63n(1000))
	}
}

func BenchmarkLenIntLog(b *testing.B) {
	n := 0
	for i := 0; i < b.N; i++ {
		for j := 0; j < 1000; j++ {
			n += int(math.Log10(math.Abs(float64(num[j])))) + 1
		}
	}
}

func BenchmarkLenIntSwitch(b *testing.B) {
	n := 0
	for i := 0; i < b.N; i++ {
		for j := 0; j < 1000; j++ {
			n += LenInt(num[j])
		}
	}
}