xref: /aosp_15_r20/art/libartbase/base/hex_dump_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "hex_dump.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "globals.h"
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker #include "gtest/gtest.h"
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker #include <stdint.h>
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art {
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker #if defined(__LP64__)
28*795d594fSAndroid Build Coastguard Worker #define ZEROPREFIX "00000000"
29*795d594fSAndroid Build Coastguard Worker #else
30*795d594fSAndroid Build Coastguard Worker #define ZEROPREFIX
31*795d594fSAndroid Build Coastguard Worker #endif
32*795d594fSAndroid Build Coastguard Worker 
TEST(HexDump,OneLine)33*795d594fSAndroid Build Coastguard Worker TEST(HexDump, OneLine) {
34*795d594fSAndroid Build Coastguard Worker   const char* test_text = "0123456789abcdef";
35*795d594fSAndroid Build Coastguard Worker   std::ostringstream oss;
36*795d594fSAndroid Build Coastguard Worker   oss << HexDump(test_text, strlen(test_text), false, "");
37*795d594fSAndroid Build Coastguard Worker   EXPECT_STREQ(oss.str().c_str(),
38*795d594fSAndroid Build Coastguard Worker                ZEROPREFIX
39*795d594fSAndroid Build Coastguard Worker                "00000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66  0123456789abcdef");
40*795d594fSAndroid Build Coastguard Worker }
41*795d594fSAndroid Build Coastguard Worker 
TEST(HexDump,MultiLine)42*795d594fSAndroid Build Coastguard Worker TEST(HexDump, MultiLine) {
43*795d594fSAndroid Build Coastguard Worker   const char* test_text = "0123456789abcdef0123456789ABCDEF";
44*795d594fSAndroid Build Coastguard Worker   std::ostringstream oss;
45*795d594fSAndroid Build Coastguard Worker   oss << HexDump(test_text, strlen(test_text), false, "");
46*795d594fSAndroid Build Coastguard Worker   EXPECT_STREQ(oss.str().c_str(),
47*795d594fSAndroid Build Coastguard Worker                ZEROPREFIX
48*795d594fSAndroid Build Coastguard Worker                "00000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66  0123456789abcdef\n"
49*795d594fSAndroid Build Coastguard Worker                ZEROPREFIX
50*795d594fSAndroid Build Coastguard Worker                "00000010: 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46  0123456789ABCDEF");
51*795d594fSAndroid Build Coastguard Worker }
52*795d594fSAndroid Build Coastguard Worker 
53*795d594fSAndroid Build Coastguard Worker uint64_t g16byte_aligned_number __attribute__ ((aligned(16)));  // NOLINT(whitespace/parens)
TEST(HexDump,ShowActualAddresses)54*795d594fSAndroid Build Coastguard Worker TEST(HexDump, ShowActualAddresses) {
55*795d594fSAndroid Build Coastguard Worker   g16byte_aligned_number = 0x6162636465666768;
56*795d594fSAndroid Build Coastguard Worker   std::ostringstream oss;
57*795d594fSAndroid Build Coastguard Worker   oss << HexDump(&g16byte_aligned_number, 8, true, "");
58*795d594fSAndroid Build Coastguard Worker   // Compare ignoring pointer.
59*795d594fSAndroid Build Coastguard Worker   EXPECT_STREQ(oss.str().c_str() + (kBitsPerIntPtrT / 4),
60*795d594fSAndroid Build Coastguard Worker                ": 68 67 66 65 64 63 62 61                          hgfedcba        ");
61*795d594fSAndroid Build Coastguard Worker }
62*795d594fSAndroid Build Coastguard Worker 
TEST(HexDump,Prefix)63*795d594fSAndroid Build Coastguard Worker TEST(HexDump, Prefix) {
64*795d594fSAndroid Build Coastguard Worker   const char* test_text = "0123456789abcdef";
65*795d594fSAndroid Build Coastguard Worker   std::ostringstream oss;
66*795d594fSAndroid Build Coastguard Worker   oss << HexDump(test_text, strlen(test_text), false, "test prefix: ");
67*795d594fSAndroid Build Coastguard Worker   EXPECT_STREQ(oss.str().c_str(),
68*795d594fSAndroid Build Coastguard Worker                "test prefix: " ZEROPREFIX "00000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66  "
69*795d594fSAndroid Build Coastguard Worker                "0123456789abcdef");
70*795d594fSAndroid Build Coastguard Worker }
71*795d594fSAndroid Build Coastguard Worker 
72*795d594fSAndroid Build Coastguard Worker }  // namespace art
73