diff --git a/tests/SConscript b/tests/SConscript index 6c86369..2d4eee2 100644 --- a/tests/SConscript +++ b/tests/SConscript @@ -1,102 +1,100 @@ from __future__ import print_function import os from os.path import join, abspath, basename # ------------------------------------------------------------------------------ def copyComStr(env, main): if 'SHCXXCOMSTR' in main: env['CXXCOMSTR'] = main['SHCXXCOMSTR'] if 'SHLINKCOMSTR' in main: env['LINKCOMSTR'] = main['SHLINKCOMSTR'] # ------------------------------------------------------------------------------ def make_python_tests(env): """Copy python tests to build directory""" test_env = env.Clone( PRINT_CMD_LINE_FUNC=main_env['gen_print']("Copying", "red", main_env)) test_files = Split(""" run_tests.sh test_hertz_pressure.py test_westergaard.py test_patch_westergaard.py test_surface.py test_autocorrelation.py test_hertz_disp.py test_hertz_kato.py test_saturated_pressure.py test_bem_grid.py test_flood_fill.py """) src_dir = "#/tests" build_dir = 'build-' + main_env['build_type'] + '/tests' for file in test_files: source = join(src_dir, file) test_env.Command(file, source, Copy("$TARGET", "$SOURCE")) # ------------------------------------------------------------------------------ def compile_google_test(env, gtest_dir): gtest_obj = env.Object('gtest.o', [join(gtest_dir, "src/gtest-all.cc")]) env.StaticLibrary('gtest', gtest_obj) # ------------------------------------------------------------------------------ def make_google_tests(env): gtest_dir = '#/third-party/googletest/googletest' gtest_dir = str(Dir(gtest_dir)) # Checking for google test repo if len(os.listdir(gtest_dir)) == 0: print("Googletest submodule was not initialized\n" + "Run 'git submodule update --init --recursive third-party/googletest'") Exit(1) gtest_env = env.Clone(CPPPATH=[gtest_dir], CXXFLAGS=['-pthread', '-isystem', join(gtest_dir, "include")]) compile_google_test(gtest_env, gtest_dir) env.AppendUnique(LIBS=['Tamaas'], CXXFLAGS=gtest_env['CXXFLAGS']) - # google_test_files = Split(""" - # test_grid.cpp - # test_loop.cpp - # test_fft.cpp - # test_model.cpp - # """) google_test_files = Split(""" - test_rough_surface.cpp + test_fft.cpp + test_grid.cpp + test_loop.cpp + test_model.cpp """) google_test_files += ['libgtest.a'] - env.Program('test_gtest_all', google_test_files) + + gtest_main = env.Object("gtest_main.o", join(gtest_dir, "src/gtest_main.cc")) + env.Program('test_gtest_all', google_test_files + [gtest_main]) # ------------------------------------------------------------------------------ def make_bare_tests(env): - pass - # env.Program("test_rough_surface.cpp") + env.Program("test_rough_surface.cpp") # ------------------------------------------------------------------------------ Import('main_env') # Setup of test environment test_env = main_env.Clone() test_env.AppendUnique(LIBS=['Tamaas'], LIBPATH=[abspath('build-' + main_env['build_type'] + '/src')]) copyComStr(test_env, main_env) # Building tests that do not require any third party make_bare_tests(test_env) # Building google tests if test_env['use_googletest']: make_google_tests(test_env) diff --git a/tests/test.hh b/tests/test.hh index 1ae0db2..9524cfa 100644 --- a/tests/test.hh +++ b/tests/test.hh @@ -1,75 +1,69 @@ /** * * @author Lucas Frérot * * @section LICENSE * * Copyright (©) 2017 EPFL (Ecole Polytechnique Fédérale de * Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des * Solides) * * Tamaas 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. * * Tamaas 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 Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with Tamaas. If not, see . * */ /* -------------------------------------------------------------------------- */ #include "tamaas.hh" #include #include #include -#include #include - -void tamaas_init() { - tamaas::initialize(); -} - -void tamaas_teardown() { - tamaas::finalize(); -} +#include +#include +#include "gtest/gtest.h" template bool compare(T&& a, U&& b) { return std::mismatch(std::begin(a), std::end(a), std::begin(b)) == std::make_pair(std::end(a), std::end(b)); } template bool compare(T&& a, U&& b, Pred&& pred) { return std::mismatch(std::begin(a), std::end(a), std::begin(b), pred) == std::make_pair(std::end(a), std::end(b)); } struct AreFloatEqual { template inline bool operator()(T&& x, U&& y) { //T y = reinterpret_cast(z); tamaas::Real tolerance = 1e-12; tamaas::Real abs_max = std::max(std::abs(x), std::abs(y)); abs_max = std::max(abs_max, tamaas::Real(1.)); return std::abs(x - y) <= (tolerance * abs_max); } }; struct AreComplexEqual { inline bool operator()(const tamaas::Complex & x, const fftw_complex & y) { auto equal = AreFloatEqual(); return equal(x.real(), y[0]) && equal(x.imag(), y[1]); } }; diff --git a/tests/test_fft.cpp b/tests/test_fft.cpp index 16db4c9..eec9114 100644 --- a/tests/test_fft.cpp +++ b/tests/test_fft.cpp @@ -1,87 +1,81 @@ /** * * @author Lucas Frérot * * @section LICENSE * * Copyright (©) 2017 EPFL (Ecole Polytechnique Fédérale de * Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des * Solides) * * Tamaas 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. * * Tamaas 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 Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with Tamaas. If not, see . * */ /* -------------------------------------------------------------------------- */ #include "test.hh" #include "grid.hh" #include "grid_hermitian.hh" #include "fft_plan_manager.hh" -#include -#include -#include -#include using namespace tamaas; /* -------------------------------------------------------------------------- */ -TestSuite(fft_interface, .init = tamaas_init, .fini = tamaas_teardown); - -Test(fft_1d, fft_interface) { +TEST(TestFFTInterface, FFT1D) { constexpr UInt size = 20; double data[size] = {0}; fftw_complex solution[size/2 + 1] = {{0}}; std::iota(std::begin(data), std::end(data), 0); fftw_plan solution_plan = fftw_plan_dft_r2c_1d(size, data, solution, FFTW_ESTIMATE); fftw_execute(solution_plan); Grid grid({size}, 1); std::iota(grid.begin(), grid.end(), 0); GridHermitian result({size/2 + 1}, 1); FFTPlanManager::get().createPlan(grid, result).forwardTransform(); #ifdef USE_CUDA cudaDeviceSynchronize(); #endif - cr_assert(compare(result, solution, AreComplexEqual()), "1D FFTW transform failed"); + ASSERT_TRUE(compare(result, solution, AreComplexEqual())) << "1D FFTW transform failed"; fftw_destroy_plan(solution_plan); FFTPlanManager::get().destroyPlan(grid, result); } -Test(fft_2d, fft_interface) { +TEST(TestFFTInterface, FFT2D) { constexpr UInt size = 10; double data[size * size] = {0}; fftw_complex solution[size * (size/2 + 1)] = {{0}}; std::iota(std::begin(data), std::end(data), 0); fftw_plan solution_plan = fftw_plan_dft_r2c_2d(size, size, data, solution, FFTW_ESTIMATE); fftw_execute(solution_plan); Grid grid({size, size}, 1); std::iota(grid.begin(), grid.end(), 0); GridHermitian result({size, size/2 + 1}, 1); FFTPlanManager::get().createPlan(grid, result).forwardTransform(); #ifdef USE_CUDA cudaDeviceSynchronize(); #endif - cr_assert(compare(result, solution, AreComplexEqual()), "2D FFTW transform failed"); + ASSERT_TRUE(compare(result, solution, AreComplexEqual())) << "2D FFTW transform failed"; fftw_destroy_plan(solution_plan); FFTPlanManager::get().destroyPlan(grid, result); } diff --git a/tests/test_grid.cpp b/tests/test_grid.cpp index 0f9e057..ec558cf 100644 --- a/tests/test_grid.cpp +++ b/tests/test_grid.cpp @@ -1,139 +1,127 @@ /** * * @author Lucas Frérot * * @section LICENSE * * Copyright (©) 2017 EPFL (Ecole Polytechnique Fédérale de * Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des * Solides) * * Tamaas 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. * * Tamaas 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 Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with Tamaas. If not, see . * */ /* -------------------------------------------------------------------------- */ #include "test.hh" #include "grid.hh" -#include -#include -#include using namespace tamaas; /* -------------------------------------------------------------------------- */ -TestSuite(grid_creation, .init = tamaas_init, .fini = tamaas_teardown); - // Testing 1D creation -Test(grid_creation, creation_1d) { +TEST(TestGridCreation, Creation1d) { Grid grid{{5}, 2}; std::array correct_size{{5}}; - cr_assert(grid.sizes() == correct_size, "Wrong sizes"); + ASSERT_TRUE(grid.sizes() == correct_size) << "Wrong sizes"; std::array correct_strides{{2, 1}}; - cr_assert(grid.getStrides() == correct_strides, "Wrong strides"); + ASSERT_TRUE(grid.getStrides() == correct_strides) << "Wrong strides"; } // Testing 2D creation -Test(grid_creation, creation_2d) { +TEST(TestGridCreation, Creation2d) { Grid grid({5, 2}, 3); std::array correct_size{{5, 2}}; - cr_assert(grid.sizes() == correct_size, "Wrong sizes"); + ASSERT_TRUE(grid.sizes() == correct_size) << "Wrong sizes"; std::array correct_strides{{6, 3, 1}}; - cr_assert(grid.getStrides() == correct_strides, "Wrong strides"); + ASSERT_TRUE(grid.getStrides() == correct_strides) << "Wrong strides"; } // Testing 3D creation -Test(grid_creation, creation_3d) { +TEST(TestGridCreation, Creation3d) { Grid grid({8, 5, 2}, 3); std::array correct_size{{8, 5, 2}}; - cr_assert(grid.sizes() == correct_size, "Wrong sizes"); + ASSERT_TRUE(grid.sizes() == correct_size) << "Wrong sizes"; std::array correct_strides{{30, 15, 3, 1}}; - cr_assert(grid.getStrides() == correct_strides, "Wrong strides"); + ASSERT_TRUE(grid.getStrides() == correct_strides) << "Wrong strides"; } /* -------------------------------------------------------------------------- */ -TestSuite(grid_iterators, .init = tamaas_init, .fini = tamaas_teardown); - // Testing iterators in STL function iota -Test(grid_iterators, iota) { +TEST(TestGridIterators, Iota) { constexpr UInt N = 20; Grid grid({N}, 1); std::iota(grid.begin(), grid.end(), 0); const UInt * p = grid.getInternalData(); for (UInt i = 0 ; i < N ; ++i) - cr_assert(p[i] == i, "Iota fill failed"); + ASSERT_TRUE(p[i] == i) << "Iota fill failed"; } // Testing filling grid with OpenMP loop -Test(grid_iterators, openmp_loops) { +TEST(TestGridIterators, OpenMPLoops) { Grid grid({20}, 1); #ifndef USE_CUDA #pragma omp parallel for #endif for (auto it = grid.begin() ; it < grid.end() ; ++it) { UInt i = it - grid.begin(); *it = i; } std::vector correct(20); std::iota(correct.begin(), correct.end(), 0); - cr_assert(compare(grid, correct), "Fill using OpenMP loop failed"); + ASSERT_TRUE(compare(grid, correct)) << "Fill using OpenMP loop failed"; } /* -------------------------------------------------------------------------- */ -TestSuite(grid_operators, .init = tamaas_init, .fini = tamaas_teardown); - // Testing scalar simple loop-based operators -Test(grid_operators, loop_operators) { +TEST(TestGridOperators, LoopOperators) { Grid grid({20}, 1); grid = 1; - cr_expect(std::all_of(grid.begin(), grid.end(), [](UInt x){return x == 1;}), - "Assignement operator failed"); + EXPECT_TRUE(std::all_of(grid.begin(), grid.end(), [](UInt x) { + return x == 1; + })) << "Assignement operator failed"; grid += 1; - cr_expect(std::all_of(grid.begin(), grid.end(), [](UInt x){return x == 2;}), - "Plus-equal operator failed"); + EXPECT_TRUE(std::all_of(grid.begin(), grid.end(), [](UInt x) { + return x == 2; + })) << "Plus-equal operator failed"; } // Testing loop-based functions with reductions -Test(grid_operators, stats) { +TEST(TestGridOperators, Stats) { constexpr UInt N = 20; Grid grid({N}, 1); std::iota(grid.begin(), grid.end(), 0); auto b = grid.begin(), e = grid.end(); Real min = *std::min_element(b, e); Real max = *std::max_element(b, e); Real acc = std::accumulate(b, e, 0); Real mea = acc / N; Real var = 35; - cr_expect(grid.min() == min, - "Minimum function failed"); - cr_expect(grid.max() == max, - "Maximum function failed"); - cr_expect(grid.sum() == acc, - "Sum function failed"); - cr_expect(grid.mean() == mea, - "Mean function failed"); - cr_expect(grid.var() == var, - "Var function failed"); + EXPECT_TRUE(grid.min() == min) << "Minimum function failed"; + EXPECT_TRUE(grid.max() == max) << "Maximum function failed"; + EXPECT_TRUE(grid.sum() == acc) <<"Sum function failed"; + EXPECT_TRUE(grid.mean() == mea) << "Mean function failed"; + EXPECT_TRUE(grid.var() == var) << "Var function failed"; } diff --git a/tests/test_loop.cpp b/tests/test_loop.cpp index 01ec886..f9d9b87 100644 --- a/tests/test_loop.cpp +++ b/tests/test_loop.cpp @@ -1,197 +1,191 @@ /** * * @author Lucas Frérot * * @section LICENSE * * Copyright (©) 2017 EPFL (Ecole Polytechnique Fédérale de * Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des * Solides) * * Tamaas 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. * * Tamaas 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 Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with Tamaas. If not, see . * */ /* -------------------------------------------------------------------------- */ #include "test.hh" #include "grid.hh" #include "static_types.hh" #include "loop.hh" /* -------------------------------------------------------------------------- */ using namespace tamaas; -TestSuite(loops, .init = tamaas_init, .fini = tamaas_teardown); - // Testing loops on one grid -Test(loops, one_arg) { +TEST(TestLoops, OneArgument) { Grid grid({20}, 1); Grid solution({20}, 1); auto add_one = [] CUDA_LAMBDA (Real & x) { return x + 1; }; std::iota(grid.begin(), grid.end(), 1); // Makeing solution std::transform(grid.begin(), grid.end(), solution.begin(), add_one); auto add_one_inplace = [] CUDA_LAMBDA (Real & x) { x += 1; }; Loop::loop(add_one_inplace, grid); - cr_assert(compare(grid, solution, AreFloatEqual()), "One argument loop failed"); + ASSERT_TRUE(compare(grid, solution, AreFloatEqual())) << "One argument loop failed"; } // Testing loops on two grids -Test(loops, two_args) { +TEST(TestLoops, TwoArguments) { // Why no ints? Grid grid({20, 20}, 1); Grid primal({20, 20}, 1); Grid solution({20, 20}, 1); primal(0, 0) = 1; primal(0, 1) = 1; primal(1, 0) = 1; primal(1, 1) = 1; std::transform(primal.begin(), primal.end(), solution.begin(), [] CUDA_LAMBDA (Int & primal) { return (primal > 0)? -1:1; }); Loop::loop([] CUDA_LAMBDA (Int & primal, Int & val) { val = (primal > 0)? -1:1; }, primal, grid); - cr_assert(compare(solution, grid), "Two argument loop failed"); + ASSERT_TRUE(compare(solution, grid)) << "Two argument loop failed"; } // Testing an enumeration -Test(loops, enumeration) { +TEST(TestLoops, Enumeration) { Grid grid({100}, 1); Grid solution({100}, 1); std::iota(solution.begin(), solution.end(), 0); Loop::loop([] CUDA_LAMBDA (UInt & x, UInt i) { x = i; }, grid, Loop::arange(100)); - cr_assert(compare(solution, grid), "Enumeration loop failed"); + ASSERT_TRUE(compare(solution, grid)) << "Enumeration loop failed"; } /* -------------------------------------------------------------------------- */ -TestSuite(reductions, .init = tamaas_init, .fini = tamaas_teardown); - // Testing one grid reductions -Test(reductions, one_arg) { +TEST(TestReductions, OneArgument) { Grid grid({6}, 1); std::iota(grid.begin(), grid.end(), 1); const auto id = [] CUDA_LAMBDA (UInt & x) {return x;}; // Sum reduction UInt sol = std::accumulate(grid.begin(), grid.end(), 0, std::plus()); UInt red = Loop::reduce(id, grid); - cr_assert(sol == red, "Addition reduction failed on one argument"); + ASSERT_TRUE(sol == red) << "Addition reduction failed on one argument"; // Product reduction sol = std::accumulate(grid.begin(), grid.end(), 1, std::multiplies()); red = Loop::reduce(id, grid); - cr_assert(sol == red, "Multiplication reduction failed on one argument"); + ASSERT_TRUE(sol == red) << "Multiplication reduction failed on one argument"; // Min reduction sol = *std::min_element(grid.begin(), grid.end()); red = Loop::reduce(id, grid); - cr_assert(sol == red, "Min reduction failed on one argument"); + ASSERT_TRUE(sol == red) << "Min reduction failed on one argument"; // Max reduction sol = *std::max_element(grid.begin(), grid.end()); red = Loop::reduce(id, grid); - cr_assert(sol == red, "Max reduction failed on one argument"); + ASSERT_TRUE(sol == red) << "Max reduction failed on one argument"; } -Test(reductions, two_args) { +TEST(TestReductions, TwoArguments) { Grid grid({20}, 1); Grid primal({20}, 1); grid = 1; primal(0) = 1; primal(1) = 1; // Reduce on values where primal > 0 UInt red = Loop::reduce([] CUDA_LAMBDA (UInt & p, UInt & val) { return (p > 0)? val:0; }, primal, grid); - cr_assert(red == 2, "Two args reduction failed"); + ASSERT_TRUE(red == 2) << "Two args reduction failed"; } /* -------------------------------------------------------------------------- */ -TestSuite(strided_loops, .init = tamaas_init, .fini = tamaas_teardown); - -Test(strided_loops, one_arg) { +TEST(TestStridedLoops, OneArgument) { Grid grid({10, 10}, 2); std::iota(grid.begin(), grid.end(), 1); Grid solution({10, 10}, 2); solution = grid; std::for_each(solution.begin(), solution.end(), [] CUDA_LAMBDA (UInt & x) { if (x % 2 == 1) x += 1; }); Loop::stridedLoop([] CUDA_LAMBDA (UInt & x) { x += 1; }, grid); - cr_assert(compare(solution, grid), "One argument strided loop failed"); + ASSERT_TRUE(compare(solution, grid)) << "One argument strided loop failed"; } template using Vector = StaticVector; -Test(strided_loops, static_vector) { +TEST(TestStridedLoops, VectorStride) { Grid grid({10, 10}, 2); std::iota(grid.begin(), grid.end(), 1); Grid solution({10, 10}, 2); solution = grid; std::for_each(solution.begin(), solution.end(), [] CUDA_LAMBDA (UInt & x) { if (x % 2 == 1) x += 1; }); Loop::stridedLoop([] CUDA_LAMBDA (Vector&& x) { x(0) += 1; }, grid); - cr_assert(compare(solution, grid), "Static vector strided loop failed"); + ASSERT_TRUE(compare(solution, grid)) << "Static vector strided loop failed"; } template using Matrix = StaticMatrix; -Test(strided_loops, static_matrix) { +TEST(TestStridedLoops, MatrixStride) { Grid grid({10, 10}, 4); Grid solution({10, 10}, 4); std::iota(solution.begin(), solution.end(), 0); std::for_each(solution.begin(), solution.end(), [] CUDA_LAMBDA (UInt & x) { if (x % 4 == 0 || x % 4 == 3) x = 1; else x = 0; }); Loop::stridedLoop([] CUDA_LAMBDA (Matrix&& x) { x(0, 0) = 1; x(1, 1) = 1; }, grid); - cr_assert(compare(solution, grid), "Static matrix strided loop failed"); + ASSERT_TRUE(compare(solution, grid)) << "Static matrix strided loop failed"; } diff --git a/tests/test_model.cpp b/tests/test_model.cpp index e1635ea..bd3958b 100644 --- a/tests/test_model.cpp +++ b/tests/test_model.cpp @@ -1,43 +1,41 @@ /** * * @author Lucas Frérot * * @section LICENSE * * Copyright (©) 2017 EPFL (Ecole Polytechnique Fédérale de * Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des * Solides) * * Tamaas 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. * * Tamaas 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 Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with Tamaas. If not, see . * */ /* -------------------------------------------------------------------------- */ #include "model_factory.hh" #include "test.hh" #include "westergaard.hh" /* -------------------------------------------------------------------------- */ using namespace tamaas; -TestSuite(model, .init = tamaas_init, .fini = tamaas_teardown); - -Test(model, influence) { +TEST(TestModel, Influence) { Model* model = ModelFactory::createModel(model_type::basic_2d, {1., 1.}, {128, 128}); - std::cout << dynamic_cast&>( - model->getBEEngine()) - .getInfluence() - << std::endl; + // std::cout << dynamic_cast&>( + // model->getBEEngine()) + // .getInfluence() + // << std::endl; delete model; }