about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAshley Williams <ashley666ashley@gmail.com>2016-07-27 10:47:19 -0700
committerAshley Williams <ashley666ashley@gmail.com>2016-09-21 14:38:26 +0200
commit3d8d55787b2c0f5b1aba01b08da13bf0d612818c (patch)
treea87b495fd37b6bcff42898432ca45bf54d7cd90b
parentc772948b687488a087356cb91432425662e034b9 (diff)
downloadrust-3d8d55787b2c0f5b1aba01b08da13bf0d612818c.tar.gz
rust-3d8d55787b2c0f5b1aba01b08da13bf0d612818c.zip
add assert_ne and debug_assert_ne macros
-rw-r--r--src/libcore/macros.rs63
-rw-r--r--src/libstd/lib.rs4
-rw-r--r--src/test/run-pass/assert-ne-macro-success.rs22
-rw-r--r--src/test/run-pass/assert-ne-macro-unsized.rs13
4 files changed, 100 insertions, 2 deletions
diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs
index f29a49dd5fe..99c24e4c48d 100644
--- a/src/libcore/macros.rs
+++ b/src/libcore/macros.rs
@@ -119,6 +119,44 @@ macro_rules! assert_eq {
     });
 }
 
+/// Asserts that two expressions are not equal to each other.
+///
+/// On panic, this macro will print the values of the expressions with their
+/// debug representations.
+///
+/// # Examples
+///
+/// ```
+/// let a = 3;
+/// let b = 2;
+/// assert_ne!(a, b);
+/// ```
+#[macro_export]
+#[stable(feature = "assert_ne", since = "1.12.0")]
+macro_rules! assert_ne {
+    ($left:expr , $right:expr) => ({
+        match (&$left, &$right) {
+            (left_val, right_val) => {
+                if *left_val == *right_val {
+                    panic!("assertion failed: `(left != right)` \
+                           (left: `{:?}`, right: `{:?}`)", left_val, right_val)
+                }
+            }
+        }
+    });
+    ($left:expr , $right:expr, $($arg:tt)*) => ({
+        match (&($left), &($right)) {
+            (left_val, right_val) => {
+                if *left_val == *right_val {
+                    panic!("assertion failed: `(left != right)` \
+                           (left: `{:?}`, right: `{:?}`): {}", left_val, right_val,
+                           format_args!($($arg)*))
+                }
+            }
+        }
+    });
+}
+
 /// Ensure that a boolean expression is `true` at runtime.
 ///
 /// This will invoke the `panic!` macro if the provided expression cannot be
@@ -189,6 +227,31 @@ macro_rules! debug_assert_eq {
     ($($arg:tt)*) => (if cfg!(debug_assertions) { assert_eq!($($arg)*); })
 }
 
+/// Asserts that two expressions are not equal to each other.
+///
+/// On panic, this macro will print the values of the expressions with their
+/// debug representations.
+///
+/// Unlike `assert_ne!`, `debug_assert_ne!` statements are only enabled in non
+/// optimized builds by default. An optimized build will omit all
+/// `debug_assert_ne!` statements unless `-C debug-assertions` is passed to the
+/// compiler. This makes `debug_assert_ne!` useful for checks that are too
+/// expensive to be present in a release build but may be helpful during
+/// development.
+///
+/// # Examples
+///
+/// ```
+/// let a = 3;
+/// let b = 2;
+/// debug_assert_ne!(a, b);
+/// ```
+#[macro_export]
+#[stable(feature = "assert_ne", since = "1.12.0")]
+macro_rules! debug_assert_ne {
+    ($($arg:tt)*) => (if cfg!(debug_assertions) { assert_ne!($($arg)*); })
+}
+
 /// Helper macro for reducing boilerplate code for matching `Result` together
 /// with converting downstream errors.
 ///
diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs
index 115a24fc83c..912045453e0 100644
--- a/src/libstd/lib.rs
+++ b/src/libstd/lib.rs
@@ -304,8 +304,8 @@ use prelude::v1::*;
 // We want to reexport a few macros from core but libcore has already been
 // imported by the compiler (via our #[no_std] attribute) In this case we just
 // add a new crate name so we can attach the reexports to it.
-#[macro_reexport(assert, assert_eq, debug_assert, debug_assert_eq,
-                 unreachable, unimplemented, write, writeln, try)]
+#[macro_reexport(assert, assert_eq, assert_ne, debug_assert, debug_assert_eq,
+                 debug_assert_ne, unreachable, unimplemented, write, writeln, try)]
 extern crate core as __core;
 
 #[macro_use]
diff --git a/src/test/run-pass/assert-ne-macro-success.rs b/src/test/run-pass/assert-ne-macro-success.rs
new file mode 100644
index 00000000000..ce671d2f1b5
--- /dev/null
+++ b/src/test/run-pass/assert-ne-macro-success.rs
@@ -0,0 +1,22 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[derive(PartialEq, Debug)]
+struct Point { x : isize }
+
+pub fn main() {
+    assert_ne!(666,14);
+    assert_ne!("666".to_string(),"abc".to_string());
+    assert_ne!(Box::new(Point{x:666}),Box::new(Point{x:34}));
+    assert_ne!(&Point{x:666},&Point{x:34});
+    assert_ne!(666, 42, "no gods no masters");
+    assert_ne!(666, 42, "6 {} 6", "6");
+    assert_ne!(666, 42, "{x}, {y}, {z}", x = 6, y = 6, z = 6);
+}
diff --git a/src/test/run-pass/assert-ne-macro-unsized.rs b/src/test/run-pass/assert-ne-macro-unsized.rs
new file mode 100644
index 00000000000..eeac0b6f9f6
--- /dev/null
+++ b/src/test/run-pass/assert-ne-macro-unsized.rs
@@ -0,0 +1,13 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+pub fn main() {
+    assert_ne!([6, 6, 6][..], vec![1, 2, 3][..]);
+}