mirror of
https://github.com/simon987/libscan.git
synced 2025-12-14 06:59:02 +00:00
Add tests (wip)
This commit is contained in:
62
test/main.cpp
Normal file
62
test/main.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "test_util.h"
|
||||
|
||||
extern "C" {
|
||||
#include "../libscan/arc/arc.h"
|
||||
#include "../libscan/text/text.h"
|
||||
}
|
||||
|
||||
static scan_arc_ctx_t arc_recurse_ctx;
|
||||
static scan_arc_ctx_t arc_list_ctx;
|
||||
static scan_text_ctx_t text_500_ctx;
|
||||
|
||||
|
||||
TEST(TextTest, BookCsvContentLen) {
|
||||
const char *filepath = "libscan-test-files/test_files/text/books.csv";
|
||||
|
||||
vfile_t f;
|
||||
document_t doc;
|
||||
doc.meta_head = nullptr;
|
||||
doc.meta_tail = nullptr;
|
||||
load_file(filepath, &f);
|
||||
parse_text(&text_500_ctx, &f, &doc);
|
||||
|
||||
ASSERT_NEAR(strlen(get_meta(&doc, MetaContent)->str_val), 500, 1);
|
||||
CLOSE_FILE(f)
|
||||
destroy_doc(&doc);
|
||||
}
|
||||
|
||||
TEST(TextTest, MemUtf8_1) {
|
||||
const char *content = "a"; //todo
|
||||
|
||||
vfile_t f;
|
||||
document_t doc;
|
||||
doc.meta_head = nullptr;
|
||||
doc.meta_tail = nullptr;
|
||||
load_mem((void *) content, strlen(content), &f);
|
||||
parse_text(&text_500_ctx, &f, &doc);
|
||||
|
||||
ASSERT_EQ(strlen(get_meta(&doc, MetaContent)->str_val), 1);
|
||||
destroy_doc(&doc);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
arc_recurse_ctx.log = noop_log;
|
||||
arc_recurse_ctx.logf = noop_logf;
|
||||
arc_recurse_ctx.store = noop_store;
|
||||
arc_recurse_ctx.mode = ARC_MODE_RECURSE;
|
||||
arc_recurse_ctx.parse = nullptr; //TODO
|
||||
|
||||
arc_list_ctx.log = noop_log;
|
||||
arc_list_ctx.logf = noop_logf;
|
||||
arc_list_ctx.store = noop_store;
|
||||
arc_list_ctx.mode = ARC_MODE_LIST;
|
||||
|
||||
text_500_ctx.content_size = 500;
|
||||
text_500_ctx.log = noop_log;
|
||||
text_500_ctx.logf = noop_logf;
|
||||
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
76
test/test_util.cpp
Normal file
76
test/test_util.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include "test_util.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#define FILE_NOT_FOUND_ERR "Could not file, did you clone the test files repo?"
|
||||
|
||||
|
||||
int fs_read(struct vfile *f, void *buf, size_t size) {
|
||||
|
||||
if (f->fd == -1) {
|
||||
f->fd = open(f->filepath, O_RDONLY);
|
||||
if (f->fd == -1) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return read(f->fd, buf, size);
|
||||
}
|
||||
|
||||
//Note: No out of bounds check
|
||||
int mem_read(vfile_t *f, void *buf, size_t size) {
|
||||
memcpy(buf, f->_test_data, size);
|
||||
f->_test_data = (char *) f->_test_data + size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fs_close(vfile_t *f) {
|
||||
if (f->fd != -1) {
|
||||
close(f->fd);
|
||||
}
|
||||
}
|
||||
|
||||
void load_file(const char *filepath, vfile_t *f) {
|
||||
stat(filepath, &f->info);
|
||||
f->fd = open(filepath, O_RDONLY);
|
||||
|
||||
if (f->fd == -1) {
|
||||
FAIL() << FILE_NOT_FOUND_ERR;
|
||||
}
|
||||
|
||||
f->filepath = filepath;
|
||||
f->read = fs_read;
|
||||
f->close = fs_close;
|
||||
f->is_fs_file = TRUE;
|
||||
}
|
||||
|
||||
void load_mem(void *mem, size_t size, vfile_t *f) {
|
||||
f->filepath = "_mem_";
|
||||
f->_test_data = mem;
|
||||
f->info.st_size = size;
|
||||
f->read = mem_read;
|
||||
f->close = nullptr;
|
||||
f->is_fs_file = TRUE;
|
||||
}
|
||||
|
||||
meta_line_t *get_meta(document_t *doc, metakey key) {
|
||||
meta_line_t *meta = doc->meta_head;
|
||||
while (meta != nullptr) {
|
||||
if (meta->key == key) {
|
||||
return meta;
|
||||
}
|
||||
meta = meta->next;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void destroy_doc(document_t *doc) {
|
||||
meta_line_t *meta = doc->meta_head;
|
||||
while (meta != nullptr) {
|
||||
meta_line_t *tmp = meta;
|
||||
meta = tmp->next;
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
28
test/test_util.h
Normal file
28
test/test_util.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef SCAN_TEST_UTIL_H
|
||||
#define SCAN_TEST_UTIL_H
|
||||
|
||||
#include "../libscan/scan.h"
|
||||
|
||||
void load_file(const char *filepath, vfile_t *f);
|
||||
void load_mem(void *mem, size_t size, vfile_t *f);
|
||||
|
||||
static void noop_logf(char *filepath, int level, char *format, ...) {
|
||||
// noop
|
||||
}
|
||||
|
||||
static void noop_log(char *filepath, int level, char *str) {
|
||||
// noop
|
||||
}
|
||||
|
||||
static void noop_store(char* key, size_t key_len, char *value, size_t value_len) {
|
||||
// noop
|
||||
}
|
||||
|
||||
meta_line_t *get_meta(document_t *doc, metakey key);
|
||||
|
||||
|
||||
#define CLOSE_FILE(f) if (f.close != NULL) {f.close(&f);};
|
||||
|
||||
void destroy_doc(document_t *doc);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user