Refactor tagged hash tests

In the tagged hash unit tests we are testing two separate things in a
single test. To improve maintainability separate the test into two.

Refactor only, no test coverage change.
This commit is contained in:
Tobin C. Harding 2024-04-23 12:46:37 +10:00
parent 216422dffc
commit 9aee65d1ba
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 14 additions and 10 deletions

View File

@ -174,6 +174,10 @@ mod tests {
108, 71, 99, 110, 96, 125, 179, 62, 234, 221, 198, 240, 201,
];
// The digest created by sha256 hashing `&[0]` starting with `TEST_MIDSTATE`.
#[cfg(feature = "alloc")]
const HASH_ZERO: &str = "29589d5122ec666ab5b4695070b6debc63881a4f85d88d93ddc90078038213ed";
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
pub struct TestHashTag;
@ -185,10 +189,17 @@ mod tests {
}
}
/// A hash tagged with `$name`.
// We support manually implementing `Tag` and creating a tagged hash from it.
#[cfg(feature = "alloc")]
pub type TestHash = sha256t::Hash<TestHashTag>;
#[test]
#[cfg(feature = "alloc")]
fn manually_created_sha256t_hash_type() {
assert_eq!(TestHash::hash(&[0]).to_string(), HASH_ZERO);
}
// We also provide a macro to create the tag and the hash type.
sha256t_hash_newtype! {
/// Test detailed explanation.
struct NewTypeTag = raw(TEST_MIDSTATE, 64);
@ -200,14 +211,7 @@ mod tests {
#[test]
#[cfg(feature = "alloc")]
fn test_sha256t() {
assert_eq!(
TestHash::hash(&[0]).to_string(),
"29589d5122ec666ab5b4695070b6debc63881a4f85d88d93ddc90078038213ed"
);
assert_eq!(
NewTypeHash::hash(&[0]).to_string(),
"29589d5122ec666ab5b4695070b6debc63881a4f85d88d93ddc90078038213ed"
);
fn macro_created_sha256t_hash_type() {
assert_eq!(NewTypeHash::hash(&[0]).to_string(), HASH_ZERO);
}
}