Skip to content

Commit

Permalink
Add unit test for fwpkg module
Browse files Browse the repository at this point in the history
Also load binary ov3.fwpkg file from ov_ftdi project
  • Loading branch information
matwey committed May 8, 2017
1 parent e0be9a9 commit ab1a38b
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ foreach(tool_target IN ITEMS ${TOOLS})
add_executable(${tool_name} ${SOURCES})
target_link_libraries(${tool_name} openvizsla)
endforeach(tool_target)

enable_testing()
file(GLOB_RECURSE TESTS test/*.c)
foreach(test_source IN ITEMS ${TESTS})
string(REPLACE ${CMAKE_SOURCE_DIR}/ "" test_name ${test_source})
string(REPLACE / _ test_name ${test_name})
string(REPLACE .c "" test_name ${test_name})
add_executable(${test_name} ${test_source})
if(CHECK_INCLUDE_DIRS)
target_include_directories(${test_name} ${CHECK_INCLUDE_DIRS})
endif(CHECK_INCLUDE_DIRS)
target_link_libraries(${test_name} ${CHECK_LIBRARIES} openvizsla)
set_target_properties(${test_name} PROPERTIES COMPILE_FLAGS -DPROJECT_ROOT='"${PROJECT_SOURCE_DIR}"')
add_test(${test_name} ${test_name})
endforeach(test_source)
3 changes: 3 additions & 0 deletions include/fwpkg.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ const char* fwpkg_get_error_string(struct fwpkg* fwpkg);
int fwpkg_read_map(struct fwpkg* fwpkg, void* buf, size_t* size);
int fwpkg_read_bitstream(struct fwpkg* fwpkg, void* buf, size_t* size);

size_t fwpkg_map_size(struct fwpkg* fwpkg);
size_t fwpkg_bitstream_size(struct fwpkg* fwpkg);

#endif // _FWPKG_H
Binary file added ov3.fwpkg
Binary file not shown.
29 changes: 29 additions & 0 deletions src/fwpkg.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ static int fwpkg_read_file(struct fwpkg* fwpkg, void* buf, size_t* size, zip_uin
return 0;
}

static size_t fwpkg_file_size(struct fwpkg* fwpkg, zip_uint64_t index) {
int ret;
struct zip_stat sb;

ret = zip_stat_index(fwpkg->pkg, index, 0, &sb);
if (ret == -1) {
fwpkg->error_str = zip_strerror(fwpkg->pkg);

return (size_t)(-1);
}

if (!(sb.valid & ZIP_STAT_SIZE)) {
fwpkg->error_str = "Can not read uncompressed size";

return (size_t)(-1);
}

return (size_t)(sb.size);
}

struct fwpkg* fwpkg_new() {
struct fwpkg* fwpkg = malloc(sizeof(struct fwpkg));

Expand Down Expand Up @@ -84,3 +104,12 @@ int fwpkg_read_map(struct fwpkg* fwpkg, void* buf, size_t* size) {
int fwpkg_read_bitstream(struct fwpkg* fwpkg, void* buf, size_t* size) {
return fwpkg_read_file(fwpkg, buf, size, fwpkg->bitstream_index);
}

size_t fwpkg_map_size(struct fwpkg* fwpkg) {
return fwpkg_file_size(fwpkg, fwpkg->map_index);
}

size_t fwpkg_bitstream_size(struct fwpkg* fwpkg) {
return fwpkg_file_size(fwpkg, fwpkg->bitstream_index);
}

61 changes: 61 additions & 0 deletions test/fwpkg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <check.h>

#include <fwpkg.h>

START_TEST (test_fwpkg_new1) {
struct fwpkg* fwpkg = fwpkg_new();
ck_assert_ptr_ne(fwpkg, NULL);
fwpkg_free(fwpkg);
}
END_TEST
START_TEST (test_fwpkg_load1) {
struct fwpkg* fwpkg = fwpkg_new();
int ret;
ck_assert_ptr_ne(fwpkg, NULL);
ret = fwpkg_from_file(fwpkg, PROJECT_ROOT "/ov3.fwpkg");
ck_assert_int_eq(ret, 0);
fwpkg_free(fwpkg);
}
END_TEST
START_TEST (test_fwpkg_size1) {
struct fwpkg* fwpkg = fwpkg_new();
int ret;
ck_assert_ptr_ne(fwpkg, NULL);
ret = fwpkg_from_file(fwpkg, PROJECT_ROOT "/ov3.fwpkg");
ck_assert_int_eq(ret, 0);
ck_assert_uint_eq(fwpkg_map_size(fwpkg), 2080);
ck_assert_uint_eq(fwpkg_bitstream_size(fwpkg), 340972);
fwpkg_free(fwpkg);
}
END_TEST

Suite* range_suite(void) {
Suite *s;
TCase *tc_core;

s = suite_create("fwpkg");

tc_core = tcase_create("Core");

tcase_add_test(tc_core, test_fwpkg_new1);
tcase_add_test(tc_core, test_fwpkg_load1);
tcase_add_test(tc_core, test_fwpkg_size1);
suite_add_tcase(s, tc_core);

return s;
}

int main(void) {
int number_failed;
Suite *s;
SRunner *sr;

s = range_suite();
sr = srunner_create(s);

srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

0 comments on commit ab1a38b

Please sign in to comment.