diff options
author | Lennart Poettering <lennart@poettering.net> | 2022-12-07 18:31:27 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2022-12-08 15:30:31 +0100 |
commit | 4f07388360a3513b9fc8d2773568b8def941f4a4 (patch) | |
tree | a15b17bf89ba2f0743113bbe82e200736564265f /src/test/test-macro.c | |
parent | sha256: move to unaligned_write_ne32() (diff) | |
download | systemd-4f07388360a3513b9fc8d2773568b8def941f4a4.tar.xz systemd-4f07388360a3513b9fc8d2773568b8def941f4a4.zip |
macro: add generic IS_ALIGNED32() anf friends
Let's generalize (and invert) the UNALIGNED32_P() macro from the sha256
code, and let's add a test for it.
Diffstat (limited to 'src/test/test-macro.c')
-rw-r--r-- | src/test/test-macro.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/test/test-macro.c b/src/test/test-macro.c index 001166d0dc..d4f32496b7 100644 --- a/src/test/test-macro.c +++ b/src/test/test-macro.c @@ -531,4 +531,57 @@ TEST(ISPOWEROF2) { assert_se(!ISPOWEROF2(u)); } +TEST(ALIGNED) { + assert_se(IS_ALIGNED16(NULL)); + assert_se(IS_ALIGNED32(NULL)); + assert_se(IS_ALIGNED64(NULL)); + + uint64_t u64; + uint32_t u32; + uint16_t u16; + + assert_se(IS_ALIGNED16(&u16)); + assert_se(IS_ALIGNED16(&u32)); + assert_se(IS_ALIGNED16(&u64)); + assert_se(IS_ALIGNED32(&u32)); + assert_se(IS_ALIGNED32(&u64)); + assert_se(IS_ALIGNED64(&u64)); + + _align_(32) uint8_t ua256; + _align_(8) uint8_t ua64; + _align_(4) uint8_t ua32; + _align_(2) uint8_t ua16; + + assert_se(IS_ALIGNED16(&ua256)); + assert_se(IS_ALIGNED32(&ua256)); + assert_se(IS_ALIGNED64(&ua256)); + + assert_se(IS_ALIGNED16(&ua64)); + assert_se(IS_ALIGNED32(&ua64)); + assert_se(IS_ALIGNED64(&ua64)); + + assert_se(IS_ALIGNED16(&ua32)); + assert_se(IS_ALIGNED32(&ua32)); + + assert_se(IS_ALIGNED16(&ua16)); + +#ifdef __x86_64__ + /* Conditionalized on x86-64, since there we know for sure that all three types are aligned to + * their size. Too lazy to figure it out for other archs */ + void *p = UINT_TO_PTR(1); /* definitely not aligned */ + assert_se(!IS_ALIGNED16(p)); + assert_se(!IS_ALIGNED32(p)); + assert_se(!IS_ALIGNED64(p)); + + assert_se(IS_ALIGNED16(ALIGN2_PTR(p))); + assert_se(IS_ALIGNED32(ALIGN4_PTR(p))); + assert_se(IS_ALIGNED64(ALIGN8_PTR(p))); + + p = UINT_TO_PTR(-1); /* also definitely not aligned */ + assert_se(!IS_ALIGNED16(p)); + assert_se(!IS_ALIGNED32(p)); + assert_se(!IS_ALIGNED64(p)); +#endif +} + DEFINE_TEST_MAIN(LOG_INFO); |