Hi guys, There are two patches the first one(cmake_test.patch) create the cmake tests infrastructure and the second one(files_test.patch) create the device and context tests. I am using check unit tests[1] because it is simple and it is used in many projects. The tests already found some bugs(like segfaults and not implemented features). All the tests has been wrote using opencl specification.
[1] http://check.sourceforge.net/
diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b26770..681ced3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,3 +21,9 @@ Find_Package(Clang REQUIRED) add_subdirectory(src) add_subdirectory(examples) + +IF (BUILD_TESTS) + ENABLE_TESTING() + Find_Package(Check REQUIRED) + add_subdirectory(tests) +ENDIF (BUILD_TESTS) diff --git a/cmake/modules/FindCheck.cmake b/cmake/modules/FindCheck.cmake new file mode 100644 index 0000000..d7a5bcd --- /dev/null +++ b/cmake/modules/FindCheck.cmake @@ -0,0 +1,57 @@ +# - Try to find the CHECK libraries +# Once done this will define +# +# Note: This module is originally found in opensync project +# +# CHECK_FOUND - system has check +# CHECK_INCLUDE_DIRS - the check include directory +# CHECK_LIBRARIES - check library +# +# Copyright (c) 2007 Daniel Gollub <gol...@b1-systems.de> +# Copyright (c) 2007-2009 Bjoern Ricks <bjoern.ri...@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 ) + diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..6660be0 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,18 @@ +INCLUDE_DIRECTORIES(${Clover_SOURCE_DIR}/include ${CHECK_INCLUDE_DIRS}) +LINK_DIRECTORIES(${Clover_BINARY_DIR}/src ${CHECK_LIBRARY_DIRS}) + +set(OPENCL_TESTS_SOURCE + tests.c + test_device.cpp + test_context.cpp + ) + +add_executable(tests ${OPENCL_TESTS_SOURCE}) +target_link_libraries(tests OpenCL ${CHECK_LIBRARIES}) + +MACRO(OPENCL_TEST EXECUTABLE_NAME TEST_NAME) + add_test(${TEST_NAME} ${EXECUTABLE_NAME} ${TEST_NAME}) +ENDMACRO(OPENCL_TEST) + +OPENCL_TEST(tests device) +OPENCL_TEST(tests context)
diff --git a/tests/test_context.cpp b/tests/test_context.cpp new file mode 100644 index 0000000..1e7d2e1 --- /dev/null +++ b/tests/test_context.cpp @@ -0,0 +1,52 @@ +#include "test_context.h" + +#include <OpenCL/cl.h> + +START_TEST (test_create_context) +{ + cl_context context = NULL; + cl_device_id device; + cl_int result = -1; + cl_int err_code; + cl_int result_context; + + result = clGetDeviceIDs(CL_DEVICE_TYPE_CPU, 1, &device, NULL); + if(result == CL_SUCCESS) { + //in clover we need plataform argument? + context = clCreateContext(0, 1, &device, NULL, NULL, NULL); + fail_if(context == NULL, "It should work, context not created"); + result_context = clReleaseContext(context); + fail_if((result_context != CL_SUCCESS) || (context != NULL), + "It should work, context no released"); + + context = clCreateContext(0, 1, NULL, NULL, NULL, &err_code); + fail_if((context != NULL) || (err_code != CL_INVALID_VALUE), + "It should not work, context or err_code returning wrong"); + result_context = clReleaseContext(context); + + context = clCreateContext(0, 0, &device, NULL, NULL, &err_code); + fail_if((context != NULL) || (err_code != CL_INVALID_VALUE), + "It should not work, context or err_code returning wrong"); + result_context = clReleaseContext(context); + } +} +END_TEST + +START_TEST (test_create_context_from_type) +{ + cl_int result = -1; + cl_context context = NULL; + + context = clCreateContextFromType(0, CL_DEVICE_TYPE_CPU, NULL, NULL, NULL); + fail_if(context == NULL, "It should work, context not created"); +} +END_TEST + +TCase *cl_context_tcase_create(void) +{ + TCase *tc = NULL; + tc = tcase_create("context"); + tcase_add_test(tc, test_create_context); + tcase_add_test(tc, test_create_context_from_type); + return tc; +} diff --git a/tests/test_context.h b/tests/test_context.h new file mode 100644 index 0000000..39723d2 --- /dev/null +++ b/tests/test_context.h @@ -0,0 +1,17 @@ +#ifndef __UTEST_CONTEXT__ +#define __UTEST_CONTEXT__ + +#include <check.h> + +#ifdef __cplusplus +extern "C" { +#endif + +TCase *cl_context_tcase_create(void); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/tests/test_device.cpp b/tests/test_device.cpp new file mode 100644 index 0000000..1ec8bcf --- /dev/null +++ b/tests/test_device.cpp @@ -0,0 +1,89 @@ +#include "test_device.h" + +#include <OpenCL/cl.h> + +#define CL_DEVICE_TYPE_INVALID 0x30 //i hope it be an invalid device + +static cl_device_id device; + +START_TEST (test_get_device_ids) +{ + cl_int result = -1; + + result = clGetDeviceIDs(CL_DEVICE_TYPE_DEFAULT, 1, &device, NULL); + fail_if(result != CL_SUCCESS, "It is not possible get default device id"); + + result = clGetDeviceIDs(CL_DEVICE_TYPE_CPU, 1, &device, NULL); + fail_if(result != CL_SUCCESS, "It is not possible get cpu device id"); + + result = clGetDeviceIDs(CL_DEVICE_TYPE_GPU, 1, &device, NULL); + fail_if(result != CL_SUCCESS, "It is not possible get gpu device id"); + + result = clGetDeviceIDs(CL_DEVICE_TYPE_ACCELERATOR, 1, &device, NULL); + fail_if(result != CL_SUCCESS, "It is not possible get accelarator device id"); + + result = clGetDeviceIDs(CL_DEVICE_TYPE_ALL, 1, &device, NULL); + fail_if(result != CL_SUCCESS, "It is not possible get all device ids"); + + + result = clGetDeviceIDs(CL_DEVICE_TYPE_DEFAULT, 0, &device, NULL); + fail_if(result != CL_INVALID_VALUE, "It should not be working"); + + result = clGetDeviceIDs(CL_DEVICE_TYPE_DEFAULT, 1, NULL, NULL); + fail_if(result != CL_INVALID_VALUE, "It should not be working"); + + + result = clGetDeviceIDs(CL_DEVICE_TYPE_INVALID, 1, &device, NULL); + fail_if(result != CL_INVALID_DEVICE_TYPE, "It should not be working,\ + device type does not exist"); +} +END_TEST + +START_TEST (test_get_device_info) +{ + cl_int result = -1; + + result = clGetDeviceIDs(CL_DEVICE_TYPE_CPU, 1, &device, NULL); + if(result == CL_SUCCESS) { + cl_int device_info_result; + cl_device_type device_type; + + device_info_result = clGetDeviceInfo(device, CL_DEVICE_TYPE, + sizeof(device_type), &device_type, NULL); + fail_if((device_info_result != CL_SUCCESS) || + (device_type != CL_DEVICE_TYPE_CPU), "It should work"); + + //FIXME: check all cl_device_info + + device_info_result = clGetDeviceInfo(0x0, CL_DEVICE_TYPE, + sizeof(device_type), &device_type, NULL); + fail_if(device_info_result != CL_INVALID_DEVICE, "It should not be working,\ + invalid device"); + + device_info_result = clGetDeviceInfo(device, CL_DEVICE_TYPE_INVALID, + sizeof(device_type), &device_type, NULL); + fail_if(device_info_result != CL_INVALID_VALUE,"It should not be working,\ + invalid param name"); + + device_info_result = clGetDeviceInfo(device, CL_DEVICE_TYPE, + sizeof(device_type), NULL, NULL); + // in opencl specification if param_value is NULL it should be ignored + // device_info_result should return cl_success? we think so + fail_if(device_info_result != CL_SUCCESS, "It should work"); + + device_info_result = clGetDeviceInfo(device, CL_DEVICE_TYPE, + sizeof(device_type)-10, &device_type, NULL); + fail_if(device_info_result != CL_INVALID_VALUE, "It should not be working,\ + param_value_size is < size of return type"); + } +} +END_TEST + +TCase *cl_device_tcase_create(void) +{ + TCase *tc = NULL; + tc = tcase_create("device"); + tcase_add_test(tc, test_get_device_ids); + tcase_add_test(tc, test_get_device_info); + return tc; +} diff --git a/tests/test_device.h b/tests/test_device.h new file mode 100644 index 0000000..34c2237 --- /dev/null +++ b/tests/test_device.h @@ -0,0 +1,17 @@ +#ifndef __UTEST_DEVICE__ +#define __UTEST_DEVICE__ + +#include <check.h> + +#ifdef __cplusplus +extern "C" { +#endif + +TCase *cl_device_tcase_create(void); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/tests/tests.c b/tests/tests.c new file mode 100644 index 0000000..0138b3f --- /dev/null +++ b/tests/tests.c @@ -0,0 +1,47 @@ +#include "test_device.h" +#include "test_context.h" + +#include <stdlib.h> +#include <stdio.h> + +static Suite *device_suite(void) +{ + Suite *s = suite_create("device"); + suite_add_tcase(s, cl_device_tcase_create()); + return s; +} + +static Suite *context_suite(void) +{ + Suite *s = suite_create("context"); + suite_add_tcase(s, cl_context_tcase_create()); + return s; +} + +int main(int argc, char **argv) +{ + int n_failed_tests; + Suite *s = NULL; + + if (argc < 2) { + printf("there is not enough arguments"); + return EXIT_FAILURE; + } + + if (!strcmp("device",argv[1])) { + s = device_suite(); + } else if (!strcmp("context", argv[1])){ + s = context_suite(); + } else { + printf("test case does not exist"); + return EXIT_FAILURE; + } + + SRunner *sr = srunner_create(s); + srunner_run_all(sr, CK_NORMAL); + + n_failed_tests = srunner_ntests_failed(sr); + srunner_free(sr); + + return (n_failed_tests == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +}
------------------------------------------------------------------------------ This SF.Net email is sponsored by the Verizon Developer Community Take advantage of Verizon's best-in-class app development support A streamlined, 14 day to market process makes app distribution fast and easy Join now and get one step closer to millions of Verizon customers http://p.sf.net/sfu/verizon-dev2dev
_______________________________________________ Mesa3d-dev mailing list Mesa3d-dev@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mesa3d-dev