2014-03-20 13:34:21 +00:00
|
|
|
/*
|
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2016-09-07 14:24:00 +00:00
|
|
|
* Copyright (C) 2014-2016 Symless Ltd.
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2014-03-20 13:34:21 +00:00
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2014-03-20 13:34:21 +00:00
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "base/String.h"
|
|
|
|
|
|
|
|
|
|
#include "test/global/gtest.h"
|
|
|
|
|
|
2014-03-21 08:32:36 +00:00
|
|
|
using namespace synergy;
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, format_formatWithArguments_formatedString) {
|
|
|
|
|
const char *format = "%%%{1}=%{2}";
|
|
|
|
|
const char *arg1 = "answer";
|
|
|
|
|
const char *arg2 = "42";
|
2014-03-21 08:32:36 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
String result = string::format(format, arg1, arg2);
|
2014-03-21 08:32:36 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ("%answer=42", result);
|
2014-03-21 08:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, findReplaceAll_inputString_replacedString) {
|
|
|
|
|
String subject = "foobar";
|
|
|
|
|
String find = "bar";
|
|
|
|
|
String replace = "baz";
|
2014-03-20 13:34:21 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
string::findReplaceAll(subject, find, replace);
|
2014-03-20 13:34:21 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ("foobaz", subject);
|
2014-03-20 13:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, sprintf_formatWithArgument_formatedString) {
|
|
|
|
|
const char *format = "%s=%d";
|
|
|
|
|
const char *arg1 = "answer";
|
|
|
|
|
int arg2 = 42;
|
2014-03-20 13:34:21 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
String result = string::sprintf(format, arg1, arg2);
|
2014-03-20 13:34:21 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ("answer=42", result);
|
2014-03-20 13:34:21 +00:00
|
|
|
}
|
2015-04-14 12:47:34 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, toHex_plaintext_hexString) {
|
|
|
|
|
String subject = "foobar";
|
|
|
|
|
int width = 2;
|
2015-04-14 12:47:34 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
string::toHex(subject, width);
|
2015-04-14 12:47:34 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ("666f6f626172", subject);
|
2015-04-14 12:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, uppercase_lowercaseInput_uppercaseOutput) {
|
|
|
|
|
String subject = "12foo3BaR";
|
2015-04-14 12:47:34 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
string::uppercase(subject);
|
2015-04-14 12:47:34 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ("12FOO3BAR", subject);
|
2015-04-14 12:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, removeChar_inputString_removeAllSpecifiedCharactors) {
|
|
|
|
|
String subject = "foobar";
|
|
|
|
|
const char c = 'o';
|
2015-04-14 12:47:34 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
string::removeChar(subject, c);
|
2015-04-14 12:47:34 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ("fbar", subject);
|
2015-04-14 12:47:34 +00:00
|
|
|
}
|
2015-05-19 21:23:43 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, intToString_inputInt_outputString) {
|
|
|
|
|
size_t value = 123;
|
2015-05-19 21:23:43 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
String number = string::sizeTypeToString(value);
|
2015-05-19 21:23:43 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ("123", number);
|
2015-05-19 21:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, stringToUint_inputString_outputInt) {
|
|
|
|
|
String number = "123";
|
2015-05-19 21:23:43 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
size_t value = string::stringToSizeType(number);
|
2015-05-19 21:23:43 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ(123, value);
|
2015-05-19 21:23:43 +00:00
|
|
|
}
|
2015-10-29 19:53:15 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, splitString_twoSeparator_returnThreeParts) {
|
|
|
|
|
String string = "stub1:stub2:stub3";
|
2015-10-29 19:53:15 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
std::vector<String> results = string::splitString(string, ':');
|
2015-10-29 19:53:15 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ(3, results.size());
|
|
|
|
|
EXPECT_EQ("stub1", results[0]);
|
|
|
|
|
EXPECT_EQ("stub2", results[1]);
|
|
|
|
|
EXPECT_EQ("stub3", results[2]);
|
2015-10-29 19:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, splitString_oneSeparator_returnTwoParts) {
|
|
|
|
|
String string = "stub1:stub2";
|
2015-10-29 19:53:15 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
std::vector<String> results = string::splitString(string, ':');
|
2015-10-29 19:53:15 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ(2, results.size());
|
|
|
|
|
EXPECT_EQ("stub1", results[0]);
|
|
|
|
|
EXPECT_EQ("stub2", results[1]);
|
2015-10-29 19:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, splitString_noSeparator_returnOriginalString) {
|
|
|
|
|
String string = "stub1";
|
2015-10-29 19:53:15 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
std::vector<String> results = string::splitString(string, ':');
|
2015-10-29 19:53:15 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ(1, results.size());
|
|
|
|
|
EXPECT_EQ("stub1", results[0]);
|
2015-10-29 19:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, splitString_emptyString_returnEmptyVector) {
|
|
|
|
|
String string;
|
2015-10-29 22:11:09 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
std::vector<String> results = string::splitString(string, ':');
|
2015-10-29 22:11:09 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ(0, results.size());
|
2015-10-29 22:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, splitString_tailSeparator_returnTwoParts) {
|
|
|
|
|
String string = "stub1:stub2:";
|
2015-10-29 20:00:03 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
std::vector<String> results = string::splitString(string, ':');
|
2015-10-29 20:00:03 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ(2, results.size());
|
|
|
|
|
EXPECT_EQ("stub1", results[0]);
|
|
|
|
|
EXPECT_EQ("stub2", results[1]);
|
2015-10-29 20:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, splitString_headSeparator_returnTwoParts) {
|
|
|
|
|
String string = ":stub1:stub2";
|
2015-10-29 20:00:03 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
std::vector<String> results = string::splitString(string, ':');
|
2015-10-29 20:00:03 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ(2, results.size());
|
|
|
|
|
EXPECT_EQ("stub1", results[0]);
|
|
|
|
|
EXPECT_EQ("stub2", results[1]);
|
2015-10-29 20:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
TEST(StringTests, splitString_headAndTailSeparators_returnTwoParts) {
|
|
|
|
|
String string = ":stub1:stub2:";
|
2015-10-29 20:00:03 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
std::vector<String> results = string::splitString(string, ':');
|
2015-10-29 20:00:03 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EXPECT_EQ(2, results.size());
|
|
|
|
|
EXPECT_EQ("stub1", results[0]);
|
|
|
|
|
EXPECT_EQ("stub2", results[1]);
|
2015-10-29 20:00:03 +00:00
|
|
|
}
|