1 /*
2  * Copyright (C) 2011 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 "android-base/stringprintf.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <string>
22 
TEST(StringPrintfTest,HexSizeT)23 TEST(StringPrintfTest, HexSizeT) {
24   size_t size = 0x00107e59;
25   EXPECT_EQ("00107e59", android::base::StringPrintf("%08zx", size));
26   EXPECT_EQ("0x00107e59", android::base::StringPrintf("0x%08zx", size));
27 }
28 
TEST(StringPrintfTest,StringAppendF)29 TEST(StringPrintfTest, StringAppendF) {
30   std::string s("a");
31   android::base::StringAppendF(&s, "b");
32   EXPECT_EQ("ab", s);
33 }
34 
TEST(StringPrintfTest,Errno)35 TEST(StringPrintfTest, Errno) {
36   errno = 123;
37   android::base::StringPrintf("hello %s", "world");
38   EXPECT_EQ(123, errno);
39 }
40 
TestN(size_t n)41 void TestN(size_t n) {
42   char* buf = new char[n + 1];
43   memset(buf, 'x', n);
44   buf[n] = '\0';
45   std::string s(android::base::StringPrintf("%s", buf));
46   EXPECT_EQ(buf, s);
47   delete[] buf;
48 }
49 
TEST(StringPrintfTest,At1023)50 TEST(StringPrintfTest, At1023) {
51   TestN(1023);
52 }
53 
TEST(StringPrintfTest,At1024)54 TEST(StringPrintfTest, At1024) {
55   TestN(1024);
56 }
57 
TEST(StringPrintfTest,At1025)58 TEST(StringPrintfTest, At1025) {
59   TestN(1025);
60 }
61