1#[macro_export]
19macro_rules! assert_eq_hex {
20 ($left:expr, $right:expr $(,)?) => ({
21 match (&$left, &$right) {
22 (left_val, right_val) => {
23 if !(*left_val == *right_val) {
24 panic!(r#"assertion `left == right` failed
28 left: {:#x?}
29 right: {:#x?}"#, &*left_val, &*right_val)
30 }
31 }
32 }
33 });
34 ($left:expr, $right:expr, $($arg:tt)+) => ({
35 match (&($left), &($right)) {
36 (left_val, right_val) => {
37 if !(*left_val == *right_val) {
38 panic!(r#"assertion `left == right` failed: {}
42 left: {:#x?}
43 right: {:#x?}"#, format_args!($($arg)+), &*left_val, &*right_val)
44 }
45 }
46 }
47 });
48}
49
50#[macro_export]
55macro_rules! assert_ne_hex {
56 ($left:expr, $right:expr $(,)?) => ({
57 match (&$left, &$right) {
58 (left_val, right_val) => {
59 if *left_val == *right_val {
60 panic!(r#"assertion `left != right` failed
64 left: {:#x?}
65 right: {:#x?}"#, &*left_val, &*right_val)
66 }
67 }
68 }
69 });
70 ($left:expr, $right:expr, $($arg:tt)+) => ({
71 match (&($left), &($right)) {
72 (left_val, right_val) => {
73 if *left_val == *right_val {
74 panic!(r#"assertion `left != right` failed: {}
78 left: {:#x?}
79 right: {:#x?}"#, format_args!($($arg)+), &*left_val, &*right_val)
80 }
81 }
82 }
83 });
84}
85
86#[cfg(test)]
87mod tests {
88 use crate::{assert_eq_hex, assert_ne_hex};
89
90 #[test]
91 #[should_panic(expected = r#"assertion `left == right` failed
92 left: 0x50
93 right: 0x46"#)]
94 fn test_eq_0() {
95 assert_eq_hex!(0x50, 0x46);
96 }
97
98 #[test]
99 #[should_panic(expected = r#"assertion `left == right` failed
100 left: 0xff
101 right: 0x46"#)]
102 fn test_eq_1() {
103 assert_eq_hex!(0xff, 0x46);
104 }
105
106 #[test]
107 #[should_panic(expected = r#"assertion `left == right` failed
108 left: 0xff
109 right: 0x46"#)]
110 fn test_eq_2() {
111 assert_eq_hex!(0xff, 0x46);
112 }
113
114 #[test]
115 #[should_panic(expected = r#"assertion `left == right` failed
116 left: [
117 0x0,
118 0x1,
119 0x2,
120]
121 right: [
122 0x46,
123 0x50,
124 0x40,
125]"#)]
126 fn test_eq_3() {
127 assert_eq_hex!(vec![0x00, 0x01, 0x02], vec![0x46, 0x50, 0x40]);
128 }
129
130 #[test]
131 #[should_panic(expected = r#"assertion `left == right` failed: yikes
132 left: 0xff
133 right: 0x0"#)]
134 fn test_eq_more() {
135 assert_eq_hex!(0xff, 0x00, "yikes");
136 }
137
138 #[test]
139 #[should_panic(expected = r#"assertion `left != right` failed
140 left: 0x50
141 right: 0x50"#)]
142 fn test_ne_0() {
143 assert_ne_hex!(0x50, 0x50);
144 }
145
146 #[test]
147 #[should_panic(expected = r#"assertion `left != right` failed
148 left: 0xff
149 right: 0xff"#)]
150 fn test_ne_1() {
151 assert_ne_hex!(0xff, 0xff);
152 }
153
154 #[test]
155 #[should_panic(expected = r#"assertion `left != right` failed: yikes
156 left: 0xff
157 right: 0xff"#)]
158 fn test_ne_more() {
159 assert_ne_hex!(0xff, 0xff, "yikes");
160 }
161}