From b11be552627c0f958cc60c574e9b75c785141838 Mon Sep 17 00:00:00 2001 From: edef Date: Fri, 29 Jul 2022 20:58:48 +0000 Subject: ripple/minitrace: factor out libc_check Change-Id: I4feb931cbf98c852f975e1e68fd5e0ec8f0eecf4 --- ripple/minitrace/src/main.rs | 46 +++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 18 deletions(-) (limited to 'ripple') 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( + 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) ); )? )* -- cgit 1.4.1