forked from jalal70/readium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptional_tests.cpp
More file actions
195 lines (155 loc) · 5.51 KB
/
optional_tests.cpp
File metadata and controls
195 lines (155 loc) · 5.51 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// optional_tests.cpp
// ePub3
//
// Created by Jim Dovey on 12/12/2013.
//
// Copyright (c) 2014 Readium Foundation and/or its licensees. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// 3. Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
#include "../ePUb3/utilities/optional.h"
#include <string>
#include <vector>
#include "catch.hpp"
using namespace EPUB3_NAMESPACE;
TEST_CASE("default-constructed std::optional<> == nullopt", "[std::optional]")
{
optional<std::string> opt;
REQUIRE(opt == nullopt);
REQUIRE_THROWS_AS(opt.value(), bad_optional_access);
}
TEST_CASE("std::optional can be assigned a value and nullopt", "[std::optional]")
{
optional<bool> opt;
REQUIRE(opt == nullopt);
opt = true;
REQUIRE(opt == true);
REQUIRE(opt != false);
REQUIRE(opt != nullopt);
REQUIRE(opt.value() == true);
opt = nullopt;
REQUIRE(opt != true);
REQUIRE(opt != false);
REQUIRE(opt == nullopt);
REQUIRE_THROWS_AS(opt.value(), bad_optional_access);
}
TEST_CASE("std::optional can be accessed like a pointer", "[std::optional]")
{
std::string aStr("Hello, world!");
optional<std::string> opt(aStr);
REQUIRE(opt != nullopt);
REQUIRE_NOTHROW(opt.value());
REQUIRE(opt == aStr);
REQUIRE(*opt == aStr);
REQUIRE(opt->length() == aStr.length());
}
TEST_CASE("std::optional supports std::swap", "[std::optional][std::swap]")
{
std::string str1("hello"), str2("world");
optional<std::string> opt1(str1), opt2(str2);
REQUIRE(opt1 == str1);
REQUIRE(opt2 == str2);
REQUIRE_NOTHROW(std::swap(opt1, opt2));
REQUIRE(opt1 == str2);
REQUIRE(opt2 == str1);
}
TEST_CASE("std::optional supports in-place construction of contained values", "[std::optional]")
{
const char* str = "Hello, world!";
optional<std::string> opt(in_place, str, 13);
REQUIRE(opt != nullopt);
REQUIRE(*opt == str);
REQUIRE(opt->length() == 13);
}
TEST_CASE("std::optional supports emplacement value setting", "[std::optional][emplace]")
{
const char* str = "Hello, world!";
optional<std::string> opt;
REQUIRE(opt == nullopt);
REQUIRE_NOTHROW(opt.emplace(str, 13));
REQUIRE(opt != nullopt);
REQUIRE(*opt == str);
REQUIRE(opt->length() == 13);
}
TEST_CASE("std::optional supports in-place construction with an initializer list", "[std::optional][std::initializer_list]")
{
std::vector<int> compare{1, 2, 3};
optional<std::vector<int>> opt(in_place, {1, 2, 3});
REQUIRE(opt != nullopt);
REQUIRE(*opt == compare);
REQUIRE(opt->size() == 3);
REQUIRE((*opt)[1] == 2);
}
TEST_CASE("std::optional supports emplacement value setting with an initializer list", "[std::optional][emplace][std::initializer_list]")
{
std::vector<int> compare{1, 2, 3};
optional<std::vector<int>> opt;
REQUIRE(opt == nullopt);
REQUIRE_NOTHROW(opt.emplace({1, 2, 3}));
REQUIRE(opt != nullopt);
REQUIRE(*opt == compare);
REQUIRE(opt->size() == 3);
REQUIRE((*opt)[1] == 2);
}
TEST_CASE("std::optional supports copy construction/assignment", "[std::optional]")
{
const optional<bool> opt1(true);
optional<bool> opt2(opt1);
REQUIRE(opt1 == opt2);
optional<bool> opt3;
REQUIRE_NOTHROW(opt3 = opt1);
REQUIRE(opt1 == opt3);
}
TEST_CASE("std::optional supports move construction/assignment", "[std::optional][std::move]")
{
const std::string val("Hello, world!");
optional<std::string> opt1(val);
REQUIRE(opt1 != nullopt);
optional<std::string> opt2(std::move(opt1));
// the *value* contents are moved -- opt1 is still engaged, but contains an
// empty instance of its value_type.
REQUIRE(opt1 != nullopt);
REQUIRE(opt1->empty());
REQUIRE(opt2 == val);
optional<std::string> opt3;
REQUIRE(opt3 == nullopt);
opt3 = std::move(opt2);
REQUIRE(opt2 != nullopt);
REQUIRE(opt2->empty());
REQUIRE(opt3 != nullopt);
REQUIRE(opt3 == val);
}
TEST_CASE("std::optional can construct by moving a value", "[std::optional][std::move]")
{
std::string val("Hello, world!");
optional<std::string> opt1(std::move(val));
REQUIRE(opt1 != nullopt);
REQUIRE(val.empty());
REQUIRE(opt1->length() == 13);
REQUIRE(*opt1 == "Hello, world!");
}
TEST_CASE("std::optional can be disengaged by using 'opt = {}' syntax", "[std::optional]")
{
optional<bool> opt(true);
REQUIRE(opt != nullopt);
opt = {};
REQUIRE(opt == nullopt);
}
TEST_CASE("std::optional can be tested for engaged state by casting to bool", "[std::optional]")
{
optional<int> opt(42);
REQUIRE(bool(opt) == true);
opt = nullopt;
REQUIRE(bool(opt) == false);
}
TEST_CASE("std::optional::value_or always returns a value", "[std::optional]")
{
optional<int> opt1(42);
optional<int> opt2;
REQUIRE(opt1.value_or(0x0badf00d) == 42);
REQUIRE(opt2.value_or(0x0badf00d) == 0x0badf00d);
}