diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-10-18 05:59:31 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-10-19 11:31:44 +0200 |
commit | 2977904cad8e2da69d03747b88bcbb5a824921e1 (patch) | |
tree | fa978839ecaa648f9588bf6736b17fbbda054c53 /src/test/test-memory-util.c | |
parent | macro: paranoia about overflow (diff) | |
download | systemd-2977904cad8e2da69d03747b88bcbb5a824921e1.tar.xz systemd-2977904cad8e2da69d03747b88bcbb5a824921e1.zip |
macro: introduce several helper functions for alignment
Some of them are not used in this commit, but will be used later.
Diffstat (limited to 'src/test/test-memory-util.c')
-rw-r--r-- | src/test/test-memory-util.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/test/test-memory-util.c b/src/test/test-memory-util.c index 2f8384ac09..cd4b64ac16 100644 --- a/src/test/test-memory-util.c +++ b/src/test/test-memory-util.c @@ -52,4 +52,74 @@ TEST(cleanup_array) { free(saved_iov); } +TEST(page_align) { + assert_se(PAGE_ALIGN(page_size() - 1) == page_size()); + assert_se(PAGE_ALIGN(page_size() ) == page_size()); + assert_se(PAGE_ALIGN(page_size() + 1) == page_size() * 2); + assert_se(PAGE_ALIGN(page_size() * 123 - 1) == page_size() * 123); + assert_se(PAGE_ALIGN(page_size() * 123 ) == page_size() * 123); + assert_se(PAGE_ALIGN(page_size() * 123 + 1) == page_size() * 124); + assert_se(PAGE_ALIGN(SIZE_MAX - page_size() - 1) == SIZE_MAX - page_size() + 1); + assert_se(PAGE_ALIGN(SIZE_MAX - page_size() ) == SIZE_MAX - page_size() + 1); + assert_se(PAGE_ALIGN(SIZE_MAX - page_size() + 1) == SIZE_MAX - page_size() + 1); + assert_se(PAGE_ALIGN(SIZE_MAX - page_size() + 2) == SIZE_MAX); /* overflow */ + assert_se(PAGE_ALIGN(SIZE_MAX) == SIZE_MAX); /* overflow */ + + assert_se(PAGE_ALIGN_U64(page_size() - 1) == page_size()); + assert_se(PAGE_ALIGN_U64(page_size() ) == page_size()); + assert_se(PAGE_ALIGN_U64(page_size() + 1) == page_size() * 2); + assert_se(PAGE_ALIGN_U64(page_size() * 123 - 1) == page_size() * 123); + assert_se(PAGE_ALIGN_U64(page_size() * 123 ) == page_size() * 123); + assert_se(PAGE_ALIGN_U64(page_size() * 123 + 1) == page_size() * 124); + assert_se(PAGE_ALIGN_U64(UINT64_MAX - page_size() - 1) == UINT64_MAX - page_size() + 1); + assert_se(PAGE_ALIGN_U64(UINT64_MAX - page_size() ) == UINT64_MAX - page_size() + 1); + assert_se(PAGE_ALIGN_U64(UINT64_MAX - page_size() + 1) == UINT64_MAX - page_size() + 1); + assert_se(PAGE_ALIGN_U64(UINT64_MAX - page_size() + 2) == UINT64_MAX); /* overflow */ + assert_se(PAGE_ALIGN_U64(UINT64_MAX) == UINT64_MAX); /* overflow */ + + assert_se(PAGE_ALIGN_DOWN(page_size() - 1) == 0); + assert_se(PAGE_ALIGN_DOWN(page_size() ) == page_size()); + assert_se(PAGE_ALIGN_DOWN(page_size() + 1) == page_size()); + assert_se(PAGE_ALIGN_DOWN(page_size() * 123 - 1) == page_size() * 122); + assert_se(PAGE_ALIGN_DOWN(page_size() * 123 ) == page_size() * 123); + assert_se(PAGE_ALIGN_DOWN(page_size() * 123 + 1) == page_size() * 123); + assert_se(PAGE_ALIGN_DOWN(SIZE_MAX - page_size() - 1) == SIZE_MAX - page_size() * 2 + 1); + assert_se(PAGE_ALIGN_DOWN(SIZE_MAX - page_size() ) == SIZE_MAX - page_size() * 2 + 1); + assert_se(PAGE_ALIGN_DOWN(SIZE_MAX - page_size() + 1) == SIZE_MAX - page_size() + 1); + assert_se(PAGE_ALIGN_DOWN(SIZE_MAX - page_size() + 2) == SIZE_MAX - page_size() + 1); + + assert_se(PAGE_ALIGN_DOWN_U64(page_size() - 1) == 0); + assert_se(PAGE_ALIGN_DOWN_U64(page_size() ) == page_size()); + assert_se(PAGE_ALIGN_DOWN_U64(page_size() + 1) == page_size()); + assert_se(PAGE_ALIGN_DOWN_U64(page_size() * 123 - 1) == page_size() * 122); + assert_se(PAGE_ALIGN_DOWN_U64(page_size() * 123 ) == page_size() * 123); + assert_se(PAGE_ALIGN_DOWN_U64(page_size() * 123 + 1) == page_size() * 123); + assert_se(PAGE_ALIGN_DOWN_U64(SIZE_MAX - page_size() - 1) == SIZE_MAX - page_size() * 2 + 1); + assert_se(PAGE_ALIGN_DOWN_U64(SIZE_MAX - page_size() ) == SIZE_MAX - page_size() * 2 + 1); + assert_se(PAGE_ALIGN_DOWN_U64(SIZE_MAX - page_size() + 1) == SIZE_MAX - page_size() + 1); + assert_se(PAGE_ALIGN_DOWN_U64(SIZE_MAX - page_size() + 2) == SIZE_MAX - page_size() + 1); + + assert_se(PAGE_OFFSET(page_size() - 1) == page_size() - 1); + assert_se(PAGE_OFFSET(page_size() ) == 0); + assert_se(PAGE_OFFSET(page_size() + 1) == 1); + assert_se(PAGE_OFFSET(page_size() * 123 - 1) == page_size() - 1); + assert_se(PAGE_OFFSET(page_size() * 123 ) == 0); + assert_se(PAGE_OFFSET(page_size() * 123 + 1) == 1); + assert_se(PAGE_OFFSET(SIZE_MAX - page_size() - 1) == page_size() - 2); + assert_se(PAGE_OFFSET(SIZE_MAX - page_size() ) == page_size() - 1); + assert_se(PAGE_OFFSET(SIZE_MAX - page_size() + 1) == 0); + assert_se(PAGE_OFFSET(SIZE_MAX - page_size() + 2) == 1); + + assert_se(PAGE_OFFSET_U64(page_size() - 1) == page_size() - 1); + assert_se(PAGE_OFFSET_U64(page_size() ) == 0); + assert_se(PAGE_OFFSET_U64(page_size() + 1) == 1); + assert_se(PAGE_OFFSET_U64(page_size() * 123 - 1) == page_size() - 1); + assert_se(PAGE_OFFSET_U64(page_size() * 123 ) == 0); + assert_se(PAGE_OFFSET_U64(page_size() * 123 + 1) == 1); + assert_se(PAGE_OFFSET_U64(UINT64_MAX - page_size() - 1) == page_size() - 2); + assert_se(PAGE_OFFSET_U64(UINT64_MAX - page_size() ) == page_size() - 1); + assert_se(PAGE_OFFSET_U64(UINT64_MAX - page_size() + 1) == 0); + assert_se(PAGE_OFFSET_U64(UINT64_MAX - page_size() + 2) == 1); +} + DEFINE_TEST_MAIN(LOG_INFO); |