Skip to content

Commit 0b713fb

Browse files
committed
More tests
1 parent 9db24fc commit 0b713fb

File tree

3 files changed

+71
-22
lines changed

3 files changed

+71
-22
lines changed

include/aws/crt/Variant.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,12 +668,16 @@ namespace Aws
668668
using EnableIfOtherIsThisVariantAlternative = typename std::
669669
enable_if<VariantDetail::Checker::HasType<typename std::decay<OtherT>::type, Ts...>::value, int>::type;
670670

671+
using FirstAlternative = typename ThisVariantAlternative<0>::type;
672+
673+
static constexpr bool isFirstAlternativeNothrowDefaultConstructible =
674+
std::is_nothrow_default_constructible<FirstAlternative>::value;
675+
671676
public:
672677
using IndexT = VariantDetail::Index::VariantIndex;
673678
static constexpr std::size_t AlternativeCount = sizeof...(Ts);
674679

675-
// TODO Check NoDefaultConstructible.
676-
Variant() = default;
680+
Variant() noexcept(isFirstAlternativeNothrowDefaultConstructible) = default;
677681

678682
template <typename T, EnableIfOtherIsThisVariantAlternative<T> = 1> Variant(const T &val) : m_variant(val)
679683
{

tests/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ add_test_case(VariantConstructor)
108108
add_test_case(VariantAssignmentOperator)
109109
add_test_case(VariantWithMoveOnlyUnderlyingType)
110110
add_test_case(VariantWithCopyOnlyUnderlyingType)
111-
add_test_case(VariantNoexceptConstructible)
111+
add_test_case(VariantWithNoDefaultConstructibleUnderlyingType)
112+
add_test_case(VariantNothrowConstructible)
113+
add_test_case(VariantThrowConstructible)
112114
add_test_case(VariantEmplace)
113115
add_test_case(VariantVisitor)
114116
add_test_case(StreamTestCreateDestroyWrapper)

tests/VariantTest.cpp

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#include <aws/crt/Variant.h>
77
#include <aws/testing/aws_test_harness.h>
88

9+
#if defined(_WIN32)
10+
# define AWS_VARIANTTEST_WINDOWS_API __declspec(dllexport)
11+
#else
12+
# define AWS_VARIANTTEST_WINDOWS_API
13+
#endif
14+
915
const char *s_variant_test_str = "This is a string, that should be long enough to avoid small string optimizations";
1016

1117
static int s_VariantBasicOperandsCompile(struct aws_allocator *allocator, void *ctx)
@@ -241,13 +247,6 @@ static int s_VariantWithMoveOnlyUnderlyingType(struct aws_allocator *allocator,
241247
* The __declspec(dllexport) directive exports class member function on Windows platform. We enable it when
242248
* building shared libraries. In the past, this directive caused msvc to generate special copy members for classes
243249
* containing Crt::Variant with move-only underlying types, which led to compile-time errors. */
244-
245-
#if defined(_WIN32)
246-
# define AWS_VARIANTTEST_WINDOWS_API __declspec(dllexport)
247-
#else
248-
# define AWS_VARIANTTEST_WINDOWS_API
249-
#endif
250-
251250
struct AWS_VARIANTTEST_WINDOWS_API MoveOnlyVariantTestResult
252251
{
253252
MoveOnlyVariant m_result;
@@ -283,13 +282,6 @@ static int s_VariantWithCopyOnlyUnderlyingType(struct aws_allocator *allocator,
283282
* The __declspec(dllexport) directive exports class member function on Windows platform. We enable it when
284283
* building shared libraries. In the past, this directive caused msvc to generate special copy members for classes
285284
* containing Crt::Variant with copy-only underlying types, which led to compile-time errors. */
286-
287-
#if defined(_WIN32)
288-
# define AWS_VARIANTTEST_WINDOWS_API __declspec(dllexport)
289-
#else
290-
# define AWS_VARIANTTEST_WINDOWS_API
291-
#endif
292-
293285
struct AWS_VARIANTTEST_WINDOWS_API CopyOnlyVariantTestResult
294286
{
295287
CopyOnlyVariant m_result;
@@ -300,25 +292,76 @@ static int s_VariantWithCopyOnlyUnderlyingType(struct aws_allocator *allocator,
300292

301293
AWS_TEST_CASE(VariantWithCopyOnlyUnderlyingType, s_VariantWithCopyOnlyUnderlyingType)
302294

303-
static int s_VariantNoexceptConstructible(struct aws_allocator *allocator, void *ctx)
295+
// Test Variant with underlying type without default constructor.
296+
// If it compiles, it's considered success.
297+
static int s_VariantWithNoDefaultConstructibleUnderlyingType(struct aws_allocator *allocator, void *ctx)
304298
{
305299
(void)ctx;
306300

307301
Aws::Crt::ApiHandle apiHandle(allocator);
308302

309-
struct NothorwConstructibleTestType
303+
struct NoDefaultConstructibleTestType
310304
{
311-
NothorwConstructibleTestType() noexcept = default;
305+
explicit NoDefaultConstructibleTestType(int) {}
306+
NoDefaultConstructibleTestType() = delete;
312307
};
313308

314-
using NothrowConstructibleVariant = Aws::Crt::Variant<NothorwConstructibleTestType>;
309+
using NoDefaultConstructibleVariant = Aws::Crt::Variant<NoDefaultConstructibleTestType>;
315310

311+
/* Regression test.
312+
* The __declspec(dllexport) directive exports class member function on Windows platform. We enable it when
313+
* building shared libraries. In the past, this directive caused msvc to generate special copy members for classes
314+
* containing Crt::Variant with copy-only underlying types, which led to compile-time errors. */
315+
struct AWS_VARIANTTEST_WINDOWS_API NoDefaultConstructibleVariantTestResult
316+
{
317+
NoDefaultConstructibleVariant m_result;
318+
};
319+
320+
NoDefaultConstructibleTestType testType(1);
321+
NoDefaultConstructibleVariant variant(testType);
322+
323+
return AWS_OP_SUCCESS;
324+
}
325+
326+
AWS_TEST_CASE(VariantWithNoDefaultConstructibleUnderlyingType, s_VariantWithNoDefaultConstructibleUnderlyingType)
327+
328+
static int s_VariantNothrowConstructible(struct aws_allocator *allocator, void *ctx)
329+
{
330+
(void)ctx;
331+
332+
Aws::Crt::ApiHandle apiHandle(allocator);
333+
334+
struct NothrowConstructibleTestType
335+
{
336+
NothrowConstructibleTestType() noexcept = default;
337+
};
338+
using NothrowConstructibleVariant = Aws::Crt::Variant<NothrowConstructibleTestType>;
316339
ASSERT_INT_EQUALS(1, std::is_nothrow_constructible<NothrowConstructibleVariant>::value);
317340

318341
return AWS_OP_SUCCESS;
319342
}
320343

321-
AWS_TEST_CASE(VariantNoexceptConstructible, s_VariantNoexceptConstructible)
344+
AWS_TEST_CASE(VariantNothrowConstructible, s_VariantNothrowConstructible)
345+
346+
static int s_VariantThrowConstructible(struct aws_allocator *allocator, void *ctx)
347+
{
348+
(void)ctx;
349+
350+
Aws::Crt::ApiHandle apiHandle(allocator);
351+
352+
struct ThrowConstructibleTestType
353+
{
354+
// Must be user-defined to be non-nothrow.
355+
ThrowConstructibleTestType() {}
356+
};
357+
358+
using ThrowConstructibleVariant = Aws::Crt::Variant<ThrowConstructibleTestType>;
359+
ASSERT_INT_EQUALS(0, std::is_nothrow_constructible<ThrowConstructibleVariant>::value);
360+
361+
return AWS_OP_SUCCESS;
362+
}
363+
364+
AWS_TEST_CASE(VariantThrowConstructible, s_VariantThrowConstructible)
322365

323366
struct TestStringOnlyVisitor
324367
{

0 commit comments

Comments
 (0)