Skip to content

Commit

Permalink
core: add simple unit test for CryptoHash::hash_borsh and hash_borsh_…
Browse files Browse the repository at this point in the history
…slice (#7760)
  • Loading branch information
mina86 authored Oct 4, 2022
1 parent 40101c1 commit 017b433
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions core/primitives-core/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,29 @@ mod tests {
hash: CryptoHash,
}

#[test]
fn test_hash_borsh() {
fn value<T: BorshSerialize>(want: &str, value: T) {
assert_eq!(want, CryptoHash::hash_borsh(&value).to_string());
}

fn slice<T: BorshSerialize>(want: &str, slice: &[T]) {
assert_eq!(want, CryptoHash::hash_borsh(&slice).to_string());
assert_eq!(want, CryptoHash::hash_borsh_slice(slice).to_string());
}

value("CuoNgQBWsXnTqup6FY3UXNz6RRufnYyQVxx8HKZLUaRt", "foo");
value("CuoNgQBWsXnTqup6FY3UXNz6RRufnYyQVxx8HKZLUaRt", "foo".as_bytes());
value("CuoNgQBWsXnTqup6FY3UXNz6RRufnYyQVxx8HKZLUaRt", &b"foo"[..]);
value("CuoNgQBWsXnTqup6FY3UXNz6RRufnYyQVxx8HKZLUaRt", [3, 0, 0, 0, b'f', b'o', b'o']);
slice("CuoNgQBWsXnTqup6FY3UXNz6RRufnYyQVxx8HKZLUaRt", "foo".as_bytes());

value("3yMApqCuCjXDWPrbjfR5mjCPTHqFG8Pux1TxQrEM35jj", b"foo");
value("3yMApqCuCjXDWPrbjfR5mjCPTHqFG8Pux1TxQrEM35jj", [b'f', b'o', b'o']);
value("3yMApqCuCjXDWPrbjfR5mjCPTHqFG8Pux1TxQrEM35jj", &[b'f', b'o', b'o']);
slice("CuoNgQBWsXnTqup6FY3UXNz6RRufnYyQVxx8HKZLUaRt", &[b'f', b'o', b'o']);
}

#[test]
fn test_base58_successes() {
for (encoded, hash) in [
Expand Down

0 comments on commit 017b433

Please sign in to comment.