Skip to content

Commit

Permalink
add tool for test tab
Browse files Browse the repository at this point in the history
  • Loading branch information
qgymib committed Aug 1, 2024
1 parent afd070e commit 05b17b6
Show file tree
Hide file tree
Showing 20 changed files with 239 additions and 187 deletions.
2 changes: 1 addition & 1 deletion src/ev/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typedef enum ev_log_level
* @brief Log
* @param[in] level Log level
* @param[in] file File name
* @param[in] func Functon name
* @param[in] func Function name
* @param[in] line Line number
* @param[in] fmt Log format
* @param[in] ... Argument list
Expand Down
10 changes: 9 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ add_library(ev_test_lib SHARED
"test/tools/echoserver.c"
"test/tools/eolcheck.c"
"test/tools/help.c"
"test/tools/init.c"
"test/tools/__init__.c"
"test/tools/ls.c"
"test/tools/pwd.c"
"test/tools/tabcheck.c"
"test/type/__init__.c"
"test/type/ev_loop_t.c"
"test/type/ssize_t.c"
Expand All @@ -15,6 +16,7 @@ add_library(ev_test_lib SHARED
"test/utils/memcheck.c"
"test/utils/random.c"
"test/utils/sockpair.c"
"test/utils/str.c"
"test/test.c"
"test/cases/c/async.c"
"test/cases/c/buf.c"
Expand Down Expand Up @@ -93,6 +95,12 @@ if (EV_DEV)
add_test(NAME ev_eol_c
COMMAND $<TARGET_FILE:ev_test> -- eolcheck --file=${CMAKE_CURRENT_SOURCE_DIR}/ev.c --eol=LF
)
add_test(NAME ev_tab_h
COMMAND $<TARGET_FILE:ev_test> -- tabcheck --file=${CMAKE_CURRENT_SOURCE_DIR}/ev.h
)
add_test(NAME ev_tab_c
COMMAND $<TARGET_FILE:ev_test> -- tabcheck --file=${CMAKE_CURRENT_SOURCE_DIR}/ev.c
)
endif()

if(EV_HAVE_COVERAGE)
Expand Down
2 changes: 1 addition & 1 deletion test/test.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define _GNU_SOURCE
#include "test.h"
#include "tools/init.h"
#include "tools/__init__.h"
#include "utils/config.h"
#include "type/__init__.h"
#include <string.h>
Expand Down
12 changes: 4 additions & 8 deletions test/tools/init.c → test/tools/__init__.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
#include "echoserver.h"
#include "eolcheck.h"
#include "help.h"
#include "ls.h"
#include "pwd.h"
#include "init.h"
#include "__init__.h"
#include "test.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

static test_tool_t* g_command_table[] = {
static const test_tool_t* g_command_table[] = {
&test_tool_echoserver,
&test_tool_eolcheck,
&test_tool_ls,
&test_tool_pwd,
&test_tool_help,
&test_tool_tabcheck,
};

int tool_exec(int argc, char* argv[])
Expand All @@ -36,7 +32,7 @@ int tool_exec(int argc, char* argv[])
return -1;
}

void tool_foreach(int (*cb)(test_tool_t*, void*), void* arg)
void tool_foreach(int (*cb)(const test_tool_t*, void*), void* arg)
{
size_t i;
for (i = 0; i < ARRAY_SIZE(g_command_table); i++)
Expand Down
9 changes: 8 additions & 1 deletion test/tools/init.h → test/tools/__init__.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ typedef struct test_tool
const char* help;
} test_tool_t;

extern const test_tool_t test_tool_echoserver;
extern const test_tool_t test_tool_eolcheck;
extern const test_tool_t test_tool_help;
extern const test_tool_t test_tool_ls;
extern const test_tool_t test_tool_pwd;
extern const test_tool_t test_tool_tabcheck;

int tool_exec(int argc, char* argv[]);

void tool_foreach(int (*cb)(test_tool_t*, void*), void* arg);
void tool_foreach(int (*cb)(const test_tool_t*, void*), void* arg);

#ifdef __cplusplus
}
Expand Down
10 changes: 5 additions & 5 deletions test/tools/echoserver.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "echoserver.h"
#include "__init__.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
Expand Down Expand Up @@ -73,8 +73,8 @@ static int tool_echoserver(int argc, char* argv[])
#endif
}

