Add tests and fix minor problems in specifier parser

This commit is contained in:
2023-06-01 22:39:10 +02:00
parent ae9425d433
commit 35ef94162b
5 changed files with 168 additions and 29 deletions

11
test/CMakeLists.txt Normal file
View File

@@ -0,0 +1,11 @@
project(simple-printf-test)
add_subdirectory(catch2 EXCLUDE_FROM_ALL)
add_custom_target(test "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}" "-r compact" "-s" DEPENDS ${PROJECT_NAME})
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/catch-framework")
aux_source_directory("src" TEST_SOURCES)
add_executable(${PROJECT_NAME} EXCLUDE_FROM_ALL ${TEST_SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE simple-printf Catch2::Catch2WithMain)

114
test/src/tests.cpp Normal file
View File

@@ -0,0 +1,114 @@
#include <array>
#include <catch2/catch_test_macros.hpp>
#include <cstdio>
#include <string>
#include <iostream>
extern "C" {
#include <simple-printf/simple-printf.h>
}
TEST_CASE("Parse Invalid Specifier")
{
const char *tst = "%m";
const char *tst2 = "%45m";
const char *tst3 = "%014m";
struct format_specifier result;
parse_format_specifier(tst, &result);
REQUIRE(result.specifier == FMT_INVALID);
parse_format_specifier(tst, &result);
REQUIRE(result.specifier == FMT_INVALID);
parse_format_specifier(tst, &result);
REQUIRE(result.specifier == FMT_INVALID);
}
TEST_CASE("Parse Format Specifier c", "Char")
{
const char *tst = "%c";
struct format_specifier result;
parse_format_specifier(tst, &result);
REQUIRE(result.length_of_provided_specifier == 2);
REQUIRE(result.specifier == FMT_CHAR);
REQUIRE(result.zero_pad == false);
}
TEST_CASE("Parse Format Specifier s", "String")
{
const char *tst = "%s";
struct format_specifier result;
parse_format_specifier(tst, &result);
REQUIRE(result.length_of_provided_specifier == 2);
REQUIRE(result.specifier == FMT_STRING);
REQUIRE(result.length == 0);
REQUIRE(result.zero_pad == false);
}
TEST_CASE("Parse Format Specifier 04s", "String")
{
const char *tst = "%04s";
struct format_specifier result;
parse_format_specifier(tst, &result);
REQUIRE(result.specifier == FMT_STRING);
REQUIRE(result.length_of_provided_specifier == 4);
REQUIRE(result.length == 4);
REQUIRE(result.zero_pad == true);
}
TEST_CASE("Parse Format Specifier 08x", "hex")
{
const char *tst = "%08x";
struct format_specifier result;
parse_format_specifier(tst, &result);
REQUIRE(result.specifier == FMT_HEXUINT);
REQUIRE(result.length_of_provided_specifier == 4);
REQUIRE(result.length == 8);
REQUIRE(result.zero_pad == true);
}
TEST_CASE("Parse Format Specifier 08x with stuff", "hex")
{
const std::array<std::string, 8> tst_arr {
std::move(std::string("%08xff458578")),
std::move(std::string("%08x45578")),
std::move(std::string("%08xx458578")),
std::move(std::string("%08x44458578")),
std::move(std::string("%08x45sdf458578")),
std::move(std::string("%08xsfsdf4558578")),
std::move(std::string("%08xX")),
std::move(std::string("%08xdsda"))
};
struct format_specifier result;
for (const auto &tst : tst_arr) {
parse_format_specifier(tst.c_str(), &result);
REQUIRE(result.specifier == FMT_HEXUINT);
REQUIRE(result.length_of_provided_specifier == 4);
REQUIRE(result.length == 8);
REQUIRE(result.zero_pad == true);
}
}
TEST_CASE("Parse Percent spefifier")
{
const char *tst = "%%";
struct format_specifier result;
parse_format_specifier(tst, &result);
REQUIRE(result.specifier == FMT_PERCENT);
REQUIRE(result.length_of_provided_specifier == 2);
REQUIRE(result.length == 0);
REQUIRE(result.zero_pad == false);
}