00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "deque.h"
00020 using namespace std;
00021
00022
00023
00024 #pragma warning( push, 1 ) // check_ok( Q ); ==> Compiler Warning (level 1) C4675
00025 #pragma warning( disable: 4675 ) // check_ok( Q ); ==> Compiler Warning (level 1) C4675
00026
00027 #include "ADH_test.h"
00028
00029
00030 template <class T>
00031 class test_deque : public TestCase {
00032 enum {
00033 N_REPEAT = 100,
00034 };
00035 public:
00036 test_deque() { }
00037 void test_deque_deque();
00038 void test_empty();
00039 void test_size();
00040 void test_capacity();
00041 void test_front();
00042 void test_front_const();
00043 void test_back();
00044 void test_back_const();
00045
00046 void test_push_front();
00047 void test_push_back();
00048 void test_pop_front();
00049 void test_pop_back();
00050
00051 void test_at();
00052
00053 void test_example();
00054
00055
00056 bool run() {
00057
00058 test_example();
00059
00060
00061 test_deque_deque();
00062 test_empty();
00063
00064
00065 test_front();
00066 test_front_const();
00067
00068 test_back_const();
00069 test_push_front();
00070 test_push_back();
00071 test_at();
00072
00073 return nError() == 0;
00074 }
00075 };
00076
00077
00078
00079
00080
00081 template <class T>
00082 void test_deque<T>::test_deque_deque() {
00083 try {
00084 deque<T> Q(0);
00085 fail_Msg("test_deque() ==> Es incorrecto usar contenedores de tamaño cero (0)");
00086 }
00087 catch (std::length_error) {
00088 assertTrue(true);
00089 }
00090 for (int i = 1; i<N_REPEAT; ++i) {
00091 try {
00092 deque<T> Q(i);
00093 assertTrue(true);
00094 assertTrue( check_ok( Q ));
00095 }
00096 catch (...) {
00097 fail_Msg("test_deque() ==> Es correcto usar contenedores de tamaño " + ::toString(i));
00098 }
00099 }
00100 }
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 template <class T>
00112 void test_deque<T>::test_empty() {
00113 deque<T>::value_type temp;
00114 for (int i=1; i<N_REPEAT; ++i) {
00115 for (int j=0; j<i; ++j) {
00116 deque<T> Q(i);
00117 Q.m_front = j;
00118 assertTrue(Q.size() == 0);
00119 assertTrue(Q.empty());
00120 assertTrue(Q.capacity() == i);
00121 for (int k=0; k<i; ++k) {
00122 assertTrue(Q.size() == k);
00123 assertTrue(Q.capacity() == i);
00124 Q.push_back(temp);
00125 assertTrue( check_ok( Q ));
00126 assertTrue(Q.size() == k+1);
00127 assertTrue(Q.capacity() == i);
00128 }
00129 }
00130 }
00131 }
00132
00133
00134
00135
00136
00137 template <class T>
00138 void test_deque<T>::test_size() { test_empty(); }
00139
00140
00141
00142
00143
00144 template <class T>
00145 inline void test_deque<T>::test_capacity() { test_size(); }
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 template <class T>
00157 void test_deque<T>::test_front() {
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 for (int i=1; i<N_REPEAT; ++i) {
00168 for (int j=0; j<i; ++j) {
00169 deque<int> Q(i);
00170 Q.m_front = j;
00171 assertTrue(Q.size() == 0);
00172
00173 for (int k=0; k<i; ++k) {
00174 Q.push_back(k);
00175 assertTrue( check_ok( Q ));
00176 assertTrue(Q.front() == 0);
00177 assertTrue(Q.back() == k);
00178 assertTrue(Q.size() == k+1);
00179 }
00180 assertTrue(Q.front() == 0);
00181 assertTrue(Q.back() == i-1);
00182 assertTrue(Q.size()==i);
00183 for (int k=0; k<i; ++k) {
00184 assertTrue(Q.front() == k);
00185 assertTrue(Q.back() == i-1);
00186 assertTrue(Q.size() == i-k);
00187 Q.pop_front();
00188 assertTrue( check_ok( Q ));
00189 }
00190 assertTrue_Msg( "Q.size()==" + ::toString(Q.size()) , Q.empty() );
00191 }
00192 }
00193 }
00194
00195
00196 template <class T>
00197 void test_deque<T>::test_front_const() {
00198
00199
00200 const deque<T> Q_const(4);
00201 const_cast< deque<T>& >(Q_const).m_size = 1;
00202 assertTrue(Q_const.front() == Q_const.front());
00203 }
00204
00205
00206 template <class T>
00207 void test_deque<T>::test_back() { test_front(); }
00208
00209
00210 template <class T>
00211 void test_deque<T>::test_back_const() {
00212
00213
00214 const deque<T> Q_const(4);
00215 const_cast< deque<T>& >(Q_const).m_size = 1;
00216 assertTrue(Q_const.back() == Q_const.back());
00217 }
00218
00219
00220 template <class T>
00221 void test_deque<T>::test_push_front() {
00222 deque<T>::value_type temp;
00223 for (int i=1; i<N_REPEAT; ++i) {
00224 for (int j=0; j<i; ++j) {
00225 deque<T> Q(i);
00226 assertTrue(Q.size() == 0);
00227 Q.m_front = j;
00228 assertTrue(Q.size() == 0);
00229
00230 int k;
00231
00232 for (k=0; k<i; ++k) {
00233 assertTrue(Q.size() == k);
00234 Q.push_front(temp);
00235 assertTrue( check_ok( Q ));
00236 assertTrue(Q.size() == k+1);
00237 }
00238
00239 for (; k>0; --k) {
00240 assertTrue(Q.size() == k);
00241 Q.pop_front();
00242 assertTrue( check_ok( Q ));
00243 assertTrue(Q.size() == k-1);
00244 }
00245 if (!Q.empty()) {
00246 assertTrue_Msg( "k==" + ::toString(k) , k==0 );
00247 assertTrue_Msg( "Q.size()==" + ::toString(Q.size()) , Q.empty() );
00248 }
00249 }
00250 }
00251 }
00252
00253
00254 template <class T>
00255 void test_deque<T>::test_push_back() {
00256 deque<T>::value_type temp;
00257 for (int i=1; i<N_REPEAT; ++i) {
00258 for (int j=0; j<i; ++j) {
00259 deque<T> Q(i);
00260 assertTrue(Q.size() == 0);
00261 Q.m_front = j;
00262 assertTrue(Q.size() == 0);
00263
00264 int k;
00265 for (k=0; k<i; ++k) {
00266 assertTrue(Q.size() == k);
00267 Q.push_back(temp);
00268 assertTrue( check_ok( Q ));
00269 assertTrue(Q.size() == k+1);
00270 }
00271
00272 for (; k>0; --k) {
00273 assertTrue(Q.size() == k);
00274 Q.pop_back();
00275 assertTrue( check_ok( Q ));
00276 assertTrue(Q.size() == k-1);
00277 }
00278 if (!Q.empty()) {
00279 assertTrue_Msg( "k==" + ::toString(k) , k==0 );
00280 assertTrue_Msg( "Q.size()==" + ::toString(Q.size()) , Q.empty() );
00281 }
00282 }
00283 }
00284 }
00285
00286
00287 template <class T>
00288 inline void test_deque<T>::test_pop_front() { test_push_front(); }
00289
00290
00291 template <class T>
00292 inline void test_deque<T>::test_pop_back() { test_push_back(); }
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 template <class T>
00308 void test_deque<T>::test_at() {
00309 deque<int> Q(4);
00310 deque<int>::value_type *remember = Q.m_vec;
00311 try {
00312 assertTrue(Q.capacity() < N_REPEAT*2);
00313 try {
00314
00315 deque<int>::value_type *Doble_Vec;
00316 Doble_Vec = new deque<int>::value_type [ 2*N_REPEAT ];
00317 Q.m_vec = Doble_Vec;
00318 assertTrue( check_ok( Q ));
00319 }
00320 catch (...) {
00321 fail_Msg("test_at()");
00322 throw;
00323 }
00324
00325
00326 int Pruebe_Con_Estos[] ={ 1,2,3,4,5, 7,9,11,12,13, 23,50, 77,N_REPEAT-2,N_REPEAT-1 };
00327 assertTrue(77<N_REPEAT-2);
00328 int CUANTOS = sizeof(Pruebe_Con_Estos)/sizeof(*Pruebe_Con_Estos);
00329 int i,j,k;
00330 for (i=0; i<CUANTOS; ++i) {
00331 for (j=0; j<Pruebe_Con_Estos[i]; ++j) {
00332 Q.m_front = j;
00333 Q.m_size = 0;
00334 Q.m_capacity = Pruebe_Con_Estos[i];
00335
00336
00337 for (k=0; k<2*N_REPEAT; ++k) {
00338 Q.m_vec[k] = -1;
00339 }
00340
00341 for (k=0; k<Pruebe_Con_Estos[i]; ++k) {
00342 Q.push_back(k);
00343 }
00344 for (k=0; k<Pruebe_Con_Estos[i]; ++k) {
00345 assertTrue( k == Q[k] );
00346 assertTrue( k == Q.at(k) );
00347 }
00348
00349
00350
00351 int suma_euler = Q.m_capacity * (Q.m_capacity - 1) / 2;
00352
00353 deque<int>::value_type suma_real = 0;
00354 for (k=0; k<Pruebe_Con_Estos[i]; ++k) {
00355 deque<int>::value_type val = Q.front();
00356 suma_real += val;
00357 Q.pop_front();
00358 }
00359 assertTrue(suma_euler == suma_real);
00360
00361
00362 int count=0;
00363 for (k=0; k<2*N_REPEAT; ++k) {
00364 if (Q.m_vec[k] >= 0) {
00365 ++count;
00366 }
00367 else {
00368 try {
00369 int bomba = Q.at(k);
00370 fail_Msg("test_at()");
00371 }
00372 catch (std::out_of_range) {
00373
00374 assertTrue(true);
00375 }
00376 catch (...) {
00377 fail_Msg("test_at()");
00378 throw;
00379 }
00380 }
00381 }
00382 assertTrue(Q.m_capacity==count);
00383 }
00384 }
00385 }
00386 catch (...) {
00387
00388
00389
00390 }
00391
00392 delete [] Q.m_vec;
00393 Q.m_vec = remember;
00394
00395
00396 }
00397
00398
00399 template <class T>
00400 void test_deque<T>::test_example() {
00401 const unsigned SIZE = 7;
00402 {
00403 try {
00404 deque<T> Q(0);
00405 fail_Msg("deque<unsigned> Q(0);");
00406 }
00407 catch (std::logic_error&) {
00408 assertTrue(true);
00409 }
00410 catch (...) {
00411 fail_Msg("Execpción inesperada");
00412 }
00413 }
00414 assertTrue(SIZE > 0);
00415
00416 {
00417 deque<T> Q(SIZE);
00418 assertTrue( check_ok( Q ));
00419 assertTrue(Q.size() == 0);
00420 try {
00421 Q.front();
00422 fail_Msg("Q.front();");
00423 }
00424 catch (std::logic_error&) {
00425 assertTrue(true);
00426 }
00427 catch (...) {
00428 fail_Msg("Q.front();");
00429 }
00430
00431 try {
00432 Q.pop_front();
00433 fail_Msg("Q.pop_front();");
00434 }
00435 catch (std::logic_error&) {
00436 assertTrue(true);
00437 }
00438 catch (...) {
00439 fail_Msg("Q.pop_front();");
00440 }
00441 assertTrue( check_ok( Q ));
00442 }
00443
00444 {
00445 deque<unsigned> Q(SIZE);
00446 try {
00447 for (unsigned i = 0; i < SIZE; ++i) {
00448 Q.push_back(i);
00449 }
00450 assertTrue(true);
00451 assertTrue( check_ok( Q ));
00452 }
00453 catch (...) {
00454 fail_Msg("Execpción inesperada");
00455 return;
00456 }
00457
00458 assertTrue(Q.size() == SIZE);
00459 assertTrue(Q.back() == SIZE-1);
00460
00461 try {
00462 Q.push_back(SIZE);
00463 fail_Msg("Q.push_back(SIZE);");
00464 }
00465 catch (std::logic_error&) {
00466 assertTrue(true);
00467 }
00468 catch (...) {
00469 fail_Msg("Execpción inesperada");
00470 }
00471
00472 for (unsigned i = 0; i < SIZE; ++i) {
00473 assertTrue(Q.front() == i);
00474 try {
00475 Q.pop_front();
00476 assertTrue(true);
00477 }
00478 catch (...) {
00479 fail_Msg("Q.pop_front();");
00480 return;
00481 }
00482 }
00483 assertTrue(Q.size() == 0);
00484 assertTrue( check_ok( Q ) );
00485
00486 try {
00487 Q.pop_front();
00488 fail_Msg("Q.pop_front();");
00489 }
00490 catch (std::logic_error&) {
00491 assertTrue(true);
00492 }
00493 catch (...) {
00494 fail_Msg("Q.pop_front();");
00495 }
00496 }
00497 }
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512 int main_TestCase() {
00513 test_deque<int> Test_I;
00514 test_deque<float> Test_F;
00515
00516 Test_I.run();
00517 Report( cout, Test_I );
00518 Test_F.run();
00519 Report( cout, Test_F );
00520 return Test_I.nError() + Test_F.nError();
00521 }
00522
00523
00524 int main() {
00525 return main_TestCase();
00526 }
00527
00528 #pragma warning( pop ) // check_ok( Q ); ==> Compiler Warning (level 1) C4675
00529
00530