From 9b7c4c991cf4a1fa7c5e8bdd462d4ae6fde2bfe8 Mon Sep 17 00:00:00 2001 From: Johan Mabille Date: Mon, 3 Apr 2017 14:28:38 +0200 Subject: [PATCH 1/2] pyvectorize c++ unit test --- test/CMakeLists.txt | 3 +- test/test_pyvectorize.cpp | 80 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 test/test_pyvectorize.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9ea6bfc..2306c0e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -106,8 +106,9 @@ include_directories(${GTEST_INCLUDE_DIRS}) set(XTENSOR_PYTHON_TESTS main.cpp - test_pytensor.cpp test_pyarray.cpp + test_pytensor.cpp + test_pyvectorize.cpp ) set(XTENSOR_PYTHON_TARGET test_xtensor_python) diff --git a/test/test_pyvectorize.cpp b/test/test_pyvectorize.cpp new file mode 100644 index 0000000..033e615 --- /dev/null +++ b/test/test_pyvectorize.cpp @@ -0,0 +1,80 @@ +/*************************************************************************** +* Copyright (c) 2016, Johan Mabille and Sylvain Corlay * +* * +* Distributed under the terms of the BSD 3-Clause License. * +* * +* The full license is in the file LICENSE, distributed with this software. * +****************************************************************************/ + +#include "gtest/gtest.h" +#include "test_common.hpp" +#include "xtensor-python/pytensor.hpp" +#include "xtensor-python/pyvectorize.hpp" +#include "pybind11/pybind11.h" +#include "pybind11/numpy.h" + +namespace xt +{ + + double f1(double a, double b) + { + return a + b; + } + + using shape_type = std::vector; + + TEST(pyvectorize, function) + { + auto vecf1 = pyvectorize(f1); + shape_type shape = { 3, 2 }; + pyarray a(shape, 1.5); + pyarray b(shape, 2.3); + pyarray c = vecf1(a, b); + EXPECT_EQ(a(0, 0) + b(0, 0), c(0, 0)); + } + + TEST(pyvectorize, lambda) + { + auto vecf1 = pyvectorize([](double a, double b) { return a + b; }); + shape_type shape = { 3, 2 }; + pyarray a(shape, 1.5); + pyarray b(shape, 2.3); + pyarray c = vecf1(a, b); + EXPECT_EQ(a(0, 0) + b(0, 0), c(0, 0)); + } + + TEST(pyvectorize, complex) + { + using complex_t = std::complex; + shape_type shape = { 3, 2 }; + pyarray a(shape, complex_t(1.2, 2.5)); + auto f = [](const pyarray& t) { + return std::make_tuple(pyvectorize([](complex_t x) { return std::abs(x); })(t), + pyvectorize([](complex_t x) { return std::arg(x); })(t)); + }; + + auto res = f(a); + double expected_abs = std::abs(a(1, 1)); + double expected_arg = std::arg(a(1, 1)); + EXPECT_EQ(expected_abs, std::get<0>(res)(1, 1)); + EXPECT_EQ(expected_arg, std::get<1>(res)(1, 1)); + } + + TEST(pyvectorize, complex_pybind) + { + using complex_t = std::complex; + shape_type shape = { 3, 2 }; + pyarray a(shape, complex_t(1.2, 2.5)); + auto f = [](const pyarray& t) { + return pybind11::make_tuple(pyvectorize([](complex_t x) { return std::abs(x); })(t), + pyvectorize([](complex_t x) { return std::arg(x); })(t)); + }; + + auto res = f(a); + double expected_abs = std::abs(a(1, 1)); + double expected_arg = std::arg(a(1, 1)); + + EXPECT_EQ(expected_abs, res[0].cast>()(1, 1)); + EXPECT_EQ(expected_arg, res[1].cast>()(1, 1)); + } +} From 39c86fe012ad33d4c93eb0d61fadb48256c9c3d9 Mon Sep 17 00:00:00 2001 From: Johan Mabille Date: Mon, 3 Apr 2017 15:36:45 +0200 Subject: [PATCH 2/2] activating c++ test in CI --- .appveyor.yml | 10 +++++++--- .travis.yml | 38 ++++++++++++++++++++++++-------------- test/test-environment.yml | 2 +- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 7ee1c35..4b3b649 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -22,9 +22,13 @@ install: - conda config --set always_yes yes --set changeps1 no - conda update -q conda - conda info -a - - conda install pytest -c conda-forge - - conda install xtensor==0.8.0 pytest numpy pybind11==2.1.0 -c conda-forge - - xcopy /S %APPVEYOR_BUILD_FOLDER%\include %MINICONDA%\include + - conda install gtest cmake -c conda-forge + - conda install xtensor==0.8.1 pytest numpy pybind11==2.1.0 -c conda-forge + - cmake -G "NMake Makefiles" -D CMAKE_INSTALL_PREFIX=%MINICONDA%\\Library -DBUILD_TESTS=ON . + - nmake test_xtensor_python + - nmake install build_script: - py.test -s + - cd test + - .\test_xtensor_python diff --git a/.travis.yml b/.travis.yml index 6d8fef3..e760cb2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,6 +52,20 @@ env: - MINCONDA_VERSION="latest" - MINCONDA_LINUX="Linux-x86_64" - MINCONDA_OSX="MacOSX-x86_64" +before_install: + - | + # Configure build variables + if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then + if [[ "$COMPILER" == "gcc" ]]; then + export CXX=g++-$GCC CC=gcc-$GCC; + fi + if [[ "$COMPILER" == "clang" ]]; then + export CXX=clang++-$CLANG CC=clang-$CLANG; + fi + elif [[ "$TRAVIS_OS_NAME" == "osx" ]]; then + export CXX=clang++ CC=clang PYTHONHOME=$HOME/miniconda; + fi + install: # Define the version of miniconda to download - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then @@ -67,21 +81,17 @@ install: - conda update -q conda # Useful for debugging any issues with conda - conda info -a - - conda install xtensor==0.8.0 pytest numpy pybind11==2.1.0 -c conda-forge - - cp -r $TRAVIS_BUILD_DIR/include/* $HOME/miniconda/include/ + - conda install xtensor==0.8.1 pytest numpy pybind11==2.1.0 -c conda-forge + - cd test + - conda env create -f ./test-environment.yml + - source activate test-xtensor-python + - cd .. + - cmake -DBUILD_TESTS=ON -D CMAKE_INSTALL_PREFIX=$HOME/miniconda . + - make -j2 test_xtensor_python + - make install script: - - | - # Configure build variables - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then - if [[ "$COMPILER" == "gcc" ]]; then - export CXX=g++-$GCC CC=gcc-$GCC; - fi - if [[ "$COMPILER" == "clang" ]]; then - export CXX=clang++-$CLANG CC=clang-$CLANG; - fi - elif [[ "$TRAVIS_OS_NAME" == "osx" ]]; then - export CXX=clang++ CC=clang; - fi - py.test -s + - cd test + - ./test_xtensor_python diff --git a/test/test-environment.yml b/test/test-environment.yml index 2f8c8fb..ba5f70f 100644 --- a/test/test-environment.yml +++ b/test/test-environment.yml @@ -1,4 +1,4 @@ -name: test-xtensor +name: test-xtensor-python channels: - conda-forge - defaults