<commit_before>package jd

import (
	"strconv"
	"testing"
)

var corpus = []string{
	``,  // void
	` `, // void
	`null`,
	`0`,
	`1`,
	`""`,
	`"foo"`,
	`"bar"`,
	`"null"`,
	`[]`,
	`[null]`,
	`[null,null,null]`,
	`[1]`,
	`[1,2,3]`,
	`[{},[],3]`,
	`[1,{},[]]`,
	`{}`,
	`{"foo":"bar"}`,
	`{"foo":null}`,
	`{"foo":1}`,
	`{"foo":[]}`,
	`{"foo":[null]}`,
	`{"foo":[1]}`,
	`{"foo":[1,2,3]}`,
	`{"foo":[1,null,3]}`,
	`{"foo":{}}`,
	`{"foo":{"bar":null}}`,
	`{"foo":{"bar":1}}`,
	`{"foo":{"bar":[]}}`,
	`{"foo":{"bar":[1,2,3]}}`,
	`{"foo":{"bar":{}}}`,
}

func FuzzJd(f *testing.F) {
	for _, a := range corpus {
		_, err := ReadJsonString(a)
		if err != nil {
			f.Errorf("corpus entry not valid JSON: %q", a)
		}
		for _, b := range corpus {
			f.Add(a, b)
		}
	}
	f.Fuzz(fuzz)
}

