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.
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.
11 extern crate collections;
13 use std::collections::{ Map, MutableMap};
14 use std::str::{SendStr, Owned, Slice};
15 use std::to_string::ToString;
16 use self::collections::TreeMap;
17 use std::option::Some;
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));
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));
33 assert_eq!(map.find(&Owned("foo".to_string())), Some(&v));
34 assert_eq!(map.find(&Slice("foo")), Some(&v));
36 let (a, b, c, d) = (50, 51, 52, 53);
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));
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));
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));
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));
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));
63 assert!(map.pop(&Slice("foo")).is_some());
64 assert_eq!(map.move_iter().map(|(k, v)| format!("{}{}", k, v))
65 .collect::<Vec<String>>()
67 "abc50bcd51cde52def53".to_string());