Skip to content

Commit bb64693

Browse files
committed
cmake: unit tests
1 parent ee59d3e commit bb64693

File tree

6 files changed

+58
-22
lines changed

6 files changed

+58
-22
lines changed

build/cmake/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ myci_declare_library(${name}
2424
DEPENDENCIES
2525
utki
2626
)
27+
28+
# ==============
29+
# = unit tests =
30+
31+
include(unit_tests.cmake)

build/cmake/unit_tests.cmake

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# if the library is compiled by vcpkg during the port build (i.e. during the package installation),
2+
# then we don't need to build unit tests
3+
if(IS_VCPKG_PORT_BUILD)
4+
return()
5+
endif()
6+
7+
# no unit tests for ios
8+
if(IOS)
9+
return()
10+
endif()
11+
12+
set(tests_srcs)
13+
myci_add_source_files(tests_srcs
14+
DIRECTORY
15+
${CMAKE_CURRENT_LIST_DIR}/../../tests/unit/src
16+
RECURSIVE
17+
)
18+
19+
myci_declare_application(${PROJECT_NAME}-tests
20+
SOURCES
21+
${tests_srcs}
22+
DEPENDENCIES
23+
tst
24+
r4
25+
)
26+
27+
myci_declare_test(
28+
RUN_TARGET
29+
run-${PROJECT_NAME}-tests
30+
)

build/vcpkg/portfile.cmake.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ vcpkg_from_github(
1010

1111
vcpkg_cmake_configure(
1212
SOURCE_PATH "${SOURCE_PATH}/build/cmake"
13+
OPTIONS
14+
-D IS_VCPKG_PORT_BUILD=True
1315
)
1416

1517
vcpkg_cmake_install()

build/vcpkg/test/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ set(CMAKE_TOOLCHAIN_FILE $ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake)
44

55
project(test)
66

7+
find_package(myci CONFIG REQUIRED)
8+
find_package(tst CONFIG REQUIRED)
79
find_package(r4 CONFIG REQUIRED)
810

9-
add_executable(test main.cpp)
10-
11-
target_link_libraries(test PRIVATE r4::r4)
11+
include(../../cmake/unit_tests.cmake)

build/vcpkg/test/main.cpp

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/r4/vector.hpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ class vector :
429429
* @return Reference to this vector.
430430
*/
431431
template <typename unary_operation_type>
432-
vector& comp_operation(unary_operation_type op)
432+
constexpr vector& comp_operation(unary_operation_type op)
433433
{
434434
std::transform(
435435
this->begin(), //
@@ -449,9 +449,18 @@ class vector :
449449
* @return Reference to this vector.
450450
*/
451451
template <typename binary_operation_type>
452-
vector& comp_operation(const vector& vec, binary_operation_type op)
452+
constexpr vector& comp_operation(
453+
const vector& vec, //
454+
binary_operation_type op
455+
)
453456
{
454-
std::transform(this->begin(), this->end(), vec.begin(), this->begin(), op);
457+
std::transform(
458+
this->begin(), //
459+
this->end(),
460+
vec.begin(),
461+
this->begin(),
462+
op
463+
);
455464
return *this;
456465
}
457466

@@ -689,7 +698,7 @@ class vector :
689698
* @param num - scalar to divide by.
690699
* @return Vector resulting from division of this vector by scalar.
691700
*/
692-
vector operator/(component_type num) const noexcept
701+
constexpr vector operator/(component_type num) const noexcept
693702
{
694703
return vector(*this) /= num;
695704
}
@@ -711,11 +720,12 @@ class vector :
711720
* @param num - scalar to divide by.
712721
* @return Reference to this vector object.
713722
*/
714-
vector& operator/=(component_type num) noexcept
723+
constexpr vector& operator/=(component_type num) noexcept
715724
{
716-
ASSERT(num != 0, [&](auto& o) {
717-
o << "vector::operator/=(): division by 0";
718-
})
725+
// TODO: uncomment when there is a solution to have the assertion in constexpr context
726+
// utki::assert(num != 0, [&](auto& o) {
727+
// o << "vector::operator/=(): division by 0";
728+
// });
719729
return this->comp_operation([&num](auto& a) {
720730
return a / num;
721731
});

0 commit comments

Comments
 (0)