Răsfoiți Sursa

add ASSERT_INT_WITHIN and its test code

hathach 13 ani în urmă
părinte
comite
beb20b975b
2 a modificat fișierele cu 24 adăugiri și 3 ștergeri
  1. 12 0
      tests/test/test_assertion.c
  2. 12 3
      tinyusb/common/assertion.h

+ 12 - 0
tests/test/test_assertion.c

@@ -87,3 +87,15 @@ void test_assert_int_eqal(void)
 
   TEST_FAIL();
 }
+
+void test_assert_int_within(void)
+{
+  ASSERT_INT_WITHIN (1, 5, 3, (void) 0);
+  ASSERT_INT_WITHIN (1, 5, 1, (void) 0);
+  ASSERT_INT_WITHIN (1, 5, 5, (void) 0);
+
+  ASSERT_INT_WITHIN (1, 5, 10, (void) 0);
+  ASSERT_INT_WITHIN (1, 5, 0, (void) 0);
+
+  TEST_FAIL();
+}

+ 12 - 3
tinyusb/common/assertion.h

@@ -109,9 +109,18 @@ extern "C"
 
 #define ASSERT_INT(...)  ASSERT_INT_EQUAL(__VA_ARGS__)
 #define ASSERT_INT_EQUAL(expected, actual, error)  \
-    ASSERT_DEFINE( uint32_t exp = (expected); uint32_t act = (actual), exp==act, error, "expected %d, actual %d", exp, act)
-
-#define ASSERT_INT_WITHIN(lower, upper, actual)
+    ASSERT_DEFINE(\
+                  uint32_t exp = (expected); uint32_t act = (actual),\
+                  exp==act,\
+                  error,\
+                  "expected %d, actual %d", exp, act)
+
+#define ASSERT_INT_WITHIN(lower, upper, actual, error) \
+    ASSERT_DEFINE(\
+                  uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\
+                  (low <= act) && (act <= up),\
+                  error,\
+                  "expected within %d-%d, actual %d", low, up, act)
 
 
 #ifdef __cplusplus