Skip to content

Commit

Permalink
Merge pull request #178 from yungyuc/refactor/gtest-buffer
Browse files Browse the repository at this point in the history
Add google-test version 1.12.1 and make extension initializer cleaner
  • Loading branch information
yungyuc committed Dec 11, 2022
2 parents b9d22ba + 73a9cb4 commit 0a1f538
Show file tree
Hide file tree
Showing 19 changed files with 298 additions and 15 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/devbuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ jobs:
echo "flake8 path: $(which flake8)"
echo "flake8 version: $(flake8 --version)"
- name: make gtest BUILD_QT=OFF
run: |
make gtest \
VERBOSE=1 USE_CLANG_TIDY=OFF \
BUILD_QT=OFF
- name: make buildext BUILD_QT=OFF
run: |
rm -f build/*/Makefile
Expand Down Expand Up @@ -195,6 +201,12 @@ jobs:
echo "flake8 path: $(which flake8)"
echo "flake8 version: $(flake8 --version)"
- name: make gtest BUILD_QT=OFF
run: |
make gtest \
VERBOSE=1 USE_CLANG_TIDY=OFF \
BUILD_QT=OFF
- name: make buildext BUILD_QT=OFF
run: |
rm -f build/*/Makefile
Expand Down Expand Up @@ -313,6 +325,12 @@ jobs:
--config ${{ matrix.cmake_build_type }} `
--target ALL_BUILD
- name: cmake run_gtest
run: |
cmake --build ${{ github.workspace }}/build `
--config ${{ matrix.cmake_build_type }} `
--target run_gtest
- name: cmake run_viewer_pytest
run: |
cmake --build ${{ github.workspace }}/build `
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
matrix:
path:
- 'cpp'
- 'gtests'

steps:

Expand Down
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,11 @@ endif()
add_custom_target(flake8)
flake8("flake8")

set(USE_GOOGLETEST True CACHE BOOL "Build with googletest")
message(STATUS "USE_GOOGLETEST: ${USE_GOOGLETEST}")

if(USE_GOOGLETEST)
add_subdirectory(gtests)
endif() # USE_GOOGLETEST

# vim: set ff=unix fenc=utf8 nobomb et sw=4 ts=4 sts=4:
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,15 @@ pytest: buildext
viewer: cmake
cmake --build $(BUILD_PATH) --target $@ VERBOSE=$(VERBOSE) $(MAKE_PARALLEL)

.PHONY: gtest
gtest: cmake
cmake --build $(BUILD_PATH) --target run_gtest VERBOSE=$(VERBOSE) $(MAKE_PARALLEL)

.PHONY: run_viewer_pytest
run_viewer_pytest: viewer
cmake --build $(BUILD_PATH) --target $@ VERBOSE=$(VERBOSE)

CFFILES = $(shell find cpp -type f -name '*.[ch]pp' | sort)
CFFILES = $(shell find cpp gtests -type f -name '*.[ch]pp' | sort)
ifeq ($(CFCMD),)
ifeq ($(FORCE_CLANG_FORMAT),)
CFCMD = clang-format --dry-run
Expand Down
2 changes: 1 addition & 1 deletion cmake/Flake8.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ macro(flake8 target_name)
add_custom_command(TARGET ${target_name}
PRE_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${flake8_BIN} . --exclude thirdparty,tmp
COMMAND ${flake8_BIN} . --exclude thirdparty,tmp,_deps
COMMENT "Running flake8 on ${CMAKE_CURRENT_SOURCE_DIR} ...")
endmacro()
1 change: 1 addition & 0 deletions cpp/modmesh/buffer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(MODMESH_BUFFER_PYMODHEADERS
CACHE FILEPATH "" FORCE)

set(MODMESH_BUFFER_PYMODSOURCES
${CMAKE_CURRENT_SOURCE_DIR}/pymod/buffer_pymod.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pymod/wrap_ConcreteBuffer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pymod/wrap_SimpleArray.cpp
CACHE FILEPATH "" FORCE)
Expand Down
5 changes: 5 additions & 0 deletions cpp/modmesh/buffer/SimpleArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@

