Skip to content

Commit

Permalink
add unit test for fixed-point fmt::Display
Browse files Browse the repository at this point in the history
  • Loading branch information
lif committed Feb 6, 2025
1 parent d6f30bf commit fc15d29
Showing 1 changed file with 69 additions and 6 deletions.
75 changes: 69 additions & 6 deletions src/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,74 @@ fn fixed_fmt_abs<const B: u32>(
let width = f.width().unwrap_or(0);
let precision = f.precision().unwrap_or(const { ((B as usize) + 1) / 3 });
let fract = abs & ((1 << B) - 1);
let fract_dec = fract
.checked_mul(10u32.pow(precision as u32))
.map(|x| x >> B)
.unwrap_or_else(|| {
(fract as u64 * 10u64.pow(precision as u32) >> B) as u32
});
let fract_dec = 10u32
.checked_pow(precision as u32)
.and_then(|digits| fract.checked_mul(digits))
.map(|x| (x >> B) as u64)
.unwrap_or_else(|| (fract as u64 * 10u64.pow(precision as u32) >> B));
write!(f, "{:width$}.{:0precision$}", abs >> B, fract_dec)
}

#[cfg(test)]
mod test {
use crate::fixed::i16fx14;

use super::i32fx8;
use core::{fmt::Write, str};

struct WriteBuf<'a>(&'a mut [u8], usize);
impl<'a> From<&'a mut [u8]> for WriteBuf<'a> {
fn from(value: &'a mut [u8]) -> Self {
Self(value, 0)
}
}
impl Write for WriteBuf<'_> {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
let src = s.as_bytes();
let len = (self.0.len() - self.1).min(src.len());
self.0[self.1..self.1 + len].copy_from_slice(&src[..len]);
self.1 += len;
if len < src.len() {
Err(core::fmt::Error)
} else {
Ok(())
}
}
}
impl WriteBuf<'_> {
fn take(&mut self) -> &str {
let len = self.1;
self.1 = 0;
str::from_utf8(&self.0[..len]).unwrap()
}
}

#[test_case]
fn decimal_display() {
let mut buf = [0u8; 16];
let mut wbuf = WriteBuf::from(&mut buf[..]);

let x = i32fx8::from_bits(0x12345678);

write!(&mut wbuf, "{x}").unwrap();
assert_eq!(wbuf.take(), "1193046.468");

write!(&mut wbuf, "{x:9.1}").unwrap();
assert_eq!(wbuf.take(), " 1193046.4");

write!(&mut wbuf, "{x:1.6}").unwrap();
assert_eq!(wbuf.take(), "1193046.468750");

let x = x.neg();
write!(&mut wbuf, "{x}").unwrap();
assert_eq!(wbuf.take(), "-1193046.468");

let x = i16fx14::from_bits(0x6544 as i16);
write!(&mut wbuf, "{x}").unwrap();
assert_eq!(wbuf.take(), "1.58227");

let x = x.neg();
write!(&mut wbuf, "{x:.10}").unwrap();
assert_eq!(wbuf.take(), "-1.5822753906");
}
}

0 comments on commit fc15d29

Please sign in to comment.