Skip to content

Commit

Permalink
fix property tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Aug 17, 2021
1 parent 4851471 commit 6cd562e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
32 changes: 25 additions & 7 deletions src/character/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,10 +1132,28 @@ mod tests {
);
}

fn digit_to_i16(i: &str) -> IResult<&str, i16> {
let (i, s) = digit1(i)?;
fn digit_to_i16(input: &str) -> IResult<&str, i16> {
let i = input;
let (i, opt_sign) = opt(alt((char('+'), char('-'))))(i)?;
let sign = match opt_sign {
Some('+') => true,
Some('-') => false,
_ => true,
};

let (i, s) = match digit1::<_, crate::error::Error<_>>(i) {
Ok((i, s)) => (i, s),
Err(_) => return Err(Err::Error(crate::error::Error::from_error_kind(input, ErrorKind::Digit))),
};

match s.parse_to() {
Some(n) => Ok((i, n)),
Some(n) => {
if sign {
Ok((i, n))
} else {
Ok((i, -n))
}
},
None => Err(Err::Error(crate::error::Error::from_error_kind(i, ErrorKind::Digit))),
}
}
Expand All @@ -1151,15 +1169,15 @@ mod tests {
proptest! {
#[test]
fn ints(s in "\\PC*") {
let res1 = digit_to_u32(&s);
let res2 = u32(s.as_str());
let res1 = digit_to_i16(&s);
let res2 = i16(s.as_str());
assert_eq!(res1, res2);
}

#[test]
fn uints(s in "\\PC*") {
let res1 = digit_to_i16(&s);
let res2 = i16(s.as_str());
let res1 = digit_to_u32(&s);
let res2 = u32(s.as_str());
assert_eq!(res1, res2);
}
}
Expand Down
32 changes: 25 additions & 7 deletions src/character/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1105,10 +1105,28 @@ mod tests {
);
}

fn digit_to_i16(i: &str) -> IResult<&str, i16> {
let (i, s) = digit1(i)?;
fn digit_to_i16(input: &str) -> IResult<&str, i16> {
let i = input;
let (i, opt_sign) = opt(alt((char('+'), char('-'))))(i)?;
let sign = match opt_sign {
Some('+') => true,
Some('-') => false,
_ => true,
};

let (i, s) = match digit1::<_, crate::error::Error<_>>(i) {
Ok((i, s)) => (i, s),
Err(Err::Incomplete(i)) => return Err(Err::Incomplete(i)),
Err(_) => return Err(Err::Error(crate::error::Error::from_error_kind(input, ErrorKind::Digit))),
};
match s.parse_to() {
Some(n) => Ok((i, n)),
Some(n) => {
if sign {
Ok((i, n))
} else {
Ok((i, -n))
}
},
None => Err(Err::Error(crate::error::Error::from_error_kind(i, ErrorKind::Digit))),
}
}
Expand All @@ -1124,15 +1142,15 @@ mod tests {
proptest! {
#[test]
fn ints(s in "\\PC*") {
let res1 = digit_to_u32(&s);
let res2 = u32(s.as_str());
let res1 = digit_to_i16(&s);
let res2 = i16(s.as_str());
assert_eq!(res1, res2);
}

#[test]
fn uints(s in "\\PC*") {
let res1 = digit_to_i16(&s);
let res2 = i16(s.as_str());
let res1 = digit_to_u32(&s);
let res2 = u32(s.as_str());
assert_eq!(res1, res2);
}
}
Expand Down

0 comments on commit 6cd562e

Please sign in to comment.