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 #include "ReusableVector.h" 00018 #include "ReusableVectorTest.h" 00019 #include <assert.h> 00020 #include <iostream> 00021 #include <string.h> 00022 00023 int ReusableVectorTestDataType::ourValue = 0; 00024 int ReusableVectorTestDataType::ourNumDestructs = 0; 00025 00026 int main(int argc, char ** argv) 00027 { 00028 00029 ReusableVectorTest myTest; 00030 00031 myTest.test(); 00032 } 00033 00034 void ReusableVectorTest::test() 00035 { 00036 assert(ReusableVectorTestDataType::ourNumDestructs == 0); 00037 testReuse(); 00038 assert(ReusableVectorTestDataType::ourNumDestructs == 8); 00039 } 00040 00041 00042 void ReusableVectorTest::testReuse() 00043 { 00044 ReusableVector<ReusableVectorTestDataType> testVector; 00045 ReusableVector<ReusableVectorTestDataType> testVector2; 00046 ReusableVectorTestDataType* dataPtr = NULL; 00047 00048 assert(testVector.size() == 0); 00049 assert(testInvalidGetIndex(testVector, 0)); 00050 assert(testInvalidGetIndex(testVector, 1)); 00051 testVector.reset(); 00052 assert(testVector.size() == 0); 00053 assert(testInvalidGetIndex(testVector, 0)); 00054 assert(testInvalidGetIndex(testVector, 1)); 00055 00056 // Get three data pointers and check they are each new. 00057 dataPtr = &(testVector.getNextEmpty()); 00058 assert(dataPtr->myValue == 0); 00059 assert(dataPtr->ourValue == 1); 00060 dataPtr = &(testVector.getNextEmpty()); 00061 assert(dataPtr->myValue == 1); 00062 assert(dataPtr->ourValue == 2); 00063 dataPtr = &(testVector.getNextEmpty()); 00064 assert(dataPtr->myValue == 2); 00065 assert(dataPtr->ourValue == 3); 00066 assert(testVector.size() == 3); 00067 00068 // Check a 2nd test vector. 00069 assert(testVector2.size() == 0); 00070 assert(testInvalidGetIndex(testVector2, 0)); 00071 assert(testInvalidGetIndex(testVector2, 1)); 00072 testVector2.reset(); 00073 assert(testVector2.size() == 0); 00074 assert(testInvalidGetIndex(testVector2, 0)); 00075 assert(testInvalidGetIndex(testVector2, 1)); 00076 00077 // Get data pointers and check they are each new. 00078 dataPtr = &(testVector2.getNextEmpty()); 00079 assert(dataPtr->myValue == 3); 00080 assert(dataPtr->ourValue == 4); 00081 dataPtr = &(testVector2.getNextEmpty()); 00082 assert(dataPtr->myValue == 4); 00083 assert(dataPtr->ourValue == 5); 00084 assert(testVector2.size() == 2); 00085 00086 // Test the get accessor. 00087 assert(testVector2.get(1).myValue == 4); 00088 assert(testVector2.get(0).myValue == 3); 00089 assert(testInvalidGetIndex(testVector2, 2)); 00090 // Test the get accessor with the first vector. 00091 assert(testVector.get(1).myValue == 1); 00092 assert(testVector.get(0).myValue == 0); 00093 assert(testVector.get(2).myValue == 2); 00094 assert(testInvalidGetIndex(testVector, 3)); 00095 00096 // Clear the 1st vector. 00097 testVector.clear(); 00098 assert(testVector.size() == 0); 00099 assert(testInvalidGetIndex(testVector, 0)); 00100 assert(testInvalidGetIndex(testVector, 1)); 00101 00102 // Check the data values are reused. 00103 dataPtr = &(testVector.getNextEmpty()); 00104 assert(dataPtr->myValue == 0); 00105 assert(dataPtr->ourValue == 5); 00106 assert(testVector.size() == 1); 00107 dataPtr = &(testVector.getNextEmpty()); 00108 assert(dataPtr->myValue == 1); 00109 assert(dataPtr->ourValue == 5); 00110 assert(testVector.size() == 2); 00111 dataPtr = &(testVector.getNextEmpty()); 00112 assert(dataPtr->myValue == 2); 00113 assert(dataPtr->ourValue == 5); 00114 assert(testVector.size() == 3); 00115 // Test allocating a new value. 00116 dataPtr = &(testVector.getNextEmpty()); 00117 assert(dataPtr->myValue == 5); 00118 assert(dataPtr->ourValue == 6); 00119 assert(testVector.size() == 4); 00120 00121 // Clear both vectors. 00122 testVector2.clear(); 00123 testVector.reset(); 00124 assert(testVector.size() == 0); 00125 assert(testInvalidGetIndex(testVector, 0)); 00126 assert(testInvalidGetIndex(testVector, 1)); 00127 assert(testVector2.size() == 0); 00128 assert(testInvalidGetIndex(testVector2, 0)); 00129 assert(testInvalidGetIndex(testVector2, 1)); 00130 00131 // Get values for the vectors and verify they are reused. 00132 dataPtr = &(testVector2.getNextEmpty()); 00133 assert(dataPtr->myValue == 3); 00134 assert(dataPtr->ourValue == 6); 00135 assert(testVector2.size() == 1); 00136 dataPtr = &(testVector.getNextEmpty()); 00137 assert(dataPtr->myValue == 0); 00138 assert(dataPtr->ourValue == 6); 00139 assert(testVector.size() == 1); 00140 dataPtr = &(testVector2.getNextEmpty()); 00141 assert(dataPtr->myValue == 4); 00142 assert(dataPtr->ourValue == 6); 00143 assert(testVector2.size() == 2); 00144 dataPtr = &(testVector2.getNextEmpty()); 00145 assert(dataPtr->myValue == 6); 00146 assert(dataPtr->ourValue == 7); 00147 assert(testVector2.size() == 3); 00148 dataPtr = &(testVector.getNextEmpty()); 00149 assert(dataPtr->myValue == 1); 00150 assert(dataPtr->ourValue == 7); 00151 assert(testVector.size() == 2); 00152 dataPtr = &(testVector.getNextEmpty()); 00153 assert(dataPtr->myValue == 2); 00154 assert(dataPtr->ourValue == 7); 00155 assert(testVector.size() == 3); 00156 dataPtr = &(testVector.getNextEmpty()); 00157 assert(dataPtr->myValue == 5); 00158 assert(dataPtr->ourValue == 7); 00159 assert(testVector.size() == 4); 00160 dataPtr = &(testVector.getNextEmpty()); 00161 assert(dataPtr->myValue == 7); 00162 assert(dataPtr->ourValue == 8); 00163 assert(testVector.size() == 5); 00164 } 00165 00166 00167 bool ReusableVectorTest::testInvalidGetIndex(ReusableVector<ReusableVectorTestDataType>& testVector, int index) 00168 { 00169 bool caught = false; 00170 try 00171 { 00172 testVector.get(index); 00173 } 00174 catch(std::exception& e) 00175 { 00176 caught = true; 00177 assert(strcmp(e.what(), "ReusableVector::get called with out of range index.") == 0); 00178 } 00179 return(caught); 00180 } 00181 00182 00183 ReusableVectorTestDataType::ReusableVectorTestDataType() 00184 { 00185 myValue = ourValue++; 00186 } 00187 00188 00189 ReusableVectorTestDataType::~ReusableVectorTestDataType() 00190 { 00191 ++ourNumDestructs; 00192 } 00193