test_tool_t test_tool_echoserver = {
"echoserver", tool_echoserver,
"Start a stdio echo server.\n"
"The echo server will get everything from stdin and print to stdout."
const test_tool_t test_tool_echoserver = {
"echoserver", tool_echoserver,
"Start a stdio echo server.\n"
"The echo server will get everything from stdin and print to stdout."
};
15 changes: 0 additions & 15 deletions test/tools/echoserver.h

This file was deleted.

90 changes: 16 additions & 74 deletions test/tools/eolcheck.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "eolcheck.h"
#include "__init__.h"
#include "utils/memcheck.h"
#include "utils/file.h"
#include "utils/str.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -66,40 +68,15 @@ static int _eolcheck_get_config(eolcheck_cfg_t* cfg, int argc, char* argv[])

static int _eolcheck_read_file(eolcheck_file_t* dst, const char* path)
{
FILE* file;

#if defined(_WIN32)
if (fopen_s(&file, path, "rb") != 0)
#else
if ((file = fopen(path, "rb")) == NULL)
#endif
{
fprintf(stderr, "failed to open `%s`.\n", path);
return EXIT_FAILURE;
}

fseek(file, 0, SEEK_END);
dst->data_sz = ftell(file);
rewind(file);

if ((dst->data = mmc_malloc(dst->data_sz + 1)) == NULL)
{
fprintf(stderr, "out of memory.\n");
fclose(file);
return EXIT_FAILURE;
}

if (fread(dst->data, dst->data_sz, 1, file) != 1)
char* content = NULL;
ssize_t ret = test_read_file(path, &content);
if (ret < 0)
{
fprintf(stderr, "read file `%s` failed.\n", path);
mmc_free(dst->data);
dst->data = NULL;
dst->data_sz = 0;
return EXIT_FAILURE;
return (int)ret;
}
dst->data[dst->data_sz] = '\0';

fclose(file);
dst->data = (uint8_t*)content;
dst->data_sz = ret;

return 0;
}
Expand Down Expand Up @@ -129,41 +106,6 @@ static void _eolcheck_release_file(eolcheck_file_t* file)
file->data_sz = 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 int _eolcheck_check_ending(eolcheck_file_t* file, eolcheck_cfg_t* cfg)
{
int ret = 0;
Expand Down Expand Up @@ -250,11 +192,11 @@ static int tool_eolcheck(int argc, char* argv[])
return ret;
}

test_tool_t test_tool_eolcheck = {
"eolcheck", tool_eolcheck,
"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)"
const test_tool_t test_tool_eolcheck = {
"eolcheck", tool_eolcheck,
"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)"
};
16 changes: 0 additions & 16 deletions test/tools/eolcheck.h

This file was deleted.

10 changes: 5 additions & 5 deletions test/tools/help.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "help.h"
#include "__init__.h"
#include <stdlib.h>
#include <stdio.h>

Expand Down Expand Up @@ -42,7 +42,7 @@ static void _print_help(const char* help, const char* prefix)
printf("\n");
}

static int _print_tools(test_tool_t* info, void* arg)
static int _print_tools(const test_tool_t* info, void* arg)
{
(void)arg;

Expand All @@ -66,7 +66,7 @@ static int _tool_help(int argc, char* argv[])
return EXIT_SUCCESS;
}

test_tool_t test_tool_help = {
"help", _tool_help,
"Show this help and exit."
const test_tool_t test_tool_help = {
"help", _tool_help,
"Show this help and exit."
};
15 changes: 0 additions & 15 deletions test/tools/help.h

This file was deleted.

8 changes: 4 additions & 4 deletions test/tools/ls.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ls.h"
#include "__init__.h"
#include "test.h"
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -159,7 +159,7 @@ static int tool_ls(int argc, char* argv[])
return ret;
}

test_tool_t test_tool_ls = {
"ls", tool_ls,
"List file names in current directory."
const test_tool_t test_tool_ls = {
"ls", tool_ls,
"List file names in current directory."
};
15 changes: 0 additions & 15 deletions test/tools/ls.h

This file was deleted.

8 changes: 4 additions & 4 deletions test/tools/pwd.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "pwd.h"
#include "__init__.h"
#include "test.h"

static int tool_pwd(int argc, char* argv[])
Expand All @@ -21,7 +21,7 @@ static int tool_pwd(int argc, char* argv[])
return EXIT_SUCCESS;
}

test_tool_t test_tool_pwd = {
"pwd", tool_pwd,
"Print the name of the current working directory."
const test_tool_t test_tool_pwd = {
"pwd", tool_pwd,
"Print the name of the current working directory."
};
16 changes: 0 additions & 16 deletions test/tools/pwd.h

This file was deleted.

Loading

0 comments on commit 05b17b6

Please sign in to comment.