Debian Codegraph @

bytes_test.go

raw symbols SCIP
1 package jsonparser
2
3 import (
4 "strconv"
5 "testing"
6 "unsafe"
7 )
8
9 type ParseIntTest struct {
10 in string
11 out int64
12 isErr bool
13 isOverflow bool
14 }
15
16 var parseIntTests = []ParseIntTest{
17 {
18 in: "0",
19 out: 0,
20 },
21 {
22 in: "1",
23 out: 1,
24 },
25 {
26 in: "-1",
27 out: -1,
28 },
29 {
30 in: "12345",
31 out: 12345,
32 },
33 {
34 in: "-12345",
35 out: -12345,
36 },
37 {
38 in: "9223372036854775807", // = math.MaxInt64
39 out: 9223372036854775807,
40 },
41 {
42 in: "-9223372036854775808", // = math.MinInt64
43 out: -9223372036854775808,
44 },
45 {
46 in: "-92233720368547758081",
47 out: 0,
48 isErr: true,
49 isOverflow: true,
50 },
51 {
52 in: "18446744073709551616", // = 2^64
53 out: 0,
54 isErr: true,
55 isOverflow: true,
56 },
57 {
58 in: "9223372036854775808", // = math.MaxInt64 - 1
59 out: 0,
60 isErr: true,
61 isOverflow: true,
62 },
63 {
64 in: "-9223372036854775809", // = math.MaxInt64 - 1
65 out: 0,
66 isErr: true,
67 isOverflow: true,
68 },
69 {
70 in: "",
71 isErr: true,
72 },
73 {
74 in: "abc",
75 isErr: true,
76 },
77 {
78 in: "12345x",
79 isErr: true,
80 },
81 {
82 in: "123e5",
83 isErr: true,
84 },
85 {
86 in: "9223372036854775807x",
87 isErr: true,
88 },
89 {
90 in: "27670116110564327410",
91 out: 0,
92 isErr: true,
93 isOverflow: true,
94 },
95 {
96 in: "-27670116110564327410",
97 out: 0,
98 isErr: true,
99 isOverflow: true,
100 },
101 }
102
103 // Verifies: SYS-REQ-015 [boundary]
104 // MCDC SYS-REQ-015: N/A
105 func TestBytesParseInt(t *testing.T) {
106 for _, test := range parseIntTests {
107 out, ok, overflow := parseInt([]byte(test.in))
108 if overflow != test.isOverflow {
109 t.Errorf("Test '%s' error return did not overflow expectation (obtained %t, expected %t)", test.in, overflow, test.isOverflow)
110 }
111 if ok != !test.isErr {
112 t.Errorf("Test '%s' error return did not match expectation (obtained %t, expected %t)", test.in, !ok, test.isErr)
113 } else if ok && out != test.out {
114 t.Errorf("Test '%s' did not return the expected value (obtained %d, expected %d)", test.in, out, test.out)
115 }
116 }
117 }
118
119 // Verifies: SYS-REQ-015 [example]
120 // MCDC SYS-REQ-015: N/A
121 func BenchmarkParseInt(b *testing.B) {
122 bytes := []byte("123")
123 for i := 0; i < b.N; i++ {
124 parseInt(bytes)
125 }
126 }
127
128 // Alternative implementation using unsafe and delegating to strconv.ParseInt
129 // Verifies: SYS-REQ-015 [example]
130 // MCDC SYS-REQ-015: N/A
131 func BenchmarkParseIntUnsafeSlower(b *testing.B) {
132 bytes := []byte("123")
133 for i := 0; i < b.N; i++ {
134 strconv.ParseInt(*(*string)(unsafe.Pointer(&bytes)), 10, 64)
135 }
136 }
137
138 // Old implementation that did not check for overflows.
139 // Verifies: SYS-REQ-015 [example]
140 // MCDC SYS-REQ-015: N/A
141 func BenchmarkParseIntOverflows(b *testing.B) {
142 bytes := []byte("123")
143 for i := 0; i < b.N; i++ {
144 parseIntOverflows(bytes)
145 }
146 }
147
148 // Test helper for SYS-REQ-015.
149 func parseIntOverflows(bytes []byte) (v int64, ok bool) {
150 if len(bytes) == 0 {
151 return 0, false
152 }
153
154 var neg bool = false
155 if bytes[0] == '-' {
156 neg = true
157 bytes = bytes[1:]
158 }
159
160 for _, c := range bytes {
161 if c >= '0' && c <= '9' {
162 v = (10 * v) + int64(c-'0')
163 } else {
164 return 0, false
165 }
166 }
167
168 if neg {
169 return -v, true
170 } else {
171 return v, true
172 }
173 }