| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 3 | // |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without modification, are |
| 7 | // permitted provided that the following conditions are met: |
| 8 | // |
| 9 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | // conditions and the following disclaimer. |
| 11 | // |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | // of conditions and the following disclaimer in the documentation and/or other |
| 14 | // materials provided with the distribution. |
| 15 | // |
| 16 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | // used to endorse or promote products derived from this software without specific |
| 18 | // prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include "gtest/gtest.h" |
| 31 | |
| 32 | #include <cstdint> |
| 33 | #include "common/aligned.h" |
| 34 | |
| 35 | TEST(aligned, large_null) { ASSERT_TRUE(aligned_malloc((size_t)-1, 1) == NULL); } |
| 36 | TEST(aligned, free_null) { aligned_free(NULL); } |
| 37 | TEST(aligned, zero) { void *ptr = aligned_malloc(0, 1); ASSERT_TRUE(ptr); aligned_free(ptr); } |
| 38 | TEST(aligned, aligned1) { void *ptr = aligned_malloc(1, 1); ASSERT_TRUE(ptr); aligned_free(ptr); } |
| 39 | TEST(aligned, aligned4096) { void *ptr = aligned_malloc(1, 4096); ASSERT_TRUE(ptr && ((uintptr_t)ptr & 4095) == 0); aligned_free(ptr); } |
| 40 | TEST(aligned, aligned8) { void *ptr = aligned_malloc(1, 8); ASSERT_TRUE(ptr && ((uintptr_t)ptr & 7) == 0); aligned_free(ptr); } |
| 41 | TEST(aligned, realloc_null) { void *ptr = aligned_realloc(NULL, 1, 4096); ASSERT_TRUE(ptr && ((uintptr_t)ptr & 4095) == 0); aligned_free(ptr); } |
| 42 | TEST(aligned, realloc_diff_align) { void *ptr = aligned_malloc(1, 4096); ASSERT_TRUE(!aligned_realloc(ptr, 1, 2048)); aligned_free(ptr); } |
| 43 | TEST(aligned, realloc_same) { void *ptr = aligned_malloc(1, 4096), *ptr2 = aligned_realloc(ptr, 1, 4096); ASSERT_TRUE(ptr == ptr2); aligned_free(ptr2); } |
| 44 | TEST(aligned, realloc_larger) { void *ptr = aligned_malloc(1, 4096), *ptr2 = aligned_realloc(ptr, 2, 4096); ASSERT_TRUE(ptr != ptr2); aligned_free(ptr2); } |
| 45 | TEST(aligned, realloc_zero) { void *ptr = aligned_malloc(1, 4096), *ptr2 = aligned_realloc(ptr, 0, 4096); ASSERT_TRUE(ptr && !ptr2); } |
| 46 | |
| 47 | TEST(aligned, contents_larger) |
| 48 | { |
| 49 | unsigned char *ptr = (unsigned char*)aligned_malloc(50, 256); |
| 50 | ASSERT_TRUE(ptr); |
| 51 | for (int n = 0; n < 50; ++n) |
| 52 | ptr[n] = n; |
| 53 | unsigned char *ptr2 = (unsigned char*)aligned_realloc(ptr, 51, 256); |
| 54 | for (int n = 0; n < 50; ++n) |
| 55 | { |
| 56 | ASSERT_TRUE(ptr2[n] == n); |
| 57 | } |
| 58 | aligned_free(ptr2); |
| 59 | } |
| 60 | |
| 61 | TEST(aligned, contents_same) |
| 62 | { |
| 63 | unsigned char *ptr = (unsigned char*)aligned_malloc(50, 256); |
| 64 | ASSERT_TRUE(ptr); |
| 65 | for (int n = 0; n < 50; ++n) |
| 66 | ptr[n] = n; |
| 67 | unsigned char *ptr2 = (unsigned char*)aligned_realloc(ptr, 50, 256); |
| 68 | for (int n = 0; n < 50; ++n) |
| 69 | { |
| 70 | ASSERT_TRUE(ptr2[n] == n); |
| 71 | } |
| 72 | aligned_free(ptr2); |
| 73 | } |
| 74 | |
| 75 | TEST(aligned, contents_smaller) |
| 76 | { |
| 77 | unsigned char *ptr = (unsigned char*)aligned_malloc(50, 256); |
| 78 | ASSERT_TRUE(ptr); |
| 79 | for (int n = 0; n < 50; ++n) |
| 80 | ptr[n] = n; |
| 81 | unsigned char *ptr2 = (unsigned char*)aligned_realloc(ptr, 49, 256); |
| 82 | for (int n = 0; n < 49; ++n) |
| 83 | { |
| 84 | ASSERT_TRUE(ptr2[n] == n); |
| 85 | } |
| 86 | aligned_free(ptr2); |
| 87 | } |
| 88 | |
| 89 | TEST(aligned, alignment) |
| 90 | { |
| 91 | static const size_t good_alignments[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192}; |
| 92 | for (size_t a = 0; a <= 8192; ++a) |
| 93 | { |
| 94 | bool good = false; |
| 95 | for (const auto t: good_alignments) if (a == t) good = true; |
| 96 | void *ptr = aligned_malloc(1, a); |
| 97 | if (good) |
| 98 | { |
| 99 | ASSERT_TRUE(ptr != NULL); |
| 100 | aligned_free(ptr); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | ASSERT_TRUE(ptr == NULL); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | ASSERT_TRUE(aligned_malloc(1, ~0) == NULL); |
| 109 | } |