File: inline_strconv_parse_test.go

package info (click to toggle)
golang-github-influxdata-influxdb1-client 0.0~git20220302.a9ab567-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 380 kB
  • sloc: makefile: 2
file content (103 lines) | stat: -rw-r--r-- 2,290 bytes parent folder | download | duplicates (5)
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
package models

import (
	"strconv"
	"testing"
	"testing/quick"
)

func TestParseIntBytesEquivalenceFuzz(t *testing.T) {
	f := func(b []byte, base int, bitSize int) bool {
		exp, expErr := strconv.ParseInt(string(b), base, bitSize)
		got, gotErr := parseIntBytes(b, base, bitSize)

		return exp == got && checkErrs(expErr, gotErr)
	}

	cfg := &quick.Config{
		MaxCount: 10000,
	}

	if err := quick.Check(f, cfg); err != nil {
		t.Fatal(err)
	}
}

func TestParseIntBytesValid64bitBase10EquivalenceFuzz(t *testing.T) {
	buf := []byte{}
	f := func(n int64) bool {
		buf = strconv.AppendInt(buf[:0], n, 10)

		exp, expErr := strconv.ParseInt(string(buf), 10, 64)
		got, gotErr := parseIntBytes(buf, 10, 64)

		return exp == got && checkErrs(expErr, gotErr)
	}

	cfg := &quick.Config{
		MaxCount: 10000,
	}

	if err := quick.Check(f, cfg); err != nil {
		t.Fatal(err)
	}
}

func TestParseFloatBytesEquivalenceFuzz(t *testing.T) {
	f := func(b []byte, bitSize int) bool {
		exp, expErr := strconv.ParseFloat(string(b), bitSize)
		got, gotErr := parseFloatBytes(b, bitSize)

		return exp == got && checkErrs(expErr, gotErr)
	}

	cfg := &quick.Config{
		MaxCount: 10000,
	}

	if err := quick.Check(f, cfg); err != nil {
		t.Fatal(err)
	}
}

func TestParseFloatBytesValid64bitEquivalenceFuzz(t *testing.T) {
	buf := []byte{}
	f := func(n float64) bool {
		buf = strconv.AppendFloat(buf[:0], n, 'f', -1, 64)

		exp, expErr := strconv.ParseFloat(string(buf), 64)
		got, gotErr := parseFloatBytes(buf, 64)

		return exp == got && checkErrs(expErr, gotErr)
	}

	cfg := &quick.Config{
		MaxCount: 10000,
	}

	if err := quick.Check(f, cfg); err != nil {
		t.Fatal(err)
	}
}

func TestParseBoolBytesEquivalence(t *testing.T) {
	var buf []byte
	for _, s := range []string{"1", "t", "T", "TRUE", "true", "True", "0", "f", "F", "FALSE", "false", "False", "fail", "TrUe", "FAlSE", "numbers", ""} {
		buf = append(buf[:0], s...)

		exp, expErr := strconv.ParseBool(s)
		got, gotErr := parseBoolBytes(buf)

		if got != exp || !checkErrs(expErr, gotErr) {
			t.Errorf("Failed to parse boolean value %q correctly: wanted (%t, %v), got (%t, %v)", s, exp, expErr, got, gotErr)
		}
	}
}

func checkErrs(a, b error) bool {
	if (a == nil) != (b == nil) {
		return false
	}

	return a == nil || a.Error() == b.Error()
}