mirror of
https://github.com/noirotm/libamf.git
synced 2025-12-21 09:49:31 -06:00
Add support for check-based unit tests
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
project(libamf C)
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
|
||||
|
||||
set(LIBAMF_VERSION "0.2.0")
|
||||
|
||||
@@ -61,6 +62,13 @@ endif(MSVC)
|
||||
|
||||
add_subdirectory(src)
|
||||
|
||||
# check unit testing framework
|
||||
find_package(Check)
|
||||
|
||||
# Enable unit testing if Check has been found
|
||||
if (CHECK_FOUND)
|
||||
enable_testing()
|
||||
endif (CHECK_FOUND)
|
||||
|
||||
# tests
|
||||
enable_testing()
|
||||
add_subdirectory(tests)
|
||||
|
||||
54
CMakeModules/FindCheck.cmake
Normal file
54
CMakeModules/FindCheck.cmake
Normal file
@@ -0,0 +1,54 @@
|
||||
# - Try to find the CHECK libraries
|
||||
# Once done this will define
|
||||
#
|
||||
# CHECK_FOUND - system has check
|
||||
# CHECK_INCLUDE_DIRS - the check include directory
|
||||
# CHECK_LIBRARIES - check library
|
||||
#
|
||||
# Copyright (c) 2007 Daniel Gollub <gollub@b1-systems.de>
|
||||
# Copyright (c) 2007-2009 Bjoern Ricks <bjoern.ricks@gmail.com>
|
||||
#
|
||||
# Redistribution and use is allowed according to the terms of the New
|
||||
# BSD license.
|
||||
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
|
||||
|
||||
|
||||
INCLUDE( FindPkgConfig )
|
||||
|
||||
IF ( Check_FIND_REQUIRED )
|
||||
SET( _pkgconfig_REQUIRED "REQUIRED" )
|
||||
ELSE( Check_FIND_REQUIRED )
|
||||
SET( _pkgconfig_REQUIRED "" )
|
||||
ENDIF ( Check_FIND_REQUIRED )
|
||||
|
||||
IF ( CHECK_MIN_VERSION )
|
||||
PKG_SEARCH_MODULE( CHECK ${_pkgconfig_REQUIRED} check>=${CHECK_MIN_VERSION} )
|
||||
ELSE ( CHECK_MIN_VERSION )
|
||||
PKG_SEARCH_MODULE( CHECK ${_pkgconfig_REQUIRED} check )
|
||||
ENDIF ( CHECK_MIN_VERSION )
|
||||
|
||||
# Look for CHECK include dir and libraries
|
||||
IF( NOT CHECK_FOUND AND NOT PKG_CONFIG_FOUND )
|
||||
|
||||
FIND_PATH( CHECK_INCLUDE_DIRS check.h )
|
||||
|
||||
FIND_LIBRARY( CHECK_LIBRARIES NAMES check )
|
||||
|
||||
IF ( CHECK_INCLUDE_DIRS AND CHECK_LIBRARIES )
|
||||
SET( CHECK_FOUND 1 )
|
||||
IF ( NOT Check_FIND_QUIETLY )
|
||||
MESSAGE ( STATUS "Found CHECK: ${CHECK_LIBRARIES}" )
|
||||
ENDIF ( NOT Check_FIND_QUIETLY )
|
||||
ELSE ( CHECK_INCLUDE_DIRS AND CHECK_LIBRARIES )
|
||||
IF ( Check_FIND_REQUIRED )
|
||||
MESSAGE( FATAL_ERROR "Could NOT find CHECK" )
|
||||
ELSE ( Check_FIND_REQUIRED )
|
||||
IF ( NOT Check_FIND_QUIETLY )
|
||||
MESSAGE( STATUS "Could NOT find CHECK" )
|
||||
ENDIF ( NOT Check_FIND_QUIETLY )
|
||||
ENDIF ( Check_FIND_REQUIRED )
|
||||
ENDIF ( CHECK_INCLUDE_DIRS AND CHECK_LIBRARIES )
|
||||
ENDIF( NOT CHECK_FOUND AND NOT PKG_CONFIG_FOUND )
|
||||
|
||||
# Hide advanced variables from CMake GUIs
|
||||
MARK_AS_ADVANCED( CHECK_INCLUDE_DIRS CHECK_LIBRARIES )
|
||||
@@ -1,4 +1,14 @@
|
||||
include_directories(${CMAKE_SOURCE_DIR}/src)
|
||||
# unit tests
|
||||
if (CHECK_FOUND)
|
||||
include_directories(${CHECK_INCLUDE_DIRS})
|
||||
include_directories(${CMAKE_SOURCE_DIR}/src)
|
||||
|
||||
add_executable(check_libamf check_libamf.c check_amf0.c)
|
||||
target_link_libraries(check_libamf ${CHECK_LIBRARIES} amf pthread)
|
||||
|
||||
add_test(check_libamf ${CMAKE_CURRENT_BINARY_DIR}/check_libamf)
|
||||
endif (CHECK_FOUND)
|
||||
|
||||
# simple demo
|
||||
add_executable(amf0_demo amf0_demo.c)
|
||||
target_link_libraries(amf0_demo amf)
|
||||
|
||||
198
tests/check_amf0.c
Normal file
198
tests/check_amf0.c
Normal file
@@ -0,0 +1,198 @@
|
||||
#include <check.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "amf0.h"
|
||||
|
||||
amf0_data * data;
|
||||
|
||||
void teardown(void) {
|
||||
amf0_data_free(data);
|
||||
}
|
||||
|
||||
/**
|
||||
AMF number
|
||||
*/
|
||||
void setup_amf0_number(void) {
|
||||
data = amf0_number_new(0);
|
||||
}
|
||||
|
||||
START_TEST(test_amf0_number_new) {
|
||||
fail_if(data == NULL,
|
||||
"data should not be NULL");
|
||||
|
||||
fail_unless(amf0_data_get_type(data) == AMF0_TYPE_NUMBER,
|
||||
"invalid data type: expected %d, got %d", AMF0_TYPE_NUMBER, amf0_data_get_type(data));
|
||||
|
||||
/* AMF number size == 1(header) + 8(data) -> 9 bytes */
|
||||
fail_unless(amf0_data_size(data) == 9,
|
||||
"invalid data size: expected 9, got %d", amf0_data_size(data));
|
||||
|
||||
fail_unless(amf0_number_get_value(data) == 0,
|
||||
"invalid data value: expected 0, got %f", amf0_number_get_value(data));
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_amf0_number_set_value) {
|
||||
amf0_number_set_value(data, -512.78);
|
||||
|
||||
fail_unless(amf0_number_get_value(data) == -512.78,
|
||||
"invalid data value: expected -512.78, got %f", amf0_number_get_value(data));
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_amf0_number_null) {
|
||||
fail_unless(amf0_number_get_value(NULL) == 0,
|
||||
"invalid data value: expected 0, got %f", amf0_number_get_value(NULL));
|
||||
|
||||
/* just making sure we don't core dump */
|
||||
amf0_number_set_value(NULL, 12);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/**
|
||||
AMF boolean
|
||||
*/
|
||||
void setup_amf0_boolean(void) {
|
||||
data = amf0_boolean_new(1);
|
||||
}
|
||||
|
||||
START_TEST(test_amf0_boolean_new) {
|
||||
fail_if(data == NULL,
|
||||
"data should not be NULL");
|
||||
|
||||
fail_unless(amf0_data_get_type(data) == AMF0_TYPE_BOOLEAN,
|
||||
"invalid data type: expected %d, got %d", AMF0_TYPE_BOOLEAN, amf0_data_get_type(data));
|
||||
|
||||
/* AMF boolean size == 1(header) + 1(data) -> 2 bytes */
|
||||
fail_unless(amf0_data_size(data) == 2,
|
||||
"invalid data size: expected 2, got %d", amf0_data_size(data));
|
||||
|
||||
fail_unless(amf0_boolean_get_value(data) == 1,
|
||||
"invalid data value: expected 1, got %d", amf0_boolean_get_value(data));
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_amf0_boolean_set_value) {
|
||||
amf0_boolean_set_value(data, 0);
|
||||
fail_unless(amf0_boolean_get_value(data) == 0,
|
||||
"invalid data value: expected 0, got %d", amf0_boolean_get_value(data));
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_amf0_boolean_null) {
|
||||
fail_unless(amf0_boolean_get_value(NULL) == 0,
|
||||
"invalid data value: expected 0, got %d", amf0_boolean_get_value(data));
|
||||
|
||||
/* just making sure we don't core dump */
|
||||
amf0_boolean_set_value(NULL, 12);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/**
|
||||
AMF string
|
||||
*/
|
||||
START_TEST(test_amf0_str) {
|
||||
char * str = "hello world";
|
||||
int length = strlen(str);
|
||||
data = amf0_str(str);
|
||||
|
||||
fail_if(data == NULL,
|
||||
"data should not be NULL");
|
||||
|
||||
fail_unless(amf0_data_get_type(data) == AMF0_TYPE_STRING,
|
||||
"invalid data type: expected %d, got %d", AMF0_TYPE_STRING, amf0_data_get_type(data));
|
||||
|
||||
/* AMF string size == 1(header) + 2(string length) + length */
|
||||
fail_unless(amf0_data_size(data) == 3 + length,
|
||||
"invalid data size: expected %d, got %d", 3 + length, amf0_data_size(data));
|
||||
|
||||
fail_unless(amf0_string_get_size(data) == length,
|
||||
"invalid string length: expected %d, got %f", length, amf0_string_get_size(data));
|
||||
|
||||
fail_unless(strcmp(amf0_string_get_bytes(data), str) == 0,
|
||||
"invalid string contents");
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_amf0_str_null) {
|
||||
data = amf0_str(NULL);
|
||||
|
||||
fail_unless(amf0_string_get_size(data) == 0,
|
||||
"invalid string length: expected 0, got %f", amf0_string_get_size(data));
|
||||
|
||||
fail_unless(!strcmp(amf0_string_get_bytes(data), ""),
|
||||
"string data should be an empty string");
|
||||
|
||||
amf0_data_free(data);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_amf0_string_new) {
|
||||
char * str = "hello world";
|
||||
data = amf0_string_new(str, 5);
|
||||
|
||||
fail_unless(amf0_string_get_size(data) == 5,
|
||||
"invalid string length: expected 5, got %f", amf0_string_get_size(data));
|
||||
|
||||
fail_unless(strncmp(amf0_string_get_bytes(data), str, 5) == 0,
|
||||
"invalid string contents");
|
||||
|
||||
amf0_data_free(data);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_amf0_string_new_null) {
|
||||
data = amf0_string_new(NULL, 12);
|
||||
|
||||
fail_unless(amf0_string_get_size(data) == 0,
|
||||
"invalid string length: expected 0, got %f", amf0_string_get_size(data));
|
||||
|
||||
fail_unless(!strcmp(amf0_string_get_bytes(data),""),
|
||||
"string data should be an empty string");
|
||||
|
||||
amf0_data_free(data);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_amf0_string_null) {
|
||||
fail_unless(amf0_string_get_size(NULL) == 0,
|
||||
"invalid string length: expected 0, got %f", amf0_string_get_size(data));
|
||||
|
||||
fail_unless(amf0_string_get_bytes(NULL) == NULL,
|
||||
"string data should be NULL");
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/**
|
||||
AMF Types Suite
|
||||
*/
|
||||
Suite * amf0_suite(void) {
|
||||
Suite * s = suite_create("AMF0");
|
||||
|
||||
/* AMF number test case */
|
||||
TCase * tc_number = tcase_create("AMF0 number");
|
||||
tcase_add_checked_fixture(tc_number, setup_amf0_number, teardown);
|
||||
tcase_add_test(tc_number, test_amf0_number_new);
|
||||
tcase_add_test(tc_number, test_amf0_number_set_value);
|
||||
tcase_add_test(tc_number, test_amf0_number_null);
|
||||
suite_add_tcase(s, tc_number);
|
||||
|
||||
/* AMF boolean test case */
|
||||
TCase * tc_boolean = tcase_create("AMF0 boolean");
|
||||
tcase_add_checked_fixture(tc_boolean, setup_amf0_boolean, teardown);
|
||||
tcase_add_test(tc_boolean, test_amf0_boolean_new);
|
||||
tcase_add_test(tc_boolean, test_amf0_boolean_set_value);
|
||||
tcase_add_test(tc_boolean, test_amf0_boolean_null);
|
||||
suite_add_tcase(s, tc_boolean);
|
||||
|
||||
/* AMF string test case */
|
||||
TCase * tc_string = tcase_create("AMF0 string");
|
||||
tcase_add_test(tc_string, test_amf0_str);
|
||||
tcase_add_test(tc_string, test_amf0_str_null);
|
||||
tcase_add_test(tc_string, test_amf0_string_new);
|
||||
tcase_add_test(tc_string, test_amf0_string_new_null);
|
||||
tcase_add_test(tc_string, test_amf0_string_null);
|
||||
suite_add_tcase(s, tc_string);
|
||||
|
||||
return s;
|
||||
}
|
||||
17
tests/check_libamf.c
Normal file
17
tests/check_libamf.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <stdlib.h>
|
||||
#include <check.h>
|
||||
|
||||
extern Suite * amf0_suite(void);
|
||||
|
||||
int main(void) {
|
||||
int number_failed;
|
||||
SRunner * sr = srunner_create(amf0_suite());
|
||||
|
||||
/* srunner_set_log (sr, "check_amf.log"); */
|
||||
|
||||
srunner_run_all(sr, CK_NORMAL);
|
||||
number_failed = srunner_ntests_failed(sr);
|
||||
srunner_free(sr);
|
||||
|
||||
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
Reference in New Issue
Block a user