1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "aemu/base/StringFormat.h"
15
16 #include <gtest/gtest.h>
17
18 namespace android {
19 namespace base {
20
TEST(StringFormat,EmptyString)21 TEST(StringFormat, EmptyString) {
22 std::string s = StringFormat("");
23 EXPECT_TRUE(s.empty());
24 EXPECT_STREQ("", s.c_str());
25 }
26
TEST(StringFormat,SimpleString)27 TEST(StringFormat, SimpleString) {
28 std::string s = StringFormat("foobar");
29 EXPECT_STREQ("foobar", s.c_str());
30 }
31
TEST(StringFormat,SimpleDecimal)32 TEST(StringFormat, SimpleDecimal) {
33 std::string s = StringFormat("Pi is about %d.%d\n", 3, 1415);
34 EXPECT_STREQ("Pi is about 3.1415\n", s.c_str());
35 }
36
TEST(StringFormat,VeryLongString)37 TEST(StringFormat, VeryLongString) {
38 static const char kPiece[] = "A hospital bed is a parked taxi with the meter running - Groucho Marx. ";
39 const size_t kPieceLen = sizeof(kPiece) - 1U;
40 std::string s = StringFormat("%s%s%s%s%s%s%s", kPiece, kPiece, kPiece,
41 kPiece, kPiece, kPiece, kPiece);
42 EXPECT_EQ(7U * kPieceLen, s.size());
43 for (size_t n = 0; n < 7U; ++n) {
44 std::string s2 = std::string(s.c_str() + n * kPieceLen, kPieceLen);
45 EXPECT_STREQ(kPiece, s2.c_str()) << "Index #" << n;
46 }
47 }
48
TEST(StringAppendFormat,EmptyString)49 TEST(StringAppendFormat, EmptyString) {
50 std::string s = "foo";
51 StringAppendFormat(&s, "");
52 EXPECT_EQ(3U, s.size());
53 EXPECT_STREQ("foo", s.c_str());
54 }
55
TEST(StringAppendFormat,SimpleString)56 TEST(StringAppendFormat, SimpleString) {
57 std::string s = "foo";
58 StringAppendFormat(&s, "bar");
59 EXPECT_EQ(6U, s.size());
60 EXPECT_STREQ("foobar", s.c_str());
61 }
62
TEST(StringAppendFormat,VeryLongString)63 TEST(StringAppendFormat, VeryLongString) {
64 static const char kPiece[] = "A hospital bed is a parked taxi with the meter running - Groucho Marx. ";
65 const size_t kPieceLen = sizeof(kPiece) - 1U;
66 const size_t kCount = 12;
67 std::string s;
68 for (size_t n = 0; n < kCount; ++n) {
69 StringAppendFormat(&s, "%s", kPiece);
70 }
71
72 EXPECT_EQ(kCount * kPieceLen, s.size());
73 for (size_t n = 0; n < kCount; ++n) {
74 std::string s2 = std::string(s.c_str() + n * kPieceLen, kPieceLen);
75 EXPECT_STREQ(kPiece, s2.c_str()) << "Index #" << n;
76 }
77 }
78
79 } // namespace base
80 } // namespace android
81