#include <stdexcept>

#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif

namespace modmesh
{

Expand Down
64 changes: 64 additions & 0 deletions cpp/modmesh/buffer/pymod/buffer_pymod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2019, Yung-Yu Chen <yyc@solvcon.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <modmesh/toggle/pymod/toggle_pymod.hpp> // Must be the first include.
#include <modmesh/buffer/pymod/buffer_pymod.hpp>

namespace modmesh
{

namespace python
{

struct buffer_pymod_tag;

template <>
OneTimeInitializer<buffer_pymod_tag> & OneTimeInitializer<buffer_pymod_tag>::me()
{
static OneTimeInitializer<buffer_pymod_tag> instance;
return instance;
}

void initialize_buffer(pybind11::module & mod)
{
auto initialize_impl = [](pybind11::module & mod)
{
import_numpy();

wrap_ConcreteBuffer(mod);
wrap_SimpleArray(mod);
};

OneTimeInitializer<buffer_pymod_tag>::me()(mod, initialize_impl);
}

} /* end namespace python */

} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 nobomb et sw=4 ts=4 sts=4:
1 change: 1 addition & 0 deletions cpp/modmesh/buffer/pymod/buffer_pymod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace modmesh
namespace python
{

void initialize_buffer(pybind11::module & mod);
void wrap_ConcreteBuffer(pybind11::module & mod);
void wrap_SimpleArray(pybind11::module & mod);

Expand Down
1 change: 1 addition & 0 deletions cpp/modmesh/mesh/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(MODMESH_MESH_PYMODHEADERS
CACHE FILEPATH "" FORCE)

set(MODMESH_MESH_PYMODSOURCES
${CMAKE_CURRENT_SOURCE_DIR}/pymod/mesh_pymod.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pymod/wrap_StaticGrid.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pymod/wrap_StaticMesh.cpp
CACHE FILEPATH "" FORCE)
Expand Down
62 changes: 62 additions & 0 deletions cpp/modmesh/mesh/pymod/mesh_pymod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2019, Yung-Yu Chen <yyc@solvcon.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <modmesh/toggle/pymod/toggle_pymod.hpp> // Must be the first include.
#include <modmesh/buffer/pymod/buffer_pymod.hpp>

namespace modmesh
{

namespace python
{

struct mesh_pymod_tag;

template <>
OneTimeInitializer<mesh_pymod_tag> & OneTimeInitializer<mesh_pymod_tag>::me()
{
static OneTimeInitializer<mesh_pymod_tag> instance;
return instance;
}

void initialize_mesh(pybind11::module & mod)
{
auto initialize_impl = [](pybind11::module & mod)
{
wrap_StaticGrid(mod);
wrap_StaticMesh(mod);
};

OneTimeInitializer<mesh_pymod_tag>::me()(mod, initialize_impl);
}

} /* end namespace python */

} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 nobomb et sw=4 ts=4 sts=4:
1 change: 1 addition & 0 deletions cpp/modmesh/mesh/pymod/mesh_pymod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace modmesh
namespace python
{

void initialize_mesh(pybind11::module & mod);
void wrap_StaticGrid(pybind11::module & mod);
void wrap_StaticMesh(pybind11::module & mod);

Expand Down
6 changes: 5 additions & 1 deletion cpp/modmesh/python/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <modmesh/python/python.hpp> // Must be the first include.
#include <modmesh/python/module.hpp>
#include <modmesh/toggle/pymod/toggle_pymod.hpp>
#include <modmesh/buffer/pymod/buffer_pymod.hpp>
#include <modmesh/mesh/pymod/mesh_pymod.hpp>
#include <modmesh/onedim/pymod/onedim_pymod.hpp>
#include <modmesh/spacetime/pymod/spacetime_pymod.hpp>
#ifdef QT_CORE_LIB
Expand All @@ -36,7 +38,9 @@ namespace python

void initialize(pybind11::module_ mod)
{
initialize_modmesh(mod);
initialize_toggle(mod);
initialize_buffer(mod);
initialize_mesh(mod);
pybind11::module_ spacetime_mod = mod.def_submodule("spacetime", "spacetime");
initialize_spacetime(spacetime_mod);
pybind11::module_ onedim_mod = mod.def_submodule("onedim", "onedim");
Expand Down
16 changes: 5 additions & 11 deletions cpp/modmesh/toggle/pymod/toggle_pymod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,24 @@ namespace modmesh
namespace python
{

struct modmesh_pymod_tag;
struct toggle_pymod_tag;

template <>
OneTimeInitializer<modmesh_pymod_tag> & OneTimeInitializer<modmesh_pymod_tag>::me()
OneTimeInitializer<toggle_pymod_tag> & OneTimeInitializer<toggle_pymod_tag>::me()
{
static OneTimeInitializer<modmesh_pymod_tag> instance;
static OneTimeInitializer<toggle_pymod_tag> instance;
return instance;
}

void initialize_modmesh(pybind11::module & mod)
void initialize_toggle(pybind11::module & mod)
{
auto initialize_impl = [](pybind11::module & mod)
{
import_numpy();

wrap_profile(mod);
wrap_ConcreteBuffer(mod);
wrap_SimpleArray(mod);
wrap_StaticGrid(mod);
wrap_StaticMesh(mod);
wrap_Toggle(mod);
};

OneTimeInitializer<modmesh_pymod_tag>::me()(mod, initialize_impl);
OneTimeInitializer<toggle_pymod_tag>::me()(mod, initialize_impl);
}

} /* end namespace python */
Expand Down
2 changes: 1 addition & 1 deletion cpp/modmesh/toggle/pymod/toggle_pymod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace modmesh
namespace python
{

void initialize_modmesh(pybind11::module & mod);
void initialize_toggle(pybind11::module & mod);
void wrap_profile(pybind11::module & mod);
void wrap_StaticGrid(pybind11::module & mod);
void wrap_StaticMesh(pybind11::module & mod);
Expand Down
35 changes: 35 additions & 0 deletions gtests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (c) 2022, Yung-Yu Chen <yyc@solvcon.net>
# BSD-style license; see COPYING

cmake_minimum_required(VERSION 3.16)

# See https://google.github.io/googletest/quickstart-cmake.html for the
# recommended cmake configuration
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/release-1.12.1.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP ON
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(
test_nopython
test_nopython_buffer.cpp
test_nopython_modmesh.cpp
)
target_link_libraries(
test_nopython
GTest::gtest_main
)

include(GoogleTest)
gtest_discover_tests(test_nopython)

add_custom_target(run_gtest
COMMAND $<TARGET_FILE:test_nopython>
DEPENDS test_nopython)
23 changes: 23 additions & 0 deletions gtests/test_nopython_buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <modmesh/buffer/buffer.hpp>

#include <gtest/gtest.h>

#ifdef Py_PYTHON_H
#error "Python.h should not be included."
#endif

TEST(nopython_buffer, dummy)
{
EXPECT_TRUE(true);
}

TEST(SimpleArray, construction)
{
namespace mm = modmesh;
mm::SimpleArray<double> arr_double(10);
EXPECT_EQ(arr_double.nbody(), 10);
mm::SimpleArray<int> arr_int(17);
EXPECT_EQ(arr_int.nbody(), 17);
}

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
14 changes: 14 additions & 0 deletions gtests/test_nopython_modmesh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <modmesh/modmesh.hpp>

#include <gtest/gtest.h>

#ifdef Py_PYTHON_H
#error "Python.h should not be included."
#endif

TEST(nopython_modmesh, dummy)
{
EXPECT_TRUE(true);
}

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
Loading

0 comments on commit 0a1f538

Please sign in to comment.