|
| 1 | +extern crate ropey; |
| 2 | + |
| 3 | +use std::hash::{Hash, Hasher}; |
| 4 | + |
| 5 | +use ropey::{Rope, RopeBuilder}; |
| 6 | + |
| 7 | +const SMALL_TEXT: &str = include_str!("small_ascii.txt"); |
| 8 | + |
| 9 | +/// This is an example `Hasher` to demonstrate a property guaranteed by |
| 10 | +/// the documentation that is not exploited by the default `Hasher` (SipHash) |
| 11 | +/// Relevant excerpt from the `Hasher` documentation: |
| 12 | +/// > Nor can you assume that adjacent |
| 13 | +/// > `write` calls are merged, so it's possible, for example, that |
| 14 | +/// > ``` |
| 15 | +/// > # fn foo(hasher: &mut impl std::hash::Hasher) { |
| 16 | +/// > hasher.write(&[1, 2]); |
| 17 | +/// > hasher.write(&[3, 4, 5, 6]); |
| 18 | +/// > # } |
| 19 | +/// > ``` |
| 20 | +/// > and |
| 21 | +/// > ``` |
| 22 | +/// > # fn foo(hasher: &mut impl std::hash::Hasher) { |
| 23 | +/// > hasher.write(&[1, 2, 3, 4]); |
| 24 | +/// > hasher.write(&[5, 6]); |
| 25 | +/// > # } |
| 26 | +/// > ``` |
| 27 | +/// > end up producing different hashes. |
| 28 | +/// |
| 29 | +/// This dummy hasher simply collects all bytes and inserts a separator byte (0xFF) at the end of `write`. |
| 30 | +/// While this hasher might seem a little silly, it is perfectly inline with the std documentation. |
| 31 | +/// Many other commonly used high performance `Hasher`s (fxhash, ahash, fnvhash) exploit the same property |
| 32 | +/// to improve the performance of `write`, so violating this property will cause issues in practice. |
| 33 | +#[derive(Default)] |
| 34 | +struct TestHasher(std::collections::hash_map::DefaultHasher); |
| 35 | +impl Hasher for TestHasher { |
| 36 | + fn finish(&self) -> u64 { |
| 37 | + self.0.finish() |
| 38 | + } |
| 39 | + |
| 40 | + fn write(&mut self, bytes: &[u8]) { |
| 41 | + self.0.write(bytes); |
| 42 | + self.0.write_u8(0xFF); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[test] |
| 47 | +#[cfg_attr(miri, ignore)] |
| 48 | +fn hash_1() { |
| 49 | + // Build two ropes with the same contents but different chunk boundaries. |
| 50 | + let r1 = { |
| 51 | + let mut b = RopeBuilder::new(); |
| 52 | + b._append_chunk("Hello w"); |
| 53 | + b._append_chunk("orld"); |
| 54 | + b._finish_no_fix() |
| 55 | + }; |
| 56 | + let r2 = { |
| 57 | + let mut b = RopeBuilder::new(); |
| 58 | + b._append_chunk("Hell"); |
| 59 | + b._append_chunk("o world"); |
| 60 | + b._finish_no_fix() |
| 61 | + }; |
| 62 | + |
| 63 | + let mut hasher1 = TestHasher::default(); |
| 64 | + let mut hasher2 = TestHasher::default(); |
| 65 | + r1.hash(&mut hasher1); |
| 66 | + r2.hash(&mut hasher2); |
| 67 | + |
| 68 | + assert_eq!(hasher1.finish(), hasher2.finish()); |
| 69 | +} |
| 70 | + |
| 71 | +#[test] |
| 72 | +#[cfg_attr(miri, ignore)] |
| 73 | +fn hash_2() { |
| 74 | + // Build two ropes with the same contents but different chunk boundaries. |
| 75 | + let r1 = { |
| 76 | + let mut b = RopeBuilder::new(); |
| 77 | + for chunk in SMALL_TEXT.as_bytes().chunks(5) { |
| 78 | + b._append_chunk(std::str::from_utf8(chunk).unwrap()); |
| 79 | + } |
| 80 | + b._finish_no_fix() |
| 81 | + }; |
| 82 | + let r2 = { |
| 83 | + let mut b = RopeBuilder::new(); |
| 84 | + for chunk in SMALL_TEXT.as_bytes().chunks(7) { |
| 85 | + b._append_chunk(std::str::from_utf8(chunk).unwrap()); |
| 86 | + } |
| 87 | + b._finish_no_fix() |
| 88 | + }; |
| 89 | + |
| 90 | + for (l1, l2) in r1.lines().zip(r2.lines()) { |
| 91 | + let mut hasher1 = TestHasher::default(); |
| 92 | + let mut hasher2 = TestHasher::default(); |
| 93 | + l1.hash(&mut hasher1); |
| 94 | + l2.hash(&mut hasher2); |
| 95 | + |
| 96 | + assert_eq!(hasher1.finish(), hasher2.finish()); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +#[test] |
| 101 | +#[cfg_attr(miri, ignore)] |
| 102 | +fn hash_3() { |
| 103 | + // Build two ropes with the same contents but different chunk boundaries. |
| 104 | + let r1 = { |
| 105 | + let mut b = RopeBuilder::new(); |
| 106 | + for chunk in SMALL_TEXT.as_bytes().chunks(521) { |
| 107 | + b._append_chunk(std::str::from_utf8(chunk).unwrap()); |
| 108 | + } |
| 109 | + b._finish_no_fix() |
| 110 | + }; |
| 111 | + let r2 = { |
| 112 | + let mut b = RopeBuilder::new(); |
| 113 | + for chunk in SMALL_TEXT.as_bytes().chunks(547) { |
| 114 | + b._append_chunk(std::str::from_utf8(chunk).unwrap()); |
| 115 | + } |
| 116 | + b._finish_no_fix() |
| 117 | + }; |
| 118 | + |
| 119 | + let mut hasher1 = TestHasher::default(); |
| 120 | + let mut hasher2 = TestHasher::default(); |
| 121 | + r1.hash(&mut hasher1); |
| 122 | + r2.hash(&mut hasher2); |
| 123 | + |
| 124 | + assert_eq!(hasher1.finish(), hasher2.finish()); |
| 125 | +} |
0 commit comments