Added more tests; is_digit(s, i) -> is_digit<s>(i); Minor other stuff

This commit is contained in:
2021-11-22 10:53:55 +01:00
parent 7b5290983a
commit 800c141a87
6 changed files with 130 additions and 19 deletions

View File

@@ -18,4 +18,5 @@ endmacro()
package_add_test(utility_test src/utility.cpp)
package_add_test(parse_test src/parse.cpp)
package_add_test(format_test src/format.cpp)

View File

@@ -1,2 +1,48 @@
#include <format.h>
#include <gtest/gtest.h>
#include <gtest/gtest.h>
using namespace detail;
TEST(Format, positive_int) {
constexpr std::array<char, 8> control1 = {'0', '0', '0', '2',
'2', '2', '2', '2'};
constexpr std::array<char, 8> formatted1 = format<"{:08}">(22222);
constexpr std::array<char, 8> control2 = {' ', ' ', ' ', '2',
'2', '2', '2', '2'};
constexpr std::array<char, 8> formatted2 = format<"{:8}">(22222);
constexpr std::array<char, 8> control3 = {'0', '0', '0', '1',
'2', '3', '4', '5'};
constexpr std::array<char, 8> formatted3 = format<"{:08.4}">(12345);
constexpr std::array<char, 4> control4 = {'6', '7', '8', '9'};
constexpr std::array<char, 4> formatted4 = format<"{:4}">(6789);
EXPECT_EQ(control1, formatted1);
EXPECT_EQ(control2, formatted2);
EXPECT_EQ(control3, formatted3);
EXPECT_EQ(control4, formatted4);
}
//TEST(Format, negative_int) {
// // TODO
// EXPECT_EQ(true, false);
//}
//
//TEST(Format, positive_float) {
// // TODO
// EXPECT_EQ(true, false);
//}
//
//TEST(Format, negative_float) {
// // TODO
// EXPECT_EQ(true, false);
//}
//
//TEST(Format, string) {
// // TODO
// EXPECT_EQ(true, false);
//}

View File

@@ -5,10 +5,26 @@
using namespace detail;
TEST(Parse, parse_number) {
constexpr auto pr1 = parse_number<"0">(0);
constexpr auto pr2 = parse_number<"9">(0);
constexpr auto pr3 = parse_number<"abcd16777216">(4);
constexpr auto pr4 = parse_number<"009">(0);
constexpr auto pr5 = parse_number<"10101010109">(6);
constexpr auto pr6 = parse_number<"">(0);
EXPECT_EQ(pr1.value, 0);
EXPECT_EQ(pr2.value, 9);
EXPECT_EQ(pr3.value, 16777216);
EXPECT_EQ(pr4.value, 9);
EXPECT_EQ(pr5.value, 10109);
EXPECT_EQ(pr6.is_valid, false);
}
TEST(Parse, ast_node) {
ast_node_t ast_node1{'a'};
ast_node_t ast_node2{'a'};
ast_node_t ast_node3{'b'};
constexpr ast_node_t ast_node1{'a'};
constexpr ast_node_t ast_node2{'a'};
constexpr ast_node_t ast_node3{'b'};
EXPECT_EQ(ast_node1 == ast_node2, true);
EXPECT_EQ(ast_node1 == ast_node3, false);
@@ -21,25 +37,60 @@ TEST(Parse, ast_chars_only) {
EXPECT_EQ(parse_string<"abcdefgh">().value, control);
}
TEST(Parse, syntax) {
constexpr auto pr1 = parse_string<"af {:012.4f}">();
constexpr auto pr2 = parse_string<"af {:.4f}">();
constexpr auto pr3 = parse_string<"asdf}{">();
constexpr auto pr4 = parse_string<"{:.}">();
constexpr auto pr5 = parse_string<"asdf{">();
constexpr auto pr6 = parse_string<"asd{d}:.8">();
EXPECT_EQ(pr1.is_valid, true);
EXPECT_EQ(pr2.is_valid, true);
EXPECT_EQ(pr3.is_valid, false);
EXPECT_EQ(pr4.is_valid, false);
EXPECT_EQ(pr5.is_valid, false);
EXPECT_EQ(pr6.is_valid, false);
}
TEST(Parse, ast_nodes_only) {
fmt_node_t fmt_node0 = {true, 6, 3, FormatType::s};
fmt_node_t fmt_node1;
fmt_node1.length = 8;
fmt_node1.type = FormatType::s;
fmt_node1.type = FormatType::s;
fmt_node_t fmt_node2;
fmt_node2.precision = 4;
fmt_node_t fmt_node3 = {};
fmt_node_t fmt_node3 = {true, 456765, 1234, FormatType::f};
fmt_node_t fmt_node4 = {};
fmt_node_t fmt_node5 = {};
fmt_node_t fmt_node6 = {};
fmt_node_t fmt_node7 = {};
std::array<ast_node_t, 8> control = {
fmt_node0, fmt_node1, fmt_node2, fmt_node3,
fmt_node4, fmt_node5, fmt_node6, fmt_node7};
std::array<ast_node_t, 8> control = {fmt_node0, fmt_node1, fmt_node2,
fmt_node3, fmt_node4, fmt_node5,
fmt_node6, fmt_node7};
EXPECT_EQ(parse_string<"{:06.3}{:8s}{:.4}{}{}{}{}{}">().value, control);
EXPECT_EQ(parse_string<"{:06.3}{:8s}{:.4}{:0456765.1234f}{}{}{}{}">().value,
control);
}
TEST(Parse, ast_chars_and_nodes) {
fmt_node_t fmt_node0 = {true, 6, 3, FormatType::s};
fmt_node_t fmt_node1;
fmt_node1.length = 8;
fmt_node1.type = FormatType::s;
fmt_node_t fmt_node2;
fmt_node2.precision = 4;
std::array<ast_node_t, 11> control = {fmt_node0, 'a', 'b', ' ',
' ', 'd', fmt_node1, fmt_node2,
' ', ' ', '-'};
EXPECT_EQ(parse_string<"{:06.3}ab d{:8s}{:.4} -">().value, control);
}