]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
c1a9b12d SL |
11 | mod sip; |
12 | ||
85aaf69f | 13 | use std::hash::{Hash, Hasher}; |
1a4d82fc JJ |
14 | use std::default::Default; |
15 | ||
16 | struct MyHasher { | |
17 | hash: u64, | |
18 | } | |
19 | ||
20 | impl Default for MyHasher { | |
21 | fn default() -> MyHasher { | |
22 | MyHasher { hash: 0 } | |
23 | } | |
24 | } | |
25 | ||
85aaf69f | 26 | impl Hasher for MyHasher { |
1a4d82fc | 27 | fn write(&mut self, buf: &[u8]) { |
85aaf69f | 28 | for byte in buf { |
1a4d82fc JJ |
29 | self.hash += *byte as u64; |
30 | } | |
31 | } | |
1a4d82fc JJ |
32 | fn finish(&self) -> u64 { self.hash } |
33 | } | |
34 | ||
35 | ||
36 | #[test] | |
37 | fn test_writer_hasher() { | |
85aaf69f | 38 | fn hash<T: Hash>(t: &T) -> u64 { |
e9174d1e SL |
39 | let mut s = MyHasher { hash: 0 }; |
40 | t.hash(&mut s); | |
41 | s.finish() | |
1a4d82fc JJ |
42 | } |
43 | ||
44 | assert_eq!(hash(&()), 0); | |
45 | ||
85aaf69f SL |
46 | assert_eq!(hash(&5_u8), 5); |
47 | assert_eq!(hash(&5_u16), 5); | |
48 | assert_eq!(hash(&5_u32), 5); | |
49 | assert_eq!(hash(&5_u64), 5); | |
50 | assert_eq!(hash(&5_usize), 5); | |
1a4d82fc | 51 | |
85aaf69f SL |
52 | assert_eq!(hash(&5_i8), 5); |
53 | assert_eq!(hash(&5_i16), 5); | |
54 | assert_eq!(hash(&5_i32), 5); | |
55 | assert_eq!(hash(&5_i64), 5); | |
56 | assert_eq!(hash(&5_isize), 5); | |
1a4d82fc JJ |
57 | |
58 | assert_eq!(hash(&false), 0); | |
59 | assert_eq!(hash(&true), 1); | |
60 | ||
61 | assert_eq!(hash(&'a'), 97); | |
62 | ||
63 | let s: &str = "a"; | |
64 | assert_eq!(hash(& s), 97 + 0xFF); | |
65 | // FIXME (#18283) Enable test | |
66 | //let s: Box<str> = box "a"; | |
67 | //assert_eq!(hasher.hash(& s), 97 + 0xFF); | |
c34b1796 | 68 | let cs: &[u8] = &[1, 2, 3]; |
1a4d82fc | 69 | assert_eq!(hash(& cs), 9); |
c34b1796 AL |
70 | // FIXME (#22405): Replace `Box::new` with `box` here when/if possible. |
71 | let cs: Box<[u8]> = Box::new([1, 2, 3]); | |
1a4d82fc JJ |
72 | assert_eq!(hash(& cs), 9); |
73 | ||
74 | // FIXME (#18248) Add tests for hashing Rc<str> and Rc<[T]> | |
75 | ||
e9174d1e SL |
76 | let ptr = 5_usize as *const i32; |
77 | assert_eq!(hash(&ptr), 5); | |
1a4d82fc | 78 | |
e9174d1e SL |
79 | let ptr = 5_usize as *mut i32; |
80 | assert_eq!(hash(&ptr), 5); | |
1a4d82fc JJ |
81 | } |
82 | ||
83 | struct Custom { hash: u64 } | |
84 | struct CustomHasher { output: u64 } | |
85 | ||
86 | impl Hasher for CustomHasher { | |
1a4d82fc | 87 | fn finish(&self) -> u64 { self.output } |
c34b1796 | 88 | fn write(&mut self, _: &[u8]) { panic!() } |
85aaf69f | 89 | fn write_u64(&mut self, data: u64) { self.output = data; } |
1a4d82fc JJ |
90 | } |
91 | ||
92 | impl Default for CustomHasher { | |
93 | fn default() -> CustomHasher { | |
94 | CustomHasher { output: 0 } | |
95 | } | |
96 | } | |
97 | ||
85aaf69f SL |
98 | impl Hash for Custom { |
99 | fn hash<H: Hasher>(&self, state: &mut H) { | |
100 | state.write_u64(self.hash); | |
1a4d82fc JJ |
101 | } |
102 | } | |
103 | ||
104 | #[test] | |
105 | fn test_custom_state() { | |
85aaf69f | 106 | fn hash<T: Hash>(t: &T) -> u64 { |
e9174d1e SL |
107 | let mut c = CustomHasher { output: 0 }; |
108 | t.hash(&mut c); | |
109 | c.finish() | |
1a4d82fc JJ |
110 | } |
111 | ||
112 | assert_eq!(hash(&Custom { hash: 5 }), 5); | |
113 | } |