func fuzz(t *testing.T, aStr, bStr string) {
	// Only valid JSON input.
	a, err := ReadJsonString(aStr)
	if err != nil {
		return
	}
	if a == nil {
		t.Errorf("nil parsed input: %q", aStr)
		return
	}
	b, err := ReadJsonString(bStr)
	if err != nil {
		return
	}
	if b == nil {
		t.Errorf("nil parsed input: %q", bStr)
		return
	}
	for _, format := range [][2]string{{
		"jd", "list",
	}, {
		"jd", "set",
	}, {
		"jd", "mset",
	}, {
		"patch", "list",
	}, {
		"merge", "list",
	}} {
		a, _ = ReadJsonString(aStr) // Fresh parsed copy.
		if format[0] == "merge" {
			if hasUnsupportedNullValue(a) {
				continue
			}
			if hasUnsupportedNullValue(b) {
				continue
			}
		}
		var metadata []Metadata
		switch format[0] {
		case "jd":
			switch format[1] {
			case "set":
				metadata = append(metadata, SET)
			case "mset":
				metadata = append(metadata, MULTISET)
			default: // list
			}
		case "merge":
			metadata = append(metadata, MERGE)
		default: // patch
		}

		// Diff A and B.
		d := a.Diff(b, metadata...)
		if d == nil {
			t.Errorf("nil diff of a and b")
			return
		}
		if format[0] == "patch" && hasUnsupportedObjectKey(d) {
			return
		}
		var diffABStr string
		var diffAB Diff
		switch format[0] {
		case "jd":
			diffABStr = d.Render()
			diffAB, err = ReadDiffString(diffABStr)
		case "patch":
			diffABStr, err = d.RenderPatch()
			if err != nil {
				t.Errorf("could not render diff %v as patch: %v", d, err)
				return
			}
			diffAB, err = ReadPatchString(diffABStr)
		case "merge":
			diffABStr, err = d.RenderMerge()
			if err != nil {
				t.Errorf("could not render diff %v as merge: %v", d, err)
				return
			}
			diffAB, err = ReadMergeString(diffABStr)
		}
		if err != nil {
			t.Errorf("error parsing diff string %q: %v", diffABStr, err)
			return
		}
		// Apply diff to A to get B.
		patchedA, err := a.Patch(diffAB)
		if err != nil {
			t.Errorf("applying patch %v to %v should give %v. Got err: %v", diffAB.Render(), aStr, bStr, err)<commit_msg>Continue fuzzing other configurations after encountering an unsupported object key.<commit_after>package jd

import (
	"strconv"
	"testing"
)

var corpus = []string{
	``,  // void
	` `, // void
	`null`,
	`0`,
	`1`,
	`""`,
	`"foo"`,
	`"bar"`,
	`"null"`,
	`[]`,
	`[null]`,
	`[null,null,null]`,
	`[1]`,
	`[1,2,3]`,
	`[{},[],3]`,
	`[1,{},[]]`,
	`{}`,
	`{"foo":"bar"}`,
	`{"foo":null}`,
	`{"foo":1}`,
	`{"foo":[]}`,
	`{"foo":[null]}`,
	`{"foo":[1]}`,
	`{"foo":[1,2,3]}`,
	`{"foo":[1,null,3]}`,
	`{"foo":{}}`,
	`{"foo":{"bar":null}}`,
	`{"foo":{"bar":1}}`,
	`{"foo":{"bar":[]}}`,
	`{"foo":{"bar":[1,2,3]}}`,
	`{"foo":{"bar":{}}}`,
}

func FuzzJd(f *testing.F) {
	for _, a := range corpus {
		_, err := ReadJsonString(a)
		if err != nil {
			f.Errorf("corpus entry not valid JSON: %q", a)
		}
		for _, b := range corpus {
			f.Add(a, b)
		}
	}
	f.Fuzz(fuzz)
}

func fuzz(t *testing.T, aStr, bStr string) {
	// Only valid JSON input.
	a, err := ReadJsonString(aStr)
	if err != nil {
		return
	}
	if a == nil {
		t.Errorf("nil parsed input: %q", aStr)
		return
	}
	b, err := ReadJsonString(bStr)
	if err != nil {
		return
	}
	if b == nil {
		t.Errorf("nil parsed input: %q", bStr)
		return
	}
	for _, format := range [][2]string{{
		"jd", "list",
	}, {
		"jd", "set",
	}, {
		"jd", "mset",
	}, {
		"patch", "list",
	}, {
		"merge", "list",
	}} {
		a, _ = ReadJsonString(aStr) // Fresh parsed copy.
		if format[0] == "merge" {
			if hasUnsupportedNullValue(a) {
				continue
			}
			if hasUnsupportedNullValue(b) {
				continue
			}
		}
		var metadata []Metadata
		switch format[0] {
		case "jd":
			switch format[1] {
			case "set":
				metadata = append(metadata, SET)
			case "mset":
				metadata = append(metadata, MULTISET)
			default: // list
			}
		case "merge":
			metadata = append(metadata, MERGE)
		default: // patch
		}

		// Diff A and B.
		d := a.Diff(b, metadata...)
		if d == nil {
			t.Errorf("nil diff of a and b")
			return
		}
		if format[0] == "patch" && hasUnsupportedObjectKey(d) {
			continue
		}
		var diffABStr string
		var diffAB Diff
		switch format[0] {
		case "jd":
			diffABStr = d.Render()
			diffAB, err = ReadDiffString(diffABStr)
		case "patch":
			diffABStr, err = d.RenderPatch()
			if err != nil {
				t.Errorf("could not render diff %v as patch: %v", d, err)
				return
			}
			diffAB, err = ReadPatchString(diffABStr)
		case "merge":
			diffABStr, err = d.RenderMerge()
			if err != nil {
				t.Errorf("could not render diff %v as merge: %v", d, err)
				return
			}
			diffAB, err = ReadMergeString(diffABStr)
		}
		if err != nil {
			t.Errorf("error parsing diff string %q: %v", diffABStr, err)
			return
		}
		// Apply diff to A to get B.
		patchedA, err := a.Patch(diffAB)
		if err != nil {
			t.Errorf("applying patch %v to %v should give %v. Got err: %v", diffAB.Render(), aStr, bStr, err)
{'fraction_non_alphanumeric': 0.2242739333094299, 'fraction_numerical': 0.008963786303334529, 'mean_word_length': 3.028158844765343, 'pattern_counts': {'":': 52, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6911990', 'n_tokens_mistral': 2611, 'n_tokens_neox': 2333, 'n_words': 570}