MCPcopy Create free account
hub / github.com/buger/jsonparser / TestPropertyParseReferenceOracle

Function TestPropertyParseReferenceOracle

property_test.go:261–339  ·  view source on GitHub ↗

--------------------------------------------------------------------------- Property: ParseInt/ParseFloat/ParseBoolean agree with strconv reference --------------------------------------------------------------------------- reqproof:proptest ParseInt, ParseFloat, ParseBoolean, ParseString Verifies:

(t *testing.T)

Source from the content-addressed store, hash-verified

259// reqproof:proptest ParseInt, ParseFloat, ParseBoolean, ParseString
260// Verifies: SYS-REQ-015 [property]
261func TestPropertyParseReferenceOracle(t *testing.T) {
262 // ParseInt vs strconv.ParseInt. Property: when BOTH accept the input,
263 // the parsed values must be equal. The parser may accept a slightly
264 // different grammar (trailing data, leading '+', etc.) so we only
265 // require agreement on the common-acceptance domain.
266 ri := func(b []byte) bool {
267 s := string(b)
268 ref, refErr := strconv.ParseInt(s, 10, 64)
269 got, err := ParseInt([]byte(s))
270 if refErr != nil {
271 return true // reference rejected — parser's grammar may be a superset
272 }
273 if err != nil {
274 return true // parser rejected a value strconv accepted; not a logic bug per se
275 }
276 return got == ref
277 }
278 if err := quick.Check(ri, &quick.Config{MaxCount: 2000}); err != nil {
279 t.Fatalf("ParseInt diverges from strconv: %v", err)
280 }
281
282 // ParseFloat vs strconv.ParseFloat. Same one-way agreement property.
283 rf := func(b []byte) bool {
284 s := string(b)
285 ref, refErr := strconv.ParseFloat(s, 64)
286 got, err := ParseFloat([]byte(s))
287 if refErr != nil {
288 return true
289 }
290 if err != nil {
291 return true
292 }
293 return got == ref || closeEnough(got, ref)
294 }
295 if err := quick.Check(rf, &quick.Config{MaxCount: 2000}); err != nil {
296 t.Fatalf("ParseFloat diverges from strconv: %v", err)
297 }
298
299 // ParseBoolean vs strconv.ParseBool. Same one-way agreement property.
300 rb := func(b []byte) bool {
301 s := string(b)
302 ref, refErr := strconv.ParseBool(s)
303 got, err := ParseBoolean([]byte(s))
304 if refErr != nil {
305 return true
306 }
307 if err != nil {
308 return true
309 }
310 return got == ref
311 }
312 if err := quick.Check(rb, &quick.Config{MaxCount: 1000}); err != nil {
313 t.Fatalf("ParseBoolean diverges from strconv: %v", err)
314 }
315
316 // ParseString: for any JSON-quoted string, ParseString(body) must match
317 // the value encoding/json would produce.
318 rs := func(s string) bool {

Callers

nothing calls this directly

Calls 5

ParseIntFunction · 0.85
ParseFloatFunction · 0.85
closeEnoughFunction · 0.85
ParseBooleanFunction · 0.85
ParseStringFunction · 0.85

Tested by

no test coverage detected