1#[allow(
23 unused_imports,
24 clippy::wildcard_imports,
25 reason = "Standard workspace crate prelude"
26)]
27pub(crate) use ctb_utilities::*;
28
29use twox_hash::{XxHash3_64, XxHash3_128, XxHash32, XxHash64};
30
31pub fn xxhash32(data: &[u8]) -> [u8; 4] {
33 XxHash32::oneshot(0, data).to_be_bytes()
34}
35
36pub fn xxhash64(data: &[u8]) -> [u8; 8] {
38 XxHash64::oneshot(0, data).to_be_bytes()
39}
40
41pub fn xxhash3_64(data: &[u8]) -> [u8; 8] {
43 XxHash3_64::oneshot(data).to_be_bytes()
44}
45
46pub fn xxhash3_128(data: &[u8]) -> [u8; 16] {
48 XxHash3_128::oneshot(data).to_be_bytes()
49}
50
51#[cfg(test)]
52#[allow(
53 clippy::panic,
54 clippy::expect_used,
55 clippy::unwrap_used,
56 clippy::unwrap_in_result,
57 clippy::panic_in_result_fn,
58 clippy::indexing_slicing,
59 clippy::arithmetic_side_effects,
60 reason = "Standard repository test boilerplate"
61)]
62mod tests {
63 use super::*;
64
65 #[crate::ctb_test]
66 fn test_xxhash32() {
67 assert_eq!(xxhash32(&[]), [0x02, 0xcc, 0x5d, 0x05]);
68 assert_eq!(xxhash32(b"hello world"), [0xce, 0xbb, 0x66, 0x22]);
69 }
70
71 #[crate::ctb_test]
72 fn test_xxhash64() {
73 assert_eq!(
74 xxhash64(&[]),
75 [0xef, 0x46, 0xdb, 0x37, 0x51, 0xd8, 0xe9, 0x99]
76 );
77 assert_eq!(
78 xxhash64(b"hello world"),
79 [0x45, 0xab, 0x67, 0x34, 0xb2, 0x1e, 0x69, 0x68]
80 );
81 }
82
83 #[crate::ctb_test]
84 fn test_xxhash3_64() {
85 assert_eq!(
86 xxhash3_64(&[]),
87 [0x2d, 0x06, 0x80, 0x05, 0x38, 0xd3, 0x94, 0xc2]
88 );
89 assert_eq!(
90 xxhash3_64(b"hello world"),
91 [0xd4, 0x47, 0xb1, 0xea, 0x40, 0xe6, 0x98, 0x8b]
92 );
93 }
94
95 #[crate::ctb_test]
96 fn test_xxhash3_128() {
97 assert_eq!(
98 xxhash3_128(&[]),
99 [
100 0x99, 0xaa, 0x06, 0xd3, 0x01, 0x47, 0x98, 0xd8, 0x60, 0x01,
101 0xc3, 0x24, 0x46, 0x8d, 0x49, 0x7f
102 ]
103 );
104 assert_eq!(
105 xxhash3_128(b"hello world"),
106 [
107 0xdf, 0x8d, 0x09, 0xe9, 0x3f, 0x87, 0x49, 0x00, 0xa9, 0x9b,
108 0x87, 0x75, 0xcc, 0x15, 0xb6, 0xc7
109 ]
110 );
111 }
112}