2021-02-16 14:22:53 +03:00
|
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
2020-11-03 18:54:06 +03:00
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
2023-08-04 10:53:11 +03:00
|
|
|
|
#ifdef ENABLE_RE2_REGEX
|
2020-11-06 15:48:11 +03:00
|
|
|
|
#include "re2/re2.h"
|
2023-08-04 10:53:11 +03:00
|
|
|
|
#endif
|
2020-12-22 04:12:32 +03:00
|
|
|
|
#include "nlohmann/json.hpp"
|
2021-05-05 03:12:28 +03:00
|
|
|
|
#include "string_utils.h"
|
2021-08-26 23:50:03 +03:00
|
|
|
|
#include "ustring.h"
|
2021-05-05 03:12:28 +03:00
|
|
|
|
|
2020-11-06 15:48:11 +03:00
|
|
|
|
|
2020-11-03 18:54:06 +03:00
|
|
|
|
TEST(utils, make_string) {
|
|
|
|
|
std::string res = MakeString("a", "b", 0);
|
|
|
|
|
EXPECT_EQ(res, "ab0");
|
|
|
|
|
}
|
2020-11-06 15:48:11 +03:00
|
|
|
|
|
2023-08-04 10:53:11 +03:00
|
|
|
|
#ifdef ENABLE_RE2_REGEX
|
2021-01-28 01:55:50 +03:00
|
|
|
|
TEST(utils, re2_basic) {
|
2020-11-06 15:48:11 +03:00
|
|
|
|
re2::StringPiece piece("1234");
|
|
|
|
|
re2::RE2 reg("[0-9]*");
|
|
|
|
|
}
|
2023-08-04 10:53:11 +03:00
|
|
|
|
#endif
|
2020-12-22 04:12:32 +03:00
|
|
|
|
|
2021-01-28 01:55:50 +03:00
|
|
|
|
TEST(utils, json) {
|
2020-12-22 04:12:32 +03:00
|
|
|
|
nlohmann::json j;
|
|
|
|
|
j.push_back("foo");
|
|
|
|
|
EXPECT_EQ(j.size(), 1);
|
|
|
|
|
}
|
2021-02-16 14:22:53 +03:00
|
|
|
|
|
2021-02-17 22:11:29 +03:00
|
|
|
|
TEST(utils, split_string) {
|
2021-02-09 20:41:00 +03:00
|
|
|
|
auto result = SplitString("a b c d e f", " ");
|
|
|
|
|
EXPECT_EQ(result.size(), 6);
|
|
|
|
|
|
|
|
|
|
// contain a space
|
|
|
|
|
result = SplitString("ab cd ef gh", " ");
|
2021-02-16 14:22:53 +03:00
|
|
|
|
EXPECT_EQ(result.size(), 5);
|
2021-02-09 20:41:00 +03:00
|
|
|
|
|
|
|
|
|
// contain a space
|
|
|
|
|
result = SplitString("ab cd ef gh", " ", true);
|
2021-02-16 14:22:53 +03:00
|
|
|
|
EXPECT_EQ(result.size(), 4);
|
2021-02-09 20:41:00 +03:00
|
|
|
|
|
|
|
|
|
result = SplitString("abcd", " ");
|
|
|
|
|
EXPECT_EQ(result.size(), 1);
|
|
|
|
|
EXPECT_EQ(result[0], "abcd");
|
|
|
|
|
|
|
|
|
|
result = SplitString("eabc\nasbd", "\n");
|
|
|
|
|
EXPECT_EQ(result.size(), 2);
|
|
|
|
|
EXPECT_EQ(result[0], "eabc");
|
|
|
|
|
|
|
|
|
|
result = SplitString("a\nb\n", "\n");
|
|
|
|
|
EXPECT_EQ(result.size(), 2);
|
|
|
|
|
EXPECT_EQ(result[1], "b");
|
|
|
|
|
|
|
|
|
|
// two seps
|
|
|
|
|
result = SplitString("ea,bc\nas,bd", ",\n");
|
|
|
|
|
EXPECT_EQ(result.size(), 4);
|
|
|
|
|
EXPECT_EQ(result[1], "bc");
|
2021-02-16 14:22:53 +03:00
|
|
|
|
}
|
2021-02-09 20:41:00 +03:00
|
|
|
|
|
2021-02-16 14:22:53 +03:00
|
|
|
|
TEST(utils, utf8) {
|
|
|
|
|
std::vector<std::string> srcs = {"abc", "ABCé", "中文"};
|
|
|
|
|
std::vector<std::string> lowered = {"abc", "abcé", "中文"};
|
|
|
|
|
for (size_t i = 0; i < srcs.size(); ++i) {
|
|
|
|
|
std::string lower;
|
|
|
|
|
lower = srcs[i];
|
|
|
|
|
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
|
|
|
|
|
EXPECT_EQ(lowered[i], lower);
|
|
|
|
|
}
|
2021-02-09 20:41:00 +03:00
|
|
|
|
}
|