libdart_openmvg/src/tests/test_image.cxx

159 lines
4.0 KiB
C++

#include <archives/binary.hpp>
#include <CUnit/Basic.h>
#include "../image.h"
#include "../streamingview.hpp"
#include <cwalk.h>
#ifdef __cplusplus
#include <iostream>
#endif // __cplusplus
#define STRLEN 2048
FILE *imageHandle = NULL;
char frame1Path[STRLEN];
/*
* Read the contents of a file into an array
* path: path to the file
* contents: array we'll write to that contains the contents of the file.
*/
size_t read_file(const char *path, uint8_t **contents) {
FILE *file = fopen(path, "rb");
if (file == NULL) {
printf("Error: Unable to open file.\n");
return 0;
}
// Determine the file size
fseek(file, 0, SEEK_END);
size_t file_size = ftell(file);
rewind(file);
// Allocate memory for the contents array
*contents = (uint8_t *) malloc(file_size);
if (*contents == NULL) {
printf("Error: Unable to allocate memory.\n");
fclose(file);
return 0;
}
// Read the file contents into the array
fread(*contents, 1, file_size, file);
fclose(file);
return file_size;
}
#define HERE __FILE__
int setUp(void)
{
char framesDir[STRLEN];
size_t length;
cwk_path_get_dirname(HERE, &length); // archimedes_mobile_lib/src/tests
cwk_path_join(HERE, "../../../assets/test/frames", framesDir, STRLEN);
cwk_path_join(framesDir, "0001.png", frame1Path, STRLEN);
printf("Opening file %s\n", frame1Path);
return 0;
}
int tearDown(void)
{
if (imageHandle) {
fclose(imageHandle);
}
return 0;
}
void test_create_frame(void)
{
imageHandle = fopen(frame1Path, "rw+");
const Frame *f = new_frame_from_handle(
imageHandle,
768,
768,
64
);
CU_ASSERT_PTR_NOT_NULL(f->stream);
CU_ASSERT(f->w == 768);
}
void test_frame_from_data(void)
{
uint8_t *contents = NULL;
size_t len = read_file(frame1Path, &contents);
const Frame *f = new_frame_from_data(contents, len, 768, 768, 64);
CU_ASSERT_PTR_NOT_NULL(f->stream);
CU_ASSERT(f->w == 768);
}
void test_archimedes_get_data(void)
{
imageHandle = fopen(frame1Path, "rw+");
const Frame *f = new_frame_from_handle(
imageHandle,
768,
768,
64
);
unsigned char *image_data = NULL;
size_t image_data_len = 0;
ImageResult *result = archimedes_get_image_data(f);
CU_ASSERT_FALSE(result->error);
CU_ASSERT_PTR_NOT_NULL(result->data);
CU_ASSERT_TRUE(result->data_len > 0);
}
void test_archimedes_streamed_view(void) {
imageHandle = fopen(frame1Path, "rw+");
Frame *f = new_frame_from_handle(
imageHandle,
768,
768,
64
);
const libdart_openmvg::StreamingView view(f);
std::cerr << "OPENING " << frame1Path << std::endl;
std::ifstream inStream(std::string(frame1Path), std::ios::in | std::ios::binary);
std::ofstream outStream;
cereal::BinaryInputArchive inArchive(inStream);
cereal::BinaryOutputArchive outArchive(outStream);
// view.save(outArchive);
view.load(inArchive);
}
int main() {
CU_pSuite pSuite = NULL;
/* initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
/* add a suite to the registry */
pSuite = CU_add_suite("ImageSuite", setUp, tearDown);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* add the tests to the suite */
if ((NULL == CU_add_test(pSuite, "frame can be created", test_create_frame)) ||
(NULL == CU_add_test(pSuite, "frame can be created from data", test_frame_from_data)) ||
(NULL == CU_add_test(pSuite, "data can be retrieved from frame", test_archimedes_get_data)) ||
(NULL == CU_add_test(pSuite, "Streaming view can read & write data", test_archimedes_streamed_view)))
{
CU_cleanup_registry();
return CU_get_error();
}
/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}