From d807f00bfded3804d5f3d01e522dfa086871f8ed Mon Sep 17 00:00:00 2001 From: Samuel Chase Date: Sat, 11 May 2013 02:14:36 +0530 Subject: [PATCH 1/9] Add to_str() for HashMaps, and some basic tests as well. --- src/libcore/to_str.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/libcore/to_str.rs b/src/libcore/to_str.rs index 7f8e6915add16..a2c4ac991a49e 100644 --- a/src/libcore/to_str.rs +++ b/src/libcore/to_str.rs @@ -15,6 +15,10 @@ The `ToStr` trait for converting to strings */ use str; +use hashmap::HashMap; +use container::Map; +use hash::Hash; +use cmp::Eq; pub trait ToStr { fn to_str(&self) -> ~str; @@ -46,6 +50,26 @@ impl ToStr for (A,) { } } +impl ToStr for HashMap { + #[inline(always)] + fn to_str(&self) -> ~str { + let mut acc = ~"{", first = true; + for self.each |key, value| { + if first { + first = false; + } + else { + str::push_str(&mut acc, ~", "); + } + str::push_str(&mut acc, key.to_str()); + str::push_str(&mut acc, ~" : "); + str::push_str(&mut acc, value.to_str()); + } + str::push_char(&mut acc, '}'); + acc + } +} + impl ToStr for (A, B) { #[inline(always)] fn to_str(&self) -> ~str { @@ -149,4 +173,16 @@ mod tests { assert!((~[~[], ~[1], ~[1, 1]]).to_str() == ~"[[], [1], [1, 1]]"); } + + #[test] + fn test_hashmap() { + let mut table: HashMap = HashMap::new(); + let mut empty: HashMap = HashMap::new(); + + table.insert(3, 4); + table.insert(1, 2); + + assert!(table.to_str() == ~"{1 : 2, 3 : 4}"); + assert!(empty.to_str() == ~"{}"); + } } From fd5a3520d5fbb009b2a5de8cf5ed164f28418cb1 Mon Sep 17 00:00:00 2001 From: Samuel Chase Date: Sat, 11 May 2013 04:00:00 +0530 Subject: [PATCH 2/9] Removed test_hashmap() for the time, being. All tests pass with make check. --- src/libcore/to_str.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/libcore/to_str.rs b/src/libcore/to_str.rs index a2c4ac991a49e..d3e3d8348b1da 100644 --- a/src/libcore/to_str.rs +++ b/src/libcore/to_str.rs @@ -144,6 +144,7 @@ impl ToStr for @[A] { #[cfg(test)] #[allow(non_implicitly_copyable_typarams)] mod tests { + use hashmap::HashMap; #[test] fn test_simple_types() { assert!(1i.to_str() == ~"1"); @@ -174,15 +175,15 @@ mod tests { ~"[[], [1], [1, 1]]"); } - #[test] - fn test_hashmap() { - let mut table: HashMap = HashMap::new(); - let mut empty: HashMap = HashMap::new(); + // #[test] + // fn test_hashmap() { + // let mut table: HashMap = HashMap::new(); + // let mut empty: HashMap = HashMap::new(); - table.insert(3, 4); - table.insert(1, 2); + // table.insert(3, 4); + // table.insert(1, 2); - assert!(table.to_str() == ~"{1 : 2, 3 : 4}"); - assert!(empty.to_str() == ~"{}"); - } -} + // assert!(table.to_str() == ~"{1 : 2, 3 : 4}"); + // assert!(empty.to_str() == ~"{}"); + //} +} \ No newline at end of file From 3c1e78788423f6036d370c4ea37718f3494868e7 Mon Sep 17 00:00:00 2001 From: Samuel Chase Date: Sat, 11 May 2013 17:40:52 +0530 Subject: [PATCH 3/9] All tests, including newly added test_hashmap() pass. The empty Hash Table doesn't need to be mutable. --- src/libcore/to_str.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/libcore/to_str.rs b/src/libcore/to_str.rs index d3e3d8348b1da..a54bb7e94a2b6 100644 --- a/src/libcore/to_str.rs +++ b/src/libcore/to_str.rs @@ -175,15 +175,17 @@ mod tests { ~"[[], [1], [1, 1]]"); } - // #[test] - // fn test_hashmap() { - // let mut table: HashMap = HashMap::new(); - // let mut empty: HashMap = HashMap::new(); + #[test] + fn test_hashmap() { + let mut table: HashMap = HashMap::new(); + let empty: HashMap = HashMap::new(); + + table.insert(3, 4); + table.insert(1, 2); - // table.insert(3, 4); - // table.insert(1, 2); + let table_str = table.to_str(); - // assert!(table.to_str() == ~"{1 : 2, 3 : 4}"); - // assert!(empty.to_str() == ~"{}"); - //} + assert!(table_str == ~"{1 : 2, 3 : 4}" || table_str == ~"{3 : 4, 1 : 2}"); + assert!(empty.to_str() == ~"{}"); + } } \ No newline at end of file From b8d0ebe124a574a55d9df7daf2b704802443aa2c Mon Sep 17 00:00:00 2001 From: Samuel Chase Date: Sat, 11 May 2013 19:09:11 +0530 Subject: [PATCH 4/9] Remove extra space between key and value. 1. Extra space removed. 2. Using acc.push_str() instead of str::push_str 3. Update test to reflect representation change. --- src/libcore/to_str.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/libcore/to_str.rs b/src/libcore/to_str.rs index a54bb7e94a2b6..3806335e2408b 100644 --- a/src/libcore/to_str.rs +++ b/src/libcore/to_str.rs @@ -15,7 +15,9 @@ The `ToStr` trait for converting to strings */ use str; +use str::OwnedStr; use hashmap::HashMap; +use hashmap::HashSet; use container::Map; use hash::Hash; use cmp::Eq; @@ -59,13 +61,13 @@ impl ToStr for HashMap { first = false; } else { - str::push_str(&mut acc, ~", "); + acc.push_str(", "); } - str::push_str(&mut acc, key.to_str()); - str::push_str(&mut acc, ~" : "); - str::push_str(&mut acc, value.to_str()); + acc.push_str(key.to_str()); + acc.push_str(": "); + acc.push_str(value.to_str()); } - str::push_char(&mut acc, '}'); + acc.push_char('}'); acc } } @@ -82,6 +84,7 @@ impl ToStr for (A, B) { } } } + impl ToStr for (A, B, C) { #[inline(always)] fn to_str(&self) -> ~str { @@ -185,7 +188,7 @@ mod tests { let table_str = table.to_str(); - assert!(table_str == ~"{1 : 2, 3 : 4}" || table_str == ~"{3 : 4, 1 : 2}"); + assert!(table_str == ~"{1: 2, 3: 4}" || table_str == ~"{3: 4, 1: 2}"); assert!(empty.to_str() == ~"{}"); } } \ No newline at end of file From e2c73ccaf22ec3908cf77e604233c5833ac5ae7d Mon Sep 17 00:00:00 2001 From: Samuel Chase Date: Sat, 11 May 2013 19:18:14 +0530 Subject: [PATCH 5/9] Add str representation for HashSet. --- src/libcore/to_str.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/libcore/to_str.rs b/src/libcore/to_str.rs index 3806335e2408b..299ba45986e61 100644 --- a/src/libcore/to_str.rs +++ b/src/libcore/to_str.rs @@ -72,6 +72,24 @@ impl ToStr for HashMap { } } +impl ToStr for HashSet { + #[inline(always)] + fn to_str(&self) -> ~str { + let mut acc = ~"{", first = true; + for self.each |element| { + if first { + first = false; + } + else { + acc.push_str(", "); + } + acc.push_str(element.to_str()); + } + acc.push_char('}'); + acc + } +} + impl ToStr for (A, B) { #[inline(always)] fn to_str(&self) -> ~str { From 9ed9e8c8d0ff0502697e715c2cd0cedd3323d423 Mon Sep 17 00:00:00 2001 From: Samuel Chase Date: Sat, 11 May 2013 19:25:14 +0530 Subject: [PATCH 6/9] Add test: test_hashset() --- src/libcore/to_str.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/libcore/to_str.rs b/src/libcore/to_str.rs index 299ba45986e61..be0aa5138c7bc 100644 --- a/src/libcore/to_str.rs +++ b/src/libcore/to_str.rs @@ -209,4 +209,18 @@ mod tests { assert!(table_str == ~"{1: 2, 3: 4}" || table_str == ~"{3: 4, 1: 2}"); assert!(empty.to_str() == ~"{}"); } + + #[test] + fn test_hashset() { + let mut set: HashSet = HashSet::new(); + let empty_set: HashSet = HashSet::new(); + + set.insert(1); + set.insert(2); + + let set_str = set.to_str(); + + assert!(set_str == ~"{1, 2}" || set_str == ~"{2, 1}"); + assert!(empty.to_str() == ~"{}"); + } } \ No newline at end of file From 7d43b12c323b07d3f3d17738d808071b66519c58 Mon Sep 17 00:00:00 2001 From: Samuel Chase Date: Sat, 11 May 2013 19:31:53 +0530 Subject: [PATCH 7/9] Use acc.push_str() instead of str::push_str(..) Also added some whitespace to enhance readabilty. --- src/libcore/to_str.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/libcore/to_str.rs b/src/libcore/to_str.rs index be0aa5138c7bc..3c75cbaec15cd 100644 --- a/src/libcore/to_str.rs +++ b/src/libcore/to_str.rs @@ -125,11 +125,15 @@ impl<'self,A:ToStr> ToStr for &'self [A] { fn to_str(&self) -> ~str { let mut acc = ~"[", first = true; for self.each |elt| { - if first { first = false; } - else { str::push_str(&mut acc, ~", "); } - str::push_str(&mut acc, elt.to_str()); + if first { + first = false; + } + else { + acc.push_str(", "); + } + acc.push_str(elt.to_str()); } - str::push_char(&mut acc, ']'); + acc.push_char(']'); acc } } @@ -139,11 +143,15 @@ impl ToStr for ~[A] { fn to_str(&self) -> ~str { let mut acc = ~"[", first = true; for self.each |elt| { - if first { first = false; } - else { str::push_str(&mut acc, ~", "); } - str::push_str(&mut acc, elt.to_str()); + if first { + first = false; + } + else { + acc.push_str(", "); + } + acc.push_str(elt.to_str()); } - str::push_char(&mut acc, ']'); + acc.push_char(']'); acc } } @@ -153,11 +161,15 @@ impl ToStr for @[A] { fn to_str(&self) -> ~str { let mut acc = ~"[", first = true; for self.each |elt| { - if first { first = false; } - else { str::push_str(&mut acc, ~", "); } - str::push_str(&mut acc, elt.to_str()); + if first { + first = false; + } + else { + acc.push_str(", "); + } + acc.push_str(elt.to_str()); } - str::push_char(&mut acc, ']'); + acc.push_char(']'); acc } } From 37bea2a7d07b8a42d9502b7318cf9317ad3680b1 Mon Sep 17 00:00:00 2001 From: Samuel Chase Date: Sat, 11 May 2013 20:35:44 +0530 Subject: [PATCH 8/9] Fix errors in test_hashset(). All tests now pass. --- src/libcore/to_str.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libcore/to_str.rs b/src/libcore/to_str.rs index 3c75cbaec15cd..08e72e8fe8624 100644 --- a/src/libcore/to_str.rs +++ b/src/libcore/to_str.rs @@ -178,6 +178,7 @@ impl ToStr for @[A] { #[allow(non_implicitly_copyable_typarams)] mod tests { use hashmap::HashMap; + use hashmap::HashSet; #[test] fn test_simple_types() { assert!(1i.to_str() == ~"1"); @@ -224,8 +225,8 @@ mod tests { #[test] fn test_hashset() { - let mut set: HashSet = HashSet::new(); - let empty_set: HashSet = HashSet::new(); + let mut set: HashSet = HashSet::new(); + let empty_set: HashSet = HashSet::new(); set.insert(1); set.insert(2); @@ -233,6 +234,6 @@ mod tests { let set_str = set.to_str(); assert!(set_str == ~"{1, 2}" || set_str == ~"{2, 1}"); - assert!(empty.to_str() == ~"{}"); + assert!(empty_set.to_str() == ~"{}"); } } \ No newline at end of file From 0acb6abb86125a1db878c256fbcf1a94b2577feb Mon Sep 17 00:00:00 2001 From: Samuel Chase Date: Sun, 12 May 2013 21:02:12 +0530 Subject: [PATCH 9/9] Add use declaration for container::Set All tests pass now. --- src/libcore/to_str.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/to_str.rs b/src/libcore/to_str.rs index 08e72e8fe8624..58a9f768644bd 100644 --- a/src/libcore/to_str.rs +++ b/src/libcore/to_str.rs @@ -14,7 +14,6 @@ The `ToStr` trait for converting to strings */ -use str; use str::OwnedStr; use hashmap::HashMap; use hashmap::HashSet; @@ -179,6 +178,7 @@ impl ToStr for @[A] { mod tests { use hashmap::HashMap; use hashmap::HashSet; + use container::Set; #[test] fn test_simple_types() { assert!(1i.to_str() == ~"1");