]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/send_str_treemap.rs
f3a730aa2b39589b823994ebd3c6ab321ff183f7
[rust.git] / src / test / run-pass / send_str_treemap.rs
1 // Copyright 2013 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 extern crate collections;
12
13 use std::collections::{ Map, MutableMap};
14 use std::str::{SendStr, Owned, Slice};
15 use std::to_str::ToString;
16 use self::collections::TreeMap;
17 use std::option::Some;
18
19 pub fn main() {
20     let mut map: TreeMap<SendStr, uint> = TreeMap::new();
21     assert!(map.insert(Slice("foo"), 42));
22     assert!(!map.insert(Owned("foo".to_string()), 42));
23     assert!(!map.insert(Slice("foo"), 42));
24     assert!(!map.insert(Owned("foo".to_string()), 42));
25
26     assert!(!map.insert(Slice("foo"), 43));
27     assert!(!map.insert(Owned("foo".to_string()), 44));
28     assert!(!map.insert(Slice("foo"), 45));
29     assert!(!map.insert(Owned("foo".to_string()), 46));
30
31     let v = 46;
32
33     assert_eq!(map.find(&Owned("foo".to_string())), Some(&v));
34     assert_eq!(map.find(&Slice("foo")), Some(&v));
35
36     let (a, b, c, d) = (50, 51, 52, 53);
37
38     assert!(map.insert(Slice("abc"), a));
39     assert!(map.insert(Owned("bcd".to_string()), b));
40     assert!(map.insert(Slice("cde"), c));
41     assert!(map.insert(Owned("def".to_string()), d));
42
43     assert!(!map.insert(Slice("abc"), a));
44     assert!(!map.insert(Owned("bcd".to_string()), b));
45     assert!(!map.insert(Slice("cde"), c));
46     assert!(!map.insert(Owned("def".to_string()), d));
47
48     assert!(!map.insert(Owned("abc".to_string()), a));
49     assert!(!map.insert(Slice("bcd"), b));
50     assert!(!map.insert(Owned("cde".to_string()), c));
51     assert!(!map.insert(Slice("def"), d));
52
53     assert_eq!(map.find(&Slice("abc")), Some(&a));
54     assert_eq!(map.find(&Slice("bcd")), Some(&b));
55     assert_eq!(map.find(&Slice("cde")), Some(&c));
56     assert_eq!(map.find(&Slice("def")), Some(&d));
57
58     assert_eq!(map.find(&Owned("abc".to_string())), Some(&a));
59     assert_eq!(map.find(&Owned("bcd".to_string())), Some(&b));
60     assert_eq!(map.find(&Owned("cde".to_string())), Some(&c));
61     assert_eq!(map.find(&Owned("def".to_string())), Some(&d));
62
63     assert!(map.pop(&Slice("foo")).is_some());
64     assert_eq!(map.move_iter().map(|(k, v)| format!("{}{}", k, v))
65                               .collect::<Vec<String>>()
66                               .concat(),
67                "abc50bcd51cde52def53".to_string());
68 }