Home | History | Annotate | Download | only in unittests
      1 //===- implTest.cpp -------------------------------------------------------===//
      2 //
      3 //                     The MCLinker Project
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 #include <mcld/Support/LEB128.h>
     10 #include "LEB128Test.h"
     11 
     12 #include <mcld/Support/SystemUtils.h>
     13 #include <ctime>
     14 #include <cstdlib>
     15 
     16 using namespace mcld;
     17 using namespace mcldtest;
     18 
     19 
     20 // Constructor can do set-up work for all test here.
     21 LEB128Test::LEB128Test()
     22 {
     23 	// Initialize the seed for random number generator using during the tests.
     24   sys::SetRandomSeed(::time(NULL));
     25 }
     26 
     27 // Destructor can do clean-up work that doesn't throw exceptions here.
     28 LEB128Test::~LEB128Test()
     29 {
     30 }
     31 
     32 // SetUp() will be called immediately before each test.
     33 void LEB128Test::SetUp()
     34 {
     35 }
     36 
     37 // TearDown() will be called immediately after each test.
     38 void LEB128Test::TearDown()
     39 {
     40 }
     41 
     42 //==========================================================================//
     43 // Testcases
     44 //
     45 
     46 TEST_F( LEB128Test, EncodeULEB_Example_from_Dwarf3_Figure22_Using_32bits) {
     47   leb128::ByteType buffer[2];
     48   leb128::ByteType *result;
     49   size_t size;
     50 
     51   result = buffer;
     52   size = leb128::encode<uint32_t>(result, 2);
     53   ASSERT_EQ(buffer[0], 2);
     54   ASSERT_EQ(result, buffer + 1);
     55   ASSERT_TRUE(size == 1);
     56 
     57   result = buffer;
     58   size = leb128::encode<uint32_t>(result, 127);
     59   ASSERT_EQ(buffer[0], 127);
     60   ASSERT_EQ(result, buffer + 1);
     61   ASSERT_TRUE(size == 1);
     62 
     63   result = buffer;
     64   size = leb128::encode<uint32_t>(result, 128);
     65   ASSERT_EQ(buffer[0], 0 + 0x80);
     66   ASSERT_EQ(buffer[1], 1);
     67   ASSERT_EQ(result, buffer + 2);
     68   ASSERT_TRUE(size == 2);
     69 
     70   result = buffer;
     71   size = leb128::encode<uint32_t>(result, 129);
     72   ASSERT_EQ(buffer[0], 1 + 0x80);
     73   ASSERT_EQ(buffer[1], 1);
     74   ASSERT_EQ(result, buffer + 2);
     75   ASSERT_TRUE(size == 2);
     76 
     77   result = buffer;
     78   size = leb128::encode<uint32_t>(result, 130);
     79   ASSERT_EQ(buffer[0], 2 + 0x80);
     80   ASSERT_EQ(buffer[1], 1);
     81   ASSERT_EQ(result, buffer + 2);
     82   ASSERT_TRUE(size == 2);
     83 
     84   result = buffer;
     85   size = leb128::encode<uint32_t>(result, 12857);
     86   ASSERT_EQ(buffer[0], 57 + 0x80);
     87   ASSERT_EQ(buffer[1], 100);
     88   ASSERT_EQ(result, buffer + 2);
     89   ASSERT_TRUE(size == 2);
     90 }
     91 
     92 TEST_F( LEB128Test, EncodeULEB_Example_from_Dwarf3_Figure22_Using_64bits) {
     93   leb128::ByteType buffer[2];
     94   leb128::ByteType *result;
     95   size_t size;
     96 
     97   result = buffer;
     98   size = leb128::encode<uint64_t>(result, 2);
     99   ASSERT_EQ(buffer[0], 2);
    100   ASSERT_EQ(result, buffer + 1);
    101   ASSERT_TRUE(size == 1);
    102 
    103   result = buffer;
    104   size = leb128::encode<uint64_t>(result, 127);
    105   ASSERT_EQ(buffer[0], 127);
    106   ASSERT_EQ(result, buffer + 1);
    107   ASSERT_TRUE(size == 1);
    108 
    109   result = buffer;
    110   size = leb128::encode<uint64_t>(result, 128);
    111   ASSERT_EQ(buffer[0], 0 + 0x80);
    112   ASSERT_EQ(buffer[1], 1);
    113   ASSERT_EQ(result, buffer + 2);
    114   ASSERT_TRUE(size == 2);
    115 
    116   result = buffer;
    117   size = leb128::encode<uint64_t>(result, 129);
    118   ASSERT_EQ(buffer[0], 1 + 0x80);
    119   ASSERT_EQ(buffer[1], 1);
    120   ASSERT_EQ(result, buffer + 2);
    121   ASSERT_TRUE(size == 2);
    122 
    123   result = buffer;
    124   size = leb128::encode<uint64_t>(result, 130);
    125   ASSERT_EQ(buffer[0], 2 + 0x80);
    126   ASSERT_EQ(buffer[1], 1);
    127   ASSERT_EQ(result, buffer + 2);
    128   ASSERT_TRUE(size == 2);
    129 
    130   result = buffer;
    131   size = leb128::encode<uint64_t>(result, 12857);
    132   ASSERT_EQ(buffer[0], 57 + 0x80);
    133   ASSERT_EQ(buffer[1], 100);
    134   ASSERT_EQ(result, buffer + 2);
    135   ASSERT_TRUE(size == 2);
    136 }
    137 
    138 TEST_F( LEB128Test, EncodeSLEB_Example_from_Dwarf3_Figure22) {
    139   leb128::ByteType buffer[2];
    140   leb128::ByteType *result;
    141   size_t size;
    142 
    143   result = buffer;
    144   size = leb128::encode<int32_t>(result, 2);
    145   ASSERT_EQ(buffer[0], 2);
    146   ASSERT_EQ(result, buffer + 1);
    147   ASSERT_TRUE(size == 1);
    148 
    149   result = buffer;
    150   size = leb128::encode<int32_t>(result, -2);
    151   ASSERT_EQ(buffer[0], 0x7e);
    152   ASSERT_EQ(result, buffer + 1);
    153   ASSERT_TRUE(size == 1);
    154 
    155   result = buffer;
    156   size = leb128::encode<int32_t>(result, 127);
    157   ASSERT_EQ(buffer[0], 127 + 0x80);
    158   ASSERT_EQ(buffer[1], 0);
    159   ASSERT_EQ(result, buffer + 2);
    160   ASSERT_TRUE(size == 2);
    161 
    162   result = buffer;
    163   size = leb128::encode<int32_t>(result, -127);
    164   ASSERT_EQ(buffer[0], 1 + 0x80);
    165   ASSERT_EQ(buffer[1], 0x7f);
    166   ASSERT_EQ(result, buffer + 2);
    167   ASSERT_TRUE(size == 2);
    168 
    169   result = buffer;
    170   size = leb128::encode<int32_t>(result, 128);
    171   ASSERT_EQ(buffer[0], 0 + 0x80);
    172   ASSERT_EQ(buffer[1], 1);
    173   ASSERT_EQ(result, buffer + 2);
    174   ASSERT_TRUE(size == 2);
    175 
    176   result = buffer;
    177   size = leb128::encode<int32_t>(result, -128);
    178   ASSERT_EQ(buffer[0], 0 + 0x80);
    179   ASSERT_EQ(buffer[1], 0x7f);
    180   ASSERT_EQ(result, buffer + 2);
    181   ASSERT_TRUE(size == 2);
    182 
    183   result = buffer;
    184   size = leb128::encode<int32_t>(result, 129);
    185   ASSERT_EQ(buffer[0], 1 + 0x80);
    186   ASSERT_EQ(buffer[1], 1);
    187   ASSERT_EQ(result, buffer + 2);
    188   ASSERT_TRUE(size == 2);
    189 
    190   result = buffer;
    191   size = leb128::encode<int32_t>(result, -129);
    192   ASSERT_EQ(buffer[0], 0x7f + 0x80);
    193   ASSERT_EQ(buffer[1], 0x7e);
    194   ASSERT_EQ(result, buffer + 2);
    195   ASSERT_TRUE(size == 2);
    196 }
    197 
    198 TEST_F( LEB128Test, DecodeULEB_Example_from_Dwarf3_Figure22) {
    199   leb128::ByteType buffer[2];
    200   size_t size;
    201 
    202   buffer[0] = 2;
    203   buffer[1] = 0;
    204   ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 2);
    205   ASSERT_TRUE(size == 1);
    206 
    207   buffer[0] = 127;
    208   buffer[1] = 0;
    209   ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 127);
    210   ASSERT_TRUE(size == 1);
    211 
    212   buffer[0] = 0 + 0x80;
    213   buffer[1] = 1;
    214   ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 128);
    215   ASSERT_TRUE(size == 2);
    216 
    217   buffer[0] = 1 + 0x80;
    218   buffer[1] = 1;
    219   ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 129);
    220   ASSERT_TRUE(size == 2);
    221 
    222   buffer[0] = 2 + 0x80;
    223   buffer[1] = 1;
    224   ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 130);
    225   ASSERT_TRUE(size == 2);
    226 
    227   buffer[0] = 57 + 0x80;
    228   buffer[1] = 100;
    229   ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 12857);
    230   ASSERT_TRUE(size == 2);
    231 
    232 }
    233 
    234 TEST_F( LEB128Test, DecodeSLEB_Example_from_Dwarf3_Figure22) {
    235   leb128::ByteType buffer[2];
    236   size_t size;
    237 
    238   buffer[0] = 2;
    239   buffer[1] = 0;
    240   ASSERT_EQ(leb128::decode<int64_t>(buffer, size), 2);
    241   ASSERT_TRUE(size == 1);
    242 
    243   buffer[0] = 0x7e;
    244   buffer[1] = 0;
    245   ASSERT_EQ(leb128::decode<int64_t>(buffer, size), -2);
    246   ASSERT_TRUE(size == 1);
    247 
    248   buffer[0] = 127 + 0x80;
    249   buffer[1] = 0;
    250   ASSERT_EQ(leb128::decode<int64_t>(buffer, size), 127);
    251   ASSERT_TRUE(size == 2);
    252 
    253   buffer[0] = 1 + 0x80;
    254   buffer[1] = 0x7f;
    255   ASSERT_EQ(leb128::decode<int64_t>(buffer, size), -127);
    256   ASSERT_TRUE(size == 2);
    257 
    258   buffer[0] = 0 + 0x80;
    259   buffer[1] = 1;
    260   ASSERT_EQ(leb128::decode<int64_t>(buffer, size), 128);
    261   ASSERT_TRUE(size == 2);
    262 
    263   buffer[0] = 0 + 0x80;
    264   buffer[1] = 0x7f;
    265   ASSERT_EQ(leb128::decode<int64_t>(buffer, size), -128);
    266   ASSERT_TRUE(size == 2);
    267 
    268   buffer[0] = 1 + 0x80;
    269   buffer[1] = 1;
    270   ASSERT_EQ(leb128::decode<int64_t>(buffer, size), 129);
    271   ASSERT_TRUE(size == 2);
    272 
    273   buffer[0] = 0x7f + 0x80;
    274   buffer[1] = 0x7e;
    275   ASSERT_EQ(leb128::decode<int64_t>(buffer, size), -129);
    276   ASSERT_TRUE(size == 2);
    277 }
    278 
    279 TEST_F( LEB128Test, DecodeULEB_Tests_Found_in_Android_dalvik_dx) {
    280   leb128::ByteType content[2];
    281   const leb128::ByteType *p;
    282 
    283   content[0] = 0;
    284   p = content;
    285   ASSERT_TRUE(leb128::decode<uint64_t>(p) == 0);
    286   ASSERT_EQ(p, content + 1);
    287 
    288   content[0] = 1;
    289   p = content;
    290   ASSERT_TRUE(leb128::decode<uint64_t>(p) == 1);
    291   ASSERT_EQ(p, content + 1);
    292 
    293   content[0] = 0x80;
    294   content[1] = 0x7f;
    295   p = content;
    296   ASSERT_TRUE(leb128::decode<uint64_t>(p) == 16256);
    297   ASSERT_EQ(p, content + 2);
    298 }
    299 
    300 TEST_F( LEB128Test, EncodeULEB_Tests_Found_in_Android_dalvik_dx) {
    301   leb128::ByteType buffer[5];
    302   leb128::ByteType *result;
    303   size_t size;
    304 
    305   result = buffer;
    306   size = leb128::encode<uint32_t>(result, 0);
    307   ASSERT_EQ(buffer[0], 0);
    308   ASSERT_EQ(result, buffer + 1);
    309   ASSERT_TRUE(size == 1);
    310 
    311   result = buffer;
    312   size = leb128::encode<uint64_t>(result, 0);
    313   ASSERT_EQ(buffer[0], 0);
    314   ASSERT_EQ(result, buffer + 1);
    315   ASSERT_TRUE(size == 1);
    316 
    317   result = buffer;
    318   size = leb128::encode<uint32_t>(result, 1);
    319   ASSERT_EQ(buffer[0], 1);
    320   ASSERT_EQ(result, buffer + 1);
    321   ASSERT_TRUE(size == 1);
    322 
    323   result = buffer;
    324   size = leb128::encode<uint64_t>(result, 1);
    325   ASSERT_EQ(buffer[0], 1);
    326   ASSERT_EQ(result, buffer + 1);
    327   ASSERT_TRUE(size == 1);
    328 
    329   result = buffer;
    330   size = leb128::encode<uint32_t>(result, 16256);
    331   ASSERT_EQ(buffer[0], 0x80);
    332   ASSERT_EQ(buffer[1], 0x7f);
    333   ASSERT_EQ(result, buffer + 2);
    334   ASSERT_TRUE(size == 2);
    335 
    336   result = buffer;
    337   size = leb128::encode<uint64_t>(result, 16256);
    338   ASSERT_EQ(buffer[0], 0x80);
    339   ASSERT_EQ(buffer[1], 0x7f);
    340   ASSERT_EQ(result, buffer + 2);
    341   ASSERT_TRUE(size == 2);
    342 
    343   result = buffer;
    344   size = leb128::encode<uint32_t>(result, 0x3b4);
    345   ASSERT_EQ(buffer[0], 0xb4);
    346   ASSERT_EQ(buffer[1], 0x07);
    347   ASSERT_EQ(result, buffer + 2);
    348   ASSERT_TRUE(size == 2);
    349 
    350   result = buffer;
    351   size = leb128::encode<uint64_t>(result, 0x3b4);
    352   ASSERT_EQ(buffer[0], 0xb4);
    353   ASSERT_EQ(buffer[1], 0x07);
    354   ASSERT_EQ(result, buffer + 2);
    355   ASSERT_TRUE(size == 2);
    356 
    357   result = buffer;
    358   size = leb128::encode<uint32_t>(result, 0x40c);
    359   ASSERT_EQ(buffer[0], 0x8c);
    360   ASSERT_EQ(buffer[1], 0x08);
    361   ASSERT_EQ(result, buffer + 2);
    362   ASSERT_TRUE(size == 2);
    363 
    364   result = buffer;
    365   size = leb128::encode<uint64_t>(result, 0x40c);
    366   ASSERT_EQ(buffer[0], 0x8c);
    367   ASSERT_EQ(buffer[1], 0x08);
    368   ASSERT_EQ(result, buffer + 2);
    369   ASSERT_TRUE(size == 2);
    370 
    371   result = buffer;
    372   size = leb128::encode<uint32_t>(result, 0xffffffff);
    373   ASSERT_EQ(buffer[0], 0xff);
    374   ASSERT_EQ(buffer[1], 0xff);
    375   ASSERT_EQ(buffer[2], 0xff);
    376   ASSERT_EQ(buffer[3], 0xff);
    377   ASSERT_EQ(buffer[4], 0xf);
    378   ASSERT_EQ(result, buffer + 5);
    379   ASSERT_TRUE(size == 5);
    380 
    381   result = buffer;
    382   size = leb128::encode<uint64_t>(result, 0xffffffff);
    383   ASSERT_EQ(buffer[0], 0xff);
    384   ASSERT_EQ(buffer[1], 0xff);
    385   ASSERT_EQ(buffer[2], 0xff);
    386   ASSERT_EQ(buffer[3], 0xff);
    387   ASSERT_EQ(buffer[4], 0xf);
    388   ASSERT_EQ(result, buffer + 5);
    389   ASSERT_TRUE(size == 5);
    390 }
    391 
    392 TEST_F( LEB128Test, DecodeSLEB_Tests_Found_in_Android_dalvik_dx) {
    393   leb128::ByteType content[2];
    394   const leb128::ByteType *p;
    395 
    396   content[0] = 0;
    397   p = content;
    398   ASSERT_EQ(leb128::decode<int64_t>(p), 0);
    399   ASSERT_EQ(p, content + 1);
    400 
    401   content[0] = 1;
    402   p = content;
    403   ASSERT_EQ(leb128::decode<int64_t>(p), 1);
    404   ASSERT_EQ(p, content + 1);
    405 
    406   content[0] = 0x7f;
    407   p = content;
    408   ASSERT_EQ(leb128::decode<int64_t>(p), -1);
    409   ASSERT_EQ(p, content + 1);
    410 
    411   content[0] = 0x3c;
    412   p = content;
    413   ASSERT_EQ(leb128::decode<int64_t>(p), 0x3c);
    414   ASSERT_EQ(p, content + 1);
    415 }
    416 
    417 TEST_F( LEB128Test, EncodeSLEB_Tests_Found_in_Android_dalvik_dx) {
    418   leb128::ByteType buffer[5];
    419   leb128::ByteType *result;
    420   size_t size;
    421 
    422   result = buffer;
    423   size = leb128::encode<int32_t>(result, 0);
    424   ASSERT_EQ(buffer[0], 0);
    425   ASSERT_EQ(result, buffer + 1);
    426   ASSERT_TRUE(size == 1);
    427 
    428   result = buffer;
    429   size = leb128::encode<int64_t>(result, 0);
    430   ASSERT_EQ(buffer[0], 0);
    431   ASSERT_EQ(result, buffer + 1);
    432   ASSERT_TRUE(size == 1);
    433 
    434   result = buffer;
    435   size = leb128::encode<int32_t>(result, 1);
    436   ASSERT_EQ(buffer[0], 1);
    437   ASSERT_EQ(result, buffer + 1);
    438   ASSERT_TRUE(size == 1);
    439 
    440   result = buffer;
    441   size = leb128::encode<int64_t>(result, 1);
    442   ASSERT_EQ(buffer[0], 1);
    443   ASSERT_EQ(result, buffer + 1);
    444   ASSERT_TRUE(size == 1);
    445 
    446   result = buffer;
    447   size = leb128::encode<int32_t>(result, -1);
    448   ASSERT_EQ(buffer[0], 0x7f);
    449   ASSERT_EQ(result, buffer + 1);
    450   ASSERT_TRUE(size == 1);
    451 
    452   result = buffer;
    453   size = leb128::encode<int64_t>(result, -1);
    454   ASSERT_EQ(buffer[0], 0x7f);
    455   ASSERT_EQ(result, buffer + 1);
    456   ASSERT_TRUE(size == 1);
    457 }
    458 
    459 TEST_F( LEB128Test, Random_Regression_Test) {
    460   leb128::ByteType buffer[9];
    461 
    462   for (int i = 0; i < 20; i++) {
    463     unsigned long int value = sys::GetRandomNum();
    464     uint64_t value2 = value * value;
    465     int64_t value3 = value * value;
    466     leb128::ByteType *result;
    467     size_t encode_size, decode_size;
    468 
    469     // Test encode<uint32_t> and decode<uint64_t> on value
    470     result = buffer;
    471     encode_size = leb128::encode<uint32_t>(result, value);
    472     ASSERT_EQ(result, buffer + encode_size);
    473     ASSERT_EQ(leb128::decode<uint64_t>(buffer, decode_size), value);
    474     ASSERT_EQ(encode_size, decode_size);
    475 
    476     // Test encode<uint64_t> and decode<uint64_t> on (value * value)
    477     result = buffer;
    478     encode_size = leb128::encode<uint64_t>(result, value2);
    479     ASSERT_EQ(result, buffer + encode_size);
    480     ASSERT_EQ(leb128::decode<uint64_t>(buffer, decode_size), value2);
    481     ASSERT_EQ(encode_size, decode_size);
    482 
    483     // Test encode<uint64_t> and decode<uint64_t> on (value * value)
    484     result = buffer;
    485     encode_size = leb128::encode<int64_t>(result, value3);
    486     ASSERT_EQ(result, buffer + encode_size);
    487     ASSERT_EQ(leb128::decode<int64_t>(buffer, decode_size), value3);
    488     ASSERT_EQ(encode_size, decode_size);
    489 
    490     // Test encode<uint64_t> and decode<uint64_t> on -(value * value)
    491     result = buffer;
    492     encode_size = leb128::encode<int64_t>(result, -value3);
    493     ASSERT_EQ(result, buffer + encode_size);
    494     ASSERT_EQ(leb128::decode<int64_t>(buffer, decode_size), -value3);
    495     ASSERT_EQ(encode_size, decode_size);
    496   }
    497 }
    498 
    499 TEST_F( LEB128Test, Other_Test) {
    500   leb128::ByteType buffer[5];
    501   leb128::ByteType *result;
    502   size_t size;
    503 
    504   result = buffer;
    505   leb128::encode<uint64_t>(result, 154452);
    506   ASSERT_EQ(result, buffer + 3);
    507   ASSERT_EQ(buffer[0], 0xd4);
    508   ASSERT_EQ(buffer[1], 0xb6);
    509   ASSERT_EQ(buffer[2], 0x9);
    510 
    511   ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 154452);
    512   ASSERT_TRUE(size == 3);
    513 }
    514 
    515 TEST_F( LEB128Test, Type_Conversion_Test) {
    516   char buffer[5];
    517   char *result;
    518   size_t size;
    519 
    520   result = buffer;
    521   leb128::encode<uint64_t>(result, 154452);
    522   ASSERT_EQ(result, buffer + 3);
    523   ASSERT_EQ(buffer[0], '\xd4');
    524   ASSERT_EQ(buffer[1], '\xb6');
    525   ASSERT_EQ(buffer[2], '\x09');
    526 
    527   ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 154452);
    528   ASSERT_TRUE(size == 3);
    529 
    530   const char *p = buffer;
    531   ASSERT_TRUE(leb128::decode<uint64_t>(p) == 154452);
    532   ASSERT_TRUE(p == (buffer + 3));
    533 }
    534