blob: fc009b1d74a60292e43dc082f1e3115ce89f2289 [file] [log] [blame]
Dan Albertaac6b7c2015-03-16 10:08:46 -07001/*
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 Hughesb6351622015-12-04 22:00:26 -080017#include "android-base/stringprintf.h"
Dan Albertaac6b7c2015-03-16 10:08:46 -070018
19#include <gtest/gtest.h>
20
21#include <string>
22
23TEST(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}
Dan Albertaac6b7c2015-03-16 10:08:46 -070028
29TEST(StringPrintfTest, StringAppendF) {
30 std::string s("a");
31 android::base::StringAppendF(&s, "b");
32 EXPECT_EQ("ab", s);
33}
34
35TEST(StringPrintfTest, Errno) {
36 errno = 123;
37 android::base::StringPrintf("hello %s", "world");
38 EXPECT_EQ(123, errno);
39}
40
41void 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
50TEST(StringPrintfTest, At1023) {
51 TestN(1023);
52}
53
54TEST(StringPrintfTest, At1024) {
55 TestN(1024);
56}
57
58TEST(StringPrintfTest, At1025) {
59 TestN(1025);
60}