Changeset 637
- Timestamp:
- May 10, 2004, 5:14:49 PM (22 years ago)
- Location:
- trunk/psLib/test/sysUtils
- Files:
-
- 1 added
- 2 edited
-
Makefile (modified) (3 diffs)
-
tst_psMemory.c (modified) (7 diffs)
-
verified/tst_psMemory.stderr (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/test/sysUtils/Makefile
r629 r637 3 3 ## Makefile: test/sysUtils 4 4 ## 5 ## $Revision: 1. 8$ $Name: not supported by cvs2svn $6 ## $Date: 2004-05-1 0 18:22:22$5 ## $Revision: 1.9 $ $Name: not supported by cvs2svn $ 6 ## $Date: 2004-05-11 03:14:49 $ 7 7 ## 8 8 ## Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 39 39 all: $(TARGET) 40 40 41 atst_psAbort_01: atst_psAbort_01.o42 atst_psAbort_02: atst_psAbort_02.o43 atst_psAbort_03: atst_psAbort_03.o44 tst_psMemory: tst_psMemory.o45 tst_psError: tst_psError.o46 tst_psHash00: tst_psHash00.o47 tst_psHash01: tst_psHash01.o48 tst_psHash02: tst_psHash02.o49 tst_psHash03: tst_psHash03.o50 tst_psHash04: tst_psHash04.o51 tst_psLogMsg00: tst_psLogMsg00.o52 tst_psLogMsg01: tst_psLogMsg01.o53 tst_psLogMsg02: tst_psLogMsg02.o54 tst_psLogMsg03: tst_psLogMsg03.o55 tst_psStringCopy: tst_psStringCopy.o56 tst_psTrace00: tst_psTrace00.o57 tst_psTrace01: tst_psTrace01.o58 tst_psTrace02: tst_psTrace02.o59 tst_psTrace03: tst_psTrace03.o60 tst_psTrace04: tst_psTrace04.o41 atst_psAbort_01: atst_psAbort_01.o 42 atst_psAbort_02: atst_psAbort_02.o 43 atst_psAbort_03: atst_psAbort_03.o 44 tst_psMemory: tst_psMemory.o 45 tst_psError: tst_psError.o 46 tst_psHash00: tst_psHash00.o 47 tst_psHash01: tst_psHash01.o 48 tst_psHash02: tst_psHash02.o 49 tst_psHash03: tst_psHash03.o 50 tst_psHash04: tst_psHash04.o 51 tst_psLogMsg00: tst_psLogMsg00.o 52 tst_psLogMsg01: tst_psLogMsg01.o 53 tst_psLogMsg02: tst_psLogMsg02.o 54 tst_psLogMsg03: tst_psLogMsg03.o 55 tst_psStringCopy: tst_psStringCopy.o 56 tst_psTrace00: tst_psTrace00.o 57 tst_psTrace01: tst_psTrace01.o 58 tst_psTrace02: tst_psTrace02.o 59 tst_psTrace03: tst_psTrace03.o 60 tst_psTrace04: tst_psTrace04.o 61 61 62 62 clean: … … 65 65 66 66 %.o : %.c 67 $(CC) $(CFLAGS) $(CPPFLAGS) -I $(PSLIB_INCL_DIR) -c -o $@ $<67 $(CC) $(CFLAGS) $(CPPFLAGS) -I.. -I$(PSLIB_INCL_DIR) -c -o $@ $< 68 68 69 69 % : %.o 70 $(CC) $(LDFLAGS) -L $(PSLIB_LIB_DIR) -lpslib -lpstest -o $@ $<70 $(CC) $(LDFLAGS) -L.. -L$(PSLIB_LIB_DIR) -lpslib -lpstest -o $@ $< 71 71 72 72 %.lint: %.c -
trunk/psLib/test/sysUtils/tst_psMemory.c
r540 r637 1 /** @file tst Memory.c1 /** @file tst_psMemory.c 2 2 * 3 3 * @brief Contains the tests for psMemory.[ch] … … 6 6 * @author Robert DeSonia, MHPCC 7 7 * 8 * @version $Revision: 1. 2$ $Name: not supported by cvs2svn $9 * @date $Date: 2004-0 4-28 02:50:56$8 * @version $Revision: 1.3 $ $Name: not supported by cvs2svn $ 9 * @date $Date: 2004-05-11 03:14:49 $ 10 10 * 11 11 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 12 12 */ 13 14 // need to allow malloc for callback use 15 #define PS_ALLOW_MALLOC 1 13 16 14 17 #include <unistd.h> … … 16 19 #include <sys/types.h> 17 20 #include <sys/stat.h> 21 #include <limits.h> 22 #include <stdlib.h> 23 18 24 19 25 #include "psTest.h" 20 #include "psMemory.h" 21 #include "psAbort.h" 26 #include "psLib.h" 22 27 23 28 static int TPFreeReferencedMemory(void); 29 static int TPOutOfMemory(void); 30 static void* TPOutOfMemoryExhaustedCallback(size_t size); 24 31 static int TPCheckBufferPositive(void); 32 static int TPrealloc(void); 33 static int TPallocCallback(void); 34 static long memAllocateCallback(const psMemBlock *ptr); 35 static long memFreeCallback(const psMemBlock *ptr); 36 static int allocCallbackCalled = 0; 37 static int freeCallbackCalled = 0; 38 39 bool TPOutOfMemoryExhaustedCallbackCalled = false; 25 40 26 41 testDescription tests[] = { 27 {TPCheckBufferPositive,"TPCheckBufferPositive",0}, 42 {TPCheckBufferPositive,"449-TPCheckBufferPositive",0}, 43 {TPOutOfMemory,"450-TPOutOfMemory",0}, 44 {TPrealloc,"451-TPrealloc",0}, 45 {TPallocCallback,"452-TPallocCallback",0}, 28 46 {TPFreeReferencedMemory,"TPFreeReferencedMemory",6}, 29 47 {NULL} … … 32 50 int main() 33 51 { 34 runTestSuite(stderr,"psMemory",tests); 35 } 36 52 psSetLogLevel(PS_LOG_INFO); 53 54 if (! runTestSuite(stderr,"psMemory",tests)) { 55 psAbort(__FILE__,"One or more tests failed"); 56 } 57 return 0; 58 } 59 60 // Testpoint #449, psAlloc shall allocate memory blocks writeable by caller. 37 61 int TPCheckBufferPositive(void) 38 62 { … … 40 64 const int size = 100; 41 65 int failed = 0; 42 43 printf("Allocating buffer\n"); 66 int currentId = psMemGetId(); 67 68 psLogMsg(__func__,PS_LOG_INFO,"psAlloc shall allocate memory blocks writeable by caller.\n"); 69 44 70 mem = (int*) psAlloc(size*sizeof(int)); 71 if (mem == NULL) { 72 psError(__FILE__,"psAlloc returned a NULL value in %s!",__func__); 73 return 1; 74 } 45 75 46 76 for (int index=0;index<size;index++) { … … 54 84 } 55 85 86 psMemCheckLeaks(currentId,NULL,NULL); 87 psMemCheckCorruption(1); 88 56 89 return failed; 57 90 } … … 61 94 // create memory 62 95 int* mem; 63 64 printf("Allocating buffer.\n"); 96 int currentId = psMemGetId(); 97 98 fprintf(stderr,"Allocating buffer.\n"); 65 99 mem = (int*) psAlloc(100*sizeof(int)); 66 100 67 101 psMemIncrRefCounter(mem); 68 printf("Freeing buffer after incrementing reference count.\n");102 fprintf(stderr,"Freeing buffer after incrementing reference count.\n"); 69 103 psFree(mem); 70 104 71 return 0; 72 } 73 105 psMemCheckLeaks(currentId,NULL,NULL); 106 psMemCheckCorruption(1); 107 108 return 0; 109 } 110 111 // Testpoint #450, Upon requesting more memory than is available, psalloc shall call 112 // the psMemExhaustedCallback. 113 int TPOutOfMemory(void) 114 { 115 int currentId = psMemGetId(); 116 int* mem; 117 psMemExhaustedCallback cb; 118 119 psLogMsg(__func__,PS_LOG_INFO,"Upon requesting more memory than is available, psalloc shall call " 120 "the psMemExhaustedCallback.\n"); 121 122 TPOutOfMemoryExhaustedCallbackCalled = false; 123 124 cb = psMemExhaustedCallbackSet(TPOutOfMemoryExhaustedCallback); 125 126 mem = (int*) psAlloc(INT_MAX); 127 128 psMemExhaustedCallbackSet(cb); 129 130 if (!TPOutOfMemoryExhaustedCallbackCalled) { 131 psError(__FILE__,"Called psAlloc with HUGE memory requirement and survived in %s!",__func__); 132 return 1; 133 } 134 135 psFree(mem); 136 137 psMemCheckLeaks(currentId,NULL,NULL); 138 psMemCheckCorruption(1); 139 140 return 0; 141 } 142 143 144 145 void* TPOutOfMemoryExhaustedCallback(size_t size) 146 { 147 psLogMsg(__func__,PS_LOG_INFO,"Custom MemExhaustedCallback was invoked."); 148 TPOutOfMemoryExhaustedCallbackCalled = true; 149 return malloc(1024); 150 } 151 152 // Testpoint #451, psRealloc shall increase/decrease memory buffer while preserving contents 153 int TPrealloc(void) 154 { 155 int currentId = psMemGetId(); 156 int* mem1; 157 int* mem2; 158 int* mem3; 159 const int initialSize = 100; 160 161 psLogMsg(__func__,PS_LOG_INFO,"psRealloc shall increase/decrease memory buffer while " 162 "preserving contents"); 163 164 // allocate buffer with known values. 165 mem1 = (int*) psAlloc(initialSize*sizeof(int)); 166 mem2 = (int*) psAlloc(initialSize*sizeof(int)); 167 mem3 = (int*) psAlloc(initialSize*sizeof(int)); 168 for (int lcv=0;lcv<initialSize;lcv++) { 169 mem1[lcv] = mem2[lcv] = mem3[lcv] = lcv; 170 } 171 172 psMemCheckCorruption(1); 173 psLogMsg(__func__,PS_LOG_INFO,"Expanding memory buffer."); 174 175 // realloc to 2x 176 mem1 = (int*) psRealloc(mem1, 2*initialSize*sizeof(int)); 177 mem2 = (int*) psRealloc(mem2, 2*initialSize*sizeof(int)); 178 mem3 = (int*) psRealloc(mem3, 2*initialSize*sizeof(int)); 179 180 // check values of initial block 181 for (int i=0;i<initialSize;i++) { 182 if (mem1[i] != i || mem2[i] != i || mem3[i] != i) { 183 psError(__FILE__,"Realloc didn't preserve the contents with expanding buffer in %s.", 184 __func__); 185 break; 186 } 187 } 188 189 psMemCheckCorruption(1); 190 psLogMsg(__func__,PS_LOG_INFO,"Shrinking memory buffer."); 191 192 // realloc to 1/2 initial value. 193 mem1 = (int*) psRealloc(mem1, (initialSize/2)*sizeof(int)); 194 mem2 = (int*) psRealloc(mem2, (initialSize/2)*sizeof(int)); 195 mem3 = (int*) psRealloc(mem3, (initialSize/2)*sizeof(int)); 196 197 // check values of initial block 198 for (int i=0;i<initialSize/2;i++) { 199 if (mem1[i] != i || mem2[i] != i || mem3[i] != i) { 200 psError(__FILE__,"Realloc didn't preserve the contents with shrinking buffer in %s.", 201 __func__); 202 break; 203 } 204 } 205 206 psFree(mem1); 207 psFree(mem2); 208 psFree(mem3); 209 210 psMemCheckLeaks(currentId,NULL,NULL); 211 psMemCheckCorruption(1); 212 213 return 0; 214 } 215 216 int TPallocCallback(void) 217 { 218 int* mem1; 219 int* mem2; 220 int* mem3; 221 int currentId = psMemGetId(); 222 const int initialSize = 100; 223 int mark; 224 225 allocCallbackCalled = 0; 226 freeCallbackCalled = 0; 227 psMemAllocateCallbackSet(memAllocateCallback); 228 psMemFreeCallbackSet(memFreeCallback); 229 230 psMemAllocateCallbackSetID(currentId+1); 231 psMemFreeCallbackSetID(currentId+1); 232 233 psLogMsg(__func__,PS_LOG_INFO,"call to psAlloc/psRealloc shall generate a callback if specified " 234 "memory ID is allocated."); 235 236 // allocate buffer with known values. 237 mem1 = (int*) psAlloc(initialSize*sizeof(int)); 238 mem2 = (int*) psAlloc(initialSize*sizeof(int)); 239 mem3 = (int*) psAlloc(initialSize*sizeof(int)); 240 241 psFree(mem1); 242 psFree(mem2); 243 psFree(mem3); 244 245 if (allocCallbackCalled != 2 || freeCallbackCalled != 2) { 246 psError(__FILE__,"alloc/free callbacks were not called the proper number of times in %s", 247 __func__); 248 return 1; 249 } 250 251 allocCallbackCalled = 0; 252 freeCallbackCalled = 0; 253 254 mark = psMemGetId(); 255 256 mem1 = (int*) psAlloc(initialSize*sizeof(int)); 257 258 psMemAllocateCallbackSetID(mark); 259 260 mem1 = (int*) psRealloc(mem1,initialSize*2*sizeof(int)); 261 262 psFree(mem1); 263 264 if (allocCallbackCalled != 2) { 265 psError(__FILE__,"realloc callbacks were not called the proper number of times in %s", 266 __func__); 267 return 1; 268 } 269 return 0; 270 271 } 272 273 long memAllocateCallback(const psMemBlock *ptr) 274 { 275 psLogMsg(__func__,PS_LOG_INFO,"block %d was (re)allocated",ptr->id); 276 allocCallbackCalled++; 277 return 1; 278 } 279 280 long memFreeCallback(const psMemBlock *ptr) 281 { 282 psLogMsg(__func__,PS_LOG_INFO,"block %d was freed",ptr->id); 283 freeCallbackCalled++; 284 return 1; 285 }
Note:
See TracChangeset
for help on using the changeset viewer.
