Skip to content

Commit

Permalink
all tests passing, about to change indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Oct 31, 2023
1 parent 6657ec7 commit 07d6292
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
1 change: 1 addition & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
corpus
artifacts
/scratch.*
9 changes: 3 additions & 6 deletions fuzz/fuzz_targets/compare_to_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,15 @@ fn remove_suffix(s: &str) -> &str {
fn errors_equal(jiter_error: &JiterError, serde_error: &SerdeError) -> bool {
let jiter_error_str = jiter_error.to_string();
let serde_error_str = serde_error.to_string();
// if jiter_error_str.starts_with("invalid escape at") {
// // strings like `"\"\\u\\"` give a EOF error for serde and invalid escape for jiter
// true
// } else if serde_error_str.starts_with("number out of range") {
if serde_error_str.starts_with("number out of range") {
// ignore this case as serde is stricter so fails on this before jiter does
true
} else if serde_error_str.starts_with("recursion limit exceeded") {
// serde has a different recursion limit to jiter
true
} else {
remove_suffix(&jiter_error_str) == remove_suffix(&serde_error_str)
return jiter_error_str == serde_error_str
// remove_suffix(&jiter_error_str) == remove_suffix(&serde_error_str)
}
}

Expand All @@ -115,7 +112,7 @@ fuzz_target!(|json: String| {
return
} else {
dbg!(json, &jiter_error, jiter_error.to_string(), &serde_error, serde_error.to_string());
panic!("errors not not equal");
panic!("errors not equal");
// return
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,17 +239,18 @@ impl FilePosition {
let mut last_line_start = 0;
let mut index = 0;
while let Some(next) = data.get(index) {
if index == find {
break;
} else if *next == b'\n' {
if *next == b'\n' {
line += 1;
last_line_start = index + 1;
}
if index == find {
break;
}
index += 1;
}
Self {
line,
column: index - last_line_start,
column: index.saturating_sub(last_line_start),
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/number_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl AbstractNumberDecoder for NumberFloat {
match NumberRange::decode(data, start, first) {
Err(e) => Err(e),
// shouldn't happen
Ok(_) => json_err!(InvalidNumber, index),
Ok(_) => unreachable!("NumberRange should always return an error"),

Check warning on line 77 in src/number_decoder.rs

View check run for this annotation

Codecov / codecov/patch

src/number_decoder.rs#L77

Added line #L77 was not covered by tests
}
}
}
Expand Down Expand Up @@ -208,6 +208,7 @@ impl AbstractNumberDecoder for NumberRange {
let end = consume_exponential(data, index)?;
Ok((start..end, end))
}
Some(b'0') => return json_err!(InvalidNumber, index),

Check warning on line 211 in src/number_decoder.rs

View check run for this annotation

Codecov / codecov/patch

src/number_decoder.rs#L211

Added line #L211 was not covered by tests
_ => return Ok((start..index, index)),
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl<'a> Parser<'a> {
if self.eat_whitespace().is_none() {
Ok(())
} else {
json_err!(TrailingCharacters, self.index)
json_err!(TrailingCharacters, self.index + 1)
}
}

Expand Down Expand Up @@ -231,11 +231,11 @@ impl<'a> Parser<'a> {
for c in expected.iter() {
match self.data.get(self.index) {
Some(v) if v == c => self.index += 1,
Some(_) => return json_err!(ExpectedSomeIdent, self.index),
Some(_) => return json_err!(ExpectedSomeIdent, self.index + 1),
_ => break,
}
}
json_err!(EofWhileParsingValue, self.data.len())
json_err!(EofWhileParsingValue, self.index)
}
}
}
Expand Down
25 changes: 15 additions & 10 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ single_tests! {
offset_true: ok => " true", "true @ 1:2";
empty: err => "", "EofWhileParsingValue @ 1:0";
string_unclosed: err => r#""foobar"#, "EofWhileParsingString @ 1:7";
bad_int: err => "-", "EofWhileParsingValue @ 1:1";
bad_true1: err => "truX", "ExpectedSomeIdent @ 1:3";
bad_int_neg: err => "-", "EofWhileParsingValue @ 1:1";
bad_true1: err => "truX", "ExpectedSomeIdent @ 1:4";
bad_true2: err => "tru", "EofWhileParsingValue @ 1:3";
bad_true3: err => "trX", "ExpectedSomeIdent @ 1:2";
bad_false1: err => "falsX", "ExpectedSomeIdent @ 1:4";
bad_true3: err => "trX", "ExpectedSomeIdent @ 1:3";
bad_false1: err => "falsX", "ExpectedSomeIdent @ 1:5";
bad_false2: err => "fals", "EofWhileParsingValue @ 1:4";
bad_null1: err => "nulX", "ExpectedSomeIdent @ 1:3";
bad_null1: err => "nulX", "ExpectedSomeIdent @ 1:4";
bad_null2: err => "nul", "EofWhileParsingValue @ 1:3";
object_trailing_comma: err => r#"{"foo": "bar",}"#, "TrailingComma @ 1:15";
array_trailing_comma: err => r#"[1, 2,]"#, "TrailingComma @ 1:7";
Expand All @@ -210,11 +210,15 @@ single_tests! {
deep_array: ok => r#"[["Not too deep"]]"#, "[ @ 1:0, [ @ 1:1, String(3..15) @ 1:2, ], ]";
object_key_int: err => r#"{4: 4}"#, "KeyMustBeAString @ 1:2";
array_no_close: err => r#"["#, "EofWhileParsingList @ 1:1";
array_double_close: err => "[1]]", "TrailingCharacters @ 1:3";
double_zero: err => "001", "InvalidNumber @ 1:0";
array_double_close: err => "[1]]", "TrailingCharacters @ 1:4";
invalid_float_e_end: err => "0E", "EofWhileParsingValue @ 1:2";
invalid_float_dot_end: err => "0.", "EofWhileParsingValue @ 1:2";
invalid_float_bracket: err => "2E[", "InvalidNumber @ 1:2";
trailing_char: err => "2z", "TrailingCharacters @ 1:2";
invalid_number_newline: err => "-\n", "InvalidNumber @ 2:0";
// double_zero: err => "00", "InvalidNumber @ 1:2";
// double_zero_one: err => "001", "InvalidNumber @ 1:2";
second_line: err => "[1\nx]", "ExpectedListCommaOrEnd @ 2:1";
}

#[test]
Expand Down Expand Up @@ -367,8 +371,9 @@ test_position! {
first_line_3rd: b"123456", 3, FilePosition::new(1, 3);
first_line_last: b"123456", 6, FilePosition::new(1, 6);
first_line_after: b"123456", 7, FilePosition::new(1, 6);
first_line_last2: b"123456\n789", 6, FilePosition::new(1, 6);
second_line: b"123456\n789", 7, FilePosition::new(2, 0);
second_line0: b"123456\n789", 6, FilePosition::new(2, 0);
second_line1: b"123456\n789", 7, FilePosition::new(2, 0);
second_line2: b"123456\n789", 8, FilePosition::new(2, 1);
}

#[test]
Expand Down Expand Up @@ -615,7 +620,7 @@ fn jiter_trailing_bracket() {
e.error_type,
JiterErrorType::JsonError(JsonErrorType::TrailingCharacters)
);
assert_eq!(jiter.error_position(&e), FilePosition::new(1, 3));
assert_eq!(jiter.error_position(&e), FilePosition::new(1, 4));
}
}
}
Expand Down

0 comments on commit 07d6292

Please sign in to comment.