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