Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests #314

Merged
merged 5 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jobs:
-D CMAKE_C_COMPILER=${{ matrix.c_compiler }}
-D CMAKE_BUILD_TYPE=${{ matrix.build_type }}
-D BUILD_SHARED_LIBS=${{ matrix.shared_libs }}
-D BUILD_TESTING=ON
-S ${{ github.workspace }}

- name: Build
Expand All @@ -83,6 +84,13 @@ jobs:
if: matrix.os != 'windows-latest'
run: sudo cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} --target install

- name: add DLL to test folder
if: matrix.os == 'windows-latest'
working-directory: ${{ steps.strings.outputs.build-output-dir }}
run: |
cp *apriltag.dll test/

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
run: ctest --build-config ${{ matrix.build_type }}
run: |
ctest --build-config ${{ matrix.build_type }} --no-tests=error --output-on-failure --verbose --test-dir test/
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ endif()

set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION 3 VERSION ${PROJECT_VERSION})
set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX "d")
set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 99)


include(GNUInstallDirs)
target_include_directories(${PROJECT_NAME} PUBLIC
Expand Down Expand Up @@ -211,3 +213,7 @@ if (BUILD_EXAMPLES)
# install example programs
install(TARGETS apriltag_demo RUNTIME DESTINATION bin)
endif()

if(BUILD_TESTING)
add_subdirectory(test)
endif()
20 changes: 20 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
enable_testing()

add_library(getline OBJECT getline.c)

add_executable(test_detection test_detection.c)
target_link_libraries(test_detection ${PROJECT_NAME} getline)

# test images with true detection
set(TEST_IMAGE_NAMES
"33369213973_9d9bb4cc96_c"
"34085369442_304b6bafd9_c"
"34139872896_defdb2f8d9_c"
)

foreach(IMG IN LISTS TEST_IMAGE_NAMES)
add_test(NAME test_detection_${IMG}
COMMAND $<TARGET_FILE:test_detection> data/${IMG}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endforeach()
Binary file added test/data/33369213973_9d9bb4cc96_c.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions test/data/33369213973_9d9bb4cc96_c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
0, 0, (277.8802 327.5163), (250.1780 329.6940), (251.7135 357.2946), (279.7680 354.8845)
1, 0, (571.4235 361.6797), (543.6642 364.9229), (544.4681 395.1039), (572.3226 391.6599)
2, 0, (760.7551 428.9775), (726.5153 428.5690), (727.0422 461.5150), (761.2695 462.2295)
3, 0, (512.2699 371.3797), (511.7118 342.6652), (485.4774 344.9952), (486.1231 374.7085)
4, 0, (640.0231 425.9307), (640.9352 458.5056), (669.8774 462.4900), (669.6273 429.5844)
5, 0, (353.0279 346.9656), (329.3977 344.6678), (330.0048 372.9047), (354.1879 375.4345)
6, 0, (465.4157 341.5291), (466.7475 370.4995), (480.0401 375.3805), (479.3255 346.8523)
7, 0, (448.3790 335.3048), (421.9637 338.3846), (422.9550 366.6558), (449.2383 363.5178)
8, 0, (414.5614 338.8829), (403.2398 333.4245), (404.6997 361.6877), (415.0851 366.5417)
Binary file added test/data/34085369442_304b6bafd9_c.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions test/data/34085369442_304b6bafd9_c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
0, 0, (712.8496 321.3379), (695.6104 319.2373), (695.0875 337.7314), (711.6552 339.5857)
1, 0, (137.1803 350.0015), (137.0391 331.3253), (119.5017 332.1772), (119.3629 350.9057)
2, 0, (400.1603 282.9774), (400.0085 299.7743), (416.9421 299.6274), (416.6747 283.0110)
3, 0, (733.9197 326.1527), (751.1302 325.8460), (751.3755 307.5261), (733.9084 307.7999)
4, 0, (416.5878 352.7041), (431.3354 354.3437), (431.1941 335.7852), (416.3476 334.0768)
5, 0, (472.2987 271.9427), (472.0169 288.8806), (484.8894 287.0572), (485.0777 270.9519)
6, 0, (318.5974 389.4719), (300.7015 391.3979), (300.9274 411.2381), (318.8591 409.5875)
7, 0, (311.2344 343.5884), (328.5275 343.2667), (328.5633 324.8743), (310.5821 324.8871)
8, 0, (136.6730 406.5262), (136.3904 386.6153), (118.5666 385.0555), (118.4830 405.8007)
9, 0, (677.7889 302.6428), (677.6320 285.3052), (666.0012 282.7271), (665.6072 300.6304)
10, 0, (467.2973 288.1754), (467.2424 271.4062), (457.5093 269.1866), (457.6929 286.1717)
11, 0, (543.4706 350.8587), (543.2719 333.1554), (525.7717 331.5527), (525.3212 350.7736)
12, 0, (49.6552 357.7258), (63.3771 355.7744), (63.1037 337.2454), (49.7023 338.9751)
13, 0, (215.9719 413.1197), (235.2605 413.4054), (236.1417 393.1873), (216.0083 392.9312)
14, 0, (615.2866 276.6579), (615.1252 293.3296), (629.6708 292.3475), (631.6648 274.3273)
15, 0, (357.4018 346.3447), (373.2110 347.5034), (373.4671 329.2181), (357.1758 328.1275)
16, 0, (55.9971 395.5411), (75.6599 395.5399), (76.0009 375.6914), (56.1971 375.6695)
17, 0, (214.3428 345.1931), (230.8877 343.9739), (231.1031 325.5886), (214.2705 326.9748)
Binary file added test/data/34139872896_defdb2f8d9_c.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions test/data/34139872896_defdb2f8d9_c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
0, 0, (450.7940 281.6134), (445.1442 246.4043), (403.9372 242.6193), (409.3882 277.7529)
1, 0, (751.6141 416.2980), (708.4382 420.6972), (709.8007 466.8162), (753.4771 462.5127)
2, 0, (422.5576 450.2083), (421.3539 405.6422), (376.8648 407.7634), (378.0238 452.7328)
3, 0, (329.0142 399.5628), (285.4985 402.8493), (286.6385 446.9015), (330.8413 443.2896)
4, 0, (585.3918 383.6006), (587.3239 427.6843), (608.2718 435.5553), (606.5024 391.1288)
5, 0, (695.4645 419.9278), (676.2806 411.6397), (677.7034 456.7264), (697.6416 466.1422)
6, 0, (658.1703 429.9575), (657.1567 385.4371), (616.7877 389.5853), (618.0186 434.5197)
6 changes: 6 additions & 0 deletions test/data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Test image from NASA Swarmathon competition at KSC. Licenced under "CC BY-SA 2.0 DEED".

Sources:
- https://www.flickr.com/photos/nasakennedy/33369213973/
- https://www.flickr.com/photos/nasakennedy/34085369442/
- https://www.flickr.com/photos/nasakennedy/34139872896/
55 changes: 55 additions & 0 deletions test/getline.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "getline.h"
#include <errno.h>
#include <stdlib.h>

// https://stackoverflow.com/a/47229318/8144672
/* The original code is public domain -- Will Hartung 4/9/09 */
/* Modifications, public domain as well, by Antti Haapala, 11/10/17 - Switched to getc on 5/23/19 */

ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
size_t pos;
int c;

if (lineptr == NULL || stream == NULL || n == NULL) {
errno = EINVAL;
return -1;
}

c = getc(stream);
if (c == EOF) {
return -1;
}

if (*lineptr == NULL) {
*lineptr = malloc(128);
if (*lineptr == NULL) {
return -1;
}
*n = 128;
}

pos = 0;
while(c != EOF) {
if (pos + 1 >= *n) {
size_t new_size = *n + (*n >> 2);
if (new_size < 128) {
new_size = 128;
}
char *new_ptr = realloc(*lineptr, new_size);
if (new_ptr == NULL) {
return -1;
}
*n = new_size;
*lineptr = new_ptr;
}

((unsigned char *)(*lineptr))[pos ++] = c;
if (c == '\n') {
break;
}
c = getc(stream);
}

(*lineptr)[pos] = '\0';
return pos;
}
9 changes: 9 additions & 0 deletions test/getline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <stddef.h>
#include <stdio.h>
#include <stdint.h>

typedef intptr_t ssize_t;

ssize_t getline(char **lineptr, size_t *n, FILE *stream);
134 changes: 134 additions & 0 deletions test/test_detection.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include <stdarg.h>
#include <apriltag.h>
#include <tag36h11.h>
#include <common/pjpeg.h>

#include "getline.h"


char*
format(const char* fmt, ...)
{
va_list args1;
va_list args2;

va_start(args1, fmt);
va_copy(args2, args1);

const int N = vsnprintf(NULL, 0, fmt, args1);
va_end(args1);

if (N < 0) {
va_end(args2);
return NULL;
}

char* res_fmt = calloc(N+1, sizeof(char));
const int n = vsnprintf(res_fmt, N+1, fmt, args2);
va_end(args2);

if (n != N) {
free(res_fmt);
return NULL;
}

// caller has to free returned string
return res_fmt;
}

int
detection_corner_compare_function(const void *_a, const void *_b)
{
apriltag_detection_t *a = *(apriltag_detection_t**) _a;
apriltag_detection_t *b = *(apriltag_detection_t**) _b;

return memcmp(a->p, b->p, sizeof(a));
}

int
main(int argc, char *argv[])
{
if (argc!=2) {
return EXIT_FAILURE;
}

// load image
char* const path_img = format("%s.jpg", argv[1]);
pjpeg_t *pjpeg = pjpeg_create_from_file(path_img, 0, NULL);
image_u8_t *im = pjpeg_to_u8_baseline(pjpeg);
free(path_img);

// load true detection
char* const path_det_true = format("%s.txt", argv[1]);
FILE *fp = fopen(path_det_true, "r");
if (fp == NULL) {
return EXIT_FAILURE;
}
free(path_det_true);

apriltag_detector_t *td = apriltag_detector_create();
apriltag_family_t *tf = tag36h11_create();
apriltag_detector_add_family(td, tf);

const char fmt[] = "%i, %i, (%.4lf %.4lf), (%.4lf %.4lf), (%.4lf %.4lf), (%.4lf %.4lf)";

bool ok = true;

zarray_t *detections = apriltag_detector_detect(td, im);

// sort detections by detected corners for deterministic sorting order
zarray_sort(detections, detection_corner_compare_function);

int i = 0;
for (; i < zarray_size(detections); i++) {
apriltag_detection_t *det;
zarray_get(detections, i, &det);

char* const det_fmt = format(fmt,
i, det->id,
det->p[0][0], det->p[0][1], det->p[1][0], det->p[1][1],
det->p[2][0], det->p[2][1], det->p[3][0], det->p[3][1]);

char* line = NULL;
size_t len = 0;
const ssize_t nread = getline(&line, &len, fp);
if (nread == -1) {
free(line);
return EXIT_FAILURE;
}

printf("Got: %s\n", det_fmt);
printf("Expected: %s\n", line);

// compare strings without the newline character (\n)
if (strncmp(det_fmt, line, nread-1) != 0) {
fprintf(stderr, "Mismatch.\nGot:\n %s\nExpected:\n %s\n", det_fmt, line);
ok = false;
}

free(det_fmt);
free(line);
}

// check that we compared the expected amount of detections
// if there are no "true" detections left, we should be at the end of the file
// if there are no "detected" detections left, we should be at the end of the array
if ((fgetc(fp) != -1) && (i != zarray_size(detections))) {
return EXIT_FAILURE;
}

fclose(fp);

apriltag_detections_destroy(detections);
image_u8_destroy(im);
pjpeg_destroy(pjpeg);

apriltag_detector_destroy(td);
tag36h11_destroy(tf);

if (!ok) {
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
Loading