start testing matching too
This commit is contained in:
parent
45d6b4dc58
commit
c7af36dbb0
118
source/main.cpp
118
source/main.cpp
@ -1,5 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "regex.h"
|
#include "regex.h"
|
||||||
|
|
||||||
static int test_counter = 0;
|
static int test_counter = 0;
|
||||||
@ -9,8 +10,30 @@ static int positive_successes = 0;
|
|||||||
static int negatives = 0;
|
static int negatives = 0;
|
||||||
static int negative_successes = 0;
|
static int negative_successes = 0;
|
||||||
|
|
||||||
static void
|
static int test_counter2 = 0;
|
||||||
TEST(const char * const what,
|
static int passed_tests2 = 0;
|
||||||
|
|
||||||
|
static
|
||||||
|
void asprint_match_t( char * * destination,
|
||||||
|
const match_t * const match){
|
||||||
|
if (match) {
|
||||||
|
asprintf(destination, "%p {%d, %d}", (void *)match, match->position, match->width);
|
||||||
|
} else {
|
||||||
|
asprintf(destination, "0x0 {N/A, N/A}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
void print_leader(const bool passed){
|
||||||
|
if (passed) {
|
||||||
|
printf("\033[32;1mSuccess\033[0;1m. - \033[0m");
|
||||||
|
} else {
|
||||||
|
printf("\033[31;1mFailiour\033[0;1m. - \033[0m");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
void TEST(const char * const what,
|
||||||
const char * const on,
|
const char * const on,
|
||||||
const bool expect){
|
const bool expect){
|
||||||
|
|
||||||
@ -18,28 +41,16 @@ TEST(const char * const what,
|
|||||||
bool result = regex_search(r, on);
|
bool result = regex_search(r, on);
|
||||||
bool passed = (result == expect);
|
bool passed = (result == expect);
|
||||||
|
|
||||||
expect ? ++positives : ++negatives;
|
passed && expect ? ++positive_successes : ++negative_successes;
|
||||||
|
|
||||||
if (passed) {
|
print_leader(passed);
|
||||||
printf("\033[32;1mSuccess\033[0;1m. - \033[0m");
|
|
||||||
expect ? ++positive_successes : ++negative_successes;
|
char * quoted_what, * quoted_on;
|
||||||
} else {
|
asprintf("ed_what, "'%s'", what);
|
||||||
printf("\033[31;1mFailiour\033[0;1m. - \033[0m");
|
asprintf("ed_on, "'%s'", on);
|
||||||
}
|
printf("%14s\033[1m vs \033[0m%14s\033[1m:\033[0m Result = %d, Expected = %d\n", quoted_what, quoted_on, result, expect);
|
||||||
int len;
|
free(quoted_what);
|
||||||
len = strlen(what);
|
free(quoted_on);
|
||||||
char quoted_what[len+3];
|
|
||||||
quoted_what[0] = '\'';
|
|
||||||
memcpy(quoted_what+1, what, len);
|
|
||||||
quoted_what[len+1] = '\'';
|
|
||||||
quoted_what[len+2] = '\0';
|
|
||||||
len = strlen(on);
|
|
||||||
char quoted_on[strlen(on)+3];
|
|
||||||
quoted_on[0] = '\'';
|
|
||||||
memcpy(quoted_on+1, on, len);
|
|
||||||
quoted_on[len+1] = '\'';
|
|
||||||
quoted_on[len+2] = '\0';
|
|
||||||
printf("%12s\033[1m vs \033[0m%12s\033[1m:\033[0m Result = %d, Expected = %d\n", quoted_what, quoted_on, result, expect);
|
|
||||||
if (passed) {
|
if (passed) {
|
||||||
++passed_tests;
|
++passed_tests;
|
||||||
}
|
}
|
||||||
@ -47,6 +58,42 @@ TEST(const char * const what,
|
|||||||
++test_counter;
|
++test_counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
void TEST2(const char * const what,
|
||||||
|
const char * const on,
|
||||||
|
const match_t expect){
|
||||||
|
|
||||||
|
regex_t * r = regex_compile(what);
|
||||||
|
match_t * result = regex_match(r, on, true);
|
||||||
|
bool passed = (
|
||||||
|
( result
|
||||||
|
&& result->position == expect.position
|
||||||
|
&& result->width == expect.width
|
||||||
|
)
|
||||||
|
||
|
||||||
|
expect.position == -1
|
||||||
|
);
|
||||||
|
|
||||||
|
print_leader(passed);
|
||||||
|
|
||||||
|
char * quoted_what, * quoted_on;
|
||||||
|
asprintf("ed_what, "'%s'", what);
|
||||||
|
asprintf("ed_on, "'%s'", on);
|
||||||
|
char * result_string, * expect_string;
|
||||||
|
asprint_match_t(&result_string, result);
|
||||||
|
asprint_match_t(&expect_string, &expect);
|
||||||
|
printf("%14s\033[1m vs \033[0m%14s\033[1m:\033[0m\n\t%s\n\t%s\n", quoted_what, quoted_on, result_string, expect_string);
|
||||||
|
free(quoted_what);
|
||||||
|
free(quoted_on);
|
||||||
|
free(result_string);
|
||||||
|
free(expect_string);
|
||||||
|
if (passed) {
|
||||||
|
++passed_tests2;
|
||||||
|
}
|
||||||
|
|
||||||
|
++test_counter2;
|
||||||
|
}
|
||||||
|
|
||||||
signed main() {
|
signed main() {
|
||||||
TEST( R"del(abc)del", "abc", true);
|
TEST( R"del(abc)del", "abc", true);
|
||||||
TEST(R"del(efg1)del", "efg1", true);
|
TEST(R"del(efg1)del", "efg1", true);
|
||||||
@ -142,25 +189,20 @@ signed main() {
|
|||||||
TEST( R"del(test\>)del", "testa", false);
|
TEST( R"del(test\>)del", "testa", false);
|
||||||
TEST(R"del(\<test\>)del", "test", true);
|
TEST(R"del(\<test\>)del", "test", true);
|
||||||
|
|
||||||
//TEST(R"del(\d{3})del","123",true);
|
|
||||||
//TEST(R"del(^\w+@\w+\.\w+$)del","example@email.com",true);
|
|
||||||
|
|
||||||
//TEST(R"del(\b\w+\b)del","This is a test",true);
|
|
||||||
//TEST(R"del(^[A-Za-z]+\s\d+)del","OpenAI 123",true);
|
|
||||||
//TEST(R"del([0-9]{4}-[0-9]{2}-[0-9]{2})del","2023-08-22",true);
|
|
||||||
|
|
||||||
//TEST(R"del(^[^abc]+$)del","def123",true);
|
|
||||||
//TEST(R"del(\b\d{5}\b)del","12345 67890",true);
|
|
||||||
//TEST(R"del(^[A-Z][a-z]+$)del","OpenAI",true);
|
|
||||||
|
|
||||||
//TEST(R"del(\d{3}-\d{2}-\d{4})del","123-45-6789",true);
|
|
||||||
//TEST(R"del(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})del","192.168.1.1",true);
|
|
||||||
//TEST(R"del(^\w{8,12})del","Password123", false);
|
|
||||||
|
|
||||||
if(test_counter == passed_tests) {
|
if(test_counter == passed_tests) {
|
||||||
fputs("\033[32m", stdout);
|
fputs("\033[32m", stdout);
|
||||||
}
|
}
|
||||||
printf("\nPassed %d out of %d tests.\033[0m\n", passed_tests, test_counter);
|
printf("\nPassed %d out of %d tests.\033[0m\n", passed_tests, test_counter);
|
||||||
printf("\tPositives: %d/%d\n", positive_successes, positives);
|
printf("\tPositives: %d/%d\n", positive_successes, positives);
|
||||||
printf("\tNegatives: %d/%d\n", negative_successes, negatives);
|
printf("\tNegatives: %d/%d\n", negative_successes, negatives);
|
||||||
|
|
||||||
|
puts("");
|
||||||
|
|
||||||
|
puts("");
|
||||||
|
|
||||||
|
TEST2( R"del(abc)del", "abc", match_t{ 0, 3});
|
||||||
|
TEST2(R"del(efg1)del", "efg1", match_t{ 0, 4});
|
||||||
|
TEST2( R"del(nig)del", "ger", match_t{-1, 0});
|
||||||
|
TEST2( R"del(ss)del", "sss", match_t{ 0, 2});
|
||||||
|
TEST2( R"del(sss)del", "ss", match_t{-1, 0});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user