| 286 | |
| 287 | |
| 288 | int uriTestMemoryManager(UriMemoryManager * memory) { |
| 289 | const size_t mallocSize = 7; |
| 290 | const size_t callocNmemb = 3; |
| 291 | const size_t callocSize = 5; |
| 292 | const size_t callocTotalSize = callocNmemb * callocSize; |
| 293 | const size_t reallocSize = 11; |
| 294 | const size_t reallocarrayNmemb = 5; |
| 295 | const size_t reallocarraySize = 7; |
| 296 | const size_t reallocarrayTotal = reallocarrayNmemb * reallocarraySize; |
| 297 | size_t index; |
| 298 | char * buffer; |
| 299 | |
| 300 | if (memory == NULL) { |
| 301 | return URI_ERROR_NULL; |
| 302 | } |
| 303 | |
| 304 | if (uriMemoryManagerIsComplete(memory) != URI_TRUE) { |
| 305 | return URI_ERROR_MEMORY_MANAGER_INCOMPLETE; |
| 306 | } |
| 307 | |
| 308 | /* malloc + free*/ |
| 309 | buffer = memory->malloc(memory, mallocSize); |
| 310 | if (buffer == NULL) { |
| 311 | return URI_ERROR_MEMORY_MANAGER_FAULTY; |
| 312 | } |
| 313 | buffer[mallocSize - 1] = '\xF1'; |
| 314 | memory->free(memory, buffer); |
| 315 | buffer = NULL; |
| 316 | |
| 317 | /* calloc + free */ |
| 318 | buffer = memory->calloc(memory, callocNmemb, callocSize); |
| 319 | if (buffer == NULL) { |
| 320 | return URI_ERROR_MEMORY_MANAGER_FAULTY; |
| 321 | } |
| 322 | for (index = 0; index < callocTotalSize; index++) { /* all zeros? */ |
| 323 | if (buffer[index] != '\0') { |
| 324 | return URI_ERROR_MEMORY_MANAGER_FAULTY; |
| 325 | } |
| 326 | } |
| 327 | buffer[callocTotalSize - 1] = '\xF2'; |
| 328 | memory->free(memory, buffer); |
| 329 | buffer = NULL; |
| 330 | |
| 331 | /* malloc + realloc + free */ |
| 332 | buffer = memory->malloc(memory, mallocSize); |
| 333 | if (buffer == NULL) { |
| 334 | return URI_ERROR_MEMORY_MANAGER_FAULTY; |
| 335 | } |
| 336 | for (index = 0; index < mallocSize; index++) { |
| 337 | buffer[index] = '\xF3'; |
| 338 | } |
| 339 | buffer = memory->realloc(memory, buffer, reallocSize); |
| 340 | if (buffer == NULL) { |
| 341 | return URI_ERROR_MEMORY_MANAGER_FAULTY; |
| 342 | } |
| 343 | for (index = 0; index < mallocSize; index++) { /* previous content? */ |
| 344 | if (buffer[index] != '\xF3') { |
| 345 | return URI_ERROR_MEMORY_MANAGER_FAULTY; |