diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-11-15 15:12:08 -0700 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-11-15 21:40:37 -0700 |
commit | 5704de985cff1d40359ecd15211cece0fbe79067 (patch) | |
tree | 5c172a6ea91716f4f8023e58d580e4b08fbd7fc1 /libmisc/include | |
parent | f753128b22b61d4f85a74ba2694b8f9a576fc238 (diff) |
Add tests to libmisc
Diffstat (limited to 'libmisc/include')
-rw-r--r-- | libmisc/include/libmisc/assert.h | 7 | ||||
-rw-r--r-- | libmisc/include/libmisc/rand.h | 4 |
2 files changed, 5 insertions, 6 deletions
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_ */ |