Skip to content

Commit

Permalink
[test, TAT.hpp] Deprecate old test code, and add new tests using gtest.
Browse files Browse the repository at this point in the history
  • Loading branch information
hzhangxyz committed Aug 20, 2023
1 parent 0068aaa commit d08e63d
Show file tree
Hide file tree
Showing 53 changed files with 106 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
!/include
!/lazy_graph
!/python
!/test
!/tests
!/tetragono
!/tetraku
!/tetraux
Expand Down
11 changes: 6 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,15 @@ endif()
# Add test
option(TAT_BUILD_TEST "Build test of TAT" ON)
if(TAT_BUILD_TEST)
add_custom_target(test_executables)
enable_testing()
file(GLOB CPP_SRC ${CMAKE_CURRENT_SOURCE_DIR}/test/*.cpp)
find_package(GTest REQUIRED)
file(GLOB CPP_SRC ${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp)
foreach(FILE ${CPP_SRC})
get_filename_component(NAME ${FILE} NAME_WE)
add_executable(${NAME} ${FILE})
target_link_libraries(${NAME} PRIVATE TAT)
add_test(build_${NAME} ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target ${NAME})
add_test(${NAME} ${CMAKE_CURRENT_BINARY_DIR}/${NAME} ${CMAKE_CURRENT_SOURCE_DIR}/test/${NAME}.out)
set_tests_properties(${NAME} PROPERTIES DEPENDS build_${NAME})
target_link_libraries(${NAME} TAT GTest::gtest_main)
gtest_discover_tests(${NAME})
add_dependencies(test_executables ${NAME})
endforeach(FILE)
endif()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* c++ compiler with c++17 support(such as GCC 7.3+, Clang 6.0+)
* LAPACK/BLAS or MKL
* MPI(optional for parallel computing)
* [[https://github.com/google/googletest][gtest]](optional for test)
* [[https://github.com/pybind/pybind11][pybind11]](optional for python binding)
* [[https://github.com/numpy/numpy][numpy]](optional in python binding to export data into numpy array)

Expand Down
8 changes: 4 additions & 4 deletions include/TAT/utility/multidimension_span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ namespace TAT {
return offset;
}
public:
template<typename Vector>
template<typename Vector = vector_t>
const T& at(const Vector& indices) const {
return data()[get_offset(indices)];
}
template<typename Vector>
template<typename Vector = vector_t>
T& at(const Vector& indices) {
return data()[get_offset(indices)];
}
Expand Down Expand Up @@ -219,7 +219,7 @@ namespace TAT {
}

public:
template<typename Vector>
template<typename Vector = vector_t>
mdspan<T, U> transpose(const Vector& plan) {
vector_t new_dimensions(rank());
vector_t new_leadings(rank());
Expand All @@ -229,7 +229,7 @@ namespace TAT {
}
return mdspan<T, U>(data(), std::move(new_dimensions), std::move(new_leadings));
}
template<typename Vector>
template<typename Vector = vector_t>
mdspan<const T, U> transpose(const Vector& plan) const {
vector_t new_dimensions(rank());
vector_t new_leadings(rank());
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions tests/test_const_integral.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <TAT/TAT.hpp>
#include <gtest/gtest.h>

TEST(test_const_integral, basic_usage_dynamic) {
const auto value = TAT::to_const_integral_0_to_16(17);
std::visit(
[](const auto& v) {
ASSERT_EQ(v.value(), 17);
ASSERT_TRUE(v.is_dynamic);
},
value);
}

TEST(test_const_integral, basic_usage_static) {
const auto value = TAT::to_const_integral_0_to_16(13);
std::visit(
[](const auto& v) {
ASSERT_EQ(v.value(), 13);
ASSERT_TRUE(v.is_static);
},
value);
}

TEST(test_const_integral, return_value) {
const auto value = TAT::to_const_integral_0_to_16(13);
auto v = std::visit(
[](const auto& v) -> int {
return v.value();
},
value);
ASSERT_EQ(v, 13);
}
62 changes: 62 additions & 0 deletions tests/test_multidimension_span.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <TAT/TAT.hpp>
#include <gtest/gtest.h>

TEST(test_multidimension_span, create_object_and_basic_attribute) {
auto data = std::vector<int>(24);
auto span = TAT::mdspan<int>(data.data(), {2, 3, 4});
ASSERT_EQ(span.rank(), 3);
ASSERT_EQ(span.size(), 24);
ASSERT_EQ(span.dimensions(0), 2);
ASSERT_EQ(span.dimensions(1), 3);
ASSERT_EQ(span.dimensions(2), 4);
ASSERT_EQ(span.leadings(2), 1);
ASSERT_EQ(span.leadings(1), 4);
ASSERT_EQ(span.leadings(0), 12);
ASSERT_EQ(span.dimensions()[0], 2);
ASSERT_EQ(span.dimensions()[1], 3);
ASSERT_EQ(span.dimensions()[2], 4);
ASSERT_EQ(span.leadings()[2], 1);
ASSERT_EQ(span.leadings()[1], 4);
ASSERT_EQ(span.leadings()[0], 12);
ASSERT_EQ(span.data(), data.data());
}

TEST(test_multidimension_span, set_data_later) {
auto span = TAT::mdspan<int>(nullptr, {2, 3, 4});
auto data = std::vector<int>(24);
span.set_data(data.data());
ASSERT_EQ(span.data(), data.data());
const auto const_span = TAT::mdspan<int>(data.data(), {2, 3, 4});
ASSERT_EQ(const_span.data(), data.data());
}

TEST(test_multidimension_span, get_set_item) {
auto data = std::vector<int>(24);
auto span = TAT::mdspan<int>(data.data(), {2, 3, 4});
span.at({0, 0, 0}) = 2333;
ASSERT_EQ(data[0], 2333);
data[13] = 666;
ASSERT_EQ(span.at({1, 0, 1}), 666);
}

TEST(test_multidimension_span, get_item_const) {
auto data = std::vector<int>(24);
const auto span = TAT::mdspan<int>(data.data(), {2, 3, 4});
data[13] = 666;
ASSERT_EQ(span.at({1, 0, 1}), 666);
}

TEST(test_multidimension_span, iterate) {
auto data = std::vector<int>{0, 1, 2, 3, 4, 5, 6, 7};
const auto span = TAT::mdspan<int>(data.data(), {2, 2, 2});
auto v = 0;
for (auto i = span.begin(); i != span.end(); ++i) {
ASSERT_EQ(*i, v++);
}
}

TEST(test_multidimension_span, iterate_on_empty) {
const auto span = TAT::mdspan<int>(nullptr, {2, 0, 2});
for (auto i = span.begin(); i != span.end(); ++i) {
}
}

0 comments on commit d08e63d

Please sign in to comment.