summary refs log tree commit diff
path: root/ripple/minitrace/src
diff options
context:
space:
mode:
authoredef <edef@unfathomable.blue>2022-07-29 20:58:48 +0000
committeredef <edef@unfathomable.blue>2022-07-29 20:58:48 +0000
commitb11be552627c0f958cc60c574e9b75c785141838 (patch)
tree1ac0bc983001bd8cbc8e9e30533ec1db1de8abf6 /ripple/minitrace/src
parente5a0383bfff3b38930cfaa20417a0f797ffea4b8 (diff)
downloadunf-legacy-b11be552627c0f958cc60c574e9b75c785141838.tar.zst
ripple/minitrace: factor out libc_check
Change-Id: I4feb931cbf98c852f975e1e68fd5e0ec8f0eecf4
Diffstat (limited to 'ripple/minitrace/src')
-rw-r--r--ripple/minitrace/src/main.rs46
1 files changed, 28 insertions, 18 deletions
diff --git a/ripple/minitrace/src/main.rs b/ripple/minitrace/src/main.rs
index 24737b0..fce614d 100644
--- a/ripple/minitrace/src/main.rs
+++ b/ripple/minitrace/src/main.rs
@@ -26,6 +26,9 @@ use {
 	},
 };
 
+#[cfg(test)]
+use std::fmt::{self, Debug};
+
 // TODO(edef): consider implementing this in terms of TID?
 // tgids are a strict subset of tids
 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -125,6 +128,23 @@ macro_rules! define_syscalls {
 	}
 }
 
+#[cfg(test)]
+fn libc_check<T: Debug + Eq + fmt::LowerHex>(
+	item: &'static str,
+	(our_name, our_value): (&'static str, T),
+	(libc_name, libc_value): (&'static str, T),
+) {
+	assert!(
+		libc_name.ends_with(our_name),
+		"{libc_name} doesn't end with {our_name}"
+	);
+
+	assert!(
+		our_value == libc_value,
+		"{item}::{our_name} ({our_value:#x}) != libc::{libc_name} ({libc_value:#x})",
+	);
+}
+
 macro_rules! syscall_bitflags {
 	(
 		$(
@@ -155,15 +175,10 @@ macro_rules! syscall_bitflags {
 				#[cfg(test)]
 				fn verify() {
 					$(
-						let libc_flag = stringify!($LIBC_FLAG);
-						let flag = stringify!($FLAG);
-						assert!(libc_flag.ends_with(flag), "{libc_flag} doesn't end with {flag}");
-
-						let left = Self::$FLAG.bits();
-						let right = libc::$LIBC_FLAG;
-						assert!(
-							left == right,
-							"{}::{} ({left:#x}) != libc::{} ({right:#x})", stringify!($BitFlags), stringify!($FLAG), stringify!($LIBC_FLAG),
+						libc_check(
+							stringify!($BitFlags),
+							(stringify!($FLAG), Self::$FLAG.bits()),
+							(stringify!($LIBC_FLAG), libc::$LIBC_FLAG)
 						);
 					)*
 				}
@@ -219,15 +234,10 @@ macro_rules! syscall_enums {
 				fn verify() {
 					$(
 						$(
-							let libc_value = stringify!($LIBC_VALUE);
-							let variant = stringify!($VARIANT);
-							assert!(libc_value.ends_with(variant), "{libc_value} doesn't end with {variant}");
-
-							let left = Self::$VARIANT as $T;
-							let right = libc::$LIBC_VALUE;
-							assert!(
-								left == right,
-								"{}::{} ({left:#x}) != libc::{} ({right:#x})", stringify!($Enum), stringify!($VARIANT), stringify!($LIBC_VALUE),
+							libc_check(
+								stringify!($Enum),
+								(stringify!($VARIANT), Self::$VARIANT as $T),
+								(stringify!($LIBC_VALUE), libc::$LIBC_VALUE)
 							);
 						)?
 					)*