Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more unit tests for float parsing #36

Merged
merged 2 commits into from
Aug 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion a2lfile/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,9 @@ impl<'a> ParserState<'a> {
pub(crate) fn get_float(&mut self, context: &ParseContext) -> Result<f32, ParserError> {
let token = self.expect_token(context, A2lTokenType::Number)?;
let text = self.get_token_text(token);
// some vendor tools are defining the characteristic UpperLimit and LowerLimit
// (float values from specifications) using 0xNNN for characteristics that
// are actually integers.
if text.starts_with("0x") || text.starts_with("0X") {
match u64::from_str_radix(&text[2..], 16) {
Ok(num) => Ok(num as f32),
Expand All @@ -617,6 +620,9 @@ impl<'a> ParserState<'a> {
pub(crate) fn get_double(&mut self, context: &ParseContext) -> Result<f64, ParserError> {
let token = self.expect_token(context, A2lTokenType::Number)?;
let text = self.get_token_text(token);
// some vendor tools are defining the characteristic UpperLimit and LowerLimit
// (float values from specifications) using 0xNNN for characteristics that
// are actually integers.
if text.starts_with("0x") || text.starts_with("0X") {
match u64::from_str_radix(&text[2..], 16) {
Ok(num) => Ok(num as f64),
Expand Down Expand Up @@ -1120,7 +1126,7 @@ mod tests {

#[test]
fn parsing_numbers_test() {
let input_text = r##"0 0x1 1.0e+2 1000 0 0.1 0x11 1.0e+2"##;
let input_text = r##"0 0x1 1.0e+2 1000 0 0.1 0x11 1.0e+2 0X1f 0X2F 2F F"##;
let tokenresult = tokenizer::tokenize(&Filename::from("test_input"), 0, input_text);
assert!(tokenresult.is_ok());

Expand Down Expand Up @@ -1177,6 +1183,27 @@ mod tests {
assert!(res.is_ok());
let val = res.unwrap();
assert_eq!(val, 100f32);

// float: 0X1f
let res = parser.get_float(&context);
assert!(res.is_ok());
let val = res.unwrap();
assert_eq!(val, 31f32);

// float: 0X2F
let res = parser.get_float(&context);
assert!(res.is_ok());
let val = res.unwrap();
assert_eq!(val, 47f32);

// float: 2F
let res = parser.get_float(&context);
assert!(res.is_err());

// float: F
let res = parser.get_float(&context);
assert!(res.is_err());

}

#[test]
Expand Down
Loading