Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/ostreams.h"
6 :
7 : #include "testing/gtest-support.h"
8 :
9 : namespace v8 {
10 : namespace internal {
11 :
12 15418 : TEST(Ostream, AsHex) {
13 11 : auto testAsHex = [](const char* expected, const AsHex& value) {
14 22 : std::ostringstream out;
15 11 : out << value;
16 : std::string result = out.str();
17 11 : EXPECT_EQ(expected, result);
18 22 : EXPECT_TRUE(result == expected)
19 0 : << "\nexpected: " << expected << "\ngot: " << result << "\n";
20 11 : };
21 :
22 1 : testAsHex("0", AsHex(0));
23 1 : testAsHex("", AsHex(0, 0));
24 1 : testAsHex("0x", AsHex(0, 0, true));
25 1 : testAsHex("0x0", AsHex(0, 1, true));
26 1 : testAsHex("0x00", AsHex(0, 2, true));
27 1 : testAsHex("123", AsHex(0x123, 0));
28 1 : testAsHex("0123", AsHex(0x123, 4));
29 1 : testAsHex("0x123", AsHex(0x123, 0, true));
30 1 : testAsHex("0x123", AsHex(0x123, 3, true));
31 1 : testAsHex("0x0123", AsHex(0x123, 4, true));
32 1 : testAsHex("0x00000123", AsHex(0x123, 8, true));
33 1 : }
34 :
35 15418 : TEST(Ostream, AsHexBytes) {
36 18 : auto testAsHexBytes = [](const char* expected, const AsHexBytes& value) {
37 36 : std::ostringstream out;
38 18 : out << value;
39 : std::string result = out.str();
40 18 : EXPECT_EQ(expected, result);
41 18 : };
42 :
43 : // Little endian (default):
44 1 : testAsHexBytes("00", AsHexBytes(0));
45 1 : testAsHexBytes("", AsHexBytes(0, 0));
46 1 : testAsHexBytes("23 01", AsHexBytes(0x123));
47 1 : testAsHexBytes("23 01", AsHexBytes(0x123, 1));
48 1 : testAsHexBytes("23 01", AsHexBytes(0x123, 2));
49 1 : testAsHexBytes("23 01 00", AsHexBytes(0x123, 3));
50 1 : testAsHexBytes("ff ff ff ff", AsHexBytes(0xFFFFFFFF));
51 1 : testAsHexBytes("00 00 00 00", AsHexBytes(0, 4));
52 1 : testAsHexBytes("56 34 12", AsHexBytes(0x123456));
53 :
54 : // Big endian:
55 1 : testAsHexBytes("00", AsHexBytes(0, 1, AsHexBytes::kBigEndian));
56 1 : testAsHexBytes("", AsHexBytes(0, 0, AsHexBytes::kBigEndian));
57 1 : testAsHexBytes("01 23", AsHexBytes(0x123, 1, AsHexBytes::kBigEndian));
58 1 : testAsHexBytes("01 23", AsHexBytes(0x123, 1, AsHexBytes::kBigEndian));
59 1 : testAsHexBytes("01 23", AsHexBytes(0x123, 2, AsHexBytes::kBigEndian));
60 1 : testAsHexBytes("00 01 23", AsHexBytes(0x123, 3, AsHexBytes::kBigEndian));
61 1 : testAsHexBytes("ff ff ff ff", AsHexBytes(0xFFFFFFFF, AsHexBytes::kBigEndian));
62 1 : testAsHexBytes("00 00 00 00", AsHexBytes(0, 4, AsHexBytes::kBigEndian));
63 1 : testAsHexBytes("12 34 56", AsHexBytes(0x123456, 1, AsHexBytes::kBigEndian));
64 1 : }
65 :
66 : } // namespace internal
67 9249 : } // namespace v8
|