blob: 950161e785351a6d90ee79b9009b46afee7df307 [file] [log] [blame]
Elliott Hughes41b31792013-01-28 10:36:31 -08001/*
2 * Copyright (C) 2012 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 <gtest/gtest.h>
18
19#if defined(__BIONIC__)
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070020#include "../libc/bionic/libc_logging.cpp"
Elliott Hughes41b31792013-01-28 10:36:31 -080021extern int __libc_format_buffer(char* buffer, size_t buffer_size, const char* format, ...);
Christopher Ferrisf04935c2013-12-20 18:43:21 -080022#endif // __BIONIC__
Elliott Hughes41b31792013-01-28 10:36:31 -080023
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070024TEST(libc_logging, smoke) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -080025#if defined(__BIONIC__)
Elliott Hughes41b31792013-01-28 10:36:31 -080026 char buf[BUFSIZ];
27
28 __libc_format_buffer(buf, sizeof(buf), "a");
29 EXPECT_STREQ("a", buf);
30
31 __libc_format_buffer(buf, sizeof(buf), "%%");
32 EXPECT_STREQ("%", buf);
33
34 __libc_format_buffer(buf, sizeof(buf), "01234");
35 EXPECT_STREQ("01234", buf);
36
37 __libc_format_buffer(buf, sizeof(buf), "a%sb", "01234");
38 EXPECT_STREQ("a01234b", buf);
39
40 char* s = NULL;
41 __libc_format_buffer(buf, sizeof(buf), "a%sb", s);
42 EXPECT_STREQ("a(null)b", buf);
43
44 __libc_format_buffer(buf, sizeof(buf), "aa%scc", "bb");
45 EXPECT_STREQ("aabbcc", buf);
46
47 __libc_format_buffer(buf, sizeof(buf), "a%cc", 'b');
48 EXPECT_STREQ("abc", buf);
49
50 __libc_format_buffer(buf, sizeof(buf), "a%db", 1234);
51 EXPECT_STREQ("a1234b", buf);
52
53 __libc_format_buffer(buf, sizeof(buf), "a%db", -8123);
54 EXPECT_STREQ("a-8123b", buf);
55
Stephen Hines6c7b3cb2013-10-11 16:03:21 -070056 __libc_format_buffer(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes41b31792013-01-28 10:36:31 -080057 EXPECT_STREQ("a16b", buf);
58
Stephen Hines6c7b3cb2013-10-11 16:03:21 -070059 __libc_format_buffer(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes41b31792013-01-28 10:36:31 -080060 EXPECT_STREQ("a16b", buf);
61
62 __libc_format_buffer(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
63 EXPECT_STREQ("a68719476736b", buf);
64
65 __libc_format_buffer(buf, sizeof(buf), "a%ldb", 70000L);
66 EXPECT_STREQ("a70000b", buf);
67
68 __libc_format_buffer(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
69 EXPECT_STREQ("a0xb0001234b", buf);
70
71 __libc_format_buffer(buf, sizeof(buf), "a%xz", 0x12ab);
72 EXPECT_STREQ("a12abz", buf);
73
74 __libc_format_buffer(buf, sizeof(buf), "a%Xz", 0x12ab);
75 EXPECT_STREQ("a12ABz", buf);
76
77 __libc_format_buffer(buf, sizeof(buf), "a%08xz", 0x123456);
78 EXPECT_STREQ("a00123456z", buf);
79
80 __libc_format_buffer(buf, sizeof(buf), "a%5dz", 1234);
81 EXPECT_STREQ("a 1234z", buf);
82
83 __libc_format_buffer(buf, sizeof(buf), "a%05dz", 1234);
84 EXPECT_STREQ("a01234z", buf);
85
86 __libc_format_buffer(buf, sizeof(buf), "a%8dz", 1234);
87 EXPECT_STREQ("a 1234z", buf);
88
89 __libc_format_buffer(buf, sizeof(buf), "a%-8dz", 1234);
90 EXPECT_STREQ("a1234 z", buf);
91
92 __libc_format_buffer(buf, sizeof(buf), "A%-11sZ", "abcdef");
93 EXPECT_STREQ("Aabcdef Z", buf);
94
95 __libc_format_buffer(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
96 EXPECT_STREQ("Ahello:1234Z", buf);
97
98 __libc_format_buffer(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
99 EXPECT_STREQ("a005:5:05z", buf);
100
101 void* p = NULL;
102 __libc_format_buffer(buf, sizeof(buf), "a%d,%pz", 5, p);
103 EXPECT_STREQ("a5,0x0z", buf);
104
105 __libc_format_buffer(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
106 EXPECT_STREQ("a68719476736,6,7,8z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800107#else // __BIONIC__
108 GTEST_LOG_(INFO) << "This test does nothing.\n";
109#endif // __BIONIC__
Elliott Hughes41b31792013-01-28 10:36:31 -0800110}
111
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700112TEST(libc_logging, d_INT_MAX) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800113#if defined(__BIONIC__)
Elliott Hughes41b31792013-01-28 10:36:31 -0800114 char buf[BUFSIZ];
115 __libc_format_buffer(buf, sizeof(buf), "%d", INT_MAX);
116 EXPECT_STREQ("2147483647", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800117#else // __BIONIC__
118 GTEST_LOG_(INFO) << "This test does nothing.\n";
119#endif // __BIONIC__
Elliott Hughes41b31792013-01-28 10:36:31 -0800120}
121
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700122TEST(libc_logging, d_INT_MIN) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800123#if defined(__BIONIC__)
Elliott Hughes41b31792013-01-28 10:36:31 -0800124 char buf[BUFSIZ];
125 __libc_format_buffer(buf, sizeof(buf), "%d", INT_MIN);
126 EXPECT_STREQ("-2147483648", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800127#else // __BIONIC__
128 GTEST_LOG_(INFO) << "This test does nothing.\n";
129#endif // __BIONIC__
Elliott Hughes41b31792013-01-28 10:36:31 -0800130}
131
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700132TEST(libc_logging, ld_LONG_MAX) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800133#if defined(__BIONIC__)
Elliott Hughes41b31792013-01-28 10:36:31 -0800134 char buf[BUFSIZ];
135 __libc_format_buffer(buf, sizeof(buf), "%ld", LONG_MAX);
Elliott Hughes925753a2013-10-18 13:17:18 -0700136#if __LP64__
137 EXPECT_STREQ("9223372036854775807", buf);
138#else
Elliott Hughes41b31792013-01-28 10:36:31 -0800139 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700140#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800141#else // __BIONIC__
142 GTEST_LOG_(INFO) << "This test does nothing.\n";
143#endif // __BIONIC__
Elliott Hughes41b31792013-01-28 10:36:31 -0800144}
145
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700146TEST(libc_logging, ld_LONG_MIN) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800147#if defined(__BIONIC__)
Elliott Hughes41b31792013-01-28 10:36:31 -0800148 char buf[BUFSIZ];
149 __libc_format_buffer(buf, sizeof(buf), "%ld", LONG_MIN);
Elliott Hughes925753a2013-10-18 13:17:18 -0700150#if __LP64__
151 EXPECT_STREQ("-9223372036854775808", buf);
152#else
Elliott Hughes41b31792013-01-28 10:36:31 -0800153 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700154#endif
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800155#else // __BIONIC__
156 GTEST_LOG_(INFO) << "This test does nothing.\n";
157#endif // __BIONIC__
Elliott Hughes41b31792013-01-28 10:36:31 -0800158}
159
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700160TEST(libc_logging, lld_LLONG_MAX) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800161#if defined(__BIONIC__)
Elliott Hughes41b31792013-01-28 10:36:31 -0800162 char buf[BUFSIZ];
163 __libc_format_buffer(buf, sizeof(buf), "%lld", LLONG_MAX);
164 EXPECT_STREQ("9223372036854775807", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800165#else // __BIONIC__
166 GTEST_LOG_(INFO) << "This test does nothing.\n";
167#endif // __BIONIC__
Elliott Hughes41b31792013-01-28 10:36:31 -0800168}
169
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700170TEST(libc_logging, lld_LLONG_MIN) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800171#if defined(__BIONIC__)
Elliott Hughes41b31792013-01-28 10:36:31 -0800172 char buf[BUFSIZ];
173 __libc_format_buffer(buf, sizeof(buf), "%lld", LLONG_MIN);
174 EXPECT_STREQ("-9223372036854775808", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800175#else // __BIONIC__
176 GTEST_LOG_(INFO) << "This test does nothing.\n";
177#endif // __BIONIC__
Elliott Hughes41b31792013-01-28 10:36:31 -0800178}