-
Notifications
You must be signed in to change notification settings - Fork 354
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
Minor improvements to Utils package #1206
Changes from all commits
a3bbf15
f7c463f
6d3fe1f
0d06247
ac008dc
326d029
dff672f
a584be8
bc3898c
bbd82b7
4323c96
c48fdab
e578d7d
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod test_checkpoint; | ||
mod test_math; | ||
mod test_nonces; | ||
mod test_snip12; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use core::integer::{u512, u512_safe_div_rem_by_u256}; | ||
use core::num::traits::OverflowingAdd; | ||
use crate::math::average; | ||
|
||
#[test] | ||
fn test_average_u8(a: u8, b: u8) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that fuzzing actually takes a significant amount of time when running tests, not only in compilation. Did you noticed the same thing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did...it's only a few tests though so relative to the whole repo, IMO it's not a huge deal. If we want to improve performance, we can configure it to a smaller amount either in the tests themselves or globally when running the test cmd i.e. |
||
let actual = average(a, b); | ||
|
||
let a: u256 = a.into(); | ||
let b: u256 = b.into(); | ||
let expected = (a + b) / 2; | ||
|
||
assert_eq!(actual, expected.try_into().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_average_u16(a: u16, b: u16) { | ||
let actual = average(a, b); | ||
|
||
let a: u256 = a.into(); | ||
let b: u256 = b.into(); | ||
let expected = (a + b) / 2; | ||
|
||
assert_eq!(actual, expected.try_into().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_average_u32(a: u32, b: u32) { | ||
let actual = average(a, b); | ||
|
||
let a: u256 = a.into(); | ||
let b: u256 = b.into(); | ||
let expected = (a + b) / 2; | ||
|
||
assert_eq!(actual, expected.try_into().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_average_u64(a: u64, b: u64) { | ||
let actual = average(a, b); | ||
|
||
let a: u256 = a.into(); | ||
let b: u256 = b.into(); | ||
let expected = (a + b) / 2; | ||
|
||
assert_eq!(actual, expected.try_into().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_average_u128(a: u128, b: u128) { | ||
let actual = average(a, b); | ||
|
||
let a: u256 = a.into(); | ||
let b: u256 = b.into(); | ||
let expected = (a + b) / 2; | ||
|
||
assert_eq!(actual, expected.try_into().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_average_u256(a: u256, b: u256) { | ||
let actual = average(a, b); | ||
let mut expected = 0; | ||
|
||
let (sum, overflow) = a.overflowing_add(b); | ||
if !overflow { | ||
expected = sum / 2; | ||
} else { | ||
let u512_sum = u512 { limb0: sum.low, limb1: sum.high, limb2: 1, limb3: 0 }; | ||
let (res, _) = u512_safe_div_rem_by_u256(u512_sum, 2); | ||
expected = res.try_into().unwrap(); | ||
}; | ||
|
||
assert_eq!(actual, expected); | ||
} |
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.
I think position is easier to read here, maybe we can update the param name?
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.
Mmm I honestly like
pos
as the param name—unless you prefer a new name? I'll switch the comment back. Looking at it now, I agree position is easier to read