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 93ba5ab commit 57e52ba
Show file tree
Hide file tree
Showing 9 changed files with 4,115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
add_executable(cutest_test
tools/__init__.c
tools/help.c
utils/file.c
utils/regex.c
utils/str.c
main.c
test.c
)
Expand Down
2 changes: 2 additions & 0 deletions test2/tools/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include "test.h"

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_help,
&test_tool_stringfinder,
};

int test_tool_exec(int argc, char* argv[])
Expand Down
11 changes: 11 additions & 0 deletions test2/tools/stringfinder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "__init__.h"

static int _string_finder(int argc, char* argv[])
{

}

const test_tool_t test_tool_stringfinder = {
"stringfinder", _string_finder,
"Find string in file.\n"
};
42 changes: 42 additions & 0 deletions test2/utils/file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "file.h"

#if !defined(_WIN32)
static int fopen_s(FILE** out, const char* path, const char* mode)
{
if ((*out = fopen(path, mode)) == NULL)
{
return errno;
}
return 0;
}
#endif

int test_file_read(const char* path, test_str_t* data)
{
FILE* f = NULL;
char buf[64];

int ret = fopen_s(&f, path, "rb");
if (ret != 0)
{
return -ret;
}

while (!feof(f))
{
if (ferror(f))
{
return -1;
}

size_t nread = fread(buf, 1, sizeof(buf), f);
test_str_append(data, buf, nread);
}

fclose(f);

return 0;
}
22 changes: 22 additions & 0 deletions test2/utils/file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef __CUTEST_TEST_UTILS_FILE_H__
#define __CUTEST_TEST_UTILS_FILE_H__

#include "str.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Read file content.
* @param[in] path - File path.
* @param[out] data - Buffer to store file content.
* @return - 0: success
* @return - -errno: on error
*/
int test_file_read(const char* path, test_str_t* data);

#ifdef __cplusplus
}
#endif
#endif
Loading

0 comments on commit 57e52ba

Please sign in to comment.