1 // leb_unittest.cc -- test read_signed_LEB_128 and read_unsigned_LEB_128
2 
3 // Copyright (C) 2012-2014 Free Software Foundation, Inc.
4 // Written by Cary Coutant <ccoutant@google.com>.
5 
6 // This file is part of gold.
7 
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12 
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22 
23 #include "gold.h"
24 
25 #include <sys/types.h>
26 
27 #include "int_encoding.h"
28 
29 #include "test.h"
30 
31 namespace gold_testsuite
32 {
33 
34 using namespace gold;
35 
36 bool
Leb128_test(Test_report *)37 Leb128_test(Test_report*)
38 {
39   size_t len;
40 
41   // Unsigned tests.
42   static unsigned char u1[] = { 0 };		// 0
43   static unsigned char u2[] = { 1 };		// 1
44   static unsigned char u3[] = { 126 };		// 126
45   static unsigned char u4[] = { 127 };		// 127
46   static unsigned char u5[] = { 0x80+0, 1 };	// 128
47   static unsigned char u6[] = { 0x80+1, 1 };	// 129
48   static unsigned char u7[] = { 0x80+57, 100 };	// 12857
49   static unsigned char u8[] = { 0x80, 0x80, 0x80, 0x80,
50 				0x80, 0x80, 0x80, 0x80,
51 				0x80, 1};	// 1ULL << 63
52 
53   // Signed tests.
54   static unsigned char s1[] = { 0 };		// 0
55   static unsigned char s2[] = { 1 };		// 1
56   static unsigned char s3[] = { 0x7e };		// -2
57   static unsigned char s4[] = { 0x80+127, 0 };	// 127
58   static unsigned char s5[] = { 0x80+1, 0x7f };	// -127
59   static unsigned char s6[] = { 0x80+0, 1 };	// 128
60   static unsigned char s7[] = { 0x80+0, 0x7f };	// -128
61   static unsigned char s8[] = { 0x80+1, 1 };	// 129
62   static unsigned char s9[] = { 0xff, 0x7e };	// -129
63 
64   CHECK(read_unsigned_LEB_128(u1, &len) == 0 && len == sizeof(u1));
65   CHECK(read_unsigned_LEB_128(u2, &len) == 1 && len == sizeof(u2));
66   CHECK(read_unsigned_LEB_128(u3, &len) == 126 && len == sizeof(u3));
67   CHECK(read_unsigned_LEB_128(u4, &len) == 127 && len == sizeof(u4));
68   CHECK(read_unsigned_LEB_128(u5, &len) == 128 && len == sizeof(u5));
69   CHECK(read_unsigned_LEB_128(u6, &len) == 129 && len == sizeof(u6));
70   CHECK(read_unsigned_LEB_128(u7, &len) == 12857 && len == sizeof(u7));
71   CHECK(read_unsigned_LEB_128(u8, &len) == (1ULL << 63) && len == sizeof(u8));
72 
73   CHECK(read_signed_LEB_128(s1, &len) == 0 && len == sizeof(s1));
74   CHECK(read_signed_LEB_128(s2, &len) == 1 && len == sizeof(s2));
75   CHECK(read_signed_LEB_128(s3, &len) == -2 && len == sizeof(s3));
76   CHECK(read_signed_LEB_128(s4, &len) == 127 && len == sizeof(s4));
77   CHECK(read_signed_LEB_128(s5, &len) == -127 && len == sizeof(s5));
78   CHECK(read_signed_LEB_128(s6, &len) == 128 && len == sizeof(s6));
79   CHECK(read_signed_LEB_128(s7, &len) == -128 && len == sizeof(s7));
80   CHECK(read_signed_LEB_128(s8, &len) == 129 && len == sizeof(s8));
81   CHECK(read_signed_LEB_128(s9, &len) == -129 && len == sizeof(s9));
82 
83   return true;
84 }
85 
86 Register_test leb128_register("LEB128", Leb128_test);
87 
88 } // End namespace gold_testsuite.
89