test_mem.c (3447B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 /* 5 * test_mem.c 6 * 7 * Tests Malloc, Realloc and Free 8 * 9 */ 10 11 #include "testutil.h" 12 #include "testutil_nss.h" 13 14 static void *plContext = NULL; 15 16 static void 17 testMalloc(PKIX_UInt32 **array) 18 { 19 PKIX_UInt32 i, arraySize = 10; 20 PKIX_TEST_STD_VARS(); 21 22 /* Create an integer array of size 10 */ 23 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Malloc( 24 (PKIX_UInt32)(arraySize * sizeof(unsigned int)), 25 (void **)array, plContext)); 26 27 /* Fill in some values */ 28 (void)printf("Setting array[i] = i...\n"); 29 for (i = 0; i < arraySize; i++) { 30 (*array)[i] = i; 31 if ((*array)[i] != i) 32 testError("Array has incorrect contents"); 33 } 34 35 /* Memory now reflects changes */ 36 (void)printf("\tArray: a[0] = %d, a[5] = %d, a[7] = %d.\n", 37 (*array[0]), (*array)[5], (*array)[7]); 38 39 cleanup: 40 PKIX_TEST_RETURN(); 41 } 42 43 static void 44 testRealloc(PKIX_UInt32 **array) 45 { 46 PKIX_UInt32 i, arraySize = 20; 47 PKIX_TEST_STD_VARS(); 48 49 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Realloc(*array, 50 (PKIX_UInt32)(arraySize * 51 sizeof(unsigned int)), 52 (void **)array, plContext)); 53 54 /* Fill in the new elements */ 55 (void)printf("Setting new portion of array to a[i] = i...\n"); 56 for (i = arraySize / 2; i < arraySize; i++) { 57 (*array)[i] = i; 58 if ((*array)[i] != i) 59 testError("Array has incorrect contents"); 60 } 61 62 /* New elements should be reflected. The old should be the same */ 63 (void)printf("\tArray: a[0] = %d, a[15] = %d, a[17] = %d.\n", 64 (*array)[0], (*array)[15], (*array)[17]); 65 66 cleanup: 67 PKIX_TEST_RETURN(); 68 } 69 70 static void 71 testFree(PKIX_UInt32 *array) 72 { 73 74 PKIX_TEST_STD_VARS(); 75 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(array, plContext)); 76 77 cleanup: 78 PKIX_TEST_RETURN(); 79 } 80 81 int 82 test_mem(int argc, char *argv[]) 83 { 84 85 unsigned int *array = NULL; 86 int arraySize = 10; 87 PKIX_UInt32 actualMinorVersion; 88 PKIX_UInt32 j = 0; 89 90 PKIX_TEST_STD_VARS(); 91 92 startTests("Memory Allocation"); 93 94 PKIX_TEST_EXPECT_NO_ERROR( 95 PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext)); 96 97 subTest("PKIX_PL_Malloc"); 98 testMalloc(&array); 99 100 subTest("PKIX_PL_Realloc"); 101 testRealloc(&array); 102 103 subTest("PKIX_PL_Free"); 104 testFree(array); 105 106 /* --Negative Test Cases------------------- */ 107 /* Create an integer array of size 10 */ 108 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Malloc( 109 (PKIX_UInt32)(arraySize * sizeof(unsigned int)), 110 (void **)&array, plContext)); 111 112 (void)printf("Attempting to reallocate 0 sized memory...\n"); 113 114 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Realloc(array, 0, (void **)&array, plContext)); 115 116 (void)printf("Attempting to allocate to null pointer...\n"); 117 118 PKIX_TEST_EXPECT_ERROR(PKIX_PL_Malloc(10, NULL, plContext)); 119 120 (void)printf("Attempting to reallocate to null pointer...\n"); 121 122 PKIX_TEST_EXPECT_ERROR(PKIX_PL_Realloc(NULL, 10, NULL, plContext)); 123 124 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(array, plContext)); 125 126 cleanup: 127 128 PKIX_Shutdown(plContext); 129 130 endTests("Memory Allocation"); 131 132 return (0); 133 }