2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
19 #if defined(__BIONIC__)
20 #include <async_safe/log.h>
23 TEST(async_safe_log, smoke) {
24 #if defined(__BIONIC__)
27 async_safe_format_buffer(buf, sizeof(buf), "a");
28 EXPECT_STREQ("a", buf);
30 async_safe_format_buffer(buf, sizeof(buf), "%%");
31 EXPECT_STREQ("%", buf);
33 async_safe_format_buffer(buf, sizeof(buf), "01234");
34 EXPECT_STREQ("01234", buf);
36 async_safe_format_buffer(buf, sizeof(buf), "a%sb", "01234");
37 EXPECT_STREQ("a01234b", buf);
40 async_safe_format_buffer(buf, sizeof(buf), "a%sb", s);
41 EXPECT_STREQ("a(null)b", buf);
43 async_safe_format_buffer(buf, sizeof(buf), "aa%scc", "bb");
44 EXPECT_STREQ("aabbcc", buf);
46 async_safe_format_buffer(buf, sizeof(buf), "a%cc", 'b');
47 EXPECT_STREQ("abc", buf);
49 async_safe_format_buffer(buf, sizeof(buf), "a%db", 1234);
50 EXPECT_STREQ("a1234b", buf);
52 async_safe_format_buffer(buf, sizeof(buf), "a%db", -8123);
53 EXPECT_STREQ("a-8123b", buf);
55 async_safe_format_buffer(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
56 EXPECT_STREQ("a16b", buf);
58 async_safe_format_buffer(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
59 EXPECT_STREQ("a16b", buf);
61 async_safe_format_buffer(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
62 EXPECT_STREQ("a68719476736b", buf);
64 async_safe_format_buffer(buf, sizeof(buf), "a%ldb", 70000L);
65 EXPECT_STREQ("a70000b", buf);
67 async_safe_format_buffer(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
68 EXPECT_STREQ("a0xb0001234b", buf);
70 async_safe_format_buffer(buf, sizeof(buf), "a%xz", 0x12ab);
71 EXPECT_STREQ("a12abz", buf);
73 async_safe_format_buffer(buf, sizeof(buf), "a%Xz", 0x12ab);
74 EXPECT_STREQ("a12ABz", buf);
76 async_safe_format_buffer(buf, sizeof(buf), "a%08xz", 0x123456);
77 EXPECT_STREQ("a00123456z", buf);
79 async_safe_format_buffer(buf, sizeof(buf), "a%5dz", 1234);
80 EXPECT_STREQ("a 1234z", buf);
82 async_safe_format_buffer(buf, sizeof(buf), "a%05dz", 1234);
83 EXPECT_STREQ("a01234z", buf);
85 async_safe_format_buffer(buf, sizeof(buf), "a%8dz", 1234);
86 EXPECT_STREQ("a 1234z", buf);
88 async_safe_format_buffer(buf, sizeof(buf), "a%-8dz", 1234);
89 EXPECT_STREQ("a1234 z", buf);
91 async_safe_format_buffer(buf, sizeof(buf), "A%-11sZ", "abcdef");
92 EXPECT_STREQ("Aabcdef Z", buf);
94 async_safe_format_buffer(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
95 EXPECT_STREQ("Ahello:1234Z", buf);
97 async_safe_format_buffer(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
98 EXPECT_STREQ("a005:5:05z", buf);
101 async_safe_format_buffer(buf, sizeof(buf), "a%d,%pz", 5, p);
102 EXPECT_STREQ("a5,0x0z", buf);
104 async_safe_format_buffer(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
105 EXPECT_STREQ("a68719476736,6,7,8z", buf);
107 GTEST_LOG_(INFO) << "This test does nothing.\n";
111 TEST(async_safe_log, d_INT_MAX) {
112 #if defined(__BIONIC__)
114 async_safe_format_buffer(buf, sizeof(buf), "%d", INT_MAX);
115 EXPECT_STREQ("2147483647", buf);
117 GTEST_LOG_(INFO) << "This test does nothing.\n";
121 TEST(async_safe_log, d_INT_MIN) {
122 #if defined(__BIONIC__)
124 async_safe_format_buffer(buf, sizeof(buf), "%d", INT_MIN);
125 EXPECT_STREQ("-2147483648", buf);
127 GTEST_LOG_(INFO) << "This test does nothing.\n";
131 TEST(async_safe_log, ld_LONG_MAX) {
132 #if defined(__BIONIC__)
134 async_safe_format_buffer(buf, sizeof(buf), "%ld", LONG_MAX);
135 #if defined(__LP64__)
136 EXPECT_STREQ("9223372036854775807", buf);
138 EXPECT_STREQ("2147483647", buf);
141 GTEST_LOG_(INFO) << "This test does nothing.\n";
145 TEST(async_safe_log, ld_LONG_MIN) {
146 #if defined(__BIONIC__)
148 async_safe_format_buffer(buf, sizeof(buf), "%ld", LONG_MIN);
149 #if defined(__LP64__)
150 EXPECT_STREQ("-9223372036854775808", buf);
152 EXPECT_STREQ("-2147483648", buf);
155 GTEST_LOG_(INFO) << "This test does nothing.\n";
159 TEST(async_safe_log, lld_LLONG_MAX) {
160 #if defined(__BIONIC__)
162 async_safe_format_buffer(buf, sizeof(buf), "%lld", LLONG_MAX);
163 EXPECT_STREQ("9223372036854775807", buf);
165 GTEST_LOG_(INFO) << "This test does nothing.\n";
169 TEST(async_safe_log, lld_LLONG_MIN) {
170 #if defined(__BIONIC__)
172 async_safe_format_buffer(buf, sizeof(buf), "%lld", LLONG_MIN);
173 EXPECT_STREQ("-9223372036854775808", buf);
175 GTEST_LOG_(INFO) << "This test does nothing.\n";
179 TEST(async_safe_log, buffer_overrun) {
180 #if defined(__BIONIC__)
182 ASSERT_EQ(11, async_safe_format_buffer(buf, sizeof(buf), "hello %s", "world"));
183 EXPECT_STREQ("hello world", buf);
184 ASSERT_EQ(11, async_safe_format_buffer(buf, 8, "hello %s", "world"));
185 EXPECT_STREQ("hello w", buf);
187 GTEST_LOG_(INFO) << "This test does nothing.\n";