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

Function TestPropertyBufferOpsPure

property_test.go:847–895  ·  view source on GitHub ↗

--------------------------------------------------------------------------- Property: WriteToBuffer / calcAllocateSpace / createInsertComponent are pure --------------------------------------------------------------------------- reqproof:proptest WriteToBuffer, calcAllocateSpace, createInsertCompon

(t *testing.T)

Source from the content-addressed store, hash-verified

845// reqproof:proptest WriteToBuffer, calcAllocateSpace, createInsertComponent
846// Verifies: SYS-REQ-009 [property]
847func TestPropertyBufferOpsPure(t *testing.T) {
848 r := newRNG(jsonSeed + 13)
849 const iterations = 500
850 // WriteToBuffer: deterministic for a fixed buffer + string, never overflows.
851 writeDeterministic := func(bufLen int, s string) bool {
852 if bufLen < 0 || bufLen > 4096 {
853 return true
854 }
855 buf1 := make([]byte, bufLen)
856 buf2 := make([]byte, bufLen)
857 n1 := WriteToBuffer(buf1, s)
858 n2 := WriteToBuffer(buf2, s)
859 if n1 != n2 {
860 return false
861 }
862 return bytes.Equal(buf1, buf2)
863 }
864 if err := quick.Check(writeDeterministic, &quick.Config{MaxCount: iterations}); err != nil {
865 t.Fatalf("WriteToBuffer not deterministic: %v", err)
866 }
867
868 // calcAllocateSpace / createInsertComponent: determinism + no panic.
869 for i := 0; i < iterations; i++ {
870 keys := []string{randKey(r), randKey(r)}
871 setValue := []byte(strconv.FormatInt(r.Int63(), 10))
872 for _, comma := range []bool{true, false} {
873 for _, object := range []bool{true, false} {
874 a := calcAllocateSpace(keys, setValue, comma, object)
875 b := calcAllocateSpace(keys, setValue, comma, object)
876 if a != b {
877 t.Fatalf("calcAllocateSpace non-deterministic: %d vs %d (keys=%v val=%q comma=%v obj=%v)", a, b, keys, setValue, comma, object)
878 }
879 if a < 0 {
880 t.Fatalf("calcAllocateSpace negative: %d", a)
881 }
882 if !recoverNoPanic(func() {
883 _ = createInsertComponent(keys, setValue, comma, object)
884 }) {
885 t.Fatalf("createInsertComponent panicked on keys=%v val=%q comma=%v obj=%v", keys, setValue, comma, object)
886 }
887 out1 := createInsertComponent(keys, setValue, comma, object)
888 out2 := createInsertComponent(keys, setValue, comma, object)
889 if !bytes.Equal(out1, out2) {
890 t.Fatalf("createInsertComponent non-deterministic on keys=%v val=%q comma=%v obj=%v", keys, setValue, comma, object)
891 }
892 }
893 }
894 }
895}
896
897// ---------------------------------------------------------------------------
898// Property: ValueType.String round-trips for all enumerated tags

Callers

nothing calls this directly

Calls 6

newRNGFunction · 0.85
WriteToBufferFunction · 0.85
randKeyFunction · 0.85
calcAllocateSpaceFunction · 0.85
recoverNoPanicFunction · 0.85
createInsertComponentFunction · 0.85

Tested by

no test coverage detected