-
Notifications
You must be signed in to change notification settings - Fork 165
log: Update precision logic #252
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
Changes from 2 commits
788ef5d
37aec95
cca1734
a94223e
902fbd1
7365278
7779cd6
d5e1053
026c624
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,9 +199,47 @@ mod tests { | |
|
|
||
| logger.clear(); | ||
|
|
||
| // This should have no effect. | ||
| // This should have no effect since it is a string. | ||
| logger.append_with_args("0123456789", &[Argument::Precision(2)]); | ||
| assert!(&*logger == "0123456789".as_bytes()); | ||
|
|
||
| // Setting a precision equal to or greater than the maximum digits should have | ||
| // no effect. The value used will be capped at the maximum digits - 1. | ||
| let mut l1 = Logger::<50>::default(); | ||
| let mut l2 = Logger::<50>::default(); | ||
|
|
||
| l1.append_with_args(2u8, &[Argument::Precision(u8::MAX)]); | ||
| assert_eq!(&*l1, "0.02".as_bytes()); | ||
|
||
| // same as 2 precision | ||
| l2.append_with_args(2u8, &[Argument::Precision(2)]); | ||
| assert_eq!(&*l1, &*l2); | ||
|
|
||
| l1.clear(); | ||
| l2.clear(); | ||
|
|
||
| l1.append_with_args(2u16, &[Argument::Precision(u8::MAX)]); | ||
| assert_eq!(&*l1, "0.0002".as_bytes()); | ||
| // same as 4 precision | ||
| l2.append_with_args(2u16, &[Argument::Precision(4)]); | ||
| assert_eq!(&*l1, &*l2); | ||
|
|
||
| l1.clear(); | ||
| l2.clear(); | ||
|
|
||
| l1.append_with_args(2u32, &[Argument::Precision(u8::MAX)]); | ||
| assert_eq!(&*l1, "0.000000002".as_bytes()); | ||
| // same as 9 precision | ||
| l2.append_with_args(2u32, &[Argument::Precision(9)]); | ||
| assert_eq!(&*l1, &*l2); | ||
|
|
||
| l1.clear(); | ||
| l2.clear(); | ||
|
|
||
| l1.append_with_args(2u64, &[Argument::Precision(u8::MAX)]); | ||
| assert_eq!(&*l1, "0.0000000000000000002".as_bytes()); | ||
| // same as 19 precision | ||
| l2.append_with_args(2u64, &[Argument::Precision(19)]); | ||
| assert_eq!(&*l1, &*l2); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -175,6 +175,10 @@ pub enum Argument { | |
| /// Number of decimal places to display for numbers. | ||
| /// | ||
| /// This is only applicable for numeric types. | ||
| /// | ||
| /// The precision cannot be equal to or greater than the maximum number | ||
| /// of digits for the type. If it is, it will be set to the maximum | ||
|
||
| /// number of digits minus one. | ||
| Precision(u8), | ||
|
|
||
| /// Truncate the output at the end when the specified maximum number of characters | ||
|
|
@@ -262,7 +266,13 @@ macro_rules! impl_log_for_unsigned_integer { | |
| .iter() | ||
| .find(|arg| matches!(arg, Argument::Precision(_))) | ||
| { | ||
| *p as usize | ||
| // Precision cannot be equal to or greater than the maximum number | ||
| // of digits for the type.. | ||
| if (*p as usize) >= MAX_DIGITS { | ||
| MAX_DIGITS - 1 | ||
| } else { | ||
| *p as usize | ||
| } | ||
| } else { | ||
| 0 | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great tests!