15460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===- implTest.cpp -------------------------------------------------------===//
25460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
35460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//                     The MCLinker Project
45460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
55460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// This file is distributed under the University of Illinois Open Source
65460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// License. See LICENSE.TXT for details.
75460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
85460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===//
95460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <mcld/Support/LEB128.h>
105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include "LEB128Test.h"
115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
12f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines#include <mcld/Support/SystemUtils.h>
135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <ctime>
145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <cstdlib>
155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaousing namespace mcld;
175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaousing namespace mcldtest;
185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// Constructor can do set-up work for all test here.
215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoLEB128Test::LEB128Test()
225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao	// Initialize the seed for random number generator using during the tests.
24f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines  sys::SetRandomSeed(::time(NULL));
255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// Destructor can do clean-up work that doesn't throw exceptions here.
285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoLEB128Test::~LEB128Test()
295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// SetUp() will be called immediately before each test.
335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaovoid LEB128Test::SetUp()
345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// TearDown() will be called immediately after each test.
385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaovoid LEB128Test::TearDown()
395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//==========================================================================//
435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// Testcases
445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoTEST_F( LEB128Test, EncodeULEB_Example_from_Dwarf3_Figure22_Using_32bits) {
475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType buffer[2];
485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType *result;
495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size_t size;
505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint32_t>(result, 2);
535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 2);
545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
5522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint32_t>(result, 127);
595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 127);
605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
6122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint32_t>(result, 128);
655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0 + 0x80);
665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 1);
675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
6822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint32_t>(result, 129);
725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 1 + 0x80);
735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 1);
745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
7522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint32_t>(result, 130);
795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 2 + 0x80);
805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 1);
815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
8222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint32_t>(result, 12857);
865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 57 + 0x80);
875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 100);
885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
8922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoTEST_F( LEB128Test, EncodeULEB_Example_from_Dwarf3_Figure22_Using_64bits) {
935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType buffer[2];
945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType *result;
955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size_t size;
965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint64_t>(result, 2);
995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 2);
1005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
10122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
1025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
1045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint64_t>(result, 127);
1055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 127);
1065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
10722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
1085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1095460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
1105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint64_t>(result, 128);
1115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0 + 0x80);
1125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 1);
1135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
11422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
1155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
1175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint64_t>(result, 129);
1185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 1 + 0x80);
1195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 1);
1205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
12122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
1225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
1245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint64_t>(result, 130);
1255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 2 + 0x80);
1265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 1);
1275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
12822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
1295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
1315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint64_t>(result, 12857);
1325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 57 + 0x80);
1335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 100);
1345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
13522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
1365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
1375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoTEST_F( LEB128Test, EncodeSLEB_Example_from_Dwarf3_Figure22) {
1395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType buffer[2];
1405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType *result;
1415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size_t size;
1425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
1445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int32_t>(result, 2);
1455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 2);
1465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
14722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
1485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
1505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int32_t>(result, -2);
1515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0x7e);
1525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
15322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
1545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
1565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int32_t>(result, 127);
1575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 127 + 0x80);
1585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 0);
1595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
16022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
1615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
1635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int32_t>(result, -127);
1645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 1 + 0x80);
1655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 0x7f);
1665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
16722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
1685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
1705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int32_t>(result, 128);
1715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0 + 0x80);
1725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 1);
1735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
17422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
1755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
1775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int32_t>(result, -128);
1785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0 + 0x80);
1795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 0x7f);
1805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
18122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
1825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
1845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int32_t>(result, 129);
1855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 1 + 0x80);
1865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 1);
1875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
18822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
1895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
1915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int32_t>(result, -129);
1925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0x7f + 0x80);
1935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 0x7e);
1945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
19522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
1965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
1975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoTEST_F( LEB128Test, DecodeULEB_Example_from_Dwarf3_Figure22) {
1995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType buffer[2];
2005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size_t size;
2015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 2;
2035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 0;
20422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 2);
20522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
2065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 127;
2085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 0;
20922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 127);
21022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
2115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 0 + 0x80;
2135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 1;
21422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 128);
21522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
2165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 1 + 0x80;
2185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 1;
21922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 129);
22022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
2215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 2 + 0x80;
2235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 1;
22422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 130);
22522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
2265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 57 + 0x80;
2285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 100;
22922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 12857);
23022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
2315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
2335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoTEST_F( LEB128Test, DecodeSLEB_Example_from_Dwarf3_Figure22) {
2355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType buffer[2];
2365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size_t size;
2375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 2;
2395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 0;
2405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(leb128::decode<int64_t>(buffer, size), 2);
24122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
2425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 0x7e;
2445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 0;
2455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(leb128::decode<int64_t>(buffer, size), -2);
24622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
2475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 127 + 0x80;
2495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 0;
2505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(leb128::decode<int64_t>(buffer, size), 127);
25122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
2525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 1 + 0x80;
2545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 0x7f;
2555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(leb128::decode<int64_t>(buffer, size), -127);
25622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
2575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 0 + 0x80;
2595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 1;
2605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(leb128::decode<int64_t>(buffer, size), 128);
26122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
2625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 0 + 0x80;
2645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 0x7f;
2655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(leb128::decode<int64_t>(buffer, size), -128);
26622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
2675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 1 + 0x80;
2695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 1;
2705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(leb128::decode<int64_t>(buffer, size), 129);
27122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
2725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[0] = 0x7f + 0x80;
2745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  buffer[1] = 0x7e;
2755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(leb128::decode<int64_t>(buffer, size), -129);
27622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
2775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
2785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoTEST_F( LEB128Test, DecodeULEB_Tests_Found_in_Android_dalvik_dx) {
2805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType content[2];
2815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  const leb128::ByteType *p;
2825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  content[0] = 0;
2845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  p = content;
28522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(leb128::decode<uint64_t>(p) == 0);
2865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(p, content + 1);
2875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  content[0] = 1;
2895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  p = content;
29022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(leb128::decode<uint64_t>(p) == 1);
2915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(p, content + 1);
2925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  content[0] = 0x80;
2945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  content[1] = 0x7f;
2955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  p = content;
29622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(leb128::decode<uint64_t>(p) == 16256);
2975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(p, content + 2);
2985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
2995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoTEST_F( LEB128Test, EncodeULEB_Tests_Found_in_Android_dalvik_dx) {
3015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType buffer[5];
3025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType *result;
3035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size_t size;
3045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
3065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint32_t>(result, 0);
3075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0);
3085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
30922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
3105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
3125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint64_t>(result, 0);
3135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0);
3145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
31522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
3165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
3185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint32_t>(result, 1);
3195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 1);
3205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
32122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
3225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
3245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint64_t>(result, 1);
3255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 1);
3265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
32722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
3285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
3305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint32_t>(result, 16256);
3315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0x80);
3325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 0x7f);
3335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
33422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
3355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
3375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint64_t>(result, 16256);
3385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0x80);
3395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 0x7f);
3405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
34122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
3425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
3445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint32_t>(result, 0x3b4);
3455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0xb4);
3465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 0x07);
3475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
34822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
3495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
3515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint64_t>(result, 0x3b4);
3525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0xb4);
3535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 0x07);
3545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
35522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
3565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
3585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint32_t>(result, 0x40c);
3595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0x8c);
3605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 0x08);
3615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
36222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
3635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
3655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint64_t>(result, 0x40c);
3665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0x8c);
3675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 0x08);
3685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 2);
36922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 2);
3705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
3725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint32_t>(result, 0xffffffff);
3735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0xff);
3745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 0xff);
3755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[2], 0xff);
3765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[3], 0xff);
3775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[4], 0xf);
3785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 5);
37922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 5);
3805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
3825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<uint64_t>(result, 0xffffffff);
3835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0xff);
3845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 0xff);
3855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[2], 0xff);
3865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[3], 0xff);
3875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[4], 0xf);
3885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 5);
38922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 5);
3905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
3915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoTEST_F( LEB128Test, DecodeSLEB_Tests_Found_in_Android_dalvik_dx) {
3935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType content[2];
3945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  const leb128::ByteType *p;
3955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  content[0] = 0;
3975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  p = content;
3985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(leb128::decode<int64_t>(p), 0);
3995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(p, content + 1);
4005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  content[0] = 1;
4025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  p = content;
4035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(leb128::decode<int64_t>(p), 1);
4045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(p, content + 1);
4055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  content[0] = 0x7f;
4075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  p = content;
4085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(leb128::decode<int64_t>(p), -1);
4095460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(p, content + 1);
4105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  content[0] = 0x3c;
4125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  p = content;
4135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(leb128::decode<int64_t>(p), 0x3c);
4145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(p, content + 1);
4155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
4165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoTEST_F( LEB128Test, EncodeSLEB_Tests_Found_in_Android_dalvik_dx) {
4185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType buffer[5];
4195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType *result;
4205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size_t size;
4215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
4235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int32_t>(result, 0);
4245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0);
4255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
42622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
4275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
4295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int64_t>(result, 0);
4305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0);
4315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
43222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
4335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
4355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int32_t>(result, 1);
4365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 1);
4375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
43822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
4395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
4415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int64_t>(result, 1);
4425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 1);
4435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
44422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
4455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
4475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int32_t>(result, -1);
4485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0x7f);
4495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
45022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
4515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
4535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size = leb128::encode<int64_t>(result, -1);
4545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0x7f);
4555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 1);
45622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 1);
4575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
4585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoTEST_F( LEB128Test, Random_Regression_Test) {
460affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  leb128::ByteType buffer[9];
4615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  for (int i = 0; i < 20; i++) {
463f7ac0f19a1c8d0ad14bcf6456ce368b830fea886Stephen Hines    unsigned long int value = sys::GetRandomNum();
4645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    uint64_t value2 = value * value;
46522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    int64_t value3 = value * value;
4665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    leb128::ByteType *result;
4675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    size_t encode_size, decode_size;
4685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // Test encode<uint32_t> and decode<uint64_t> on value
4705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    result = buffer;
4715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    encode_size = leb128::encode<uint32_t>(result, value);
4725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ASSERT_EQ(result, buffer + encode_size);
4735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ASSERT_EQ(leb128::decode<uint64_t>(buffer, decode_size), value);
4745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ASSERT_EQ(encode_size, decode_size);
4755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // Test encode<uint64_t> and decode<uint64_t> on (value * value)
4775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    result = buffer;
4785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    encode_size = leb128::encode<uint64_t>(result, value2);
4795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ASSERT_EQ(result, buffer + encode_size);
4805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ASSERT_EQ(leb128::decode<uint64_t>(buffer, decode_size), value2);
4815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ASSERT_EQ(encode_size, decode_size);
4825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // Test encode<uint64_t> and decode<uint64_t> on (value * value)
4845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    result = buffer;
48522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    encode_size = leb128::encode<int64_t>(result, value3);
4865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ASSERT_EQ(result, buffer + encode_size);
48722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    ASSERT_EQ(leb128::decode<int64_t>(buffer, decode_size), value3);
4885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ASSERT_EQ(encode_size, decode_size);
4895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // Test encode<uint64_t> and decode<uint64_t> on -(value * value)
4915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    result = buffer;
49222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    encode_size = leb128::encode<int64_t>(result, -value3);
4935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ASSERT_EQ(result, buffer + encode_size);
49422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao    ASSERT_EQ(leb128::decode<int64_t>(buffer, decode_size), -value3);
4955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    ASSERT_EQ(encode_size, decode_size);
4965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
4975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
4985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoTEST_F( LEB128Test, Other_Test) {
5005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType buffer[5];
5015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::ByteType *result;
5025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size_t size;
5035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
5045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
5055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::encode<uint64_t>(result, 154452);
5065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 3);
5075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], 0xd4);
5085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], 0xb6);
5095460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[2], 0x9);
5105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
51122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 154452);
51222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 3);
5135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
5145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
5155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei LiaoTEST_F( LEB128Test, Type_Conversion_Test) {
5165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  char buffer[5];
5175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  char *result;
5185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size_t size;
5195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
5205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  result = buffer;
5215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  leb128::encode<uint64_t>(result, 154452);
5225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(result, buffer + 3);
5235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[0], '\xd4');
5245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[1], '\xb6');
5255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ASSERT_EQ(buffer[2], '\x09');
5265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
52722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(leb128::decode<uint64_t>(buffer, size) == 154452);
52822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(size == 3);
5295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
5305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  const char *p = buffer;
53122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(leb128::decode<uint64_t>(p) == 154452);
53222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ASSERT_TRUE(p == (buffer + 3));
5335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}
534