1 /*
2 * Copyright (C) 2018 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
17 #include "utils/strings/append.h"
18
19 #include "gtest/gtest.h"
20
21 namespace libtextclassifier3 {
22 namespace strings {
23
TEST(StringUtilTest,SStringAppendF)24 TEST(StringUtilTest, SStringAppendF) {
25 std::string str;
26 SStringAppendF(&str, 5, "%d %d", 0, 1);
27 EXPECT_EQ(str, "0 1");
28
29 SStringAppendF(&str, 1, "%d", 9);
30 EXPECT_EQ(str, "0 19");
31
32 SStringAppendF(&str, 1, "%d", 10);
33 EXPECT_EQ(str, "0 191");
34
35 str.clear();
36
37 SStringAppendF(&str, 5, "%d", 100);
38 EXPECT_EQ(str, "100");
39 }
40
TEST(StringUtilTest,SStringAppendFBufCalc)41 TEST(StringUtilTest, SStringAppendFBufCalc) {
42 std::string str;
43 SStringAppendF(&str, 0, "%d %s %d", 1, "hello", 2);
44 EXPECT_EQ(str, "1 hello 2");
45 }
46
TEST(StringUtilTest,JoinStrings)47 TEST(StringUtilTest, JoinStrings) {
48 std::vector<std::string> vec;
49 vec.push_back("1");
50 vec.push_back("2");
51 vec.push_back("3");
52
53 EXPECT_EQ("1,2,3", JoinStrings(",", vec));
54 EXPECT_EQ("123", JoinStrings("", vec));
55 EXPECT_EQ("1, 2, 3", JoinStrings(", ", vec));
56 EXPECT_EQ("", JoinStrings(",", std::vector<std::string>()));
57 }
58
59 } // namespace strings
60 } // namespace libtextclassifier3
61