• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Android Open Source Project
2 //
3 // This software is licensed under the terms of the GNU General Public
4 // License version 2, as published by the Free Software Foundation, and
5 // may be copied, distributed, and modified under those terms.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License for more details.
11 
12 #include "android/base/StringFormat.h"
13 
14 #include <gtest/gtest.h>
15 
16 #include "android/base/String.h"
17 
18 namespace android {
19 namespace base {
20 
TEST(StringFormat,EmptyString)21 TEST(StringFormat, EmptyString) {
22     String s = StringFormat("");
23     EXPECT_TRUE(s.empty());
24     EXPECT_STREQ("", s.c_str());
25 }
26 
TEST(StringFormat,SimpleString)27 TEST(StringFormat, SimpleString) {
28     String s = StringFormat("foobar");
29     EXPECT_STREQ("foobar", s.c_str());
30 }
31 
TEST(StringFormat,SimpleDecimal)32 TEST(StringFormat, SimpleDecimal) {
33     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     String s = StringFormat("%s%s%s%s%s%s%s",
41                             kPiece,
42                             kPiece,
43                             kPiece,
44                             kPiece,
45                             kPiece,
46                             kPiece,
47                             kPiece
48                            );
49     EXPECT_EQ(7U * kPieceLen, s.size());
50     for (size_t n = 0; n < 7U; ++n) {
51         String s2 = String(s.c_str() + n * kPieceLen, kPieceLen);
52         EXPECT_STREQ(kPiece, s2.c_str()) << "Index #" << n;
53     }
54 }
55 
TEST(StringAppendFormat,EmptyString)56 TEST(StringAppendFormat, EmptyString) {
57     String s = "foo";
58     StringAppendFormat(&s, "");
59     EXPECT_EQ(3U, s.size());
60     EXPECT_STREQ("foo", s.c_str());
61 }
62 
TEST(StringAppendFormat,SimpleString)63 TEST(StringAppendFormat, SimpleString) {
64     String s = "foo";
65     StringAppendFormat(&s, "bar");
66     EXPECT_EQ(6U, s.size());
67     EXPECT_STREQ("foobar", s.c_str());
68 }
69 
TEST(StringAppendFormat,VeryLongString)70 TEST(StringAppendFormat, VeryLongString) {
71     static const char kPiece[] = "A hospital bed is a parked taxi with the meter running - Groucho Marx. ";
72     const size_t kPieceLen = sizeof(kPiece) - 1U;
73     const size_t kCount = 12;
74     String s;
75     for (size_t n = 0; n < kCount; ++n) {
76         StringAppendFormat(&s, "%s", kPiece);
77     }
78 
79     EXPECT_EQ(kCount * kPieceLen, s.size());
80     for (size_t n = 0; n < kCount; ++n) {
81         String s2 = String(s.c_str() + n * kPieceLen, kPieceLen);
82         EXPECT_STREQ(kPiece, s2.c_str()) << "Index #" << n;
83     }
84 }
85 
86 }  // namespace base
87 }  // namespace android
88