Skip to content

Commit

Permalink
rewrite test
Browse files Browse the repository at this point in the history
  • Loading branch information
qgymib committed Apr 25, 2024
1 parent 57e52ba commit 45d93ed
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 53 deletions.
1 change: 0 additions & 1 deletion test/doc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ add_custom_command(

add_executable(doc_verify
"test.c"
"case/encoding.c"
"case/mainpage.c"
"utils/foreachline.c"
"utils/strtok_f.c"
Expand Down
33 changes: 0 additions & 33 deletions test/doc/case/encoding.c

This file was deleted.

12 changes: 12 additions & 0 deletions test2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_executable(cutest_test
tools/__init__.c
tools/eolcheck.c
tools/help.c
utils/file.c
utils/regex.c
Expand All @@ -20,3 +21,14 @@ target_link_libraries(cutest_test
cutest_setup_target_wall(cutest_test)

add_test(NAME cutest_test COMMAND $<TARGET_FILE:cutest_test>)

add_test(NAME cutest_eol_h
COMMAND $<TARGET_FILE:cutest_test> -- eolcheck
--file=${CMAKE_CURRENT_SOURCE_DIR}/../include/cutest.h
--eol=LF
)
add_test(NAME cutest_eol_c
COMMAND $<TARGET_FILE:cutest_test> -- eolcheck
--file=${CMAKE_CURRENT_SOURCE_DIR}/../src/cutest.c
--eol=LF
)
4 changes: 2 additions & 2 deletions test2/tools/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include "__init__.h"
#include "test.h"

extern const test_tool_t test_tool_eolcheck;
extern const test_tool_t test_tool_help;
extern const test_tool_t test_tool_stringfinder;

static const test_tool_t* const g_command_table[] = {
&test_tool_eolcheck,
&test_tool_help,
&test_tool_stringfinder,
};

int test_tool_exec(int argc, char* argv[])
Expand Down
225 changes: 225 additions & 0 deletions test2/tools/eolcheck.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "utils/file.h"
#include "__init__.h"

typedef struct eolcheck_config
{
const char* path;
const char* eol;
} eolcheck_config_t;

static int _eol_check_parser(eolcheck_config_t* cfg, int argc, char* argv[])
{
int i;
const char* opt;

cfg->eol = NULL;
cfg->path = NULL;

for (i = 0; i < argc; i++)
{
opt = "--file=";
if (strncmp(argv[i], opt, strlen(opt)) == 0)
{
cfg->path = argv[i] + strlen(opt);
continue;
}

opt = "--eol=";
if (strncmp(argv[i], opt, strlen(opt)) == 0)
{
cfg->eol = argv[i] + strlen(opt);
continue;
}
}

if (cfg->path == NULL)
{
fprintf(stderr, "missing argument to `--file`.\n");
return EXIT_FAILURE;
}
if (cfg->eol == NULL)
{
fprintf(stderr, "missing argument to `--eol`.\n");
return EXIT_FAILURE;
}

if (strcmp(cfg->eol, "CR") != 0 && strcmp(cfg->eol, "LF") != 0 && strcmp(cfg->eol, "CRLF") != 0)
{
fprintf(stderr, "unknown option to `--eol`.\n");
return EXIT_FAILURE;
}

return 0;
}

static int _eolcheck_is_match(char c, const char* delim)
{
for (; *delim != '\0'; delim++)
{
if (*delim == c)
{
return 1;
}
}

return 0;
}

static char* _eolcheck_strtok(char* str, const char* delim, char** saveptr)
{
if (*saveptr == NULL)
{
*saveptr = str;
}

char* pos_start = *saveptr;

for (; **saveptr != '\0'; *saveptr = *saveptr + 1)
{
if (_eolcheck_is_match(**saveptr, delim))
{
**saveptr = '\0';
*saveptr = *saveptr + 1;
return pos_start;
}
}

return NULL;
}

static char _mmc_ascii_to_char(unsigned char c)
{
if (c >= 32 && c <= 126)
{
return c;
}
return '.';
}

static void mmc_dump_hex(const void* data, size_t size, size_t width)
{
const unsigned char* pdat = (unsigned char*)data;

size_t idx_line;
for (idx_line = 0; idx_line < size; idx_line += width)
{
fprintf(stdout, "%p: ", pdat + idx_line);

/* printf hex */
size_t idx_colume;
for (idx_colume = 0; idx_colume < width; idx_colume++)
{
const char* postfix = (idx_colume < width - 1) ? "" : "|";

if (idx_colume + idx_line < size)
{
fprintf(stdout, "%02x %s", pdat[idx_colume + idx_line], postfix);
}
else
{
fprintf(stdout, " %s", postfix);
}
}
fprintf(stdout, " ");
/* printf char */
for (idx_colume = 0; (idx_colume < width) && (idx_colume + idx_line < size); idx_colume++)
{
fprintf(stdout, "%c", _mmc_ascii_to_char(pdat[idx_colume + idx_line]));
}
fprintf(stdout, "\n");
}
}

static int _eol_check_dat(const test_str_t* dat, const eolcheck_config_t* cfg)
{
int ret = 0;
test_str_t cpy = test_str_dup(dat);

size_t cnt_line = 1;
char* line = NULL;
char* saveptr = NULL;

for (; (line = _eolcheck_strtok(cpy.str, "\r\n", &saveptr)) != NULL; cnt_line++)
{
size_t line_sz = strlen(line);
size_t line_offset = line - cpy.str;
size_t eol_offset = line_offset + line_sz;

switch (dat->str[eol_offset])
{
case '\r':
if (eol_offset < dat->len && dat->str[eol_offset + 1] == '\n')
{
saveptr++;
if (strcmp(cfg->eol, "CRLF") != 0)
{
fprintf(stderr, "CRLF found on line %u in file `%s`.\n", (unsigned)cnt_line, cfg->path);
mmc_dump_hex(dat->str + line_offset, line_sz + 2, 16);
ret = EXIT_FAILURE;
goto fin;
}
}
else
{
if (strcmp(cfg->eol, "CR") != 0)
{
fprintf(stderr, "CR found on line %u in file `%s`.\n", (unsigned)cnt_line, cfg->path);
mmc_dump_hex(dat->str + line_offset, line_sz + 1, 16);
ret = EXIT_FAILURE;
goto fin;
}
}
break;

case '\n':
if (strcmp(cfg->eol, "LF") != 0)
{
fprintf(stderr, "LF found on line %u in file `%s`.\n", (unsigned)cnt_line, cfg->path);
mmc_dump_hex(dat->str + line_offset, line_sz + 1, 16);
ret = EXIT_FAILURE;
goto fin;
}
break;

default:
fprintf(stderr, "unexcept character `%c`.\n", dat->str[eol_offset]);
abort();
}
}

fin:
test_str_exit(&cpy);
return ret;
}

static int _eol_check(int argc, char* argv[])
{
eolcheck_config_t cfg;
if (_eol_check_parser(&cfg, argc, argv) != 0)
{
return EXIT_FAILURE;
}

test_str_t dat = TEST_STR_INIT;
if (test_file_read(cfg.path, &dat) != 0)
{
return EXIT_FAILURE;
}

int ret = _eol_check_dat(&dat, &cfg);
test_str_exit(&dat);

return ret;
}

const test_tool_t test_tool_eolcheck = {
"eolcheck", _eol_check,
"Check if a file contains all line ending.\n"
"--file=[PATH]\n"
" Path to check.\n"
"--eol=CR|LF|CRLF\n"
" Excepet line ending. CR (Macintosh) / LF (Unix) / CRLF (Windows)."
};
11 changes: 0 additions & 11 deletions test2/tools/stringfinder.c

This file was deleted.

29 changes: 24 additions & 5 deletions test2/utils/file.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "file.h"

#if !defined(_WIN32)
Expand All @@ -14,7 +15,20 @@ static int fopen_s(FILE** out, const char* path, const char* mode)
}
#endif

static int _test_file_read_inner(const char* data, size_t size, void* arg)
{
test_str_t* dat = arg;
test_str_append(dat, data, size);
return 0;
}

int test_file_read(const char* path, test_str_t* data)
{
return test_file_reader(path, _test_file_read_inner, data);
}

int test_file_reader(const char* path,
int (*cb)(const char* data, size_t size, void* arg), void* arg)
{
FILE* f = NULL;
char buf[64];
Expand All @@ -29,14 +43,19 @@ int test_file_read(const char* path, test_str_t* data)
{
if (ferror(f))
{
return -1;
ret = -1;
break;
}

size_t nread = fread(buf, 1, sizeof(buf), f);
test_str_append(data, buf, nread);
}
size_t nread = fread(buf, 1, sizeof(buf) - 1, f);
buf[nread] = '\0';

if ((ret = cb(buf, nread, arg)) != 0)
{
break;
}
}
fclose(f);

return 0;
return ret;
}
11 changes: 11 additions & 0 deletions test2/utils/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ extern "C" {
*/
int test_file_read(const char* path, test_str_t* data);

/**
* @brief Read file content.
* @param[in] path - File path.
* @param[in] cb - Callback function.
* @param[in] arg - Callback argument.
* @return - 0: success
* @return - -errno: on error
*/
int test_file_reader(const char* path,
int (*cb)(const char*, size_t, void*), void* arg);

#ifdef __cplusplus
}
#endif
Expand Down
7 changes: 7 additions & 0 deletions test2/utils/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ void test_str_append(test_str_t* str, const char* data, size_t len)
str->str[required_sz] = '\0';
str->len = required_sz;
}

test_str_t test_str_dup(const test_str_t* str)
{
test_str_t ret = TEST_STR_INIT;
test_str_append(&ret, str->str, str->len);
return ret;
}
Loading

0 comments on commit 45d93ed

Please sign in to comment.