| 1 | #include "test/IntegrationTest/test.h" |
| 2 | |
| 3 | #include "src/__support/GPU/utils.h" |
| 4 | #include "src/stdlib/free.h" |
| 5 | #include "src/stdlib/malloc.h" |
| 6 | #include "src/stdlib/realloc.h" |
| 7 | |
| 8 | using namespace LIBC_NAMESPACE; |
| 9 | |
| 10 | TEST_MAIN(int, char **, char **) { |
| 11 | // realloc(nullptr, size) is equivalent to malloc. |
| 12 | int *alloc = reinterpret_cast<int *>(LIBC_NAMESPACE::realloc(nullptr, 32)); |
| 13 | EXPECT_NE(alloc, nullptr); |
| 14 | *alloc = 42; |
| 15 | EXPECT_EQ(*alloc, 42); |
| 16 | |
| 17 | // realloc to same size returns the same pointer. |
| 18 | void *same = LIBC_NAMESPACE::realloc(alloc, 32); |
| 19 | EXPECT_EQ(same, alloc); |
| 20 | EXPECT_EQ(reinterpret_cast<int *>(same)[0], 42); |
| 21 | |
| 22 | // realloc to smaller size returns same pointer. |
| 23 | void *smaller = LIBC_NAMESPACE::realloc(same, 16); |
| 24 | EXPECT_EQ(smaller, alloc); |
| 25 | EXPECT_EQ(reinterpret_cast<int *>(smaller)[0], 42); |
| 26 | |
| 27 | // realloc to larger size returns new pointer and preserves contents. |
| 28 | int *larger = reinterpret_cast<int *>(LIBC_NAMESPACE::realloc(smaller, 128)); |
| 29 | EXPECT_NE(larger, nullptr); |
| 30 | EXPECT_EQ(larger[0], 42); |
| 31 | |
| 32 | // realloc works when called with a divergent size. |
| 33 | int *div = reinterpret_cast<int *>( |
| 34 | LIBC_NAMESPACE::malloc((gpu::get_thread_id() + 1) * 16)); |
| 35 | EXPECT_NE(div, nullptr); |
| 36 | div[0] = static_cast<int>(gpu::get_thread_id()); |
| 37 | int *div_realloc = reinterpret_cast<int *>( |
| 38 | LIBC_NAMESPACE::realloc(div, ((gpu::get_thread_id() + 1) * 32))); |
| 39 | EXPECT_NE(div_realloc, nullptr); |
| 40 | EXPECT_EQ(div_realloc[0], static_cast<int>(gpu::get_thread_id())); |
| 41 | LIBC_NAMESPACE::free(div_realloc); |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |