aboutsummaryrefslogtreecommitdiff
path: root/gold/testsuite/leb128_unittest.cc
blob: 5b438b35a2cf10673245ffb0cedecc33e98df416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// leb_unittest.cc -- test read_signed_LEB_128 and read_unsigned_LEB_128 

// Copyright (C) 2012-2014 Free Software Foundation, Inc.
// Written by Cary Coutant <ccoutant@google.com>.

// This file is part of gold.

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
// MA 02110-1301, USA.

#include "gold.h"

#include <sys/types.h>

#include "int_encoding.h"

#include "test.h"

namespace gold_testsuite
{

using namespace gold;

bool
Leb128_test(Test_report*)
{
  size_t len;

  // Unsigned tests.
  static unsigned char u1[] = { 0 };		// 0
  static unsigned char u2[] = { 1 };		// 1
  static unsigned char u3[] = { 126 };		// 126
  static unsigned char u4[] = { 127 };		// 127
  static unsigned char u5[] = { 0x80+0, 1 };	// 128
  static unsigned char u6[] = { 0x80+1, 1 };	// 129
  static unsigned char u7[] = { 0x80+57, 100 };	// 12857
  static unsigned char u8[] = { 0x80, 0x80, 0x80, 0x80,
				0x80, 0x80, 0x80, 0x80,
				0x80, 1};	// 1ULL << 63

  // Signed tests.
  static unsigned char s1[] = { 0 };		// 0
  static unsigned char s2[] = { 1 };		// 1
  static unsigned char s3[] = { 0x7e };		// -2
  static unsigned char s4[] = { 0x80+127, 0 };	// 127
  static unsigned char s5[] = { 0x80+1, 0x7f };	// -127
  static unsigned char s6[] = { 0x80+0, 1 };	// 128
  static unsigned char s7[] = { 0x80+0, 0x7f };	// -128
  static unsigned char s8[] = { 0x80+1, 1 };	// 129
  static unsigned char s9[] = { 0xff, 0x7e };	// -129

  CHECK(read_unsigned_LEB_128(u1, &len) == 0 && len == sizeof(u1));
  CHECK(read_unsigned_LEB_128(u2, &len) == 1 && len == sizeof(u2));
  CHECK(read_unsigned_LEB_128(u3, &len) == 126 && len == sizeof(u3));
  CHECK(read_unsigned_LEB_128(u4, &len) == 127 && len == sizeof(u4));
  CHECK(read_unsigned_LEB_128(u5, &len) == 128 && len == sizeof(u5));
  CHECK(read_unsigned_LEB_128(u6, &len) == 129 && len == sizeof(u6));
  CHECK(read_unsigned_LEB_128(u7, &len) == 12857 && len == sizeof(u7));
  CHECK(read_unsigned_LEB_128(u8, &len) == (1ULL << 63) && len == sizeof(u8));

  CHECK(read_signed_LEB_128(s1, &len) == 0 && len == sizeof(s1));
  CHECK(read_signed_LEB_128(s2, &len) == 1 && len == sizeof(s2));
  CHECK(read_signed_LEB_128(s3, &len) == -2 && len == sizeof(s3));
  CHECK(read_signed_LEB_128(s4, &len) == 127 && len == sizeof(s4));
  CHECK(read_signed_LEB_128(s5, &len) == -127 && len == sizeof(s5));
  CHECK(read_signed_LEB_128(s6, &len) == 128 && len == sizeof(s6));
  CHECK(read_signed_LEB_128(s7, &len) == -128 && len == sizeof(s7));
  CHECK(read_signed_LEB_128(s8, &len) == 129 && len == sizeof(s8));
  CHECK(read_signed_LEB_128(s9, &len) == -129 && len == sizeof(s9));

  return true;
}

Register_test leb128_register("LEB128", Leb128_test);

} // End namespace gold_testsuite.