forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLEB128Test.cpp
More file actions
98 lines (80 loc) · 3.19 KB
/
LEB128Test.cpp
File metadata and controls
98 lines (80 loc) · 3.19 KB
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
89
90
91
92
93
94
95
96
97
98
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <map>
#include <string>
#include "gtest/gtest.h"
#include "hermes/Support/LEB128.h"
#include "llvh/Support/LEB128.h"
#include "llvh/Support/raw_ostream.h"
namespace {
// Strings can contain null bytes, but if they are initialised using string
// literals, then they will treat null bytes as terminators. This function
// avoids this problem by initialising the string using the list of bytes
// directly.
inline std::string ubyte_str(std::initializer_list<uint8_t> bytes) {
return std::string(bytes.begin(), bytes.end());
}
extern const std::map<uint32_t, std::string> kSignedEncodings{
{0x80000000, ubyte_str({0x80, 0x80, 0x80, 0x80, 0x78})},
{0xF7FFFFFF, ubyte_str({0xFF, 0xFF, 0xFF, 0xBF, 0x7F})},
{0xF8000000, ubyte_str({0x80, 0x80, 0x80, 0x40})},
{0xFFEFFFFF, ubyte_str({0xFF, 0xFF, 0xBF, 0x7F})},
{0xFFF00000, ubyte_str({0x80, 0x80, 0x40})},
{0xFFF6789B, ubyte_str({0x9B, 0xF1, 0x59})},
{0xFFFFDFFF, ubyte_str({0xFF, 0xBF, 0x7F})},
{0xFFFFE000, ubyte_str({0x80, 0x40})},
{0xFFFFFFBF, ubyte_str({0xBF, 0x7F})},
{0xFFFFFFC0, ubyte_str({0x40})},
{0xFFFFFFFF, ubyte_str({0x7F})},
{0x00000000, ubyte_str({0x00})},
{0x00000001, ubyte_str({0x01})},
{0x0000002A, ubyte_str({0x2A})},
{0x0000003F, ubyte_str({0x3F})},
{0x00000040, ubyte_str({0xC0, 0x00})},
{0x0000007F, ubyte_str({0xFF, 0x00})},
{0x000000FF, ubyte_str({0xFF, 0x01})},
{0x00001FFF, ubyte_str({0xFF, 0x3F})},
{0x00002000, ubyte_str({0x80, 0xC0, 0x00})},
{0x0000FFFF, ubyte_str({0xFF, 0xFF, 0x03})},
{0x00098765, ubyte_str({0xE5, 0x8E, 0x26})},
{0x000FFFFF, ubyte_str({0xFF, 0xFF, 0x3F})},
{0x00100000, ubyte_str({0x80, 0x80, 0xC0, 0x00})},
{0x07FFFFFF, ubyte_str({0xFF, 0xFF, 0xFF, 0x3F})},
{0x08000000, ubyte_str({0x80, 0x80, 0x80, 0xC0, 0x00})},
};
TEST(LEB128, EncodeSLEB128) {
for (const auto &kvp : kSignedEncodings) {
int32_t input = static_cast<int32_t>(kvp.first);
const std::string &expected = kvp.second;
std::string buf = "";
llvh::raw_string_ostream os{buf};
hermes::encodeSLEB128(input, os);
EXPECT_EQ(expected, os.str()) << "When writing " << input;
}
}
TEST(LEB128, EncodeSLEB128Padded) {
constexpr size_t kMinSize = 4;
for (const auto &kvp : kSignedEncodings) {
int32_t input = static_cast<int32_t>(kvp.first);
const std::string &expected = kvp.second;
std::string buf = "";
llvh::raw_string_ostream os{buf};
hermes::encodeSLEB128(input, os, kMinSize);
std::string encoded = os.str();
if (expected.size() <= kMinSize) {
EXPECT_EQ(kMinSize, encoded.size()) << "Output padding " << input;
} else {
EXPECT_LT(kMinSize, encoded.size()) << "Output big enough " << input;
}
const uint8_t *cStr = reinterpret_cast<const uint8_t *>(encoded.c_str());
unsigned written = 0;
int64_t decoded = llvh::decodeSLEB128(cStr, &written);
EXPECT_EQ(input, decoded) << "Encode then decode";
EXPECT_EQ(written, encoded.size()) << "Consumed all of output";
}
}
} // anonymous namespace