-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptional-fun.t.cpp
More file actions
126 lines (96 loc) · 3.19 KB
/
optional-fun.t.cpp
File metadata and controls
126 lines (96 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// Copyright 2014-2017 by Martin Moene
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// optional-fun is inspired by:
// - Simon Brand. Functional exceptionless error-handling with optional and expected. 30 November 2017.
// - Vittorio Romeo. scelta: (experimental) Syntactic sugar for variant and optional types. GitHub
#include "optional-fun-main.t.hpp"
using namespace nonstd;
namespace {
//
// Functional extension:
//
int double_int( int arg ) { return 2 * arg; }
void voider_int( int /*arg*/ ) {}
CASE( "optional map(f): non-void" "[functional]")
{
const int v = 21;
EXPECT( 2*v == (optional<int>(v) | map( double_int )).value() );
#if optional_CPP11_OR_GREATER
// EXPECT( 2*v == optional<int>(v) | map( [](int arg) {return 2*arg; } ).value() );
#endif
}
CASE( "optional map(f): void" "[functional]")
{
using nonstd::optfun_lite::monostate;
const int v = 21;
EXPECT( monostate() == (optional<int>(v) | map( voider_int )).value() );
#if optional_CPP11_OR_GREATER
// EXPECT( monostate() == optional<int>(v) | map( [](int arg) -> void {} ).value() );
#endif
}
CASE( "optional map_or(f): " "[functional]")
{
EXPECT( 14 == (optional<int>( 7 ) | map_or( double_int, 42 )) );
EXPECT( 42 == (optional<int>( ) | map_or( double_int, 42 )) );
}
int seven() { return 7; }
CASE( "optional map_or_else(f): " "[functional]")
{
EXPECT( 14 == (optional<int>( 7 ) | map_or_else( double_int, seven )) );
EXPECT( 7 == (optional<int>( ) | map_or_else( double_int, seven )) );
}
optional<int> double_opt( int arg ) { return 2 * arg; }
optional<int> fail_opt ( int /*arg*/ ) { return nullopt; }
CASE( "optional and_then(f): success" "[functional]")
{
const int v = 21;
EXPECT( 2*v == (optional<int>(v) | then( double_opt )).value() );
EXPECT( 2*v == (optional<int>(v) | and_then( double_opt )).value() );
}
CASE( "optional and_then(f): fail" "[functional]")
{
EXPECT_NOT( (optional<int>(7)
| and_then( double_opt )
| and_then( fail_opt )
| and_then( double_opt )).has_value() );
}
int fun_or_else_nonvoid() { return 7; }
void fun_or_else_void() {}
CASE( "optional or_else(f): non-void" "[functional]")
{
EXPECT( -1 == (optional<int>(-1) | or_else( fun_or_else_nonvoid )).value() );
EXPECT( fun_or_else_nonvoid() == (optional<int>( ) | or_else( fun_or_else_nonvoid )).value() );
}
CASE( "optional or_else(f): void" "[functional]")
{
EXPECT( -1 == (optional<int>(-1) | or_else( fun_or_else_void )).value() );
EXPECT_NOT( (optional<int>( ) | or_else( fun_or_else_void )).has_value() );
}
CASE( "optional and_(u): " "[functional]")
{
EXPECT( 42 == (optional<int>(7) | and_( 42 )).value() );
EXPECT_NOT( (optional<int>( ) | and_( 42 )).has_value() );
}
CASE( "optional or_(rhs): " "[functional]")
{
EXPECT( 7 == (optional<int>(7) | or_( 42 )) );
EXPECT( 42 == (optional<int>( ) | or_( 42 )) );
}
//CASE( "optional take(): " "[functional]")
//{
//}
//
// Negative tests:
//
//
// Tests that print information:
//
//
// Issues:
//
} // anonymous namespace
// end of file