From 5704de985cff1d40359ecd15211cece0fbe79067 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Fri, 15 Nov 2024 15:12:08 -0700 Subject: Add tests to libmisc --- libmisc/include/libmisc/assert.h | 7 +++---- libmisc/include/libmisc/rand.h | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'libmisc/include') diff --git a/libmisc/include/libmisc/assert.h b/libmisc/include/libmisc/assert.h index 10cedfa..525d7d5 100644 --- a/libmisc/include/libmisc/assert.h +++ b/libmisc/include/libmisc/assert.h @@ -11,10 +11,9 @@ # define assert_msg(expr, msg) ((void)0) #else # define assert_msg(expr, msg) do { if (!(expr)) __assert_msg_fail(#expr, __FILE__, __LINE__, __func__, msg); } while (0) -__attribute__((__noreturn__)) void -__assert_msg_fail(const char *expr, - const char *file, unsigned int line, const char *func, - const char *msg); +void __assert_msg_fail(const char *expr, + const char *file, unsigned int line, const char *func, + const char *msg); #endif #define assert(expr) assert_msg(expr, NULL) /* C89, POSIX-2001 */ diff --git a/libmisc/include/libmisc/rand.h b/libmisc/include/libmisc/rand.h index bdb0db9..8072841 100644 --- a/libmisc/include/libmisc/rand.h +++ b/libmisc/include/libmisc/rand.h @@ -17,6 +17,7 @@ * `cnt` must not be greater than 1<<63. */ static inline uint64_t rand_uint63n(uint64_t cnt) { + assert(cnt != 0 && ((cnt-1) & 0x8000000000000000) == 0); if (cnt <= UINT64_C(1)<<31) { uint32_t fair_cnt = ((UINT32_C(1)<<31) / cnt) * cnt; uint32_t rnd; @@ -38,9 +39,8 @@ static inline uint64_t rand_uint63n(uint64_t cnt) { rnd = (random() << 62) | (random() << 31) | random(); } while (rnd >= fair_cnt); return rnd % cnt; - } else { - assert_notreached("cnt is out of bounds"); } + assert_notreached("cnt is out of bounds"); } #endif /* _LIBMISC_RAND_H_ */ -- cgit v1.2.3-2-g168b