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

Function TestOracleParseBoolean

reference_oracle_test.go:883–929  ·  view source on GitHub ↗

reqproof:proptest ParseBoolean Verifies: SYS-REQ-012 [property]

(t *testing.T)

Source from the content-addressed store, hash-verified

881// reqproof:proptest ParseBoolean
882// Verifies: SYS-REQ-012 [property]
883func TestOracleParseBoolean(t *testing.T) {
884 // The valid JSON booleans are exactly "true" and "false" (RFC 8259).
885 // jsonparser correctly accepts only those. Assert exact value match
886 // on the strict-JSON subset (the only inputs both parsers should accept
887 // under JSON semantics — see divergence D2 in the file header).
888 for _, tc := range []struct {
889 tok string
890 val bool
891 }{
892 {"true", true},
893 {"false", false},
894 } {
895 got, err := ParseBoolean([]byte(tc.tok))
896 if err != nil {
897 t.Fatalf("ParseBoolean rejected %q: %v", tc.tok, err)
898 }
899 if got != tc.val {
900 t.Fatalf("ParseBoolean(%q) = %v, want %v", tc.tok, got, tc.val)
901 }
902 }
903
904 // Adversarial inputs: assert agreement on the common-acceptance domain.
905 // jsonparser follows strict JSON; strconv accepts Go-idiomatic forms
906 // (t/f/T/F/1/0/TRUE/FALSE). When both accept, values must match.
907 rb := func(b []byte) bool {
908 s := string(b)
909 _, refErr := strconv.ParseBool(s)
910 _, err := ParseBoolean([]byte(s))
911 // If strconv rejects, jsonparser may also reject — that's fine.
912 if refErr != nil {
913 return true
914 }
915 // strconv accepted. jsonparser may reject (narrower JSON grammar)
916 // — that's a documented divergence, not a bug. But if jsonparser
917 // ALSO accepts, the value must match.
918 if err != nil {
919 return true
920 }
921 // Re-fetch both for comparison.
922 ref, _ := strconv.ParseBool(s)
923 got, _ := ParseBoolean([]byte(s))
924 return got == ref
925 }
926 if err := quick.Check(rb, &quick.Config{MaxCount: 5000}); err != nil {
927 t.Fatalf("ParseBoolean diverges from strconv on commonly-accepted input: %v", err)
928 }
929}
930
931// closeEnoughFloat reports whether two float64 values agree to within a
932// relative tolerance of ~4 ULP — used only for adversarial inputs where

Callers

nothing calls this directly

Calls 1

ParseBooleanFunction · 0.85

Tested by

no test coverage detected