File: decimal_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 (59 lines) | stat: -rw-r--r-- 1,187 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
package strconv

import (
	"fmt"
	"testing"

	"github.com/tdewolff/test"
)

func TestParseDecimal(t *testing.T) {
	tests := []struct {
		f        string
		expected float64
	}{
		{"5", 5},
		{"5.1", 5.1},
		{"0.0000000000000000000000000005", 5e-28},
		{"18446744073709551620", 18446744073709551620.0},
		{"1000000000000000000000000.0000", 1e24},              // TODO
		{"1000000000000000000000000000000000000000000", 1e42}, // TODO
	}
	for _, tt := range tests {
		t.Run(fmt.Sprint(tt.f), func(t *testing.T) {
			f, n := ParseDecimal([]byte(tt.f))
			test.T(t, n, len(tt.f))
			test.Float(t, f, tt.expected)
		})
	}
}

func TestParseDecimalError(t *testing.T) {
	tests := []struct {
		f        string
		n        int
		expected float64
	}{
		{"+1", 0, 0},
		{"-1", 0, 0},
		{".", 0, 0},
		{"1e1", 1, 1},
	}
	for _, tt := range tests {
		t.Run(fmt.Sprint(tt.f), func(t *testing.T) {
			f, n := ParseDecimal([]byte(tt.f))
			test.T(t, n, tt.n)
			test.T(t, f, tt.expected)
		})
	}
}

func FuzzParseDecimal(f *testing.F) {
	f.Add("5")
	f.Add("5.1")
	f.Add("18446744073709551620")
	f.Add("0.0000000000000000000000000005")
	f.Fuzz(func(t *testing.T, s string) {
		ParseDecimal([]byte(s))
	})
}