]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/hash/mod.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / libcoretest / hash / mod.rs
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
11 use std::mem;
12 use std::hash::{Hash, Hasher};
13 use std::default::Default;
14
15 struct MyHasher {
16     hash: u64,
17 }
18
19 impl Default for MyHasher {
20     fn default() -> MyHasher {
21         MyHasher { hash: 0 }
22     }
23 }
24
25 impl Hasher for MyHasher {
26     fn write(&mut self, buf: &[u8]) {
27         for byte in buf {
28             self.hash += *byte as u64;
29         }
30     }
31     fn finish(&self) -> u64 { self.hash }
32 }
33
34
35 #[test]
36 fn test_writer_hasher() {
37     fn hash<T: Hash>(t: &T) -> u64 {
38         ::std::hash::hash::<_, MyHasher>(t)
39     }
40
41     assert_eq!(hash(&()), 0);
42
43     assert_eq!(hash(&5_u8), 5);
44     assert_eq!(hash(&5_u16), 5);
45     assert_eq!(hash(&5_u32), 5);
46     assert_eq!(hash(&5_u64), 5);
47     assert_eq!(hash(&5_usize), 5);
48
49     assert_eq!(hash(&5_i8), 5);
50     assert_eq!(hash(&5_i16), 5);
51     assert_eq!(hash(&5_i32), 5);
52     assert_eq!(hash(&5_i64), 5);
53     assert_eq!(hash(&5_isize), 5);
54
55     assert_eq!(hash(&false), 0);
56     assert_eq!(hash(&true), 1);
57
58     assert_eq!(hash(&'a'), 97);
59
60     let s: &str = "a";
61     assert_eq!(hash(& s), 97 + 0xFF);
62     // FIXME (#18283) Enable test
63     //let s: Box<str> = box "a";
64     //assert_eq!(hasher.hash(& s), 97 + 0xFF);
65     let cs: &[u8] = &[1u8, 2u8, 3u8];
66     assert_eq!(hash(& cs), 9);
67     let cs: Box<[u8]> = box [1u8, 2u8, 3u8];
68     assert_eq!(hash(& cs), 9);
69
70     // FIXME (#18248) Add tests for hashing Rc<str> and Rc<[T]>
71
72     unsafe {
73         let ptr: *const i32 = mem::transmute(5_usize);
74         assert_eq!(hash(&ptr), 5);
75     }
76
77     unsafe {
78         let ptr: *mut i32 = mem::transmute(5_usize);
79         assert_eq!(hash(&ptr), 5);
80     }
81 }
82
83 struct Custom { hash: u64 }
84 struct CustomHasher { output: u64 }
85
86 impl Hasher for CustomHasher {
87     fn finish(&self) -> u64 { self.output }
88     fn write(&mut self, data: &[u8]) { panic!() }
89     fn write_u64(&mut self, data: u64) { self.output = data; }
90 }
91
92 impl Default for CustomHasher {
93     fn default() -> CustomHasher {
94         CustomHasher { output: 0 }
95     }
96 }
97
98 impl Hash for Custom {
99     fn hash<H: Hasher>(&self, state: &mut H) {
100         state.write_u64(self.hash);
101     }
102 }
103
104 #[test]
105 fn test_custom_state() {
106     fn hash<T: Hash>(t: &T) -> u64 {
107         ::std::hash::hash::<_, CustomHasher>(t)
108     }
109
110     assert_eq!(hash(&Custom { hash: 5 }), 5);
111 }