quickcheck.rs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) 2020 Stu Small
  2. //
  3. // Licensed under the Apache License, Version 2.0
  4. // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
  5. // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  6. // option. All files in the project carrying such notice may not be copied,
  7. // modified, or distributed except according to those terms.
  8. #[macro_use]
  9. extern crate quickcheck;
  10. #[cfg(test)]
  11. mod test {
  12. extern crate murmur3;
  13. extern crate murmur3_sys;
  14. use std::io::Cursor;
  15. use murmur3::{murmur3_32, murmur3_32_of_slice};
  16. use murmur3_sys::MurmurHash3_x86_32;
  17. use murmur3::murmur3_x86_128;
  18. use murmur3_sys::MurmurHash3_x86_128;
  19. use murmur3::murmur3_x64_128;
  20. use murmur3_sys::MurmurHash3_x64_128;
  21. quickcheck! {
  22. #[cfg_attr(miri, ignore)]
  23. fn quickcheck_32(input:(u32, Vec<u8>)) -> bool{
  24. let seed = input.0;
  25. let xs = input.1;
  26. let output: [u8; 4] = [0; 4];
  27. unsafe {
  28. MurmurHash3_x86_32(xs.as_ptr() as _, xs.len() as i32,seed,output.as_ptr() as *mut _)
  29. };
  30. let output = u32::from_le_bytes(output);
  31. let output2 = murmur3_32(&mut Cursor::new(xs), seed).unwrap_or_else(|err| panic!("Panicked with {err:?} at {}:{} (git sha: {:?})", file!(), line!(), option_env!("GIT_SHA")));
  32. output == output2
  33. }
  34. }
  35. quickcheck! {
  36. #[cfg_attr(miri, ignore)]
  37. fn quickcheck_32_slice(input:(u32, Vec<u8>)) -> bool{
  38. let seed = input.0;
  39. let xs = input.1;
  40. let mut output: [u8; 4] = [0; 4];
  41. unsafe {
  42. MurmurHash3_x86_32(xs.as_ptr() as _, xs.len() as i32, seed, output.as_mut_ptr() as _)
  43. };
  44. let output = u32::from_le_bytes(output);
  45. let output2 = murmur3_32_of_slice(&xs[..], seed);
  46. output == output2
  47. }
  48. }
  49. quickcheck! {
  50. #[cfg_attr(miri, ignore)]
  51. fn quickcheck_x86_128(input:(u32, Vec<u8>)) -> bool {
  52. let seed = input.0;
  53. let xs = input.1;
  54. let output_bytes: [u8; 16] = [0; 16];
  55. unsafe {
  56. MurmurHash3_x86_128(xs.as_ptr() as _, xs.len() as i32,seed,output_bytes.as_ptr() as *mut _)
  57. };
  58. let output = u128::from_le_bytes(output_bytes);
  59. let output2 = murmur3_x86_128(&mut Cursor::new(xs), seed).unwrap_or_else(|err| panic!("Panicked with {err:?} at {}:{} (git sha: {:?})", file!(), line!(), option_env!("GIT_SHA")));
  60. output == output2
  61. }
  62. }
  63. quickcheck! {
  64. #[cfg_attr(miri, ignore)]
  65. fn quickcheck_x64_128(input:(u32, Vec<u8>)) -> bool {
  66. let seed = input.0;
  67. let xs = input.1;
  68. let output_bytes: [u8; 16] = [0; 16];
  69. unsafe {
  70. MurmurHash3_x64_128(xs.as_ptr() as _, xs.len() as i32,seed, output_bytes.as_ptr() as *mut _)
  71. };
  72. let output = u128::from_le_bytes(output_bytes);
  73. let output2 = murmur3_x64_128(&mut Cursor::new(xs), seed).unwrap_or_else(|err| panic!("Panicked with {err:?} at {}:{} (git sha: {:?})", file!(), line!(), option_env!("GIT_SHA")));
  74. output == output2
  75. }
  76. }
  77. }