Add tests for string utils
Reviewers: florijan, teon.banek Reviewed By: teon.banek Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D941
This commit is contained in:
parent
00f76d1391
commit
b39231f7df
@ -27,10 +27,10 @@ inline std::string Trim(const std::string &s) {
|
||||
// Need to check this to be sure that prev(end) exists.
|
||||
return s;
|
||||
}
|
||||
while (begin != end && isspace(*begin)) {
|
||||
while (begin < end && isspace(*begin)) {
|
||||
++begin;
|
||||
}
|
||||
while (prev(end) != begin && isspace(*prev(end))) {
|
||||
while (end > begin && isspace(*prev(end))) {
|
||||
--end;
|
||||
}
|
||||
return std::string(begin, end);
|
||||
@ -49,7 +49,6 @@ inline std::string ToLowerCase(std::string s) {
|
||||
* Return string with all uppercased characters (locale independent).
|
||||
*/
|
||||
inline std::string ToUpperCase(std::string s) {
|
||||
std::string s2(s.size(), ' ');
|
||||
std::transform(s.begin(), s.end(), s.begin(),
|
||||
[](char c) { return toupper(c); });
|
||||
return s;
|
||||
|
@ -1,50 +1,113 @@
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "utils/exceptions.hpp"
|
||||
#include "utils/string.hpp"
|
||||
|
||||
using vec = std::vector<std::string>;
|
||||
|
||||
using namespace utils;
|
||||
|
||||
TEST(String, Trim) {
|
||||
EXPECT_EQ(Trim(" \t\n\r ab\r\n\t ab \r\t "), "ab\r\n\t ab");
|
||||
EXPECT_EQ(Trim(" \t\n\r"), "");
|
||||
EXPECT_EQ(Trim("run()"), "run()");
|
||||
EXPECT_EQ(Trim(""), "");
|
||||
}
|
||||
|
||||
TEST(String, ToLowerCase) {
|
||||
EXPECT_EQ(ToLowerCase("MemGraph"), "memgraph");
|
||||
EXPECT_EQ(ToLowerCase(" ( Mem Graph ) "), " ( mem graph ) ");
|
||||
}
|
||||
|
||||
TEST(String, ToUpperCase) {
|
||||
EXPECT_EQ(ToUpperCase("MemGraph"), "MEMGRAPH");
|
||||
EXPECT_EQ(ToUpperCase(" ( Mem Graph ) "), " ( MEM GRAPH ) ");
|
||||
// ToUpperCase ignores unicode.
|
||||
EXPECT_EQ(ToUpperCase("\u0161memgraph"), "\u0161MEMGRAPH");
|
||||
}
|
||||
|
||||
TEST(String, Join) {
|
||||
EXPECT_EQ(Join({}, " "), "");
|
||||
EXPECT_EQ(Join({"mem", "gra", "ph"}, ""), "memgraph");
|
||||
EXPECT_EQ(Join({"mirko", "slavko", "pero"}, ", "), "mirko, slavko, pero");
|
||||
EXPECT_EQ(Join({"", "abc", "", "def", ""}, " "), " abc def ");
|
||||
}
|
||||
|
||||
TEST(String, Replace) {
|
||||
EXPECT_EQ(Replace("ab.abab.", "ab", "cd"), "cd.cdcd.");
|
||||
EXPECT_EQ(Replace("ababab", "ab", ""), "");
|
||||
EXPECT_EQ(Replace("ababccab.", "ab", ""), "cc.");
|
||||
EXPECT_EQ(Replace("aabb", "ab", ""), "ab");
|
||||
EXPECT_EQ(Replace("ababcab.", "ab", "abab"), "ababababcabab.");
|
||||
}
|
||||
|
||||
TEST(String, SplitNoLimit) {
|
||||
EXPECT_EQ(utils::Split("aba", "a"), vec({"", "b", ""}));
|
||||
EXPECT_EQ(utils::Split("aba", "b"), vec({"a", "a"}));
|
||||
EXPECT_EQ(utils::Split("abba", "b"), vec({"a", "", "a"}));
|
||||
EXPECT_EQ(utils::Split("aba", "c"), vec{"aba"});
|
||||
EXPECT_EQ(Split("aba", "a"), vec({"", "b", ""}));
|
||||
EXPECT_EQ(Split("aba", "b"), vec({"a", "a"}));
|
||||
EXPECT_EQ(Split("abba", "b"), vec({"a", "", "a"}));
|
||||
EXPECT_EQ(Split("aba", "c"), vec{"aba"});
|
||||
}
|
||||
|
||||
TEST(String, RSplitNoLimit) {
|
||||
// Tests same like for Split
|
||||
EXPECT_EQ(utils::RSplit("aba", "a"), vec({"", "b", ""}));
|
||||
EXPECT_EQ(utils::RSplit("aba", "b"), vec({"a", "a"}));
|
||||
EXPECT_EQ(utils::RSplit("abba", "b"), vec({"a", "", "a"}));
|
||||
EXPECT_EQ(utils::RSplit("aba", "c"), vec{"aba"});
|
||||
EXPECT_EQ(RSplit("aba", "a"), vec({"", "b", ""}));
|
||||
EXPECT_EQ(RSplit("aba", "b"), vec({"a", "a"}));
|
||||
EXPECT_EQ(RSplit("abba", "b"), vec({"a", "", "a"}));
|
||||
EXPECT_EQ(RSplit("aba", "c"), vec{"aba"});
|
||||
}
|
||||
|
||||
TEST(String, SplitWithLimit) {
|
||||
EXPECT_EQ(utils::Split("a.b.c.d", ".", 0), vec({"a.b.c.d"}));
|
||||
EXPECT_EQ(utils::Split("a.b.c.d", ".", 1), vec({"a", "b.c.d"}));
|
||||
EXPECT_EQ(utils::Split("a.b.c.d", ".", 2), vec({"a", "b", "c.d"}));
|
||||
EXPECT_EQ(utils::Split("a.b.c.d", ".", 100), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(utils::Split("a.b.c.d", ".", -1), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(utils::Split("a.b.c.d", ".", -2), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(utils::Split("a.b.c.d", ".", -100), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(utils::Split("a..b..c", ".", 1), vec({"a", ".b..c"}));
|
||||
EXPECT_EQ(utils::Split("a..b..c", ".", 2), vec({"a","", "b..c"}));
|
||||
EXPECT_EQ(Split("a.b.c.d", ".", 0), vec({"a.b.c.d"}));
|
||||
EXPECT_EQ(Split("a.b.c.d", ".", 1), vec({"a", "b.c.d"}));
|
||||
EXPECT_EQ(Split("a.b.c.d", ".", 2), vec({"a", "b", "c.d"}));
|
||||
EXPECT_EQ(Split("a.b.c.d", ".", 100), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(Split("a.b.c.d", ".", -1), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(Split("a.b.c.d", ".", -2), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(Split("a.b.c.d", ".", -100), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(Split("a..b..c", ".", 1), vec({"a", ".b..c"}));
|
||||
EXPECT_EQ(Split("a..b..c", ".", 2), vec({"a", "", "b..c"}));
|
||||
}
|
||||
|
||||
TEST(String, RSplitWithLimit) {
|
||||
EXPECT_EQ(utils::RSplit("a.b.c.d", ".", 0), vec({"a.b.c.d"}));
|
||||
EXPECT_EQ(utils::RSplit("a.b.c.d", ".", 1), vec({"a.b.c", "d"}));
|
||||
EXPECT_EQ(utils::RSplit("a.b.c.d", ".", 2), vec({"a.b", "c", "d"}));
|
||||
EXPECT_EQ(utils::RSplit("a.b.c.d", ".", 100), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(utils::RSplit("a.b.c.d", ".", -1), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(utils::RSplit("a.b.c.d", ".", -2), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(utils::RSplit("a.b.c.d", ".", -100), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(utils::RSplit("a..b..c", ".", 1), vec({"a..b.", "c"}));
|
||||
EXPECT_EQ(utils::RSplit("a..b..c", ".", 2), vec({"a..b","", "c"}));
|
||||
EXPECT_EQ(RSplit("a.b.c.d", ".", 0), vec({"a.b.c.d"}));
|
||||
EXPECT_EQ(RSplit("a.b.c.d", ".", 1), vec({"a.b.c", "d"}));
|
||||
EXPECT_EQ(RSplit("a.b.c.d", ".", 2), vec({"a.b", "c", "d"}));
|
||||
EXPECT_EQ(RSplit("a.b.c.d", ".", 100), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(RSplit("a.b.c.d", ".", -1), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(RSplit("a.b.c.d", ".", -2), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(RSplit("a.b.c.d", ".", -100), vec({"a", "b", "c", "d"}));
|
||||
EXPECT_EQ(RSplit("a..b..c", ".", 1), vec({"a..b.", "c"}));
|
||||
EXPECT_EQ(RSplit("a..b..c", ".", 2), vec({"a..b", "", "c"}));
|
||||
}
|
||||
|
||||
TEST(String, SplitWhistespace) {
|
||||
EXPECT_EQ(utils::Split(" "), vec({}));
|
||||
EXPECT_EQ(utils::Split(" a b "), vec({"a", "b"}));
|
||||
TEST(String, SplitWhitespace) {
|
||||
EXPECT_EQ(Split(" "), vec({}));
|
||||
EXPECT_EQ(Split(" a b "), vec({"a", "b"}));
|
||||
}
|
||||
|
||||
TEST(String, ParseDouble) {
|
||||
EXPECT_EQ(ParseDouble(".5"), 0.5);
|
||||
EXPECT_EQ(ParseDouble("5"), 5.0);
|
||||
EXPECT_EQ(ParseDouble("1.5"), 1.5);
|
||||
EXPECT_EQ(ParseDouble("15e-1"), 1.5);
|
||||
EXPECT_THROW(ParseDouble("1.5a"), BasicException);
|
||||
}
|
||||
|
||||
TEST(String, StartsWith) {
|
||||
EXPECT_TRUE(StartsWith("memgraph", "mem"));
|
||||
EXPECT_TRUE(StartsWith("memgraph", ""));
|
||||
EXPECT_TRUE(StartsWith("", ""));
|
||||
EXPECT_TRUE(StartsWith("memgraph", "memgraph"));
|
||||
EXPECT_FALSE(StartsWith("memgraph", "MEM"));
|
||||
EXPECT_FALSE(StartsWith("memgrap", "memgraph"));
|
||||
}
|
||||
|
||||
TEST(String, EndsWith) {
|
||||
EXPECT_TRUE(EndsWith("memgraph", "graph"));
|
||||
EXPECT_TRUE(EndsWith("memgraph", ""));
|
||||
EXPECT_TRUE(EndsWith("", ""));
|
||||
EXPECT_TRUE(EndsWith("memgraph", "memgraph"));
|
||||
EXPECT_FALSE(EndsWith("memgraph", "GRAPH"));
|
||||
EXPECT_FALSE(EndsWith("memgraph", "the memgraph"));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user