timing and launcher changes
authorNiki Roo <niki@nikiroo.be>
Tue, 25 Jun 2024 18:10:48 +0000 (20:10 +0200)
committerNiki Roo <niki@nikiroo.be>
Tue, 25 Jun 2024 18:10:48 +0000 (20:10 +0200)
check/launcher.c
check/launcher.h
timing.h

index 855bf2531388c02bea908dd62f292a0dcb7593b7..cc39935dd0467f13cce83834c426247d5cf71403 100644 (file)
@@ -109,6 +109,31 @@ void test_failure() {
                        stop_color());
 }
 
+void assert_str(const char title[], const char expected[], 
+               const char received[]) {
+       if (strcmp(expected, received)) {
+               failure("%s\n\tExpected: <%s>\n\tReceived: <%s>",
+                       title, expected, received
+               );
+       }
+}
+
+void assert_int(const char title[], long long expected, long long received) {
+       if (received != expected) {
+               failure("%s\n\tExpected: %lld\n\tReceived: %lld",
+                       title, expected, received
+               );
+       }
+}
+
+void assert_sz(const char title[], size_t expected, size_t received) {
+       if (received != expected) {
+               failure("%s\n\tExpected: %zu\n\tReceived: %zu",
+                       title, expected, received
+               );
+       }
+}
+
 int test_start(int more) {
        int failed;
        SRunner *runner;
index 8df774a951d0f91d1d72c8e9d84aa15788e88f85..ffc45e5f32bf528324ac28cfd0480689f5d3cc5f 100644 (file)
@@ -12,24 +12,17 @@ START_TEST(name) {\
 }\
 END_TEST\
 
-#define FAIL(...) \
-/* TODO: test_failure(); ??? */ \
-ck_abort_msg(__VA_ARGS__)\
-
-#define ASSERT_EQUALS_STR(title, expected, received) \
-       do { if (strcmp(expected, received)) { \
-ck_abort_msg("%s\n\tExpected: <%s>\n\tReceived: <%s>", title, expected, received); \
-}} while(0)
-
-#define ASSERT_EQUALS_INT(title, expected, received) \
-       do { if (expected != received) { \
-ck_abort_msg("%s\n\tExpected: %lld\n\tReceived: %lld", title, (long long)expected, (long long)received); \
-}} while(0)
-
-#define ASSERT_EQUALS_SIZE(title, expected, received) \
-       do { if (expected != received) { \
-ck_abort_msg("%s\n\tExpected: %zu\n\tReceived: %zu", title, expected, received); \
-}} while(0)
+#define failure(...) do { \
+       test_failure(); \
+       ck_abort_msg(__VA_ARGS__); \
+} while(0)
+
+void assert_str(const char title[], const char expected[],
+               const char received[]);
+
+void assert_int(const char title[], long long expected, long long received);
+
+void assert_sz(const char title[], size_t expected, size_t received);
 
 extern int launcher_color;
 
index bd190efb57ffba9c7ff56647c1e678db90d1efcc..81cb8cfaddd447137845bb23081e2a383a6dfa81 100644 (file)
--- a/timing.h
+++ b/timing.h
@@ -36,23 +36,50 @@ extern "C" {
 
 #include <sys/time.h>
 
+typedef struct {
+       struct timeval start;
+       struct timeval stop;
+       size_t sec;
+       int msec;
+       int usec;
+} timing_t;
+
+/**
+ * Start the timing.
+ */
+#define TIMING_START(timing) \
+       timing_t timing; \
+       gettimeofday(&(timing.start), NULL); \
+       timing.sec = timing.msec = timing.usec = 0; \
+       timing.stop.tv_sec = 0; \
+       timing.stop.tv_usec = 0; \
+while(0)
+
 /**
- * Start the timer.
+ * Stop the timing and return the elapsed time in milliseconds into msec.
  */
-#define TIMING_START struct timeval TIMING_start, TIMING_stop; \
-       /* 1 usec = 0.000001 s */ \
-       char cusec[7]; \
-       gettimeofday(&TIMING_start, NULL);
+#define TIMING_STOP(timing) \
+       gettimeofday(&(timing.stop), NULL); \
+       if (timing.stop.tv_usec >= timing.start.tv_usec) { \
+               timing.sec = timing.stop.tv_sec - timing.start.tv_sec; \
+               timing.usec = timing.stop.tv_usec - timing.start.tv_usec; \
+       } else { \
+               timing.sec = (timing.stop.tv_sec - timing.start.tv_sec) - 1; \
+               timing.usec = timing.start.tv_usec - timing.stop.tv_usec; \
+       } \
+       timing.msec = timing.usec / 1000; \
+       timing.usec = timing.usec % 1000; \
+       gettimeofday(&(timing.start), NULL); \
+while(0)
 
 /**
- * Stop the timer and print the elapsed time to stdout.
+ * Stop the timing and print the elapsed time.
  */
-#define TIMING_STOP gettimeofday(&TIMING_stop, NULL); \
-       TIMING_stop.tv_sec  = TIMING_stop.tv_sec  - TIMING_start.tv_sec; \
-       TIMING_stop.tv_usec = TIMING_stop.tv_usec - TIMING_start.tv_usec; \
-       sprintf(cusec, "%0.6d", TIMING_stop.tv_usec); \
-       printf("TIME: %d.%s sec\n", TIMING_stop.tv_sec, cusec); \
-       gettimeofday(&TIMING_start, NULL);
+#define TIMING_PRINT(timing) \
+       TIMING_STOP(timing); \
+       printf("TIME: %zu.%03d%03d sec\n", \
+               timing.sec, timing.msec, timing.usec); \
+while(0)
 
 #endif // TIMING_H