Skip to content

Commit

Permalink
Optional test (#13)
Browse files Browse the repository at this point in the history
* Add flag

* Update optional_test.cc

* Update cmake-multi-platform.yml

* Fix std::tuple_xxx on clang

* Update cmake-multi-platform.yml

* Update cmake-multi-platform.yml

* fix

* Update CMakeLists.txt

* Update specialize_tuple.h

* Update specialize_tuple.h

* Update specialize_tuple.h

* Update specialize_tuple.h

* Update specialize_tuple.h

* Update specialize_tuple.h

* Fix optional and add test

* Update optional_test.cc

* Update CMakeLists.txt

* Update CMakeLists.txt

* Update optional_test.cc

* Update optional.h
  • Loading branch information
lackhole authored Jun 20, 2024
1 parent b30484f commit db1a0c9
Show file tree
Hide file tree
Showing 8 changed files with 443 additions and 32 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,23 @@ jobs:
os: [ubuntu-latest]
build_type: [Debug, Release]
compiler: [clang]
compiler_version: [13, 14, 15]
compiler_version: [11, 12, 13, 14, 15]
include:
- cc_compiler: clang
- cxx_compiler: clang++

steps:
- uses: actions/checkout@v4

- name: Set up Clang
uses: egor-tensin/setup-clang@v1
with:
version: ${{ matrix.compiler_version }}

- name: Configure CMake
env:
CC: ${{ matrix.cc_compiler }}-${{ matrix.compiler_version }}
CXX: ${{ matrix.cxx_compiler }}-${{ matrix.compiler_version }}
CC: cc
CXX: c++
run: >
cmake -B build
-DCMAKE_CXX_COMPILER=${{ env.CXX }}
Expand Down
24 changes: 13 additions & 11 deletions include/preview/__optional/optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct optional_storage {
, has_value(false) {}

template<typename... Args>
constexpr optional_storage(in_place_t, Args&&... args)
constexpr explicit optional_storage(in_place_t, Args&&... args)
: value(std::forward<Args>(args)...)
, has_value(true) {}

Expand All @@ -58,14 +58,11 @@ struct optional_storage<T, false> {
, has_value(false) {}

template<typename... Args>
constexpr optional_storage(in_place_t, Args&&... args)
constexpr explicit optional_storage(in_place_t, Args&&... args)
: value(std::forward<Args>(args)...)
, has_value(true) {}

PREVIEW_CONSTEXPR_AFTER_CXX20 ~optional_storage() {
if (has_value)
preview::destroy_at(std::addressof(value));
}
PREVIEW_CONSTEXPR_AFTER_CXX20 ~optional_storage() noexcept {}

union {
char null;
Expand Down Expand Up @@ -135,7 +132,9 @@ struct optional_base {
}

// control-special
constexpr void construct_from(optional_base&& other) {
constexpr void construct_from(optional_base&& other)
noexcept(std::is_nothrow_move_constructible<T>::value)
{
if (other.has_value())
construct_with(std::move(*other));
}
Expand All @@ -153,7 +152,10 @@ struct optional_base {
}

// control-special
constexpr void assign_from(optional_base&& other) {
constexpr void assign_from(optional_base&& other)
noexcept(std::is_nothrow_move_assignable<T>::value &&
std::is_nothrow_move_constructible<T>::value)
{
if (!other.has_value()) {
reset();
} else {
Expand Down Expand Up @@ -382,7 +384,7 @@ class optional : private detail::optional_control_smf<T> {
>::value, int> = 0>
optional& operator=(U&& value) {
if (has_value()) {
this->val = std::forward<U>(value);
**this = std::forward<U>(value);
} else {
this->construct_with(std::forward<U>(value));
}
Expand All @@ -398,7 +400,7 @@ class optional : private detail::optional_control_smf<T> {
optional& operator=(const optional<U>& other) {
if (other.has_value()) {
if (this->has_value())
this->val = *other;
**this = *other;
else
this->construct_with(*other);
} else { // !other.has_value()
Expand All @@ -417,7 +419,7 @@ class optional : private detail::optional_control_smf<T> {
optional& operator=(optional<U>&& other) {
if (other.has_value()) {
if (this->has_value())
this->val = std::move(*other);
**this = std::move(*other);
else
this->construct_with(std::move(*other));
} else { // !other.has_value()
Expand Down
20 changes: 11 additions & 9 deletions include/preview/__ranges/subrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "preview/__ranges/sized_range.h"
#include "preview/__ranges/subrange_kind.h"
#include "preview/__ranges/view_interface.h"
#include "preview/__tuple/specialize_tuple.h"
#include "preview/__tuple/tuple_like.h"
#include "preview/__type_traits/bool_constant.h"
#include "preview/__type_traits/conjunction.h"
Expand Down Expand Up @@ -388,20 +389,21 @@ struct tuple_like_uncvref<ranges::subrange<I, S, K>> : std::true_type {};
} // namespace preview

template<typename I, typename S, preview::ranges::subrange_kind K>
struct std::tuple_size<preview::ranges::subrange<I, S, K>> : std::integral_constant<std::size_t, 2> {};

namespace std {

using ::preview::ranges::get;
PREVIEW_SPECIALIZE_STD_TUPLE_SIZE(preview::ranges::subrange<I, S, K>)
: public std::integral_constant<std::size_t, 2> {};

template<typename I, typename S, preview::ranges::subrange_kind K>
struct tuple_element<0, preview::ranges::subrange<I, S, K>> { using type = I; };
PREVIEW_SPECIALIZE_STD_TUPLE_ELEMENT(0, preview::ranges::subrange<I, S, K>) { using type = I; };
template<typename I, typename S, preview::ranges::subrange_kind K>
struct tuple_element<0, const preview::ranges::subrange<I, S, K>> { using type = I; };
PREVIEW_SPECIALIZE_STD_TUPLE_ELEMENT(0, const preview::ranges::subrange<I, S, K>) { using type = I; };
template<typename I, typename S, preview::ranges::subrange_kind K>
struct tuple_element<1, preview::ranges::subrange<I, S, K>> { using type = S; };
PREVIEW_SPECIALIZE_STD_TUPLE_ELEMENT(1, preview::ranges::subrange<I, S, K>) { using type = S; };
template<typename I, typename S, preview::ranges::subrange_kind K>
struct tuple_element<1, const preview::ranges::subrange<I, S, K>> { using type = S; };
PREVIEW_SPECIALIZE_STD_TUPLE_ELEMENT(1, const preview::ranges::subrange<I, S, K>) { using type = S; };

namespace std {

using ::preview::ranges::get;

} // namespace std

Expand Down
32 changes: 32 additions & 0 deletions include/preview/__tuple/specialize_tuple.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#/**
# * Created by YongGyu Lee on 2024. 6. 20..
# */
#
#ifndef PREVIEW_TUPLE_SPECIALIZE_TUPLE_H_
#define PREVIEW_TUPLE_SPECIALIZE_TUPLE_H_
#
#include "preview/__core/android_version.h"
#
#define PREVIEW_STD_TUPLE_SIZE_STRUCT struct
#define PREVIEW_STD_TUPLE_ELEMENT_STRUCT struct
#
#if defined(__clang__)
# if (__clang_major__ < 8) || (defined(PREVIEW_NDK_VERSION_MAJOR) && PREVIEW_NDK_VERSION_MAJOR < 20)
# undef PREVIEW_STD_TUPLE_SIZE_STRUCT
# define PREVIEW_STD_TUPLE_SIZE_STRUCT class
# endif
# if __clang_major__ < 9
# undef PREVIEW_STD_TUPLE_ELEMENT_STRUCT
# define PREVIEW_STD_TUPLE_ELEMENT_STRUCT class
# endif
#endif
#
#include <tuple>

#define PREVIEW_SPECIALIZE_STD_TUPLE_SIZE(...) \
PREVIEW_STD_TUPLE_SIZE_STRUCT std::tuple_size<__VA_ARGS__>

#define PREVIEW_SPECIALIZE_STD_TUPLE_ELEMENT(...) \
PREVIEW_STD_TUPLE_ELEMENT_STRUCT std::tuple_element<__VA_ARGS__>

# endif // PREVIEW_TUPLE_SPECIALIZE_TUPLE_H_
10 changes: 6 additions & 4 deletions include/preview/__utility/compressed_pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# include <utility>
#
# include "preview/__core/inline_variable.h"
# include "preview/__tuple/specialize_tuple.h"
# include "preview/__type_traits/conjunction.h"
# include "preview/__type_traits/is_swappable.h"

Expand Down Expand Up @@ -130,11 +131,12 @@ struct compressed_pair_getter<1> {

} // namespace preview

namespace std {
template<typename T, typename U> PREVIEW_SPECIALIZE_STD_TUPLE_SIZE(preview::compressed_pair<T, U>)
: public std::integral_constant<std::size_t, 2> {};
template<typename T, typename U> PREVIEW_SPECIALIZE_STD_TUPLE_ELEMENT(0, preview::compressed_pair<T, U>) { using type = T; };
template<typename T, typename U> PREVIEW_SPECIALIZE_STD_TUPLE_ELEMENT(1, preview::compressed_pair<T, U>) { using type = U; };

template<typename T, typename U> struct tuple_element<0, preview::compressed_pair<T, U>> { using type = T; };
template<typename T, typename U> struct tuple_element<1, preview::compressed_pair<T, U>> { using type = U; };
template<typename T, typename U> struct tuple_size<preview::compressed_pair<T, U>> : std::integral_constant<std::size_t, 2> {};
namespace std {

template<std::size_t I, typename T, typename U>
constexpr tuple_element_t<I, preview::compressed_pair<T, U>>& get(preview::compressed_pair<T, U>& p) noexcept {
Expand Down
28 changes: 24 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ int main() { return 0; }")
endif ()
endmacro()

macro(target_compile_option_if_possible target flag)
check_cxx_compiler_flag("${flag}" CHECK_CXX_FLAG_${flag})
if (CHECK_CXX_FLAG_${flag})
target_compile_options(${target} PRIVATE "${flag}")
endif()
endmacro()

preview_check_compiler_support_std(14 PREVIEW_COMPILER_SUPPORT_CXX14)
if (NOT PREVIEW_COMPILER_SUPPORT_CXX14)
message(FATAL_ERROR "Compiler must support C++14")
Expand Down Expand Up @@ -110,12 +117,25 @@ function(preview_add_multi_version_test name_prefix test_src)
# COMMAND ${ADB} shell \"export LD_LIBRARY_PATH=/data/local/tmp/${ANDROID_ABI}\; /data/local/tmp/${ANDROID_ABI}/${name_prefix}_${std}\")
else()
if (EMSCRIPTEN)
target_link_libraries(${name_prefix}_${std} "-sEXIT_RUNTIME=1 -sALLOW_MEMORY_GROWTH=1")
elseif (MSVC)
target_compile_options(${name_prefix}_${std} PRIVATE "/bigobj" PRIVATE "/WX")
target_link_libraries(${name_prefix}_${std} "-sEXIT_RUNTIME=1 -sALLOW_MEMORY_GROWTH=1 -Wno-pthreads-mem-growth -fexceptions")
endif()
gtest_discover_tests(${name_prefix}_${std})
gtest_discover_tests(${name_prefix}_${std} DISCOVERY_TIMEOUT 60)
endif()

if (EMSCRIPTEN)
target_compile_options(${name_prefix}_${std}
PUBLIC "-fexceptions"
)
elseif (MSVC)
target_compile_options(${name_prefix}_${std}
PRIVATE "/bigobj"
PRIVATE "/WX")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(${name_prefix}_${std}
PRIVATE "-Wall"
PRIVATE "-Werror"
PRIVATE "-Wno-unused-local-typedef")
endif ()
endforeach ()
endfunction()

Expand Down
5 changes: 5 additions & 0 deletions test/gtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

#include <gtest/gtest.h>

#include <type_traits>

#include "preview/__core/std_version.h"

#define VERSION_CAT2(x, y) x ## _CXXSTD_ ## y
#define VERSION_CAT(x, y) VERSION_CAT2(x, y)

#define VERSIONED(name) VERSION_CAT(name, PREVIEW_CXX_VERSION)

#define EXPECT_EQ_TYPE(type1, type2) \
EXPECT_TRUE((std::is_same<type1, type2>::value))

#endif //PREVIEW_TEST_GTEST_H_
Loading

0 comments on commit db1a0c9

Please sign in to comment.