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

Function TestOracleParseInt

reference_oracle_test.go:757–811  ·  view source on GitHub ↗

--------------------------------------------------------------------------- Property 4: ParseInt / ParseFloat / ParseBoolean agree with strconv. --------------------------------------------------------------------------- genIntToken generates random decimal integer strings (including boundary values

(t *testing.T)

Source from the content-addressed store, hash-verified

755// reqproof:proptest ParseInt
756// Verifies: SYS-REQ-015 [property]
757func TestOracleParseInt(t *testing.T) {
758 r := oracleNewRNG(oracleSeed + 3)
759 const iterations = 10000
760 for i := 0; i < iterations; i++ {
761 // Generate a random int64 (with bias toward boundary values).
762 var val int64
763 switch r.Intn(4) {
764 case 0:
765 val = oracleLargeInts[r.Intn(len(oracleLargeInts))]
766 case 1:
767 val = r.Int63()
768 case 2:
769 val = -r.Int63()
770 default:
771 val = int64(r.Intn(200)) - 100
772 }
773 // Format as decimal, optionally with a leading sign.
774 tok := strconv.FormatInt(val, 10)
775
776 ref, refErr := strconv.ParseInt(tok, 10, 64)
777 got, err := ParseInt([]byte(tok))
778
779 // The token was produced by strconv.FormatInt, so strconv MUST accept.
780 if refErr != nil {
781 t.Fatalf("strconv.ParseInt rejected its own output %q: %v", tok, refErr)
782 }
783 if err != nil {
784 t.Fatalf("ParseInt rejected valid int %q: %v", tok, err)
785 }
786 if got != ref {
787 t.Fatalf("ParseInt divergence on %q: jsonparser=%d, strconv=%d", tok, got, ref)
788 }
789 }
790
791 // Adversarial inputs: assert agreement on the common-acceptance domain.
792 // When both accept, values MUST match (this is the property that catches
793 // silent arithmetic bugs). When one rejects, the other may also reject —
794 // divergence of acceptance is allowed (D1 in file header: jsonparser
795 // follows strict JSON grammar, strconv accepts Go-idiomatic extensions).
796 adversarial := []string{
797 "", " ", " 123 ", "+42", "-0", "00", "0x10", "1e5",
798 "123abc", "99999999999999999999999999", "-99999999999999999999999999",
799 "12.34", "1_000", "0b11", "0o17", "\t\n", " -1 ",
800 "9223372036854775808", // max int64 + 1 (overflow)
801 "-9223372036854775809", // min int64 - 1 (underflow)
802 }
803 for _, tok := range adversarial {
804 ref, refErr := strconv.ParseInt(tok, 10, 64)
805 got, err := ParseInt([]byte(tok))
806 // If both accept, values must match.
807 if refErr == nil && err == nil && got != ref {
808 t.Fatalf("ParseInt divergence on adversarial %q: jsonparser=%d, strconv=%d", tok, got, ref)
809 }
810 }
811}
812
813// reqproof:proptest ParseFloat
814// Verifies: SYS-REQ-013 [property]

Callers

nothing calls this directly

Calls 2

oracleNewRNGFunction · 0.85
ParseIntFunction · 0.85

Tested by

no test coverage detected