From 02882fbd7edcb8d0d152afcdc8571216efcbd664 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 28 Feb 2014 01:23:06 -0800 Subject: std: Change assert_eq!() to use {} instead of {:?} Formatting via reflection has been a little questionable for some time now, and it's a little unfortunate that one of the standard macros will silently use reflection when you weren't expecting it. This adds small bits of code bloat to libraries, as well as not always being necessary. In light of this information, this commit switches assert_eq!() to using {} in the error message instead of {:?}. In updating existing code, there were a few error cases that I encountered: * It's impossible to define Show for [T, ..N]. I think DST will alleviate this because we can define Show for [T]. * A few types here and there just needed a #[deriving(Show)] * Type parameters needed a Show bound, I often moved this to `assert!(a == b)` * `Path` doesn't implement `Show`, so assert_eq!() cannot be used on two paths. I don't think this is much of a regression though because {:?} on paths looks awful (it's a byte array). Concretely speaking, this shaved 10K off a 656K binary. Not a lot, but sometime significant for smaller binaries. --- src/libcollections/bitv.rs | 4 ++-- src/libcollections/dlist.rs | 4 ++-- src/libcollections/enum_set.rs | 2 +- src/libcollections/hashmap.rs | 4 ++-- src/libcollections/list.rs | 8 ++++---- src/libcollections/lru_cache.rs | 2 +- src/libcollections/ringbuf.rs | 15 ++++++++------- 7 files changed, 20 insertions(+), 19 deletions(-) (limited to 'src/libcollections') diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs index 0e14b28eda3..116bb80d8c0 100644 --- a/src/libcollections/bitv.rs +++ b/src/libcollections/bitv.rs @@ -1542,7 +1542,7 @@ mod tests { let mut b = a.clone(); - assert_eq!(&a, &b); + assert!(a == b); assert!(b.remove(&1)); assert!(a.contains(&1)); @@ -1561,7 +1561,7 @@ mod tests { let mut r = rng(); let mut bitv = 0 as uint; b.iter(|| { - bitv |= (1 << ((r.next_u32() as uint) % uint::BITS)); + bitv |= 1 << ((r.next_u32() as uint) % uint::BITS); &bitv }) } diff --git a/src/libcollections/dlist.rs b/src/libcollections/dlist.rs index 4a4ec01f6d2..6c059d3f40c 100644 --- a/src/libcollections/dlist.rs +++ b/src/libcollections/dlist.rs @@ -982,11 +982,11 @@ mod tests { fn test_eq() { let mut n: DList = list_from([]); let mut m = list_from([]); - assert_eq!(&n, &m); + assert!(n == m); n.push_front(1); assert!(n != m); m.push_back(1); - assert_eq!(&n, &m); + assert!(n == m); let n = list_from([2,3,4]); let m = list_from([1,2,3]); diff --git a/src/libcollections/enum_set.rs b/src/libcollections/enum_set.rs index f33d77ba682..7fda99d8d2c 100644 --- a/src/libcollections/enum_set.rs +++ b/src/libcollections/enum_set.rs @@ -141,7 +141,7 @@ mod test { use enum_set::{EnumSet, CLike}; - #[deriving(Eq)] + #[deriving(Eq, Show)] #[repr(uint)] enum Foo { A, B, C diff --git a/src/libcollections/hashmap.rs b/src/libcollections/hashmap.rs index 0ffe7c31bb8..b4b843289f8 100644 --- a/src/libcollections/hashmap.rs +++ b/src/libcollections/hashmap.rs @@ -1065,7 +1065,7 @@ mod test_map { let mut observed = 0; for (k, v) in m.iter() { assert_eq!(*v, *k * 2); - observed |= (1 << *k); + observed |= 1 << *k; } assert_eq!(observed, 0xFFFF_FFFF); } @@ -1293,7 +1293,7 @@ mod test_set { } let mut observed = 0; for k in a.iter() { - observed |= (1 << *k); + observed |= 1 << *k; } assert_eq!(observed, 0xFFFF_FFFF); } diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 2359b7ec769..18da9671419 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -153,7 +153,7 @@ mod tests { #[test] fn test_from_vec_empty() { let empty : list::List = List::from_vec([]); - assert_eq!(empty, Nil::); + assert!(empty == Nil::); } #[test] @@ -222,8 +222,8 @@ mod tests { #[test] fn test_append() { - assert_eq!(List::from_vec([1, 2, 3, 4]), - List::from_vec([1, 2]).append(List::from_vec([3, 4]))); + assert!(List::from_vec([1, 2, 3, 4]) == + List::from_vec([1, 2]).append(List::from_vec([3, 4]))); } #[test] @@ -232,6 +232,6 @@ mod tests { let new_list = list.unshift(0); assert_eq!(list.len(), 1u); assert_eq!(new_list.len(), 2u); - assert_eq!(new_list, List::from_vec([0, 1])); + assert!(new_list == List::from_vec([0, 1])); } } diff --git a/src/libcollections/lru_cache.rs b/src/libcollections/lru_cache.rs index 68bc5f1b6c4..0aace71813e 100644 --- a/src/libcollections/lru_cache.rs +++ b/src/libcollections/lru_cache.rs @@ -277,7 +277,7 @@ mod tests { fn assert_opt_eq(opt: Option<&V>, v: V) { assert!(opt.is_some()); - assert_eq!(opt.unwrap(), &v); + assert!(opt.unwrap() == &v); } #[test] diff --git a/src/libcollections/ringbuf.rs b/src/libcollections/ringbuf.rs index 4b97bfbd18d..e09bf1023d6 100644 --- a/src/libcollections/ringbuf.rs +++ b/src/libcollections/ringbuf.rs @@ -409,6 +409,7 @@ mod tests { use deque::Deque; use std::clone::Clone; use std::cmp::Eq; + use std::fmt::Show; use super::RingBuf; #[test] @@ -493,7 +494,7 @@ mod tests { } #[cfg(test)] - fn test_parameterized(a: T, b: T, c: T, d: T) { + fn test_parameterized(a: T, b: T, c: T, d: T) { let mut deq = RingBuf::new(); assert_eq!(deq.len(), 0); deq.push_front(a.clone()); @@ -578,21 +579,21 @@ mod tests { }) } - #[deriving(Clone, Eq)] + #[deriving(Clone, Eq, Show)] enum Taggy { One(int), Two(int, int), Three(int, int, int), } - #[deriving(Clone, Eq)] + #[deriving(Clone, Eq, Show)] enum Taggypar { Onepar(int), Twopar(int, int), Threepar(int, int, int), } - #[deriving(Clone, Eq)] + #[deriving(Clone, Eq, Show)] struct RecCy { x: int, y: int, @@ -812,7 +813,7 @@ mod tests { #[test] fn test_eq() { let mut d = RingBuf::new(); - assert_eq!(&d, &RingBuf::with_capacity(0)); + assert!(d == RingBuf::with_capacity(0)); d.push_front(137); d.push_front(17); d.push_front(42); @@ -822,11 +823,11 @@ mod tests { e.push_back(17); e.push_back(137); e.push_back(137); - assert_eq!(&e, &d); + assert!(&e == &d); e.pop_back(); e.push_back(0); assert!(e != d); e.clear(); - assert_eq!(e, RingBuf::new()); + assert!(e == RingBuf::new()); } } -- cgit 1.4.1-3-g733a5