| 237 | // --------------------------------------------------------------------------------------------------- |
| 238 | |
| 239 | void test_optional_int_monads() |
| 240 | { |
| 241 | dlib::optional<int> o1{42}; |
| 242 | |
| 243 | { |
| 244 | auto res = o1.and_then([](int i) { return dlib::optional<long>(i); }); |
| 245 | |
| 246 | static_assert(std::is_same<decltype(res), dlib::optional<long>>::value, "bad map"); |
| 247 | DLIB_TEST(*res == 42); |
| 248 | } |
| 249 | |
| 250 | { |
| 251 | auto res = o1.transform([](int i) { return (long)i; }); |
| 252 | |
| 253 | static_assert(std::is_same<decltype(res), dlib::optional<long>>::value, "bad map"); |
| 254 | DLIB_TEST(*res == 42); |
| 255 | } |
| 256 | |
| 257 | { |
| 258 | auto res = o1.and_then([](int i) { return dlib::optional<std::string>(std::to_string(i)); }); |
| 259 | |
| 260 | static_assert(std::is_same<decltype(res), dlib::optional<std::string>>::value, "bad map"); |
| 261 | DLIB_TEST(*res == "42"); |
| 262 | } |
| 263 | |
| 264 | { |
| 265 | auto res = o1.transform([](int i) { return std::to_string(i); }); |
| 266 | |
| 267 | static_assert(std::is_same<decltype(res), dlib::optional<std::string>>::value, "bad map"); |
| 268 | DLIB_TEST(*res == "42"); |
| 269 | } |
| 270 | |
| 271 | { |
| 272 | auto res = o1.transform([](int i) {return i+1;}) |
| 273 | .transform([](int i) {return i*2;}) |
| 274 | .transform([](int i) {return std::to_string(i);}) |
| 275 | .transform([](const std::string& str) {return std::stoi(str);}) |
| 276 | .transform([](int i) {return i - 2;}) |
| 277 | .or_else([] {return dlib::make_optional<int>(0);}); |
| 278 | |
| 279 | static_assert(std::is_same<decltype(res), dlib::optional<int>>::value, "bad map"); |
| 280 | DLIB_TEST(*res == 84); |
| 281 | } |
| 282 | |
| 283 | dlib::optional<int> o2; |
| 284 | |
| 285 | { |
| 286 | auto res = o2.transform([](int i) {return i+1;}) |
| 287 | .or_else([] {return dlib::make_optional<int>(0);}); |
| 288 | |
| 289 | static_assert(std::is_same<decltype(res), dlib::optional<int>>::value, "bad map"); |
| 290 | DLIB_TEST(*res == 0); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // --------------------------------------------------------------------------------------------------- |
| 295 |
no test coverage detected