blob: ce99f59d7c1d9562c8320a07ee0d2a93c375ec8c [file] [log] [blame]
Wei Wang78f2a372016-10-20 23:18:17 -07001/*
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
23#include "android-base/file.h"
24#include "android-base/test_utils.h"
25#include <gtest/gtest.h>
26
27#include <binder/Parcel.h>
28#include <binder/TextOutput.h>
29#include <binder/Debug.h>
30
Christopher Ferris12fe72b2018-08-31 14:13:51 -070031static void CheckMessage(CapturedStderr& cap,
Wei Wang78f2a372016-10-20 23:18:17 -070032 const char* expected,
33 bool singleline) {
Christopher Ferris12fe72b2018-08-31 14:13:51 -070034 cap.Stop();
35 std::string output = cap.str();
Wei Wang78f2a372016-10-20 23:18:17 -070036 if (singleline)
37 output.erase(std::remove(output.begin(), output.end(), '\n'));
Christopher Ferris12fe72b2018-08-31 14:13:51 -070038 ASSERT_EQ(output, expected);
Wei Wang78f2a372016-10-20 23:18:17 -070039}
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
59TEST(TextOutput, HandlesStdEndl) {
60 CapturedStderr cap;
61 android::aerr << "foobar" << std::endl;
Christopher Ferris12fe72b2018-08-31 14:13:51 -070062 cap.Stop();
63 ASSERT_EQ(cap.str(), "foobar\n");
Wei Wang78f2a372016-10-20 23:18:17 -070064}
65
66TEST(TextOutput, HandlesCEndl) {
67 CapturedStderr cap;
68 android::aerr << "foobar" << "\n";
Christopher Ferris12fe72b2018-08-31 14:13:51 -070069 cap.Stop();
70 ASSERT_EQ(cap.str(), "foobar\n");
Wei Wang78f2a372016-10-20 23:18:17 -070071}
72
73TEST(TextOutput, HandlesAndroidEndl) {
74 CapturedStderr cap;
75 android::aerr << "foobar" << android::endl;
Christopher Ferris12fe72b2018-08-31 14:13:51 -070076 cap.Stop();
77 ASSERT_EQ(cap.str(), "foobar\n");
Wei Wang78f2a372016-10-20 23:18:17 -070078}
79
80TEST(TextOutput, HandleEmptyString) {
81 CHECK_LOG("", "");
82}
83
84TEST(TextOutput, HandleString) {
85 CHECK_LOG("foobar", "foobar");
86}
87
88TEST(TextOutput, HandleNum) {
89 CHECK_LOG(12345, "12345");
90}
91
92TEST(TextOutput, HandleBool) {
93 CHECK_LOG(false, "false");
94}
95
96TEST(TextOutput, HandleChar) {
97 CHECK_LOG('T', "T");
98}
99
100TEST(TextOutput, HandleParcel) {
101 android::Parcel val;
102 CHECK_LOG(val, "Parcel(NULL)");
103}
104
105TEST(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
111TEST(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
117TEST(TextOutput, HandleTypeCode) {
118 android::TypeCode val(1234);
119 CHECK_LOG(val, "'\\x04\\xd2'");
120}
121
122TEST(TextOutput, HandleCookie) {
123 int32_t val = 321; //0x141
124 CHECK_LOG((void*)(long)val, "0x141");
125}
126
127TEST(TextOutput, HandleString8) {
128 android::String8 val("foobar");
129 CHECK_LOG(val, "foobar");
130}
131
132TEST(TextOutput, HandleString16) {
133 android::String16 val("foobar");
134 CHECK_LOG(val, "foobar");
135}
136
137template <typename T>
138class TextTest : public testing::Test {};
139
140typedef 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;
145TYPED_TEST_CASE(TextTest, TestTypes);
146
147TYPED_TEST(TextTest, TextMax)
148{
149 TypeParam max = std::numeric_limits<TypeParam>::max();
150 CHECK_VAL(max);
151}
152
153TYPED_TEST(TextTest, TestMin)
154{
155 TypeParam min = std::numeric_limits<TypeParam>::min();
156 CHECK_VAL(min);
157}
158
159TYPED_TEST(TextTest, TestDenom)
160{
161 TypeParam min = std::numeric_limits<TypeParam>::denorm_min();
162 CHECK_VAL(min);
163}
164
165TYPED_TEST(TextTest, TestEpsilon)
166{
167 TypeParam eps = std::numeric_limits<TypeParam>::epsilon();
168 CHECK_VAL(eps);
169}