memgraph/tests/unit/utils_string.cpp
Marin Tomic 4a3808b982 Remove keyword lowercasing from stripper
Summary: This is a simple thing that enables us to use keywords as symbolic names. A bad thing is that planning will be done for queries which differ only in keyword case (e.g. "MATCH (x) RETURN x" and "match (x) return x"), but that should not be a significant performance impact as queries are done programmaticaly when high throughput is expected and we don't expect letter case change in that case.

Reviewers: buda, mferencevic, teon.banek

Reviewed By: teon.banek

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1496
2018-07-19 17:08:45 +02:00

146 lines
4.7 KiB
C++

#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, LTrim) {
EXPECT_EQ(LTrim(" \t\n\r ab\r\n\t ab \r\t "), "ab\r\n\t ab \r\t ");
EXPECT_EQ(LTrim(" \t\n\r"), "");
EXPECT_EQ(LTrim("run()"), "run()");
EXPECT_EQ(LTrim(""), "");
}
TEST(String, RTrim) {
EXPECT_EQ(RTrim(" \t\n\r ab\r\n\t ab \r\t "), " \t\n\r ab\r\n\t ab");
EXPECT_EQ(RTrim(" \t\n\r"), "");
EXPECT_EQ(RTrim("run()"), "run()");
EXPECT_EQ(RTrim(""), "");
}
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(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(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(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(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, 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"));
}
TEST(String, IEquals) {
EXPECT_TRUE(IEquals("", ""));
EXPECT_FALSE(IEquals("", "fdasfa"));
EXPECT_TRUE(IEquals("abcv", "AbCV"));
EXPECT_FALSE(IEquals("abcv", "AbCd"));
}
TEST(String, RandomString) {
EXPECT_EQ(RandomString(0).size(), 0);
EXPECT_EQ(RandomString(1).size(), 1);
EXPECT_EQ(RandomString(42).size(), 42);
std::set<std::string> string_set;
for (int i = 0; i < 20; ++i) string_set.emplace(RandomString(256));
EXPECT_EQ(string_set.size(), 20);
}