diff --git a/CMakeLists.txt b/CMakeLists.txt index 972455342fce389f67aaa1b310e8edbe0f052d41..0117c94ade85e2201ae903d8a2de20f6e201f0f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,5 +44,6 @@ SET (${PROJECT_NAME}_HEADERS PKG_CONFIG_APPEND_LIBS("hpp-statistics") ADD_SUBDIRECTORY(src) +ADD_SUBDIRECTORY(tests) SETUP_PROJECT_FINALIZE() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..4c27c4abf68b6a1e89f1c94afa0698f9ca390efc --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,41 @@ +# Copyright 2012, 2013, 2014 CNRS-LAAS +# +# Author: Mathieu Geisert +# +# This file is part of hpp-core +# hpp-model-urdf is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# hpp-core is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Lesser Public License for more details. +# You should have received a copy of the GNU Lesser General Public License +# along with hpp-core If not, see <http://www.gnu.org/licenses/>. + +# ADD_TESTCASE(NAME) +# ------------------------ +# +# Define a test named `NAME'. +# +# This macro will create a binary from `NAME.cc', link it against +# Boost and add it to the test suite. +# +MACRO(ADD_TESTCASE NAME GENERATED) + IF (${GENERATED} STREQUAL TRUE) + ADD_EXECUTABLE(${NAME} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.cc) + ELSE() + ADD_EXECUTABLE(${NAME} ${NAME}.cc) + ENDIF() + ADD_TEST(${NAME} ${RUNTIME_OUTPUT_DIRECTORY}/${NAME}) + + # Link against Boost and project library. + TARGET_LINK_LIBRARIES(${NAME} + ${PROJECT_NAME} + ) + +ENDMACRO(ADD_TESTCASE) + +ADD_TESTCASE (test-successstatistics FALSE) diff --git a/tests/test-successstatistics.cc b/tests/test-successstatistics.cc new file mode 100644 index 0000000000000000000000000000000000000000..bd3eeacf19580091bbfd70e0247e002b706cb8fe --- /dev/null +++ b/tests/test-successstatistics.cc @@ -0,0 +1,60 @@ +// Copyright (c) 2014, LAAS-CNRS +// Authors: Joseph Mirabel (joseph.mirabel@laas.fr) +// +// This file is part of hpp-statistics. +// hpp-statistics is free software: you can redistribute it +// and/or modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation, either version +// 3 of the License, or (at your option) any later version. +// +// hpp-statistics is distributed in the hope that it will be +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty +// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Lesser Public License for more details. You should have +// received a copy of the GNU Lesser General Public License along with +// hpp-statistics. If not, see <http://www.gnu.org/licenses/>. + +#include <stdlib.h> +#include <time.h> +#include <iostream> + +#include "hpp/statistics/success-bin.hh" + +HPP_DEFINE_REASON_FAILURE (REASON_TEST, "Fake reason for testing purpose"); + +int main () +{ + /* initialize random seed: */ + srand (time(NULL)); + + using namespace hpp::statistics; + SuccessStatistics ss; + unsigned int counter[3]; + counter[0]=0; counter[1]=0; counter[2]=0; + for (int i = 0; i < 100; i++) { + unsigned int nb = rand() % 3; + counter[nb]++; + switch (nb) { + case 0: + ss.addSuccess (); + break; + case 1: + ss.addFailure (); + break; + case 2: + ss.addFailure (REASON_TEST); + break; + } + } + if ( ss.nbSuccess () != counter[0] + || ss.nbFailure (SuccessBin::REASON_UNKNOWN) != counter[1] + || ss.nbFailure (REASON_TEST) != counter[2]) { + std::cout << ss; + std::cout << "Real frequencies are: ( " << counter[0] << ", " << counter[1] + << ", " << counter [2] << ")" << std::endl; + return EXIT_FAILURE; + } + if (ss.nbFailure () != counter[1] + counter[2]) + return EXIT_FAILURE; + return EXIT_SUCCESS; +}