Adds a test and CMake function.

This commit is contained in:
falsycat 2020-12-31 00:00:00 +00:00
parent 5765321487
commit d418241f95
6 changed files with 199 additions and 2 deletions

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
*.swp
/.build
/test

View File

@ -34,3 +34,25 @@ target_sources(biner
c/zone.h
)
target_include_directories(biner PRIVATE . ${CMAKE_CURRENT_BINARY_DIR})
function(target_biner_sources target)
cmake_parse_arguments("FUNC" "" "OUTPUT" "SOURCES" ${ARGN})
file(MAKE_DIRECTORY "${FUNC_OUTPUT}")
foreach(path ${FUNC_SOURCES})
get_filename_component(basename "${path}" NAME)
set(in "${CMAKE_CURRENT_SOURCE_DIR}/${path}")
set(out "${FUNC_OUTPUT}/${basename}.h")
add_custom_command(
OUTPUT "${out}"
COMMAND $<TARGET_FILE:biner> < "${in}" > "${out}"
DEPENDS "${in}" biner
COMMENT "transpiling ${in} to C"
VERBATIM)
target_sources(${target} PRIVATE "${out}")
endforeach()
endfunction()
if (BUILD_TESTING)
add_subdirectory(test)
endif()

View File

@ -10,7 +10,7 @@ X conditional switching support
X union support
X C transpiler for unpacking
X C transpiler for packing
setup tests
X setup tests
handling of overflown or negative expression
add bitmanip operators
release 1.0.0

18
test/CMakeLists.txt Normal file
View File

@ -0,0 +1,18 @@
add_executable(biner-test)
target_sources(biner-test
PRIVATE
main.c
)
target_biner_sources(biner-test
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated
SOURCES
user.biner
)
target_include_directories(biner-test
PRIVATE
${CMAKE_CURRENT_BINARY_DIR}
)
add_test(
NAME biner-test
COMMAND $<TARGET_FILE:biner-test>
)

109
test/main.c Normal file
View File

@ -0,0 +1,109 @@
#undef NDEBUG
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../c/pack.h"
#include "../c/unpack.h"
#include "../c/zone.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#include "generated/user.biner.h"
#pragma GCC diagnostic pop
static inline void print_user_(const user_t* u) {
assert(u != NULL);
const char* sex =
u->sex == SEX_MALE? "male":
u->sex == SEX_FEMALE? "female":
u->sex == SEX_CAT? "cat":
"unknown";
const char* role =
u->role == ROLE_TEACHER? "teacher":
u->role == ROLE_STUDENT? "student":
"unknown";
printf("name: %.*s\n", (int) u->name.len, u->name.ptr);
printf("age: %" PRIu8 "\n", u->age);
printf("sex: %s\n", sex);
printf("role: %s\n", role);
printf("properties:\n");
for (size_t i = 0; i < u->property_count; ++i) {
printf(" %.*s: %.*s\n",
(int) u->properties[i].key.len, u->properties[i].key.ptr,
(int) u->properties[i].value.len, u->properties[i].value.ptr);
}
switch (u->role) {
case ROLE_TEACHER:
printf("salary: %" PRIu32 "\n", u->teacher.salary);
printf("payday: %" PRIu8 "/%" PRIu8 "\n",
u->teacher.payday.mon, u->teacher.payday.day);
break;
case ROLE_STUDENT:
printf("scores:\n");
for (size_t i = 0; i < 10; ++i) {
printf(" - %" PRIu8 "\n", u->student.scores[i]);
}
printf("absents: %" PRIu8 "\n", u->student.absents);
break;
}
}
int main(void) {
# define str_(v) { .ptr = (uint8_t*) (v), .len = strlen(v), }
user_t src = {
.name = str_("neko"),
.age = 4,
.sex = SEX_CAT,
.role = ROLE_STUDENT,
.property_count = 2,
.properties = (strpair_t[]) {
{ .key = str_("like"), .value = str_("rat"), },
{ .key = str_("hate"), .value = str_("dog"), },
},
.student = {
.scores = { 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, },
.absents = 1,
},
};
# undef str_
user_pack_context_t pack_ctx = {0};
user_pack_context_t unpack_ctx = {0};
user_t dst = {0};
size_t b = 0;
bool completed = false;
while (!completed) {
uint8_t c;
completed = user_pack(&pack_ctx, &src, &c);
if (user_unpack(&unpack_ctx, &dst, c) != completed) {
fprintf(stderr, "fail at the %zu byte: ", b);
if (completed) {
fprintf(stderr, "pack function finsihed but unpack not\n");
} else {
fprintf(stderr, "unpack function finsihed but pack not\n");
}
return EXIT_FAILURE;
}
++b;
}
printf("[src]\n");
print_user_(&src);
printf("[dst]\n");
print_user_(&dst);
return EXIT_SUCCESS;
}

49
test/user.biner Normal file
View File

@ -0,0 +1,49 @@
struct string_t {
lu64 len;
lu8[len] ptr;
};
struct strpair_t {
string_t key;
string_t value;
};
enum sex_t {
SEX_MALE = 0x0,
SEX_FEMALE = 0x1,
SEX_CAT = 0x2,
};
enum role_t {
ROLE_TEACHER = 0x0,
ROLE_STUDENT = 0x1,
};
struct date_t {
lu8 mon;
lu8 day;
};
struct teacher_t {
lu32 salary;
date_t payday;
};
struct student_t {
lu8[10] scores;
lu8 absents;
};
struct user_t {
string_t name;
lu8 age;
lu8 sex; /* sex_t */
lu8 role; /* role_t */
lu16 property_count;
strpair_t[property_count] properties;
union {
(role == ROLE_TEACHER) teacher_t teacher;
(role == ROLE_STUDENT) student_t student;
};
};