libStatGen Software
1
|
00001 /* 00002 * Copyright (C) 2011 Regents of the University of Michigan 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #include "TestSamRecordPool.h" 00019 #include "SamRecordPool.h" 00020 #include <assert.h> 00021 00022 void testSamRecordPool() 00023 { 00024 // Call generic test. 00025 SamRecordPoolTest::testSamRecordPool(); 00026 } 00027 00028 00029 void SamRecordPoolTest::testSamRecordPool() 00030 { 00031 00032 // Attempt to allocate with max size 0, 00033 // fails to get a record. 00034 SamRecordPool pool(0); 00035 assert(pool.getRecord() == NULL); 00036 00037 // Up the max size to 3. 00038 pool.setMaxAllocatedRecs(3); 00039 00040 // Successfully get 1st record. 00041 SamRecord* rec1 = pool.getRecord(); 00042 assert(rec1 != NULL); 00043 00044 // Successfully get 2nd record. 00045 SamRecord* rec2 = pool.getRecord(); 00046 assert(rec2 != NULL); 00047 assert(rec2 != rec1); 00048 00049 // Successfully get 3rd record. 00050 SamRecord* rec3 = pool.getRecord(); 00051 assert(rec3 != NULL); 00052 assert((rec3 != rec1) && (rec3 != rec2)); 00053 00054 // Fail to get a 4th record. 00055 assert(pool.getRecord() == NULL); 00056 00057 // Release a record and confirm its reuse. 00058 pool.releaseRecord(rec2); 00059 SamRecord* rec = pool.getRecord(); 00060 assert(rec == rec2); 00061 00062 // Release multiple records and check reuse. 00063 pool.releaseRecord(rec3); 00064 pool.releaseRecord(rec1); 00065 pool.releaseRecord(rec); 00066 SamRecord* release1 = pool.getRecord(); 00067 SamRecord* release2 = pool.getRecord(); 00068 SamRecord* release3 = pool.getRecord(); 00069 assert(release1 == rec3); 00070 assert(release2 == rec1); 00071 assert(release3 == rec); 00072 assert(pool.getRecord() == NULL); 00073 00074 // Up the max allocated size but don't allocate any, then 00075 // reduce the max allocated size and release all the records 00076 // but the already allocated records will still be used. 00077 pool.setMaxAllocatedRecs(4); 00078 pool.setMaxAllocatedRecs(0); 00079 pool.releaseRecord(release3); 00080 pool.releaseRecord(release1); 00081 pool.releaseRecord(release2); 00082 rec1 = pool.getRecord(); 00083 rec2 = pool.getRecord(); 00084 rec3 = pool.getRecord(); 00085 assert(rec1 == release3); 00086 assert(rec2 == release1); 00087 assert(rec3 == release2); 00088 assert(pool.getRecord() == NULL); 00089 00090 00091 // Up the max allocated size and allocate another record. 00092 pool.setMaxAllocatedRecs(4); 00093 rec = pool.getRecord(); 00094 assert(rec != NULL); 00095 assert(rec != rec1); 00096 assert(rec != rec2); 00097 assert(rec != rec3); 00098 assert(pool.getRecord() == NULL); 00099 }