Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nirosys committed May 9, 2024
1 parent 705749d commit 6d55b88
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 60 deletions.
119 changes: 69 additions & 50 deletions src/lazy/binary/raw/v1_1/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ impl<'data> LazyRawReader<'data, BinaryEncoding_1_1> for LazyRawBinaryReader_1_1
mod tests {
use crate::lazy::binary::raw::v1_1::reader::LazyRawBinaryReader_1_1;
use crate::{IonResult, IonType};
use rstest::*;

#[test]
fn nop() -> IonResult<()> {
Expand Down Expand Up @@ -311,59 +312,77 @@ mod tests {
Ok(())
}

#[test]
fn decimals() -> IonResult<()> {
use crate::types::decimal::Decimal;

#[rustfmt::skip]
let data: Vec<u8> = vec![
// IVM
0xe0, 0x01, 0x01, 0xea,

// 0d0
0x60,

// 7d8
0x62, 0x01, 0x07,

// 1.27
0xF6, 0x05, 0xFD, 0x7F,

// 0d3
0x61, 0x07,

// -0
0x62, 0x07, 0x00,
];

let mut reader = LazyRawBinaryReader_1_1::new(&data);
let _ivm = reader.next()?.expect_ivm()?;
#[rstest]
#[case("0.", &[0x60])]
#[case("0d1", &[0x61, 0x03])]
#[case("0d63", &[0x61, 0x7F])]
#[case("0d64", &[0x62, 0x02, 0x01])]
#[case("0d99", &[0x62, 0x8E, 0x01])]
#[case("0.0", &[0x61, 0xFF])]
#[case("0.00", &[0x61, 0xFD])]
#[case("0.000", &[0x61, 0xFB])]
#[case("0d-64", &[0x61, 0x81])]
#[case("0d-99", &[0x62, 0x76, 0xFE])]
#[case("-0.", &[0x62, 0x01, 0x00])]
#[case("-0d1", &[0x62, 0x03, 0x00])]
#[case("-0d3", &[0x62, 0x07, 0x00])]
#[case("-0d63", &[0x62, 0x7F, 0x00])]
#[case("-0d199", &[0x63, 0x1E, 0x03, 0x00])]
#[case("-0d-1", &[0x62, 0xFF, 0x00])]
#[case("-0d-2", &[0x62, 0xFD, 0x00])]
#[case("-0d-3", &[0x62, 0xFB, 0x00])]
#[case("-0d-63", &[0x62, 0x83, 0x00])]
#[case("-0d-64", &[0x62, 0x81, 0x00])]
#[case("-0d-65", &[0x63, 0xFE, 0xFE, 0x00])]
#[case("-0d-199", &[0x63, 0xE6, 0xFC, 0x00])]
#[case("0.01", &[0x62, 0xFD, 0x01])]
#[case("0.1", &[0x62, 0xFF, 0x01])]
#[case("1d0", &[0x62, 0x01, 0x01])]
#[case("1d1", &[0x62, 0x03, 0x01])]
#[case("1d2", &[0x62, 0x05, 0x01])]
#[case("1d63", &[0x62, 0x7F, 0x01])]
#[case("1d64", &[0x63, 0x02, 0x01, 0x01])]
#[case("1d65536", &[0x64, 0x04, 0x00, 0x08, 0x01])]
#[case("2.", &[0x62, 0x01, 0x02])]
#[case("7.", &[0x62, 0x01, 0x07])]
#[case("14d0", &[0x62, 0x01, 0x0E])]
#[case("14d0", &[0x63, 0x02, 0x00, 0x0E])] // overpadded exponent
#[case("14d0", &[0x64, 0x01, 0x0E, 0x00, 0x00])] // Overpadded coefficient
#[case("14d0", &[0x65, 0x02, 0x00, 0x0E, 0x00, 0x00])] // Overpadded coefficient and exponent
#[case("1.0", &[0x62, 0xFF, 0x0A])]
#[case("1.00", &[0x62, 0xFD, 0x64])]
#[case("1.27", &[0x62, 0xFD, 0x7F])]
#[case("1.28", &[0x63, 0xFD, 0x80, 0x00])]
#[case("3.142", &[0x63, 0xFB, 0x46, 0x0C])]
#[case("3.14159", &[0x64, 0xF7, 0x2F, 0xCB, 0x04])]
#[case("3.1415927", &[0x65, 0xF3, 0x77, 0x5E, 0xDF, 0x01])]
#[case("3.141592653", &[0x66, 0xEF, 0x4D, 0xE6, 0x40, 0xBB, 0x00])]
#[case("3.141592653590", &[0x67, 0xE9, 0x16, 0x9F, 0x83, 0x75, 0xDB, 0x02])]
#[case("3.14159265358979323", &[0x69, 0xDF, 0xFB, 0xA0, 0x9E, 0xF6, 0x2F, 0x1E, 0x5C, 0x04])]
#[case("3.1415926535897932384626", &[0x6B, 0xD5, 0x72, 0x49, 0x64, 0xCC, 0xAF, 0xEF, 0x8F, 0x0F, 0xA7, 0x06])]
#[case("3.141592653589793238462643383", &[0x6D, 0xCB, 0xB7, 0x3C, 0x92, 0x86, 0x40, 0x9F, 0x1B, 0x01, 0x1F, 0xAA, 0x26, 0x0A])]
#[case("3.14159265358979323846264338327950", &[0x6F, 0xC1, 0x8E, 0x29, 0xE5, 0xE3, 0x56, 0xD5, 0xDF, 0xC5, 0x10, 0x8F, 0x55, 0x3F, 0x7D, 0x0F])]
#[case("3.141592653589793238462643383279503", &[0xF6, 0x21, 0xBF, 0x8F, 0x9F, 0xF3, 0xE6, 0x64, 0x55, 0xBE, 0xBA, 0xA7, 0x96, 0x57, 0x79, 0xE4, 0x9A, 0x00])]
fn decimals(#[case] expected_txt: &str, #[case] ion_data: &[u8]) -> IonResult<()> {
use crate::lazy::decoder::{LazyRawReader, LazyRawValue};
use crate::lazy::text::raw::v1_1::reader::LazyRawTextReader_1_1;
let bump = bumpalo::Bump::new();

let mut reader_txt = LazyRawTextReader_1_1::new(expected_txt.as_bytes());
let mut reader_bin = LazyRawBinaryReader_1_1::new(ion_data);

assert_eq!(
reader.next()?.expect_value()?.read()?.expect_decimal()?,
0.into()
);

assert_eq!(
reader.next()?.expect_value()?.read()?.expect_decimal()?,
7.into()
);

assert_eq!(
reader.next()?.expect_value()?.read()?.expect_decimal()?,
1.27f64.try_into()?
reader_bin
.next()?
.expect_value()?
.read()?
.expect_decimal()?,
reader_txt
.next(&bump)?
.expect_value()?
.read()?
.expect_decimal()?,
);

assert_eq!(
reader.next()?.expect_value()?.read()?.expect_decimal()?,
0.0f64.try_into()?
);

assert_eq!(
reader.next()?.expect_value()?.read()?.expect_decimal()?,
Decimal::negative_zero()
);

Ok(())
}
}
16 changes: 6 additions & 10 deletions src/lazy/binary/raw/v1_1/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,12 @@ impl<'top> LazyRawBinaryValue_1_1<'top> {

let value_bytes = self.value_body()?;
let exponent = FlexInt::read(value_bytes, 0)?;
let coefficient_size = self.encoded_value.value_length() - exponent.size_in_bytes();
let coefficient = if coefficient_size > 0 {
FixedInt::read(
&value_bytes[exponent.size_in_bytes()..],
coefficient_size,
0,
)?
} else {
0i64.into()
};
let coefficient_size = self.encoded_value.value_body_length - exponent.size_in_bytes();
let coefficient = FixedInt::read(
&value_bytes[exponent.size_in_bytes()..],
coefficient_size,
0,
)?;
Decimal::new(coefficient, exponent.value())
};

Expand Down

0 comments on commit 6d55b88

Please sign in to comment.