Wei Wang | 78f2a37 | 2016-10-20 23:18:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 <fcntl.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <limits> |
| 21 | #include <cstddef> |
| 22 | |
Wei Wang | 78f2a37 | 2016-10-20 23:18:17 -0700 | [diff] [blame] | 23 | #include "android-base/test_utils.h" |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include <binder/Parcel.h> |
| 27 | #include <binder/TextOutput.h> |
Wei Wang | 78f2a37 | 2016-10-20 23:18:17 -0700 | [diff] [blame] | 28 | |
Tomasz Wasilczyk | 26db5e4 | 2023-11-02 11:45:11 -0700 | [diff] [blame] | 29 | #include "../file.h" |
| 30 | |
Christopher Ferris | 12fe72b | 2018-08-31 14:13:51 -0700 | [diff] [blame] | 31 | static void CheckMessage(CapturedStderr& cap, |
Wei Wang | 78f2a37 | 2016-10-20 23:18:17 -0700 | [diff] [blame] | 32 | const char* expected, |
| 33 | bool singleline) { |
Christopher Ferris | 12fe72b | 2018-08-31 14:13:51 -0700 | [diff] [blame] | 34 | cap.Stop(); |
| 35 | std::string output = cap.str(); |
Wei Wang | 78f2a37 | 2016-10-20 23:18:17 -0700 | [diff] [blame] | 36 | if (singleline) |
| 37 | output.erase(std::remove(output.begin(), output.end(), '\n')); |
Christopher Ferris | 12fe72b | 2018-08-31 14:13:51 -0700 | [diff] [blame] | 38 | ASSERT_EQ(output, expected); |
Wei Wang | 78f2a37 | 2016-10-20 23:18:17 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | #define CHECK_LOG_(input, expect, singleline) \ |
| 42 | { \ |
| 43 | CapturedStderr cap; \ |
| 44 | android::aerr << input << android::endl; \ |
| 45 | CheckMessage(cap, expect, singleline); \ |
| 46 | } \ |
| 47 | |
| 48 | #define CHECK_VAL_(val, singleline) \ |
| 49 | { \ |
| 50 | std::stringstream ss; \ |
| 51 | ss << val; \ |
| 52 | std::string s = ss.str(); \ |
| 53 | CHECK_LOG_(val, s.c_str(), singleline); \ |
| 54 | } \ |
| 55 | |
| 56 | #define CHECK_LOG(input, expect) CHECK_LOG_(input, expect, true) |
| 57 | #define CHECK_VAL(val) CHECK_VAL_(val, true) |
| 58 | |
| 59 | TEST(TextOutput, HandlesStdEndl) { |
| 60 | CapturedStderr cap; |
| 61 | android::aerr << "foobar" << std::endl; |
Christopher Ferris | 12fe72b | 2018-08-31 14:13:51 -0700 | [diff] [blame] | 62 | cap.Stop(); |
| 63 | ASSERT_EQ(cap.str(), "foobar\n"); |
Wei Wang | 78f2a37 | 2016-10-20 23:18:17 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST(TextOutput, HandlesCEndl) { |
| 67 | CapturedStderr cap; |
| 68 | android::aerr << "foobar" << "\n"; |
Christopher Ferris | 12fe72b | 2018-08-31 14:13:51 -0700 | [diff] [blame] | 69 | cap.Stop(); |
| 70 | ASSERT_EQ(cap.str(), "foobar\n"); |
Wei Wang | 78f2a37 | 2016-10-20 23:18:17 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST(TextOutput, HandlesAndroidEndl) { |
| 74 | CapturedStderr cap; |
| 75 | android::aerr << "foobar" << android::endl; |
Christopher Ferris | 12fe72b | 2018-08-31 14:13:51 -0700 | [diff] [blame] | 76 | cap.Stop(); |
| 77 | ASSERT_EQ(cap.str(), "foobar\n"); |
Wei Wang | 78f2a37 | 2016-10-20 23:18:17 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | TEST(TextOutput, HandleEmptyString) { |
| 81 | CHECK_LOG("", ""); |
| 82 | } |
| 83 | |
| 84 | TEST(TextOutput, HandleString) { |
| 85 | CHECK_LOG("foobar", "foobar"); |
| 86 | } |
| 87 | |
| 88 | TEST(TextOutput, HandleNum) { |
| 89 | CHECK_LOG(12345, "12345"); |
| 90 | } |
| 91 | |
| 92 | TEST(TextOutput, HandleBool) { |
| 93 | CHECK_LOG(false, "false"); |
| 94 | } |
| 95 | |
| 96 | TEST(TextOutput, HandleChar) { |
| 97 | CHECK_LOG('T', "T"); |
| 98 | } |
| 99 | |
| 100 | TEST(TextOutput, HandleParcel) { |
| 101 | android::Parcel val; |
| 102 | CHECK_LOG(val, "Parcel(NULL)"); |
| 103 | } |
| 104 | |
| 105 | TEST(TextOutput, HandleHexDump) { |
| 106 | const char buf[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; |
| 107 | android::HexDump val(buf, sizeof(buf)); |
| 108 | CHECK_LOG(val, "03020100 07060504 0b0a0908 0f0e0d0c '................'"); |
| 109 | } |
| 110 | |
| 111 | TEST(TextOutput, HandleHexDumpCustom) { |
| 112 | const char buf[4] = {0x11,0x22,0x33,0x44}; |
| 113 | android::HexDump val(buf, sizeof(buf), 4); |
| 114 | CHECK_LOG(val, "11 22 33 44 '.\"3D'"); |
| 115 | } |
| 116 | |
| 117 | TEST(TextOutput, HandleTypeCode) { |
| 118 | android::TypeCode val(1234); |
| 119 | CHECK_LOG(val, "'\\x04\\xd2'"); |
| 120 | } |
| 121 | |
| 122 | TEST(TextOutput, HandleCookie) { |
| 123 | int32_t val = 321; //0x141 |
| 124 | CHECK_LOG((void*)(long)val, "0x141"); |
| 125 | } |
| 126 | |
| 127 | TEST(TextOutput, HandleString8) { |
| 128 | android::String8 val("foobar"); |
| 129 | CHECK_LOG(val, "foobar"); |
| 130 | } |
| 131 | |
| 132 | TEST(TextOutput, HandleString16) { |
| 133 | android::String16 val("foobar"); |
| 134 | CHECK_LOG(val, "foobar"); |
| 135 | } |
| 136 | |
| 137 | template <typename T> |
| 138 | class TextTest : public testing::Test {}; |
| 139 | |
| 140 | typedef testing::Types<short, unsigned short, |
| 141 | int, unsigned int, |
| 142 | long, unsigned long, |
| 143 | long long, unsigned long long, |
| 144 | float, double, long double> TestTypes; |
| 145 | TYPED_TEST_CASE(TextTest, TestTypes); |
| 146 | |
| 147 | TYPED_TEST(TextTest, TextMax) |
| 148 | { |
| 149 | TypeParam max = std::numeric_limits<TypeParam>::max(); |
| 150 | CHECK_VAL(max); |
| 151 | } |
| 152 | |
| 153 | TYPED_TEST(TextTest, TestMin) |
| 154 | { |
| 155 | TypeParam min = std::numeric_limits<TypeParam>::min(); |
| 156 | CHECK_VAL(min); |
| 157 | } |
| 158 | |
| 159 | TYPED_TEST(TextTest, TestDenom) |
| 160 | { |
| 161 | TypeParam min = std::numeric_limits<TypeParam>::denorm_min(); |
| 162 | CHECK_VAL(min); |
| 163 | } |
| 164 | |
| 165 | TYPED_TEST(TextTest, TestEpsilon) |
| 166 | { |
| 167 | TypeParam eps = std::numeric_limits<TypeParam>::epsilon(); |
| 168 | CHECK_VAL(eps); |
| 169 | } |