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