Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <malloc.h> |
| 21 | |
| 22 | TEST(malloc, malloc_std) { |
| 23 | // Simple malloc test. |
| 24 | void *ptr = malloc(100); |
| 25 | ASSERT_TRUE(ptr != NULL); |
| 26 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 27 | |
| 28 | free(ptr); |
| 29 | } |
| 30 | |
| 31 | TEST(malloc, calloc_std) { |
| 32 | // Simple calloc test. |
| 33 | size_t alloc_len = 100; |
| 34 | char *ptr = (char *)calloc(1, alloc_len); |
| 35 | ASSERT_TRUE(ptr != NULL); |
| 36 | ASSERT_LE(alloc_len, malloc_usable_size(ptr)); |
| 37 | for (size_t i = 0; i < alloc_len; i++) { |
| 38 | ASSERT_EQ(0, ptr[i]); |
| 39 | } |
| 40 | |
| 41 | free(ptr); |
| 42 | } |
| 43 | |
| 44 | TEST(malloc, memalign_multiple) { |
| 45 | // Memalign test where the alignment is any value. |
| 46 | for (size_t i = 0; i <= 12; i++) { |
| 47 | for (size_t alignment = 1 << i; alignment < (1U << (i+1)); alignment++) { |
| 48 | char *ptr = (char*)memalign(alignment, 100); |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 49 | ASSERT_TRUE(ptr != NULL) << alignment; |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 50 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 51 | ASSERT_EQ(0, (intptr_t)ptr % (1 << i)); |
| 52 | |
| 53 | free(ptr); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | TEST(malloc, memalign_realloc) { |
| 59 | // Memalign and then realloc the pointer a couple of times. |
| 60 | for (size_t alignment = 1; alignment <= 4096; alignment <<= 1) { |
| 61 | char *ptr = (char*)memalign(alignment, 100); |
| 62 | ASSERT_TRUE(ptr != NULL); |
| 63 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 64 | ASSERT_EQ(0U, (intptr_t)ptr % alignment); |
| 65 | memset(ptr, 0x23, 100); |
| 66 | |
| 67 | ptr = (char*)realloc(ptr, 200); |
| 68 | ASSERT_TRUE(ptr != NULL); |
| 69 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 70 | ASSERT_TRUE(ptr != NULL); |
| 71 | for (size_t i = 0; i < 100; i++) { |
| 72 | ASSERT_EQ(0x23, ptr[i]); |
| 73 | } |
| 74 | memset(ptr, 0x45, 200); |
| 75 | |
| 76 | ptr = (char*)realloc(ptr, 300); |
| 77 | ASSERT_TRUE(ptr != NULL); |
| 78 | ASSERT_LE(300U, malloc_usable_size(ptr)); |
| 79 | for (size_t i = 0; i < 200; i++) { |
| 80 | ASSERT_EQ(0x45, ptr[i]); |
| 81 | } |
| 82 | memset(ptr, 0x67, 300); |
| 83 | |
| 84 | ptr = (char*)realloc(ptr, 250); |
| 85 | ASSERT_TRUE(ptr != NULL); |
| 86 | ASSERT_LE(250U, malloc_usable_size(ptr)); |
| 87 | for (size_t i = 0; i < 250; i++) { |
| 88 | ASSERT_EQ(0x67, ptr[i]); |
| 89 | } |
| 90 | |
| 91 | free(ptr); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | TEST(malloc, malloc_realloc_larger) { |
| 96 | // Realloc to a larger size, malloc is used for the original allocation. |
| 97 | char *ptr = (char *)malloc(100); |
| 98 | ASSERT_TRUE(ptr != NULL); |
| 99 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 100 | memset(ptr, 67, 100); |
| 101 | |
| 102 | ptr = (char *)realloc(ptr, 200); |
| 103 | ASSERT_TRUE(ptr != NULL); |
| 104 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 105 | for (size_t i = 0; i < 100; i++) { |
| 106 | ASSERT_EQ(67, ptr[i]); |
| 107 | } |
| 108 | |
| 109 | free(ptr); |
| 110 | } |
| 111 | |
| 112 | TEST(malloc, malloc_realloc_smaller) { |
| 113 | // Realloc to a smaller size, malloc is used for the original allocation. |
| 114 | char *ptr = (char *)malloc(200); |
| 115 | ASSERT_TRUE(ptr != NULL); |
| 116 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 117 | memset(ptr, 67, 200); |
| 118 | |
| 119 | ptr = (char *)realloc(ptr, 100); |
| 120 | ASSERT_TRUE(ptr != NULL); |
| 121 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 122 | for (size_t i = 0; i < 100; i++) { |
| 123 | ASSERT_EQ(67, ptr[i]); |
| 124 | } |
| 125 | |
| 126 | free(ptr); |
| 127 | } |
| 128 | |
| 129 | TEST(malloc, malloc_multiple_realloc) { |
| 130 | // Multiple reallocs, malloc is used for the original allocation. |
| 131 | char *ptr = (char *)malloc(200); |
| 132 | ASSERT_TRUE(ptr != NULL); |
| 133 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 134 | memset(ptr, 0x23, 200); |
| 135 | |
| 136 | ptr = (char *)realloc(ptr, 100); |
| 137 | ASSERT_TRUE(ptr != NULL); |
| 138 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 139 | for (size_t i = 0; i < 100; i++) { |
| 140 | ASSERT_EQ(0x23, ptr[i]); |
| 141 | } |
| 142 | |
| 143 | ptr = (char*)realloc(ptr, 50); |
| 144 | ASSERT_TRUE(ptr != NULL); |
| 145 | ASSERT_LE(50U, malloc_usable_size(ptr)); |
| 146 | for (size_t i = 0; i < 50; i++) { |
| 147 | ASSERT_EQ(0x23, ptr[i]); |
| 148 | } |
| 149 | |
| 150 | ptr = (char*)realloc(ptr, 150); |
| 151 | ASSERT_TRUE(ptr != NULL); |
| 152 | ASSERT_LE(150U, malloc_usable_size(ptr)); |
| 153 | for (size_t i = 0; i < 50; i++) { |
| 154 | ASSERT_EQ(0x23, ptr[i]); |
| 155 | } |
| 156 | memset(ptr, 0x23, 150); |
| 157 | |
| 158 | ptr = (char*)realloc(ptr, 425); |
| 159 | ASSERT_TRUE(ptr != NULL); |
| 160 | ASSERT_LE(425U, malloc_usable_size(ptr)); |
| 161 | for (size_t i = 0; i < 150; i++) { |
| 162 | ASSERT_EQ(0x23, ptr[i]); |
| 163 | } |
| 164 | |
| 165 | free(ptr); |
| 166 | } |
| 167 | TEST(malloc, calloc_realloc_larger) { |
| 168 | // Realloc to a larger size, calloc is used for the original allocation. |
| 169 | char *ptr = (char *)calloc(1, 100); |
| 170 | ASSERT_TRUE(ptr != NULL); |
| 171 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 172 | |
| 173 | ptr = (char *)realloc(ptr, 200); |
| 174 | ASSERT_TRUE(ptr != NULL); |
| 175 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 176 | for (size_t i = 0; i < 100; i++) { |
| 177 | ASSERT_EQ(0, ptr[i]); |
| 178 | } |
| 179 | |
| 180 | free(ptr); |
| 181 | } |
| 182 | |
| 183 | TEST(malloc, calloc_realloc_smaller) { |
| 184 | // Realloc to a smaller size, calloc is used for the original allocation. |
| 185 | char *ptr = (char *)calloc(1, 200); |
| 186 | ASSERT_TRUE(ptr != NULL); |
| 187 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 188 | |
| 189 | ptr = (char *)realloc(ptr, 100); |
| 190 | ASSERT_TRUE(ptr != NULL); |
| 191 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 192 | for (size_t i = 0; i < 100; i++) { |
| 193 | ASSERT_EQ(0, ptr[i]); |
| 194 | } |
| 195 | |
| 196 | free(ptr); |
| 197 | } |
| 198 | |
| 199 | TEST(malloc, calloc_multiple_realloc) { |
| 200 | // Multiple reallocs, calloc is used for the original allocation. |
| 201 | char *ptr = (char *)calloc(1, 200); |
| 202 | ASSERT_TRUE(ptr != NULL); |
| 203 | ASSERT_LE(200U, malloc_usable_size(ptr)); |
| 204 | |
| 205 | ptr = (char *)realloc(ptr, 100); |
| 206 | ASSERT_TRUE(ptr != NULL); |
| 207 | ASSERT_LE(100U, malloc_usable_size(ptr)); |
| 208 | for (size_t i = 0; i < 100; i++) { |
| 209 | ASSERT_EQ(0, ptr[i]); |
| 210 | } |
| 211 | |
| 212 | ptr = (char*)realloc(ptr, 50); |
| 213 | ASSERT_TRUE(ptr != NULL); |
| 214 | ASSERT_LE(50U, malloc_usable_size(ptr)); |
| 215 | for (size_t i = 0; i < 50; i++) { |
| 216 | ASSERT_EQ(0, ptr[i]); |
| 217 | } |
| 218 | |
| 219 | ptr = (char*)realloc(ptr, 150); |
| 220 | ASSERT_TRUE(ptr != NULL); |
| 221 | ASSERT_LE(150U, malloc_usable_size(ptr)); |
| 222 | for (size_t i = 0; i < 50; i++) { |
| 223 | ASSERT_EQ(0, ptr[i]); |
| 224 | } |
| 225 | memset(ptr, 0, 150); |
| 226 | |
| 227 | ptr = (char*)realloc(ptr, 425); |
| 228 | ASSERT_TRUE(ptr != NULL); |
| 229 | ASSERT_LE(425U, malloc_usable_size(ptr)); |
| 230 | for (size_t i = 0; i < 150; i++) { |
| 231 | ASSERT_EQ(0, ptr[i]); |
| 232 | } |
| 233 | |
| 234 | free(ptr); |
| 235 | } |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 236 | |
| 237 | TEST(malloc, posix_memalign_non_power2) { |
| 238 | void* ptr; |
| 239 | |
| 240 | ASSERT_EQ(EINVAL, posix_memalign(&ptr, 17, 1024)); |
| 241 | } |
| 242 | |
| 243 | TEST(malloc, memalign_non_power2) { |
| 244 | void* ptr; |
| 245 | for (size_t align = 0; align <= 256; align++) { |
| 246 | ptr = memalign(align, 1024); |
| 247 | ASSERT_TRUE(ptr != NULL) << "Failed at align " << align; |
| 248 | free(ptr); |
| 249 | } |
| 250 | } |