Select Git revision
string_tools.test.cpp 2.86 KiB
#include "string_tools.h"
#include <catch2/catch_test_macros.hpp>
#include <cstdio>
TEST_CASE("palindrome") {
REQUIRE(isWordPalindrom(""));
REQUIRE(isWordPalindrom("a"));
REQUIRE(isWordPalindrom("anna"));
REQUIRE(isWordPalindrom("Anna"));
REQUIRE_FALSE(isWordPalindrom("Tomate"));
}
constexpr char UNWRITTEN = (char)0xFF;
static void init_buffer(char* buf, size_t bufsize) {
for (size_t i = 0; i < bufsize; ++i) {
*buf++ = UNWRITTEN;
}
}
TEST_CASE("concat") {
// use one byte before and two bytes after buffer that is given to the function to assert that
// the function doesn't touch the surounding bytes.
constexpr size_t TRUEBUFLEN = 13;
char true_buf[TRUEBUFLEN];
constexpr size_t BUFLEN = 10;
char* buf = true_buf + 1;
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "Test", "that");
REQUIRE(strcmp(buf, "Testthat") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, " 0Test", " 2 ");
REQUIRE(strcmp(buf, " 0Test 2 ") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "1234567890123", "ABC");
REQUIRE(strcmp(buf, "123456789") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "ABC", "1234567890123");
REQUIRE(strcmp(buf, "ABC123456") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "123456789", "ABC");
REQUIRE(strcmp(buf, "123456789") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "xy", "");
REQUIRE(strcmp(buf, "xy") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "", "ab");
REQUIRE(strcmp(buf, "ab") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, BUFLEN, "", "");
REQUIRE(strcmp(buf, "") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[BUFLEN] == UNWRITTEN);
REQUIRE(buf[BUFLEN + 1] == UNWRITTEN);
init_buffer(true_buf, TRUEBUFLEN);
concat_s(buf, 1, "a", "b");
REQUIRE(strcmp(buf, "") == 0);
REQUIRE(buf[-1] == UNWRITTEN);
REQUIRE(buf[1] == UNWRITTEN);
REQUIRE(buf[2] == UNWRITTEN);
}