Ian Rogers | 43b2e0f | 2014-01-30 16:58:39 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | namespace art { |
| 26 | |
Andreas Gampe | cfa5c48 | 2014-03-12 14:11:40 -0700 | [diff] [blame] | 27 | #if defined(__LP64__) |
| 28 | #define ZEROPREFIX "00000000" |
| 29 | #else |
| 30 | #define ZEROPREFIX |
| 31 | #endif |
| 32 | |
Ian Rogers | 43b2e0f | 2014-01-30 16:58:39 -0800 | [diff] [blame] | 33 | TEST(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 Gampe | cfa5c48 | 2014-03-12 14:11:40 -0700 | [diff] [blame] | 38 | ZEROPREFIX |
Ian Rogers | 43b2e0f | 2014-01-30 16:58:39 -0800 | [diff] [blame] | 39 | "00000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 0123456789abcdef"); |
| 40 | } |
| 41 | |
| 42 | TEST(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 Gampe | cfa5c48 | 2014-03-12 14:11:40 -0700 | [diff] [blame] | 47 | ZEROPREFIX |
Ian Rogers | 43b2e0f | 2014-01-30 16:58:39 -0800 | [diff] [blame] | 48 | "00000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 0123456789abcdef\n" |
Andreas Gampe | cfa5c48 | 2014-03-12 14:11:40 -0700 | [diff] [blame] | 49 | ZEROPREFIX |
Ian Rogers | 43b2e0f | 2014-01-30 16:58:39 -0800 | [diff] [blame] | 50 | "00000010: 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 0123456789ABCDEF"); |
| 51 | } |
| 52 | |
| 53 | uint64_t g16byte_aligned_number __attribute__ ((aligned(16))); // NOLINT(whitespace/parens) |
| 54 | TEST(HexDump, ShowActualAddresses) { |
| 55 | g16byte_aligned_number = 0x6162636465666768; |
| 56 | std::ostringstream oss; |
| 57 | oss << HexDump(&g16byte_aligned_number, 8, true, ""); |
| 58 | // Compare ignoring pointer. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 59 | EXPECT_STREQ(oss.str().c_str() + (kBitsPerIntPtrT / 4), |
Ian Rogers | 43b2e0f | 2014-01-30 16:58:39 -0800 | [diff] [blame] | 60 | ": 68 67 66 65 64 63 62 61 hgfedcba "); |
| 61 | } |
| 62 | |
| 63 | TEST(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 Gampe | cfa5c48 | 2014-03-12 14:11:40 -0700 | [diff] [blame] | 68 | "test prefix: " ZEROPREFIX "00000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 " |
Ian Rogers | 43b2e0f | 2014-01-30 16:58:39 -0800 | [diff] [blame] | 69 | "0123456789abcdef"); |
| 70 | } |
| 71 | |
| 72 | } // namespace art |