Home | History | Annotate | Download | only in tests
      1 /*
      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__)
     20 
     21 #include "../libc/bionic/libc_logging.cpp"
     22 
     23 extern int __libc_format_buffer(char* buffer, size_t buffer_size, const char* format, ...);
     24 
     25 TEST(libc_logging, smoke) {
     26   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 
     56   __libc_format_buffer(buf, sizeof(buf), "a%hdb", 0x7fff0010);
     57   EXPECT_STREQ("a16b", buf);
     58 
     59   __libc_format_buffer(buf, sizeof(buf), "a%hhdb", 0x7fffff10);
     60   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);
    107 }
    108 
    109 TEST(libc_logging, d_INT_MAX) {
    110   char buf[BUFSIZ];
    111   __libc_format_buffer(buf, sizeof(buf), "%d", INT_MAX);
    112   EXPECT_STREQ("2147483647", buf);
    113 }
    114 
    115 TEST(libc_logging, d_INT_MIN) {
    116   char buf[BUFSIZ];
    117   __libc_format_buffer(buf, sizeof(buf), "%d", INT_MIN);
    118   EXPECT_STREQ("-2147483648", buf);
    119 }
    120 
    121 TEST(libc_logging, ld_LONG_MAX) {
    122   char buf[BUFSIZ];
    123   __libc_format_buffer(buf, sizeof(buf), "%ld", LONG_MAX);
    124   EXPECT_STREQ("2147483647", buf);
    125 }
    126 
    127 TEST(libc_logging, ld_LONG_MIN) {
    128   char buf[BUFSIZ];
    129   __libc_format_buffer(buf, sizeof(buf), "%ld", LONG_MIN);
    130   EXPECT_STREQ("-2147483648", buf);
    131 }
    132 
    133 TEST(libc_logging, lld_LLONG_MAX) {
    134   char buf[BUFSIZ];
    135   __libc_format_buffer(buf, sizeof(buf), "%lld", LLONG_MAX);
    136   EXPECT_STREQ("9223372036854775807", buf);
    137 }
    138 
    139 TEST(libc_logging, lld_LLONG_MIN) {
    140   char buf[BUFSIZ];
    141   __libc_format_buffer(buf, sizeof(buf), "%lld", LLONG_MIN);
    142   EXPECT_STREQ("-9223372036854775808", buf);
    143 }
    144 
    145 #endif
    146