-
Notifications
You must be signed in to change notification settings - Fork 49
/
StringUtilsTests.cpp
162 lines (132 loc) · 6.09 KB
/
StringUtilsTests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
#include "common/Common.hpp"
#include "utils/StringConversion.hpp"
#include "utils/StringUtils.hpp"
using namespace testing;
using namespace MAT;
using std::string;
using std::vector;
TEST(StringUtilsTests, SplitString)
{
// testing method
// void SplitString(const std::string& s, const char separator, std::vector<std::string>& parts);
vector<string> parts;
// empty input gives empty output
parts.clear();
StringUtils::SplitString("", ' ', parts);
ASSERT_EQ(parts.size(), 0ul);
// string with no separator gives single entry
parts.clear();
StringUtils::SplitString("abcdef0123456\t\r\n", ' ', parts);
ASSERT_EQ(parts.size(), 1ul);
ASSERT_EQ(parts.size() > 0 ? parts[0] : "null", "abcdef0123456\t\r\n");
// special case '\0' character gives single entry
parts.clear();
StringUtils::SplitString("abcdef0123456\t\r\n", '\0', parts);
ASSERT_EQ(parts.size(), 1ul);
ASSERT_EQ(parts.size() > 0 ? parts[0] : "null", "abcdef0123456\t\r\n");
// string with separator gives the correct number of entries
parts.clear();
StringUtils::SplitString("ab:cd:ef", ':', parts);
ASSERT_EQ(parts.size(), 3ul);
ASSERT_EQ(parts.size() > 0 ? parts[0] : "null", "ab");
ASSERT_EQ(parts.size() > 1 ? parts[1] : "null", "cd");
ASSERT_EQ(parts.size() > 2 ? parts[2] : "null", "ef");
// consequitive separators produce empty string entries
parts.clear();
StringUtils::SplitString("..test..", '.', parts);
ASSERT_EQ(parts.size(), 5ul);
ASSERT_EQ(parts.size() > 0 ? parts[0] : "null", "");
ASSERT_EQ(parts.size() > 1 ? parts[1] : "null", "");
ASSERT_EQ(parts.size() > 2 ? parts[2] : "null", "test");
ASSERT_EQ(parts.size() > 3 ? parts[3] : "null", "");
ASSERT_EQ(parts.size() > 4 ? parts[4] : "null", "");
// if parts was not empty these values will remain in the list while the new values are being added
parts.clear();
parts.push_back("ab");
parts.push_back("cd");
StringUtils::SplitString(" 01 23 ", ' ', parts);
ASSERT_EQ(parts.size(), 6ul);
ASSERT_EQ(parts.size() > 0 ? parts[0] : "null", "ab");
ASSERT_EQ(parts.size() > 1 ? parts[1] : "null", "cd");
ASSERT_EQ(parts.size() > 2 ? parts[2] : "null", "");
ASSERT_EQ(parts.size() > 3 ? parts[3] : "null", "01");
ASSERT_EQ(parts.size() > 4 ? parts[4] : "null", "23");
ASSERT_EQ(parts.size() > 5 ? parts[5] : "null", "");
}
TEST(StringUtilsTests, AreAllCharactersAllowlisted)
{
// testing method
// bool AreAllCharactersAllowlisted(const std::string& stringToTest, const std::string& allowlist);
string allAsciiChars;
for (int i = 1; i <= 255; i++)
{
allAsciiChars += ((char)i);
}
// any string is allowed against the full list of characters
EXPECT_TRUE(StringUtils::AreAllCharactersAllowlisted(allAsciiChars, allAsciiChars));
EXPECT_TRUE(StringUtils::AreAllCharactersAllowlisted("", allAsciiChars));
EXPECT_TRUE(StringUtils::AreAllCharactersAllowlisted("a", allAsciiChars));
EXPECT_TRUE(StringUtils::AreAllCharactersAllowlisted("aaaaaaaaaaa", allAsciiChars));
EXPECT_TRUE(StringUtils::AreAllCharactersAllowlisted(string(10000, ' '), allAsciiChars));
// any non-empty string is NOT allowed against an empty list of characters
EXPECT_FALSE(StringUtils::AreAllCharactersAllowlisted(allAsciiChars, ""));
EXPECT_FALSE(StringUtils::AreAllCharactersAllowlisted("a", ""));
EXPECT_FALSE(StringUtils::AreAllCharactersAllowlisted("aaaaaaaaaaa", ""));
EXPECT_FALSE(StringUtils::AreAllCharactersAllowlisted(string(10000, ' '), ""));
// empty string is allowed against any list of characters
EXPECT_TRUE(StringUtils::AreAllCharactersAllowlisted("", allAsciiChars));
EXPECT_TRUE(StringUtils::AreAllCharactersAllowlisted("", ""));
EXPECT_TRUE(StringUtils::AreAllCharactersAllowlisted("", "a"));
EXPECT_TRUE(StringUtils::AreAllCharactersAllowlisted("", "aaaaaaaaaaa"));
EXPECT_TRUE(StringUtils::AreAllCharactersAllowlisted("", string(10000, ' ')));
// a few more positive and negative tests
EXPECT_TRUE(StringUtils::AreAllCharactersAllowlisted("abc123", "abcdef123456"));
EXPECT_FALSE(StringUtils::AreAllCharactersAllowlisted("abc123", "abcdef23456"));
}
TEST(StringUtilsTests, ToString)
{
EXPECT_THAT(toString(true), Eq("true"));
EXPECT_THAT(toString(false), Eq("false"));
EXPECT_THAT(toString('A'), Eq("65"));
EXPECT_THAT(toString(static_cast<char>(-128)), Eq("-128"));
EXPECT_THAT(toString(static_cast<char>(127)), Eq("127"));
EXPECT_THAT(toString(-12345), Eq("-12345"));
EXPECT_THAT(toString(12345), Eq("12345"));
EXPECT_THAT(toString(-1234567l), Eq("-1234567"));
EXPECT_THAT(toString(1234567l), Eq("1234567"));
EXPECT_THAT(toString(-12345678901ll), Eq("-12345678901"));
EXPECT_THAT(toString(12345678901ll), Eq("12345678901"));
EXPECT_THAT(toString(static_cast<unsigned char>(255)), Eq("255"));
EXPECT_THAT(toString(12345u), Eq("12345"));
EXPECT_THAT(toString(1234567ul), Eq("1234567"));
EXPECT_THAT(toString(12345678901ull), Eq("12345678901"));
EXPECT_THAT(toString(1234.5f), Eq("1234.500000"));
EXPECT_THAT(toString(1234.567), Eq("1234.567000"));
EXPECT_THAT(toString(1234.567891l), Eq("1234.567891"));
}
#ifdef _WIN32
TEST(StringUtilsTests, Utf8Utf16Conversion)
{
std::vector<std::string> test_strings = {
"Falsches Üben von Xylophonmusik quält jeden größeren Zwerg",
"The quick brown fox jumps over the lazy dog",
"El pingüino Wenceslao hizo kilómetros bajo exhaustiva lluvia y frío,"
"añoraba a su querido cachorro.",
"Le cœur déçu mais l'âme plutôt naïve, Louÿs rêva de crapaüter en canoë"
"au delà des îles, près du mälström où brûlent les novæ.",
"Árvíztűrő tükörfúrógép",
"いろはにほへとちりぬるを",
"イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム",
"В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!",
"我能吞下玻璃而不伤身体。",
"!@#$%^&*()-=_+[]\\{}|;':\",./<>?",
};
for (const auto& str : test_strings) {
EXPECT_EQ(str, to_utf8_string(to_utf16_string(str)));
}
}
#endif // _WIN32