Skip to content

Commit

Permalink
Merge pull request #36 from servo/fmt
Browse files Browse the repository at this point in the history
Rustfmt
  • Loading branch information
Manishearth committed Dec 11, 2023
2 parents e234719 + fdd8d5a commit 62fbdc8
Showing 1 changed file with 210 additions and 63 deletions.
273 changes: 210 additions & 63 deletions lib.rs
Expand Up @@ -21,7 +21,9 @@
//! denial-of-service attacks, and can assume that its inputs are going to be
//! small—a perfect use case for FNV.
//!
#![cfg_attr(feature = "std", doc = r#"
#![cfg_attr(
feature = "std",
doc = r#"
## Using FNV in a `HashMap`
Expand Down Expand Up @@ -60,7 +62,8 @@ set = FnvHashSet::with_capacity_and_hasher(10, Default::default());
set.insert(1);
set.insert(2);
```
"#)]
"#
)]
//!
//! [chongo]: http://www.isthe.com/chongo/tech/comp/fnv/index.html
//! [docs]: https://doc.rust-lang.org/std/collections/struct.HashMap.html
Expand All @@ -71,16 +74,16 @@ set.insert(2);
#[cfg(all(not(feature = "std"), test))]
extern crate alloc;

#[cfg(feature = "std")]
use std::default::Default;
#[cfg(feature = "std")]
use std::hash::{Hasher, BuildHasherDefault};
#[cfg(feature = "std")]
use std::collections::{HashMap, HashSet};
#[cfg(not(feature = "std"))]
use core::default::Default;
#[cfg(not(feature = "std"))]
use core::hash::{Hasher, BuildHasherDefault};
use core::hash::{BuildHasherDefault, Hasher};
#[cfg(feature = "std")]
use std::collections::{HashMap, HashSet};
#[cfg(feature = "std")]
use std::default::Default;
#[cfg(feature = "std")]
use std::hash::{BuildHasherDefault, Hasher};

const INITIAL_STATE: u64 = 0xcbf29ce484222325;
const PRIME: u64 = 0x100000001b3;
Expand All @@ -93,7 +96,6 @@ const PRIME: u64 = 0x100000001b3;
pub struct FnvHasher(u64);

impl Default for FnvHasher {

#[inline]
fn default() -> FnvHasher {
FnvHasher(INITIAL_STATE)
Expand Down Expand Up @@ -139,7 +141,6 @@ pub type FnvHashMap<K, V> = HashMap<K, V, FnvBuildHasher>;
#[cfg(feature = "std")]
pub type FnvHashSet<T> = HashSet<T, FnvBuildHasher>;


/// Const version of FNV hash.
#[inline]
pub const fn fnv_hash(bytes: &[u8]) -> u64 {
Expand All @@ -153,14 +154,13 @@ pub const fn fnv_hash(bytes: &[u8]) -> u64 {
hash
}


#[cfg(test)]
mod test {
use super::*;
#[cfg(feature = "std")]
use std::hash::Hasher;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::hash::Hasher;

fn fnv1a(bytes: &[u8]) -> u64 {
let mut hasher = FnvHasher::default();
Expand Down Expand Up @@ -299,63 +299,195 @@ mod test {
assert_eq!(fnv1a(b"feedfacedeadbeef"), 0xcac54572bb1a6fc8);
assert_eq!(fnv1a(b"feedfacedeadbeef\0"), 0xa7a4c9f3edebf0d8);
assert_eq!(fnv1a(b"line 1\nline 2\nline 3"), 0x7829851fac17b143);
assert_eq!(fnv1a(b"chongo <Landon Curt Noll> /\\../\\"), 0x2c8f4c9af81bcf06);
assert_eq!(fnv1a(b"chongo <Landon Curt Noll> /\\../\\\0"), 0xd34e31539740c732);
assert_eq!(fnv1a(b"chongo (Landon Curt Noll) /\\../\\"), 0x3605a2ac253d2db1);
assert_eq!(fnv1a(b"chongo (Landon Curt Noll) /\\../\\\0"), 0x08c11b8346f4a3c3);
assert_eq!(fnv1a(b"http://antwrp.gsfc.nasa.gov/apod/astropix.html"), 0x6be396289ce8a6da);
assert_eq!(fnv1a(b"http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash"), 0xd9b957fb7fe794c5);
assert_eq!(
fnv1a(b"chongo <Landon Curt Noll> /\\../\\"),
0x2c8f4c9af81bcf06
);
assert_eq!(
fnv1a(b"chongo <Landon Curt Noll> /\\../\\\0"),
0xd34e31539740c732
);
assert_eq!(
fnv1a(b"chongo (Landon Curt Noll) /\\../\\"),
0x3605a2ac253d2db1
);
assert_eq!(
fnv1a(b"chongo (Landon Curt Noll) /\\../\\\0"),
0x08c11b8346f4a3c3
);
assert_eq!(
fnv1a(b"http://antwrp.gsfc.nasa.gov/apod/astropix.html"),
0x6be396289ce8a6da
);
assert_eq!(
fnv1a(b"http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash"),
0xd9b957fb7fe794c5
);
assert_eq!(fnv1a(b"http://epod.usra.edu/"), 0x05be33da04560a93);
assert_eq!(fnv1a(b"http://exoplanet.eu/"), 0x0957f1577ba9747c);
assert_eq!(fnv1a(b"http://hvo.wr.usgs.gov/cam3/"), 0xda2cc3acc24fba57);
assert_eq!(fnv1a(b"http://hvo.wr.usgs.gov/cams/HMcam/"), 0x74136f185b29e7f0);
assert_eq!(fnv1a(b"http://hvo.wr.usgs.gov/kilauea/update/deformation.html"), 0xb2f2b4590edb93b2);
assert_eq!(fnv1a(b"http://hvo.wr.usgs.gov/kilauea/update/images.html"), 0xb3608fce8b86ae04);
assert_eq!(fnv1a(b"http://hvo.wr.usgs.gov/kilauea/update/maps.html"), 0x4a3a865079359063);
assert_eq!(fnv1a(b"http://hvo.wr.usgs.gov/volcanowatch/current_issue.html"), 0x5b3a7ef496880a50);
assert_eq!(
fnv1a(b"http://hvo.wr.usgs.gov/cams/HMcam/"),
0x74136f185b29e7f0
);
assert_eq!(
fnv1a(b"http://hvo.wr.usgs.gov/kilauea/update/deformation.html"),
0xb2f2b4590edb93b2
);
assert_eq!(
fnv1a(b"http://hvo.wr.usgs.gov/kilauea/update/images.html"),
0xb3608fce8b86ae04
);
assert_eq!(
fnv1a(b"http://hvo.wr.usgs.gov/kilauea/update/maps.html"),
0x4a3a865079359063
);
assert_eq!(
fnv1a(b"http://hvo.wr.usgs.gov/volcanowatch/current_issue.html"),
0x5b3a7ef496880a50
);
assert_eq!(fnv1a(b"http://neo.jpl.nasa.gov/risk/"), 0x48fae3163854c23b);
assert_eq!(fnv1a(b"http://norvig.com/21-days.html"), 0x07aaa640476e0b9a);
assert_eq!(fnv1a(b"http://primes.utm.edu/curios/home.php"), 0x2f653656383a687d);
assert_eq!(
fnv1a(b"http://primes.utm.edu/curios/home.php"),
0x2f653656383a687d
);
assert_eq!(fnv1a(b"http://slashdot.org/"), 0xa1031f8e7599d79c);
assert_eq!(fnv1a(b"http://tux.wr.usgs.gov/Maps/155.25-19.5.html"), 0xa31908178ff92477);
assert_eq!(fnv1a(b"http://volcano.wr.usgs.gov/kilaueastatus.php"), 0x097edf3c14c3fb83);
assert_eq!(fnv1a(b"http://www.avo.alaska.edu/activity/Redoubt.php"), 0xb51ca83feaa0971b);
assert_eq!(
fnv1a(b"http://tux.wr.usgs.gov/Maps/155.25-19.5.html"),
0xa31908178ff92477
);
assert_eq!(
fnv1a(b"http://volcano.wr.usgs.gov/kilaueastatus.php"),
0x097edf3c14c3fb83
);
assert_eq!(
fnv1a(b"http://www.avo.alaska.edu/activity/Redoubt.php"),
0xb51ca83feaa0971b
);
assert_eq!(fnv1a(b"http://www.dilbert.com/fast/"), 0xdd3c0d96d784f2e9);
assert_eq!(fnv1a(b"http://www.fourmilab.ch/gravitation/orbits/"), 0x86cd26a9ea767d78);
assert_eq!(
fnv1a(b"http://www.fourmilab.ch/gravitation/orbits/"),
0x86cd26a9ea767d78
);
assert_eq!(fnv1a(b"http://www.fpoa.net/"), 0xe6b215ff54a30c18);
assert_eq!(fnv1a(b"http://www.ioccc.org/index.html"), 0xec5b06a1c5531093);
assert_eq!(fnv1a(b"http://www.isthe.com/cgi-bin/number.cgi"), 0x45665a929f9ec5e5);
assert_eq!(fnv1a(b"http://www.isthe.com/chongo/bio.html"), 0x8c7609b4a9f10907);
assert_eq!(fnv1a(b"http://www.isthe.com/chongo/index.html"), 0x89aac3a491f0d729);
assert_eq!(fnv1a(b"http://www.isthe.com/chongo/src/calc/lucas-calc"), 0x32ce6b26e0f4a403);
assert_eq!(fnv1a(b"http://www.isthe.com/chongo/tech/astro/venus2004.html"), 0x614ab44e02b53e01);
assert_eq!(fnv1a(b"http://www.isthe.com/chongo/tech/astro/vita.html"), 0xfa6472eb6eef3290);
assert_eq!(fnv1a(b"http://www.isthe.com/chongo/tech/comp/c/expert.html"), 0x9e5d75eb1948eb6a);
assert_eq!(fnv1a(b"http://www.isthe.com/chongo/tech/comp/calc/index.html"), 0xb6d12ad4a8671852);
assert_eq!(fnv1a(b"http://www.isthe.com/chongo/tech/comp/fnv/index.html"), 0x88826f56eba07af1);
assert_eq!(fnv1a(b"http://www.isthe.com/chongo/tech/math/number/howhigh.html"), 0x44535bf2645bc0fd);
assert_eq!(fnv1a(b"http://www.isthe.com/chongo/tech/math/number/number.html"), 0x169388ffc21e3728);
assert_eq!(fnv1a(b"http://www.isthe.com/chongo/tech/math/prime/mersenne.html"), 0xf68aac9e396d8224);
assert_eq!(fnv1a(b"http://www.isthe.com/chongo/tech/math/prime/mersenne.html#largest"), 0x8e87d7e7472b3883);
assert_eq!(fnv1a(b"http://www.lavarnd.org/cgi-bin/corpspeak.cgi"), 0x295c26caa8b423de);
assert_eq!(fnv1a(b"http://www.lavarnd.org/cgi-bin/haiku.cgi"), 0x322c814292e72176);
assert_eq!(fnv1a(b"http://www.lavarnd.org/cgi-bin/rand-none.cgi"), 0x8a06550eb8af7268);
assert_eq!(fnv1a(b"http://www.lavarnd.org/cgi-bin/randdist.cgi"), 0xef86d60e661bcf71);
assert_eq!(fnv1a(b"http://www.lavarnd.org/index.html"), 0x9e5426c87f30ee54);
assert_eq!(fnv1a(b"http://www.lavarnd.org/what/nist-test.html"), 0xf1ea8aa826fd047e);
assert_eq!(
fnv1a(b"http://www.ioccc.org/index.html"),
0xec5b06a1c5531093
);
assert_eq!(
fnv1a(b"http://www.isthe.com/cgi-bin/number.cgi"),
0x45665a929f9ec5e5
);
assert_eq!(
fnv1a(b"http://www.isthe.com/chongo/bio.html"),
0x8c7609b4a9f10907
);
assert_eq!(
fnv1a(b"http://www.isthe.com/chongo/index.html"),
0x89aac3a491f0d729
);
assert_eq!(
fnv1a(b"http://www.isthe.com/chongo/src/calc/lucas-calc"),
0x32ce6b26e0f4a403
);
assert_eq!(
fnv1a(b"http://www.isthe.com/chongo/tech/astro/venus2004.html"),
0x614ab44e02b53e01
);
assert_eq!(
fnv1a(b"http://www.isthe.com/chongo/tech/astro/vita.html"),
0xfa6472eb6eef3290
);
assert_eq!(
fnv1a(b"http://www.isthe.com/chongo/tech/comp/c/expert.html"),
0x9e5d75eb1948eb6a
);
assert_eq!(
fnv1a(b"http://www.isthe.com/chongo/tech/comp/calc/index.html"),
0xb6d12ad4a8671852
);
assert_eq!(
fnv1a(b"http://www.isthe.com/chongo/tech/comp/fnv/index.html"),
0x88826f56eba07af1
);
assert_eq!(
fnv1a(b"http://www.isthe.com/chongo/tech/math/number/howhigh.html"),
0x44535bf2645bc0fd
);
assert_eq!(
fnv1a(b"http://www.isthe.com/chongo/tech/math/number/number.html"),
0x169388ffc21e3728
);
assert_eq!(
fnv1a(b"http://www.isthe.com/chongo/tech/math/prime/mersenne.html"),
0xf68aac9e396d8224
);
assert_eq!(
fnv1a(b"http://www.isthe.com/chongo/tech/math/prime/mersenne.html#largest"),
0x8e87d7e7472b3883
);
assert_eq!(
fnv1a(b"http://www.lavarnd.org/cgi-bin/corpspeak.cgi"),
0x295c26caa8b423de
);
assert_eq!(
fnv1a(b"http://www.lavarnd.org/cgi-bin/haiku.cgi"),
0x322c814292e72176
);
assert_eq!(
fnv1a(b"http://www.lavarnd.org/cgi-bin/rand-none.cgi"),
0x8a06550eb8af7268
);
assert_eq!(
fnv1a(b"http://www.lavarnd.org/cgi-bin/randdist.cgi"),
0xef86d60e661bcf71
);
assert_eq!(
fnv1a(b"http://www.lavarnd.org/index.html"),
0x9e5426c87f30ee54
);
assert_eq!(
fnv1a(b"http://www.lavarnd.org/what/nist-test.html"),
0xf1ea8aa826fd047e
);
assert_eq!(fnv1a(b"http://www.macosxhints.com/"), 0x0babaf9a642cb769);
assert_eq!(fnv1a(b"http://www.mellis.com/"), 0x4b3341d4068d012e);
assert_eq!(fnv1a(b"http://www.nature.nps.gov/air/webcams/parks/havoso2alert/havoalert.cfm"), 0xd15605cbc30a335c);
assert_eq!(fnv1a(b"http://www.nature.nps.gov/air/webcams/parks/havoso2alert/timelines_24.cfm"), 0x5b21060aed8412e5);
assert_eq!(
fnv1a(b"http://www.nature.nps.gov/air/webcams/parks/havoso2alert/havoalert.cfm"),
0xd15605cbc30a335c
);
assert_eq!(
fnv1a(b"http://www.nature.nps.gov/air/webcams/parks/havoso2alert/timelines_24.cfm"),
0x5b21060aed8412e5
);
assert_eq!(fnv1a(b"http://www.paulnoll.com/"), 0x45e2cda1ce6f4227);
assert_eq!(fnv1a(b"http://www.pepysdiary.com/"), 0x50ae3745033ad7d4);
assert_eq!(fnv1a(b"http://www.sciencenews.org/index/home/activity/view"), 0xaa4588ced46bf414);
assert_eq!(fnv1a(b"http://www.skyandtelescope.com/"), 0xc1b0056c4a95467e);
assert_eq!(fnv1a(b"http://www.sput.nl/~rob/sirius.html"), 0x56576a71de8b4089);
assert_eq!(
fnv1a(b"http://www.sciencenews.org/index/home/activity/view"),
0xaa4588ced46bf414
);
assert_eq!(
fnv1a(b"http://www.skyandtelescope.com/"),
0xc1b0056c4a95467e
);
assert_eq!(
fnv1a(b"http://www.sput.nl/~rob/sirius.html"),
0x56576a71de8b4089
);
assert_eq!(fnv1a(b"http://www.systemexperts.com/"), 0xbf20965fa6dc927e);
assert_eq!(fnv1a(b"http://www.tq-international.com/phpBB3/index.php"), 0x569f8383c2040882);
assert_eq!(fnv1a(b"http://www.travelquesttours.com/index.htm"), 0xe1e772fba08feca0);
assert_eq!(fnv1a(b"http://www.wunderground.com/global/stations/89606.html"), 0x4ced94af97138ac4);
assert_eq!(
fnv1a(b"http://www.tq-international.com/phpBB3/index.php"),
0x569f8383c2040882
);
assert_eq!(
fnv1a(b"http://www.travelquesttours.com/index.htm"),
0xe1e772fba08feca0
);
assert_eq!(
fnv1a(b"http://www.wunderground.com/global/stations/89606.html"),
0x4ced94af97138ac4
);
assert_eq!(fnv1a(&repeat_10(b"21701")), 0xc4112ffb337a82fb);
assert_eq!(fnv1a(&repeat_10(b"M21701")), 0xd64a4fd41de38b7d);
assert_eq!(fnv1a(&repeat_10(b"2^21701-1")), 0x4cfc32329edebcbb);
Expand All @@ -368,15 +500,30 @@ mod test {
assert_eq!(fnv1a(&repeat_10(b"\xa9\x5a")), 0x5d5cfce63359ab19);
assert_eq!(fnv1a(&repeat_10(b"391581216093")), 0x33b96c3cd65b5f71);
assert_eq!(fnv1a(&repeat_10(b"391581*2^216093-1")), 0xd845097780602bb9);
assert_eq!(fnv1a(&repeat_10(b"\x05\xf9\x9d\x03\x4c\x81")), 0x84d47645d02da3d5);
assert_eq!(
fnv1a(&repeat_10(b"\x05\xf9\x9d\x03\x4c\x81")),
0x84d47645d02da3d5
);
assert_eq!(fnv1a(&repeat_10(b"FEDCBA9876543210")), 0x83544f33b58773a5);
assert_eq!(fnv1a(&repeat_10(b"\xfe\xdc\xba\x98\x76\x54\x32\x10")), 0x9175cbb2160836c5);
assert_eq!(
fnv1a(&repeat_10(b"\xfe\xdc\xba\x98\x76\x54\x32\x10")),
0x9175cbb2160836c5
);
assert_eq!(fnv1a(&repeat_10(b"EFCDAB8967452301")), 0xc71b3bc175e72bc5);
assert_eq!(fnv1a(&repeat_10(b"\xef\xcd\xab\x89\x67\x45\x23\x01")), 0x636806ac222ec985);
assert_eq!(
fnv1a(&repeat_10(b"\xef\xcd\xab\x89\x67\x45\x23\x01")),
0x636806ac222ec985
);
assert_eq!(fnv1a(&repeat_10(b"0123456789ABCDEF")), 0xb6ef0e6950f52ed5);
assert_eq!(fnv1a(&repeat_10(b"\x01\x23\x45\x67\x89\xab\xcd\xef")), 0xead3d8a0f3dfdaa5);
assert_eq!(
fnv1a(&repeat_10(b"\x01\x23\x45\x67\x89\xab\xcd\xef")),
0xead3d8a0f3dfdaa5
);
assert_eq!(fnv1a(&repeat_10(b"1032547698BADCFE")), 0x922908fe9a861ba5);
assert_eq!(fnv1a(&repeat_10(b"\x10\x32\x54\x76\x98\xba\xdc\xfe")), 0x6d4821de275fd5c5);
assert_eq!(
fnv1a(&repeat_10(b"\x10\x32\x54\x76\x98\xba\xdc\xfe")),
0x6d4821de275fd5c5
);
assert_eq!(fnv1a(&repeat_500(b"\x00")), 0x1fe3fce62bd816b5);
assert_eq!(fnv1a(&repeat_500(b"\x07")), 0xc23e9fccd6f70591);
assert_eq!(fnv1a(&repeat_500(b"~")), 0xc1af12bdfe16b5b5);
Expand Down

0 comments on commit 62fbdc8

Please sign in to comment.