00001
00012 #include <sstream>
00013
00014 #include "Elias.hpp"
00015 #include "EliasCasoPrueba.hpp"
00016
00017 CPPUNIT_TEST_SUITE_REGISTRATION(EliasCasoPrueba);
00018
00019 using namespace std;
00020
00021
00022
00023
00024 #define HEX__(n) 0x##n##LU
00025
00026
00027 #define B8__(x) ((x&0x0000000FLU)?1:0) \
00028 +((x&0x000000F0LU)?2:0) \
00029 +((x&0x00000F00LU)?4:0) \
00030 +((x&0x0000F000LU)?8:0) \
00031 +((x&0x000F0000LU)?16:0) \
00032 +((x&0x00F00000LU)?32:0) \
00033 +((x&0x0F000000LU)?64:0) \
00034 +((x&0xF0000000LU)?128:0)
00035
00036
00037
00038
00039 #define B8(d) ((unsigned char)B8__(HEX__(d)))
00040
00041
00042 #define B16(dmsb,dlsb) (((unsigned long)B8(dmsb)<< 8) + (unsigned long)B8(dlsb))
00043
00044
00045
00046 #define B32(dmsb,db2,db3,dlsb) (((unsigned long)B8(dmsb)<<24) \
00047 + ((unsigned long)B8(db2)<<16) \
00048 + ((unsigned long)B8(db3)<< 8) \
00049 + B8(dlsb))
00050
00051
00052
00053
00054
00055
00056
00057
00058 void EliasCasoPrueba::prueba_piso_logb2()
00059 {
00060 CPPUNIT_ASSERT_THROW(piso_logb2(0), std::string );
00061
00062 CPPUNIT_ASSERT(piso_logb2((unsigned int)1) == 0);
00063 CPPUNIT_ASSERT(piso_logb2((unsigned int)2) == 1);
00064 CPPUNIT_ASSERT(piso_logb2((unsigned int)5) == 2);
00065 CPPUNIT_ASSERT(piso_logb2((unsigned int)7) == 2);
00066 CPPUNIT_ASSERT(piso_logb2((unsigned int)8) == 3);
00067 }
00068
00069 void EliasCasoPrueba::prueba_techo_logb2()
00070 {
00071 CPPUNIT_ASSERT_THROW(techo_logb2(0), std::string);
00072
00073 CPPUNIT_ASSERT(techo_logb2(1) == 0);
00074 CPPUNIT_ASSERT(techo_logb2(2) == 1);
00075 CPPUNIT_ASSERT(techo_logb2(5) == 3);
00076 CPPUNIT_ASSERT(techo_logb2(7) == 3);
00077 CPPUNIT_ASSERT(techo_logb2(8) == 3);
00078 CPPUNIT_ASSERT(techo_logb2(9) == 4);
00079 }
00080
00081 void EliasCasoPrueba::prueba_pot2()
00082 {
00083 CPPUNIT_ASSERT(pot2(0) == 1);
00084 CPPUNIT_ASSERT(pot2(1) == 2);
00085 CPPUNIT_ASSERT(pot2(2) == 4);
00086 CPPUNIT_ASSERT(pot2(3) == 8);
00087 }
00088
00089 void EliasCasoPrueba::prueba_unario()
00090 {
00091
00092 vector<bool> vb(0);
00093
00094 CPPUNIT_ASSERT_THROW(codifica_unario(0, vb), std::string);
00095
00096 codifica_unario(10, vb);
00097 CPPUNIT_ASSERT(vb2str(vb) == "1111111110");
00098
00099 long r = decodifica_unario(vb);
00100 CPPUNIT_ASSERT(r == 10);
00101 vb.clear();
00102 }
00103
00104 void EliasCasoPrueba::prueba_pone_un_entero()
00105 {
00106 vector<bool> vb(0);
00107
00108 pone_un_entero(1, 2, vb);
00109 CPPUNIT_ASSERT(vb2str(vb) == "01");
00110 vb.clear();
00111 pone_un_entero(2, 2, vb);
00112 CPPUNIT_ASSERT(vb2str(vb) == "10");
00113 vb.clear();
00114 pone_un_entero(3, 3, vb);
00115 CPPUNIT_ASSERT(vb2str(vb) == "011");
00116 }
00117
00118 void EliasCasoPrueba::prueba_toma_un_entero()
00119 {
00120 vector<bool> vb(0);
00121 CPPUNIT_ASSERT_THROW(toma_un_entero(10,vb), std::string);
00122
00123 vb.clear();
00124 vb = str2vb("0");
00125 CPPUNIT_ASSERT(toma_un_entero(1,vb) == 0);
00126 vb.clear();
00127 vb = str2vb("01");
00128 CPPUNIT_ASSERT(toma_un_entero(2,vb) == 1);
00129 vb.clear();
00130 vb = str2vb("101");
00131 CPPUNIT_ASSERT(toma_un_entero(3,vb) == 5);
00132 }
00133
00134
00135 void EliasCasoPrueba::prueba_codifica_elias_gama()
00136 {
00137 vector<bool> vb(0);
00138
00139 CPPUNIT_ASSERT_THROW(codifica_elias_gama(0,vb), std::string);
00140
00141 codifica_elias_gama(1, vb);
00142 CPPUNIT_ASSERT(vb2str(vb) == "0");
00143 vb.clear();
00144 codifica_elias_gama(2, vb);
00145 CPPUNIT_ASSERT(vb2str(vb) == "100");
00146 vb.clear();
00147 codifica_elias_gama(3, vb);
00148 CPPUNIT_ASSERT(vb2str(vb) == "101");
00149 vb.clear();
00150 codifica_elias_gama(4, vb);
00151 CPPUNIT_ASSERT(vb2str(vb) == "11000");
00152 vb.clear();
00153 codifica_elias_gama(5, vb);
00154 CPPUNIT_ASSERT(vb2str(vb) == "11001");
00155 vb.clear();
00156 codifica_elias_gama(6, vb);
00157 CPPUNIT_ASSERT(vb2str(vb) == "11010");
00158 vb.clear();
00159 codifica_elias_gama(7, vb);
00160 CPPUNIT_ASSERT(vb2str(vb) == "11011");
00161 vb.clear();
00162 codifica_elias_gama(8, vb);
00163 string rs = vb2str(vb);
00164 CPPUNIT_ASSERT(rs == "1110000");
00165 vb.clear();
00166 codifica_elias_gama(9, vb);
00167 CPPUNIT_ASSERT(vb2str(vb) == "1110001");
00168 }
00169
00170
00171 void EliasCasoPrueba::prueba_decodifica_elias_gama()
00172 {
00173 vector<bool> vb(0);
00174
00175 CPPUNIT_ASSERT_THROW(decodifica_elias_gama(vb), std::string);
00176
00177 vb.clear();
00178 vb = str2vb("1110001");
00179 CPPUNIT_ASSERT(decodifica_elias_gama(vb) == 9);
00180 vb.clear();
00181 vb = str2vb("1110000");
00182 CPPUNIT_ASSERT(decodifica_elias_gama(vb) == 8);
00183 vb.clear();
00184 vb = str2vb("11011");
00185 CPPUNIT_ASSERT(decodifica_elias_gama(vb) == 7);
00186 vb.clear();
00187 vb = str2vb("11010");
00188 CPPUNIT_ASSERT(decodifica_elias_gama(vb) == 6);
00189 vb.clear();
00190 vb = str2vb("11001");
00191 CPPUNIT_ASSERT(decodifica_elias_gama(vb) == 5);
00192 vb.clear();
00193 vb = str2vb("11000");
00194 CPPUNIT_ASSERT(decodifica_elias_gama(vb) == 4);
00195 vb.clear();
00196 vb = str2vb("101");
00197 CPPUNIT_ASSERT(decodifica_elias_gama(vb) == 3);
00198 vb.clear();
00199 vb = str2vb("100");
00200 CPPUNIT_ASSERT(decodifica_elias_gama(vb) == 2);
00201 vb.clear();
00202 vb = str2vb("0");
00203 CPPUNIT_ASSERT(decodifica_elias_gama(vb) == 1);
00204 }
00205
00206
00207 void EliasCasoPrueba::prueba_long_elias_gama()
00208 {
00209
00210 CPPUNIT_ASSERT_THROW((void)long_elias_gama(0), std::string);
00211
00212 CPPUNIT_ASSERT(long_elias_gama(1) == 1);
00213 CPPUNIT_ASSERT(long_elias_gama(2) == 1);
00214 CPPUNIT_ASSERT(long_elias_gama(3) == 1);
00215 CPPUNIT_ASSERT(long_elias_gama(4) == 1);
00216 CPPUNIT_ASSERT(long_elias_gama(5) == 1);
00217 CPPUNIT_ASSERT(long_elias_gama(6) == 1);
00218 CPPUNIT_ASSERT(long_elias_gama(7) == 1);
00219 CPPUNIT_ASSERT(long_elias_gama(8) == 1);
00220 CPPUNIT_ASSERT(long_elias_gama(9) == 1);
00221 CPPUNIT_ASSERT(long_elias_gama(15) == 1);
00222 CPPUNIT_ASSERT(long_elias_gama(16) == 2);
00223 CPPUNIT_ASSERT(long_elias_gama(17) == 2);
00224 CPPUNIT_ASSERT(long_elias_gama(31) == 2);
00225 CPPUNIT_ASSERT(long_elias_gama(32) == 2);
00226 CPPUNIT_ASSERT(long_elias_gama(33) == 2);
00227 CPPUNIT_ASSERT(long_elias_gama(63) == 2);
00228 CPPUNIT_ASSERT(long_elias_gama(64) == 2);
00229 CPPUNIT_ASSERT(long_elias_gama(65) == 2);
00230 CPPUNIT_ASSERT(long_elias_gama(127) == 2);
00231 CPPUNIT_ASSERT(long_elias_gama(128) == 2);
00232 CPPUNIT_ASSERT(long_elias_gama(129) == 2);
00233 CPPUNIT_ASSERT(long_elias_gama(255) == 2);
00234 CPPUNIT_ASSERT(long_elias_gama(256) == 3);
00235 CPPUNIT_ASSERT(long_elias_gama(257) == 3);
00236 CPPUNIT_ASSERT(long_elias_gama(4095) == 3);
00237 CPPUNIT_ASSERT(long_elias_gama(4096) == 4);
00238 CPPUNIT_ASSERT(long_elias_gama(4097) == 4);
00239
00240 CPPUNIT_ASSERT(long_elias_gama(pot2(16)-1) == 4);
00241 CPPUNIT_ASSERT(long_elias_gama(pot2(16)) == 5);
00242 CPPUNIT_ASSERT(long_elias_gama(pot2(16)+1) == 5);
00243
00244 CPPUNIT_ASSERT(long_elias_gama(pot2(32)-1) == 8);
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 }
00272
00273
00274
00275 void EliasCasoPrueba::prueba_escribe_elias_gama()
00276 {
00277 vector<bool> vb(0);
00278
00279 stringstream ss;
00280 ss.clear();
00281 ss.str("");
00282
00283 CPPUNIT_ASSERT_THROW(escribe_elias_gama(ss, 0), std::string);
00284
00285 escribe_elias_gama(ss, 1);
00286
00287 CPPUNIT_ASSERT(ss.str()[0] == (char)0);
00288 ss.clear();
00289 ss.str("");
00290 escribe_elias_gama(ss, 2);
00291
00292 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(10000000));
00293 ss.clear();
00294 ss.str("");
00295 escribe_elias_gama(ss, 3);
00296
00297 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(10100000));
00298 ss.clear();
00299 ss.str("");
00300 escribe_elias_gama(ss, 4);
00301
00302 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11000000));
00303 ss.clear();
00304 ss.str("");
00305 escribe_elias_gama(ss, 5);
00306 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11001000));
00307 ss.clear();
00308 ss.str("");
00309 escribe_elias_gama(ss, 6);
00310 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11010000));
00311 ss.clear();
00312 ss.str("");
00313 escribe_elias_gama(ss, 7);
00314 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11011000));
00315 ss.clear();
00316 ss.str("");
00317 escribe_elias_gama(ss, 8);
00318 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11100000));
00319 ss.clear();
00320 ss.str("");
00321 escribe_elias_gama(ss, 9);
00322 string rs = ss.str();
00323 CPPUNIT_ASSERT(rs[0] == (char)B8(11100010));
00324 ofstream os("gama.elias", ios::out | ios::binary);
00325 escribe_elias_gama(os, 16);
00326 os.close();
00327
00328 ss.str("");
00329 ss.clear();
00330 escribe_elias_gama(ss, 16);
00331 rs = ss.str();
00332 CPPUNIT_ASSERT(rs[0] == (char)B8(11110000));
00333 CPPUNIT_ASSERT(rs[1] == (char)B8(00000000));
00334
00335 ss.str("");
00336 ss.clear();
00337 escribe_elias_gama(ss, 32);
00338 rs = ss.str();
00339 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111000));
00340 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(00000000));
00341
00342 ss.str("");
00343 ss.clear();
00344 escribe_elias_gama(ss, 64);
00345 rs = ss.str();
00346 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111100));
00347 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(00000000));
00348
00349 ss.str("");
00350 ss.clear();
00351 escribe_elias_gama(ss, 128);
00352 rs = ss.str();
00353 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111110));
00354 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(00000000));
00355
00356 ss.str("");
00357 ss.clear();
00358 escribe_elias_gama(ss, 256);
00359 rs = ss.str();
00360 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111111));
00361 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(00000000));
00362 CPPUNIT_ASSERT(ss.str()[2] == (char)B8(00000000));
00363
00364 ss.str("");
00365 ss.clear();
00366 escribe_elias_gama(ss, 512);
00367 rs = ss.str();
00368 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111111));
00369 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(10000000));
00370 CPPUNIT_ASSERT(ss.str()[2] == (char)B8(00000000));
00371
00372 ss.str("");
00373 ss.clear();
00374 escribe_elias_gama(ss, 1024);
00375 rs = ss.str();
00376 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111111));
00377 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(11000000));
00378 CPPUNIT_ASSERT(ss.str()[2] == (char)B8(00000000));
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 ss.str("");
00399 ss.clear();
00400 escribe_elias_gama(ss, 65536);
00401 rs = ss.str();
00402
00403
00404
00405
00406
00407
00408
00409 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111111));
00410 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(11111111));
00411 CPPUNIT_ASSERT(ss.str()[2] == (char)B8(00000000));
00412 CPPUNIT_ASSERT(ss.str()[3] == (char)B8(00000000));
00413 CPPUNIT_ASSERT(ss.str()[4] == (char)B8(00000000));
00414
00415 ss.str("");
00416 ss.clear();
00417 escribe_elias_gama(ss, (unsigned int)(pot2(32)-1));
00418 rs = ss.str();
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111111));
00430 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(11111111));
00431 CPPUNIT_ASSERT(ss.str()[2] == (char)B8(11111111));
00432 CPPUNIT_ASSERT(ss.str()[3] == (char)B8(11111110));
00433 CPPUNIT_ASSERT(ss.str()[4] == (char)B8(11111111));
00434 CPPUNIT_ASSERT(ss.str()[5] == (char)B8(11111111));
00435 CPPUNIT_ASSERT(ss.str()[6] == (char)B8(11111111));
00436 CPPUNIT_ASSERT(ss.str()[7] == (char)B8(11111110));
00437
00438 }
00439
00440 void EliasCasoPrueba::prueba_escribe_elias_gama2()
00441 {
00442 stringstream ss;
00443 ss.clear();
00444 ss.str("");
00445
00446 CPPUNIT_ASSERT_THROW(escribe_elias_gama2(ss, 0), std::string);
00447
00448 escribe_elias_gama2(ss, 1);
00449
00450 CPPUNIT_ASSERT(ss.str()[0] == (char)0);
00451 ss.clear();
00452 ss.str("");
00453 escribe_elias_gama2(ss, 2);
00454
00455 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(10000000));
00456 ss.clear();
00457 ss.str("");
00458 escribe_elias_gama2(ss, 3);
00459
00460 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(10100000));
00461 ss.clear();
00462 ss.str("");
00463 escribe_elias_gama2(ss, 4);
00464
00465 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11000000));
00466 ss.clear();
00467 ss.str("");
00468 escribe_elias_gama2(ss, 5);
00469 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11001000));
00470 ss.clear();
00471 ss.str("");
00472 escribe_elias_gama2(ss, 6);
00473 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11010000));
00474 ss.clear();
00475 ss.str("");
00476 escribe_elias_gama2(ss, 7);
00477 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11011000));
00478 ss.clear();
00479 ss.str("");
00480 escribe_elias_gama2(ss, 8);
00481 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11100000));
00482 ss.clear();
00483 ss.str("");
00484 escribe_elias_gama2(ss, 9);
00485 string rs = ss.str();
00486 CPPUNIT_ASSERT(rs[0] == (char)B8(11100010));
00487 ofstream os("gama.elias", ios::out | ios::binary);
00488 escribe_elias_gama2(os, 16);
00489 os.close();
00490
00491 ss.str("");
00492 ss.clear();
00493 escribe_elias_gama2(ss, 16);
00494 rs = ss.str();
00495 CPPUNIT_ASSERT(rs[0] == (char)B8(11110000));
00496 CPPUNIT_ASSERT(rs[1] == (char)B8(00000000));
00497
00498 ss.str("");
00499 ss.clear();
00500 escribe_elias_gama2(ss, 32);
00501 rs = ss.str();
00502 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111000));
00503 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(00000000));
00504
00505 ss.str("");
00506 ss.clear();
00507 escribe_elias_gama2(ss, 64);
00508 rs = ss.str();
00509 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111100));
00510 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(00000000));
00511
00512 ss.str("");
00513 ss.clear();
00514 escribe_elias_gama2(ss, 128);
00515 rs = ss.str();
00516 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111110));
00517 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(00000000));
00518
00519 ss.str("");
00520 ss.clear();
00521 escribe_elias_gama2(ss, 256);
00522 rs = ss.str();
00523 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111111));
00524 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(00000000));
00525 CPPUNIT_ASSERT(ss.str()[2] == (char)B8(00000000));
00526
00527 ss.str("");
00528 ss.clear();
00529 escribe_elias_gama2(ss, 512);
00530 rs = ss.str();
00531 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111111));
00532 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(10000000));
00533 CPPUNIT_ASSERT(ss.str()[2] == (char)B8(00000000));
00534
00535 ss.str("");
00536 ss.clear();
00537 escribe_elias_gama2(ss, 1024);
00538 rs = ss.str();
00539 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111111));
00540 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(11000000));
00541 CPPUNIT_ASSERT(ss.str()[2] == (char)B8(00000000));
00542
00543 ss.str("");
00544 ss.clear();
00545 escribe_elias_gama2(ss, 65536);
00546 rs = ss.str();
00547
00548 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111111));
00549 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(11111111));
00550 CPPUNIT_ASSERT(ss.str()[2] == (char)B8(00000000));
00551 CPPUNIT_ASSERT(ss.str()[3] == (char)B8(00000000));
00552 CPPUNIT_ASSERT(ss.str()[4] == (char)B8(00000000));
00553
00554 ss.clear();
00555 ss.str("");
00556 escribe_elias_gama2(ss, 40);
00557 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111001));
00558 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(00000000));
00559
00560 ss.clear();
00561 ss.str("");
00562 escribe_elias_gama2(ss, 41);
00563 cout << "41 ss=" << ss.str() << endl;
00564 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111001));
00565 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(00100000));
00566
00567
00568 ss.str("");
00569 ss.clear();
00570 escribe_elias_gama2(ss, (unsigned int)(pot2(32)-1));
00571 rs = ss.str();
00572
00573 CPPUNIT_ASSERT(ss.str()[0] == (char)B8(11111111));
00574 CPPUNIT_ASSERT(ss.str()[1] == (char)B8(11111111));
00575 CPPUNIT_ASSERT(ss.str()[2] == (char)B8(11111111));
00576 CPPUNIT_ASSERT(ss.str()[3] == (char)B8(11111110));
00577 CPPUNIT_ASSERT(ss.str()[4] == (char)B8(11111111));
00578 CPPUNIT_ASSERT(ss.str()[5] == (char)B8(11111111));
00579 CPPUNIT_ASSERT(ss.str()[6] == (char)B8(11111111));
00580 CPPUNIT_ASSERT(ss.str()[7] == (char)B8(11111110));
00581
00582 }
00583
00584
00585
00586 void EliasCasoPrueba::prueba_lee_elias_gama()
00587 {
00588 vector<bool> vb(0);
00589
00590 std::stringstream ss;
00591 CPPUNIT_ASSERT_THROW(lee_elias_gama(ss), std::string);
00592
00593 ss.clear();
00594 ss.str("");
00595 ss << (char)B8(11111110);
00596 CPPUNIT_ASSERT_THROW(lee_elias_gama(ss), std::string);
00597
00598 ss.clear();
00599 ss.str("");
00600 ss << (char)B8(11110000) << (char) 0;
00601 CPPUNIT_ASSERT(lee_elias_gama(ss) == 16);
00602 ss.clear();
00603 ss.str("");
00604 ss << (char)B8(11100010);
00605 CPPUNIT_ASSERT(lee_elias_gama(ss) == 9);
00606 ss.clear();
00607 ss.str("");
00608 ss << (char)B8(11100000);
00609 CPPUNIT_ASSERT(lee_elias_gama(ss) == 8);
00610 ss.clear();
00611 ss.str("");
00612 ss << (char)B8(11011000);
00613 CPPUNIT_ASSERT(lee_elias_gama(ss) == 7);
00614 ss.clear();
00615 ss.str("");
00616 ss << (char)B8(11010000);
00617 CPPUNIT_ASSERT(lee_elias_gama(ss) == 6);
00618 ss.clear();
00619 ss.str("");
00620 ss << (char)B8(11001000);
00621 CPPUNIT_ASSERT(lee_elias_gama(ss) == 5);
00622 ss.clear();
00623 ss.str("");
00624 ss << (char)B8(11000000);
00625 CPPUNIT_ASSERT(lee_elias_gama(ss) == 4);
00626
00627 ss.clear();
00628 ss.str("");
00629 ss << (char)B8(10100000);
00630 CPPUNIT_ASSERT(lee_elias_gama(ss) == 3);
00631 ss.clear();
00632 ss.str("");
00633 ss << (char)B8(10000000);
00634 CPPUNIT_ASSERT(lee_elias_gama(ss) == 2);
00635 ss.clear();
00636 ss.str("");
00637 ss << (char)0;
00638 CPPUNIT_ASSERT(lee_elias_gama(ss) == 1);
00639
00640 ss.clear();
00641 ss.str("");
00642 ss << (char)B8(11110000);
00643 ss << (char)B8(00000000);
00644 CPPUNIT_ASSERT(lee_elias_gama(ss) == 16);
00645
00646
00647 ss.clear();
00648 ss.str("");
00649 ss << (char)B8(11111111);
00650 ss << (char)B8(11111111);
00651 ss << (char)B8(11111111);
00652 ss << (char)B8(11111110);
00653 ss << (char)B8(11111111);
00654 ss << (char)B8(11111111);
00655 ss << (char)B8(11111111);
00656 ss << (char)B8(11111110);
00657 CPPUNIT_ASSERT(lee_elias_gama(ss) == pot2(32)-1);
00658
00659 }
00660
00661 void EliasCasoPrueba::prueba_lee_elias_gama2()
00662 {
00663 vector<bool> vb(0);
00664
00665 std::stringstream ss;
00666 CPPUNIT_ASSERT_THROW(lee_elias_gama2(ss), std::string);
00667
00668 ss.clear();
00669 ss.str("");
00670 ss << (char)B8(11110000) << (char) 0;
00671 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 16);
00672 ss.clear();
00673 ss.str("");
00674 ss << (char)B8(11100010);
00675 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 9);
00676 ss.clear();
00677 ss.str("");
00678 ss << (char)B8(11100000);
00679 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 8);
00680 ss.clear();
00681 ss.str("");
00682 ss << (char)B8(11011000);
00683 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 7);
00684 ss.clear();
00685 ss.str("");
00686 ss << (char)B8(11010000);
00687 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 6);
00688 ss.clear();
00689 ss.str("");
00690 ss << (char)B8(11001000);
00691 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 5);
00692 ss.clear();
00693 ss.str("");
00694 ss << (char)B8(11000000);
00695 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 4);
00696
00697 ss.clear();
00698 ss.str("");
00699 ss << (char)B8(10100000);
00700 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 3);
00701 ss.clear();
00702 ss.str("");
00703 ss << (char)B8(10000000);
00704 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 2);
00705 ss.clear();
00706 ss.str("");
00707 ss << (char)0;
00708 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 1);
00709
00710 ss.clear();
00711 ss.str("");
00712 ss << (char)B8(11110000);
00713 ss << (char)B8(00000000);
00714 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 16);
00715
00716 ss.clear();
00717 ss.str("");
00718 ss << (char)B8(11111001);
00719 ss << (char)B8(00000000);
00720 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 40);
00721
00722 ss.clear();
00723 ss.str("");
00724 ss << (char)B8(11111001);
00725 ss << (char)B8(00100000);
00726 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 41);
00727
00728 ss.clear();
00729 ss.str("");
00730 ss << (char)B8(11111101);
00731 ss << (char)B8(00000000);
00732 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 96);
00733
00734 ss.clear();
00735 ss.str("");
00736 ss << (char)B8(11111101);
00737 ss << (char)B8(00001000);
00738 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 97);
00739
00740 ss.clear();
00741 ss.str("");
00742 ss << (char)B8(11111110);
00743 ss << (char)B8(10000000);
00744 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 192);
00745
00746 ss.clear();
00747 ss.str("");
00748 ss << (char)B8(11111110);
00749 ss << (char)B8(10000010);
00750 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 193);
00751
00752 ss.clear();
00753 ss.str("");
00754 ss << (char)B8(11111111);
00755 ss << (char)B8(01000000);
00756 ss << (char)B8(00000000);
00757 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 384);
00758
00759 ss.clear();
00760 ss.str("");
00761 ss << (char)B8(11111111);
00762 ss << (char)B8(01000000);
00763 ss << (char)B8(10000000);
00764 CPPUNIT_ASSERT(lee_elias_gama2(ss) == 385);
00765
00766 ss.clear();
00767 ss.str("");
00768 ss << (char)B8(11111111);
00769 ss << (char)B8(10100000);
00770 ss << (char)B8(00000000);
00771 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (512+256));
00772
00773 ss.clear();
00774 ss.str("");
00775 ss << (char)B8(11111111);
00776 ss << (char)B8(10100000);
00777 ss << (char)B8(00100000);
00778 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (512+256+1));
00779
00780 ss.clear();
00781 ss.str("");
00782 ss << (char)B8(11111111);
00783 ss << (char)B8(11010000);
00784 ss << (char)B8(00000000);
00785 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (1024+512));
00786
00787 ss.clear();
00788 ss.str("");
00789 ss << (char)B8(11111111);
00790 ss << (char)B8(11010000);
00791 ss << (char)B8(00001000);
00792 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (1024+512+1));
00793
00794 ss.clear();
00795 ss.str("");
00796 ss << (char)B8(11111111);
00797 ss << (char)B8(11101000);
00798 ss << (char)B8(00000000);
00799 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (2048+1024));
00800
00801 ss.clear();
00802 ss.str("");
00803 ss << (char)B8(11111111);
00804 ss << (char)B8(11101000);
00805 ss << (char)B8(00000010);
00806 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (2048+1024+1));
00807
00808 ss.clear();
00809 ss.str("");
00810 ss << (char)B8(11111111);
00811 ss << (char)B8(11110100);
00812 ss << (char)B8(00000000);
00813 ss << (char)B8(00000000);
00814 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (4096+2048));
00815
00816 ss.clear();
00817 ss.str("");
00818 ss << (char)B8(11111111);
00819 ss << (char)B8(11110100);
00820 ss << (char)B8(00000000);
00821 ss << (char)B8(10000000);
00822 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (4096+2048+1));
00823
00824 ss.clear();
00825 ss.str("");
00826 ss << (char)B8(11111111);
00827 ss << (char)B8(11111010);
00828 ss << (char)B8(00000000);
00829 ss << (char)B8(00000000);
00830 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (8192+4096));
00831
00832 ss.clear();
00833 ss.str("");
00834 ss << (char)B8(11111111);
00835 ss << (char)B8(11111010);
00836 ss << (char)B8(00000000);
00837 ss << (char)B8(00100000);
00838 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (8192+4096+1));
00839
00840 ss.clear();
00841 ss.str("");
00842 ss << (char)B8(11111111);
00843 ss << (char)B8(11111110);
00844 ss << (char)B8(10000000);
00845 ss << (char)B8(00000000);
00846 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (pot2(15)+pot2(14)));
00847
00848 ss.clear();
00849 ss.str("");
00850 ss << (char)B8(11111111);
00851 ss << (char)B8(11111110);
00852 ss << (char)B8(10000000);
00853 ss << (char)B8(00000010);
00854 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (pot2(15)+pot2(14)+1));
00855
00856 ss.clear();
00857 ss.str("");
00858 ss << (char)B8(11111111);
00859 ss << (char)B8(11111111);
00860 ss << (char)B8(01000000);
00861 ss << (char)B8(00000000);
00862 ss << (char)B8(00000000);
00863 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (pot2(16)+pot2(15)));
00864
00865 ss.clear();
00866 ss.str("");
00867 ss << (char)B8(11111111);
00868 ss << (char)B8(11111111);
00869 ss << (char)B8(01000000);
00870 ss << (char)B8(00000000);
00871 ss << (char)B8(10000000);
00872 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (pot2(16)+pot2(15)+1));
00873
00874 ss.clear();
00875 ss.str("");
00876 ss << (char)B8(11111111);
00877 ss << (char)B8(11111111);
00878 ss << (char)B8(11110100);
00879 ss << (char)B8(00000000);
00880 ss << (char)B8(00000000);
00881 ss << (char)B8(00000000);
00882 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (pot2(20)+pot2(19)));
00883
00884 ss.clear();
00885 ss.str("");
00886 ss << (char)B8(11111111);
00887 ss << (char)B8(11111111);
00888 ss << (char)B8(11110100);
00889 ss << (char)B8(00000000);
00890 ss << (char)B8(00000000);
00891 ss << (char)B8(10000000);
00892 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (pot2(20)+pot2(19)+1));
00893
00894 ss.clear();
00895 ss.str("");
00896 ss << (char)B8(11111111);
00897 ss << (char)B8(11111111);
00898 ss << (char)B8(11111111);
00899 ss << (char)B8(01000000);
00900 ss << (char)B8(00000000);
00901 ss << (char)B8(00000000);
00902 ss << (char)B8(00000000);
00903 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (pot2(24)+pot2(23)));
00904
00905 ss.clear();
00906 ss.str("");
00907 ss << (char)B8(11111111);
00908 ss << (char)B8(11111111);
00909 ss << (char)B8(11111111);
00910 ss << (char)B8(01000000);
00911 ss << (char)B8(00000000);
00912 ss << (char)B8(00000000);
00913 ss << (char)B8(10000000);
00914 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (pot2(24)+pot2(23)+1));
00915
00916 ss.clear();
00917 ss.str("");
00918 ss << (char)B8(11111111);
00919 ss << (char)B8(11111111);
00920 ss << (char)B8(11111111);
00921 ss << (char)B8(11110100);
00922 ss << (char)B8(00000000);
00923 ss << (char)B8(00000000);
00924 ss << (char)B8(00000000);
00925 ss << (char)B8(00000000);
00926 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (pot2(28)+pot2(27)));
00927
00928 ss.clear();
00929 ss.str("");
00930 ss << (char)B8(11111111);
00931 ss << (char)B8(11111111);
00932 ss << (char)B8(11111111);
00933 ss << (char)B8(11110100);
00934 ss << (char)B8(00000000);
00935 ss << (char)B8(00000000);
00936 ss << (char)B8(00000000);
00937 ss << (char)B8(10000000);
00938 CPPUNIT_ASSERT(lee_elias_gama2(ss) == (pot2(28)+pot2(27)+1));
00939
00940
00941
00942 ss.clear();
00943 ss.str("");
00944 ss << (char)B8(11111111);
00945 ss << (char)B8(11111111);
00946 ss << (char)B8(11111111);
00947 ss << (char)B8(11111110);
00948 ss << (char)B8(11111111);
00949 ss << (char)B8(11111111);
00950 ss << (char)B8(11111111);
00951 ss << (char)B8(11111110);
00952
00953 CPPUNIT_ASSERT(lee_elias_gama2(ss) == pot2(32) - 1);
00954
00955 }
00956
00957
00958 void EliasCasoPrueba::prueba_codifica_binaria_minima()
00959 {
00960 vector<bool> vb(0);
00961
00962 CPPUNIT_ASSERT_THROW(codifica_binaria_minima(10, 0, vb), std::string);
00963
00964 vb.clear();
00965 codifica_binaria_minima(1, 10, vb);
00966 CPPUNIT_ASSERT(vb2str(vb) == "000");
00967 vb.clear();
00968 codifica_binaria_minima(2, 10, vb);
00969 CPPUNIT_ASSERT(vb2str(vb) == "001");
00970 vb.clear();
00971 codifica_binaria_minima(3, 10, vb);
00972 CPPUNIT_ASSERT(vb2str(vb) == "010");
00973 vb.clear();
00974 codifica_binaria_minima(4, 10, vb);
00975 CPPUNIT_ASSERT(vb2str(vb) == "011");
00976 vb.clear();
00977 codifica_binaria_minima(5, 10, vb);
00978 CPPUNIT_ASSERT(vb2str(vb) == "100");
00979 vb.clear();
00980 codifica_binaria_minima(6, 10, vb);
00981 CPPUNIT_ASSERT(vb2str(vb) == "101");
00982 vb.clear();
00983 codifica_binaria_minima(7, 10, vb);
00984 CPPUNIT_ASSERT(vb2str(vb) == "1100");
00985 vb.clear();
00986 codifica_binaria_minima(8, 10, vb);
00987 CPPUNIT_ASSERT(vb2str(vb) == "1101");
00988 vb.clear();
00989 codifica_binaria_minima(9, 10, vb);
00990 CPPUNIT_ASSERT(vb2str(vb) == "1110");
00991 vb.clear();
00992 codifica_binaria_minima(10, 10, vb);
00993 CPPUNIT_ASSERT(vb2str(vb) == "1111");
00994 }
00995
00996
00997 void EliasCasoPrueba::prueba_decodifica_binaria_minima()
00998 {
00999 vector<bool> vb(0);
01000
01001 CPPUNIT_ASSERT_THROW(decodifica_binaria_minima(0, vb), std::string);
01002 CPPUNIT_ASSERT_THROW(decodifica_binaria_minima(10, vb), std::string);
01003
01004 vb.clear();
01005 vb = str2vb("000");
01006 unsigned long r = decodifica_binaria_minima(10, vb);
01007 CPPUNIT_ASSERT(r == 1);
01008 vb.clear();
01009 vb = str2vb("001");
01010 r = decodifica_binaria_minima(10, vb);
01011 CPPUNIT_ASSERT(r == 2);
01012 vb.clear();
01013 vb = str2vb("010");
01014 CPPUNIT_ASSERT(decodifica_binaria_minima(10, vb) == 3);
01015 vb.clear();
01016 vb = str2vb("011");
01017 CPPUNIT_ASSERT(decodifica_binaria_minima(10, vb) == 4);
01018 vb.clear();
01019 vb = str2vb("100");
01020 CPPUNIT_ASSERT(decodifica_binaria_minima(10, vb) == 5);
01021 vb.clear();
01022 vb = str2vb("101");
01023 CPPUNIT_ASSERT(decodifica_binaria_minima(10, vb) == 6);
01024 vb.clear();
01025 vb = str2vb("1100");
01026 CPPUNIT_ASSERT(decodifica_binaria_minima(10, vb) == 7);
01027 vb.clear();
01028 vb = str2vb("1101");
01029 CPPUNIT_ASSERT(decodifica_binaria_minima(10, vb) == 8);
01030 vb.clear();
01031 vb = str2vb("1110");
01032 CPPUNIT_ASSERT(decodifica_binaria_minima(10, vb) == 9);
01033 vb.clear();
01034 vb = str2vb("1111");
01035 CPPUNIT_ASSERT(decodifica_binaria_minima(10, vb) == 10);
01036
01037 }
01038