align_ut.cpp 640 B

1234567891011121314151617181920212223
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <util/generic/scope.h>
  3. Y_UNIT_TEST_SUITE(MemAlign) {
  4. Y_UNIT_TEST(ShouldAlign)
  5. {
  6. for (ui64 size = 8; size <= 32 * 1024; size *= 8) {
  7. for (ui64 align = 8; align <= 4096; align *=2) {
  8. void* ptr = nullptr;
  9. int res = posix_memalign(&ptr, align, size);
  10. UNIT_ASSERT_C(res == 0 && ptr != nullptr, "memalign failed");
  11. Y_DEFER {
  12. free(ptr);
  13. };
  14. UNIT_ASSERT_C((uintptr_t)ptr % align == 0, "non aligned memory");
  15. }
  16. }
  17. }
  18. }