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