blob: 54b2b6c9e158da9b5becbf59bab3aeee2347bab5 [file] [log] [blame]
Elliott Hughes6b3be292015-02-03 14:23:53 -08001/*
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
Dan Albertc007bc32015-03-16 10:08:46 -070017#include "base/stringprintf.h"
Elliott Hughes6b3be292015-02-03 14:23:53 -080018
19#include <gtest/gtest.h>
20
Dan Albertc007bc32015-03-16 10:08:46 -070021#include <string>
22
Dan Albert0c4b3a32015-04-29 11:32:23 -070023// The z size sepcifier isn't supported on Windows, so this test isn't useful.
24#if !defined(_WIN32)
Elliott Hughes6b3be292015-02-03 14:23:53 -080025TEST(StringPrintfTest, HexSizeT) {
26 size_t size = 0x00107e59;
Dan Albertc007bc32015-03-16 10:08:46 -070027 EXPECT_EQ("00107e59", android::base::StringPrintf("%08zx", size));
28 EXPECT_EQ("0x00107e59", android::base::StringPrintf("0x%08zx", size));
Elliott Hughes6b3be292015-02-03 14:23:53 -080029}
Dan Albert0c4b3a32015-04-29 11:32:23 -070030#endif
Elliott Hughes6b3be292015-02-03 14:23:53 -080031
32TEST(StringPrintfTest, StringAppendF) {
33 std::string s("a");
Dan Albertc007bc32015-03-16 10:08:46 -070034 android::base::StringAppendF(&s, "b");
Elliott Hughes6b3be292015-02-03 14:23:53 -080035 EXPECT_EQ("ab", s);
36}
37
38TEST(StringPrintfTest, Errno) {
39 errno = 123;
Dan Albertc007bc32015-03-16 10:08:46 -070040 android::base::StringPrintf("hello %s", "world");
Elliott Hughes6b3be292015-02-03 14:23:53 -080041 EXPECT_EQ(123, errno);
42}
43
44void TestN(size_t n) {
45 char* buf = new char[n + 1];
46 memset(buf, 'x', n);
47 buf[n] = '\0';
Dan Albertc007bc32015-03-16 10:08:46 -070048 std::string s(android::base::StringPrintf("%s", buf));
Elliott Hughes6b3be292015-02-03 14:23:53 -080049 EXPECT_EQ(buf, s);
50 delete[] buf;
51}
52
53TEST(StringPrintfTest, At1023) {
54 TestN(1023);
55}
56
57TEST(StringPrintfTest, At1024) {
58 TestN(1024);
59}
60
61TEST(StringPrintfTest, At1025) {
62 TestN(1025);
63}