tests add to namespace sls (#464)

This commit is contained in:
Dhanya Thattil
2022-05-20 15:41:37 +02:00
committed by GitHub
parent c7ba79644a
commit f5745fcf18
44 changed files with 350 additions and 228 deletions

View File

@ -7,6 +7,8 @@
#include "sls/string_utils.h"
namespace sls {
TEST_CASE("copy a string") {
char src[10] = "hej";
@ -14,7 +16,7 @@ TEST_CASE("copy a string") {
char dst[20];
sls::strcpy_safe(dst, src);
strcpy_safe(dst, src);
REQUIRE(dst[0] == 'h');
REQUIRE(dst[1] == 'e');
REQUIRE(dst[2] == 'j');
@ -26,7 +28,7 @@ TEST_CASE("copy a string") {
TEST_CASE("copy a long string") {
auto src = "some very very long sting that does not fit";
char dst[3];
sls::strcpy_safe(dst, src);
strcpy_safe(dst, src);
REQUIRE(dst[0] == 's');
REQUIRE(dst[1] == 'o');
REQUIRE(dst[2] == '\0');
@ -35,7 +37,7 @@ TEST_CASE("copy a long string") {
TEST_CASE("split a string with end delimiter") {
std::string s("abra+kadabra+");
auto r = sls::split(s, '+');
auto r = split(s, '+');
REQUIRE(r.size() == 2);
REQUIRE(r[0] == "abra");
REQUIRE(r[1] == "kadabra");
@ -43,7 +45,7 @@ TEST_CASE("split a string with end delimiter") {
TEST_CASE("split a string without end delimiter") {
std::string s("abra+kadabra+filibom");
auto r = sls::split(s, '+');
auto r = split(s, '+');
REQUIRE(r.size() == 3);
REQUIRE(r[0] == "abra");
REQUIRE(r[1] == "kadabra");
@ -52,58 +54,60 @@ TEST_CASE("split a string without end delimiter") {
TEST_CASE("Remove char from string") {
char str[] = "sometest";
sls::removeChar(str, 'e');
removeChar(str, 'e');
REQUIRE(std::string(str) == "somtst");
}
TEST_CASE("Remove char from empty string") {
char str[50] = {};
sls::removeChar(str, 'e');
removeChar(str, 'e');
REQUIRE(std::string(str).empty());
}
TEST_CASE("Many characters in a row") {
char str[] = "someeequitellll::ongstring";
sls::removeChar(str, 'l');
removeChar(str, 'l');
REQUIRE(std::string(str) == "someeequite::ongstring");
}
TEST_CASE("Check is string is integer") {
REQUIRE(sls::is_int("75"));
REQUIRE(sls::is_int("11675"));
REQUIRE_FALSE(sls::is_int("7.5"));
REQUIRE_FALSE(sls::is_int("hej"));
REQUIRE_FALSE(sls::is_int("7a"));
REQUIRE_FALSE(sls::is_int(""));
REQUIRE(is_int("75"));
REQUIRE(is_int("11675"));
REQUIRE_FALSE(is_int("7.5"));
REQUIRE_FALSE(is_int("hej"));
REQUIRE_FALSE(is_int("7a"));
REQUIRE_FALSE(is_int(""));
}
TEST_CASE("Replace substring in string") {
std::string s = "this string should be replaced";
auto r = sls::replace_first(&s, "string ", "");
auto r = replace_first(&s, "string ", "");
REQUIRE(r == true);
REQUIRE(s == "this should be replaced");
}
TEST_CASE("Replace --help in command") {
std::string s = "sls_detector_get --help exptime";
auto r = sls::replace_first(&s, " --help", "");
auto r = replace_first(&s, " --help", "");
REQUIRE(r == true);
REQUIRE(s == "sls_detector_get exptime");
}
TEST_CASE("Replace -h in command") {
std::string s = "sls_detector_get -h exptime";
auto r = sls::replace_first(&s, " -h", "");
auto r = replace_first(&s, " -h", "");
REQUIRE(r == true);
REQUIRE(s == "sls_detector_get exptime");
}
TEST_CASE("replace --help") {
std::string s = "list --help";
auto r = sls::replace_first(&s, " --help", "");
auto r = replace_first(&s, " --help", "");
REQUIRE(r == true);
REQUIRE(s == "list");
}
// TEST_CASE("concat things not being strings")
// TEST_CASE("concat things not being strings")
} // namespace sls