| 1 | /* |
| 2 | * Distributed under the Boost Software License, Version 1.0. |
| 3 | * (See accompanying file LICENSE_1_0.txt or copy at |
| 4 | * https://www.boost.org/LICENSE_1_0.txt) |
| 5 | * |
| 6 | * Copyright (c) 2023 Andrey Semashev |
| 7 | */ |
| 8 | /*! |
| 9 | * \file scope_success.cpp |
| 10 | * \author Andrey Semashev |
| 11 | * |
| 12 | * \brief This file contains tests for \c scope_success. |
| 13 | */ |
| 14 | |
| 15 | #include <boost/scope/scope_success.hpp> |
| 16 | #include <boost/scope/error_code_checker.hpp> |
| 17 | #include <boost/core/lightweight_test.hpp> |
| 18 | #include <boost/core/lightweight_test_trait.hpp> |
| 19 | #include <boost/config.hpp> |
| 20 | #include <utility> |
| 21 | #include <stdexcept> |
| 22 | #include <system_error> |
| 23 | #include "function_types.hpp" |
| 24 | |
| 25 | #if defined(_MSC_VER) && !defined(__clang__) |
| 26 | // warning C4702: unreachable code |
| 27 | // This warning is triggered by tests that unconditionally throw exception at some point |
| 28 | // and have code after that (e.g. parts of scope guard constructor and a check that verifies |
| 29 | // that the following code is not reached). |
| 30 | #pragma warning(disable: 4702) |
| 31 | #endif |
| 32 | |
| 33 | int g_n = 0, g_c = 0; |
| 34 | |
| 35 | void check_normal() |
| 36 | { |
| 37 | int n = 0; |
| 38 | { |
| 39 | boost::scope::scope_success< normal_func > guard{ normal_func(n) }; |
| 40 | BOOST_TEST(guard.active()); |
| 41 | } |
| 42 | BOOST_TEST_EQ(n, 1); |
| 43 | |
| 44 | n = 0; |
| 45 | { |
| 46 | boost::scope::scope_success< moveable_only_func > guard{ moveable_only_func(n) }; |
| 47 | BOOST_TEST(guard.active()); |
| 48 | guard.set_active(false); |
| 49 | BOOST_TEST(!guard.active()); |
| 50 | guard.set_active(true); |
| 51 | BOOST_TEST(guard.active()); |
| 52 | } |
| 53 | BOOST_TEST_EQ(n, 1); |
| 54 | |
| 55 | n = 0; |
| 56 | { |
| 57 | boost::scope::scope_success< normal_func > guard(normal_func(n), false); |
| 58 | BOOST_TEST(!guard.active()); |
| 59 | } |
| 60 | BOOST_TEST_EQ(n, 0); |
| 61 | |
| 62 | n = 0; |
| 63 | { |
| 64 | boost::scope::scope_success< normal_func > guard(normal_func(n), false); |
| 65 | BOOST_TEST(!guard.active()); |
| 66 | guard.set_active(true); |
| 67 | BOOST_TEST(guard.active()); |
| 68 | } |
| 69 | BOOST_TEST_EQ(n, 1); |
| 70 | |
| 71 | n = 0; |
| 72 | { |
| 73 | boost::scope::scope_success< moveable_only_func > guard1{ moveable_only_func(n) }; |
| 74 | BOOST_TEST(guard1.active()); |
| 75 | boost::scope::scope_success< moveable_only_func > guard2 = std::move(guard1); |
| 76 | BOOST_TEST(!guard1.active()); |
| 77 | BOOST_TEST(guard2.active()); |
| 78 | boost::scope::scope_success< moveable_only_func > guard3 = std::move(guard1); |
| 79 | BOOST_TEST(!guard3.active()); |
| 80 | } |
| 81 | BOOST_TEST_EQ(n, 1); |
| 82 | |
| 83 | n = 0; |
| 84 | { |
| 85 | normal_func func(n); |
| 86 | boost::scope::scope_success< normal_func& > guard(func); |
| 87 | BOOST_TEST(guard.active()); |
| 88 | } |
| 89 | BOOST_TEST_EQ(n, 1); |
| 90 | |
| 91 | n = 0; |
| 92 | { |
| 93 | normal_func func(n); |
| 94 | boost::scope::scope_success< normal_func& > guard1(func); |
| 95 | BOOST_TEST(guard1.active()); |
| 96 | boost::scope::scope_success< normal_func& > guard2 = std::move(guard1); |
| 97 | BOOST_TEST(!guard1.active()); |
| 98 | BOOST_TEST(guard2.active()); |
| 99 | boost::scope::scope_success< normal_func& > guard3 = std::move(guard1); |
| 100 | BOOST_TEST(!guard3.active()); |
| 101 | } |
| 102 | BOOST_TEST_EQ(n, 1); |
| 103 | |
| 104 | struct local |
| 105 | { |
| 106 | static void raw_func() |
| 107 | { |
| 108 | ++g_n; |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | g_n = 0; |
| 113 | { |
| 114 | boost::scope::scope_success< void (&)() > guard(local::raw_func); |
| 115 | BOOST_TEST(guard.active()); |
| 116 | } |
| 117 | BOOST_TEST_EQ(g_n, 1); |
| 118 | |
| 119 | g_n = 0; |
| 120 | { |
| 121 | boost::scope::scope_success< void (&)() > guard1(local::raw_func); |
| 122 | BOOST_TEST(guard1.active()); |
| 123 | boost::scope::scope_success< void (&)() > guard2 = std::move(guard1); |
| 124 | BOOST_TEST(!guard1.active()); |
| 125 | BOOST_TEST(guard2.active()); |
| 126 | boost::scope::scope_success< void (&)() > guard3 = std::move(guard1); |
| 127 | BOOST_TEST(!guard3.active()); |
| 128 | } |
| 129 | BOOST_TEST_EQ(g_n, 1); |
| 130 | } |
| 131 | |
| 132 | void check_throw() |
| 133 | { |
| 134 | int n = 0; |
| 135 | try |
| 136 | { |
| 137 | boost::scope::scope_success< normal_func > guard{ normal_func(n) }; |
| 138 | BOOST_TEST(guard.active()); |
| 139 | throw std::runtime_error("error" ); |
| 140 | } |
| 141 | catch (...) {} |
| 142 | BOOST_TEST_EQ(n, 0); |
| 143 | |
| 144 | n = 0; |
| 145 | try |
| 146 | { |
| 147 | boost::scope::scope_success< normal_func > guard1{ normal_func(n) }; |
| 148 | BOOST_TEST(guard1.active()); |
| 149 | throw std::runtime_error("error" ); |
| 150 | } |
| 151 | catch (...) |
| 152 | { |
| 153 | BOOST_TEST_EQ(n, 0); |
| 154 | |
| 155 | boost::scope::scope_success< normal_func > guard2{ normal_func(n) }; |
| 156 | BOOST_TEST(guard2.active()); |
| 157 | } |
| 158 | BOOST_TEST_EQ(n, 1); |
| 159 | |
| 160 | #if !defined(BOOST_CORE_UNCAUGHT_EXCEPTIONS_EMULATED) |
| 161 | n = 0; |
| 162 | try |
| 163 | { |
| 164 | boost::scope::scope_success< normal_func > guard1{ normal_func(n) }; |
| 165 | BOOST_TEST(guard1.active()); |
| 166 | throw std::runtime_error("error 1" ); |
| 167 | } |
| 168 | catch (...) |
| 169 | { |
| 170 | BOOST_TEST_EQ(n, 0); |
| 171 | |
| 172 | try |
| 173 | { |
| 174 | boost::scope::scope_success< normal_func > guard2{ normal_func(n) }; |
| 175 | BOOST_TEST(guard2.active()); |
| 176 | throw std::runtime_error("error 2" ); |
| 177 | } |
| 178 | catch (...) {} |
| 179 | } |
| 180 | BOOST_TEST_EQ(n, 0); |
| 181 | #endif // !defined(BOOST_CORE_UNCAUGHT_EXCEPTIONS_EMULATED) |
| 182 | |
| 183 | n = 0; |
| 184 | try |
| 185 | { |
| 186 | boost::scope::scope_success< throw_on_copy_func > guard{ throw_on_copy_func(n) }; |
| 187 | BOOST_ERROR("An exception is expected to be thrown by throw_on_copy_func" ); |
| 188 | } |
| 189 | catch (...) {} |
| 190 | BOOST_TEST_EQ(n, 0); |
| 191 | |
| 192 | n = 0; |
| 193 | try |
| 194 | { |
| 195 | boost::scope::scope_success< throw_on_move_func > guard{ throw_on_move_func(n) }; |
| 196 | } |
| 197 | catch (...) |
| 198 | { |
| 199 | BOOST_ERROR("An exception is not expected to be thrown by throw_on_move_func (copy ctor should be used)" ); |
| 200 | } |
| 201 | BOOST_TEST_EQ(n, 1); |
| 202 | |
| 203 | n = 0; |
| 204 | bool scope_ended = false, exception_thrown = false, func_destroyed = false; |
| 205 | try |
| 206 | { |
| 207 | boost::scope::scope_success< throw_on_call_func > guard{ throw_on_call_func(n, func_destroyed) }; |
| 208 | func_destroyed = false; |
| 209 | scope_ended = true; |
| 210 | } |
| 211 | catch (...) |
| 212 | { |
| 213 | exception_thrown = true; |
| 214 | } |
| 215 | BOOST_TEST_EQ(n, 1); |
| 216 | BOOST_TEST(scope_ended); |
| 217 | BOOST_TEST(exception_thrown); |
| 218 | BOOST_TEST(func_destroyed); |
| 219 | } |
| 220 | |
| 221 | void check_cond() |
| 222 | { |
| 223 | int n = 0; |
| 224 | { |
| 225 | int err = 0; |
| 226 | boost::scope::scope_success< normal_func, boost::scope::error_code_checker< int > > guard{ normal_func(n), boost::scope::check_error_code(ec&: err) }; |
| 227 | BOOST_TEST(guard.active()); |
| 228 | } |
| 229 | BOOST_TEST_EQ(n, 1); |
| 230 | |
| 231 | n = 0; |
| 232 | { |
| 233 | int err = 0; |
| 234 | boost::scope::scope_success< normal_func, boost::scope::error_code_checker< int > > guard{ normal_func(n), boost::scope::check_error_code(ec&: err) }; |
| 235 | BOOST_TEST(guard.active()); |
| 236 | err = -1; |
| 237 | } |
| 238 | BOOST_TEST_EQ(n, 0); |
| 239 | |
| 240 | n = 0; |
| 241 | { |
| 242 | int err = 0; |
| 243 | boost::scope::scope_success< normal_func, boost::scope::error_code_checker< int > > guard{ normal_func(n), boost::scope::check_error_code(ec&: err), false }; |
| 244 | BOOST_TEST(!guard.active()); |
| 245 | } |
| 246 | BOOST_TEST_EQ(n, 0); |
| 247 | |
| 248 | n = 0; |
| 249 | { |
| 250 | std::error_code err{}; |
| 251 | boost::scope::scope_success< normal_func, boost::scope::error_code_checker< std::error_code > > guard{ normal_func(n), boost::scope::check_error_code(ec&: err) }; |
| 252 | BOOST_TEST(guard.active()); |
| 253 | } |
| 254 | BOOST_TEST_EQ(n, 1); |
| 255 | |
| 256 | n = 0; |
| 257 | { |
| 258 | std::error_code err{}; |
| 259 | boost::scope::scope_success< normal_func, boost::scope::error_code_checker< std::error_code > > guard{ normal_func(n), boost::scope::check_error_code(ec&: err) }; |
| 260 | BOOST_TEST(guard.active()); |
| 261 | err = std::make_error_code(e: std::errc::invalid_argument); |
| 262 | } |
| 263 | BOOST_TEST_EQ(n, 0); |
| 264 | |
| 265 | n = 0; |
| 266 | try |
| 267 | { |
| 268 | int err = 0; |
| 269 | boost::scope::scope_success< normal_func, boost::scope::error_code_checker< int > > guard{ normal_func(n), boost::scope::check_error_code(ec&: err) }; |
| 270 | BOOST_TEST(guard.active()); |
| 271 | throw std::runtime_error("error" ); |
| 272 | } |
| 273 | catch (...) {} |
| 274 | BOOST_TEST_EQ(n, 1); // exception is not the failure condition, err was still 0 when the scope guard was destroyed |
| 275 | } |
| 276 | |
| 277 | void check_deduction() |
| 278 | { |
| 279 | int n = 0; |
| 280 | { |
| 281 | auto guard = boost::scope::make_scope_success(func: normal_func(n)); |
| 282 | BOOST_TEST(guard.active()); |
| 283 | BOOST_TEST_TRAIT_SAME(decltype(guard), boost::scope::scope_success< normal_func >); |
| 284 | } |
| 285 | BOOST_TEST_EQ(n, 1); |
| 286 | |
| 287 | n = 0; |
| 288 | { |
| 289 | auto guard = boost::scope::make_scope_success(func: normal_func(n), active: false); |
| 290 | BOOST_TEST(!guard.active()); |
| 291 | BOOST_TEST_TRAIT_SAME(decltype(guard), boost::scope::scope_success< normal_func >); |
| 292 | } |
| 293 | BOOST_TEST_EQ(n, 0); |
| 294 | |
| 295 | n = 0; |
| 296 | { |
| 297 | const normal_func func{ n }; |
| 298 | auto guard = boost::scope::make_scope_success(func, active: true); |
| 299 | BOOST_TEST(guard.active()); |
| 300 | BOOST_TEST_TRAIT_SAME(decltype(guard), boost::scope::scope_success< normal_func >); |
| 301 | } |
| 302 | BOOST_TEST_EQ(n, 1); |
| 303 | |
| 304 | n = 0; |
| 305 | { |
| 306 | int err = 0; |
| 307 | auto guard = boost::scope::make_scope_success(func: normal_func(n), cond: boost::scope::check_error_code(ec&: err)); |
| 308 | BOOST_TEST(guard.active()); |
| 309 | BOOST_TEST_TRAIT_SAME(decltype(guard), boost::scope::scope_success< normal_func, boost::scope::error_code_checker< int > >); |
| 310 | } |
| 311 | BOOST_TEST_EQ(n, 1); |
| 312 | |
| 313 | struct local |
| 314 | { |
| 315 | static void raw_func() |
| 316 | { |
| 317 | ++g_n; |
| 318 | } |
| 319 | |
| 320 | static bool raw_cond() |
| 321 | { |
| 322 | ++g_c; |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | #if !defined(BOOST_SCOPE_NO_CXX17_NOEXCEPT_FUNCTION_TYPES) |
| 327 | static void raw_func_noexcept() noexcept |
| 328 | { |
| 329 | ++g_n; |
| 330 | } |
| 331 | |
| 332 | static bool raw_cond_noexcept() noexcept |
| 333 | { |
| 334 | ++g_c; |
| 335 | return false; |
| 336 | } |
| 337 | #endif |
| 338 | }; |
| 339 | |
| 340 | g_n = 0; |
| 341 | g_c = 0; |
| 342 | { |
| 343 | auto guard = boost::scope::make_scope_success(func&: local::raw_func, cond&: local::raw_cond); |
| 344 | BOOST_TEST(guard.active()); |
| 345 | BOOST_TEST_TRAIT_SAME(decltype(guard), boost::scope::scope_success< void (*)(), bool (*)() >); |
| 346 | } |
| 347 | BOOST_TEST_EQ(g_n, 1); |
| 348 | BOOST_TEST_EQ(g_c, 1); |
| 349 | |
| 350 | #if !defined(BOOST_SCOPE_NO_CXX17_NOEXCEPT_FUNCTION_TYPES) |
| 351 | g_n = 0; |
| 352 | g_c = 0; |
| 353 | { |
| 354 | auto guard = boost::scope::make_scope_success(func&: local::raw_func_noexcept, cond&: local::raw_cond_noexcept); |
| 355 | BOOST_TEST(guard.active()); |
| 356 | BOOST_TEST_TRAIT_SAME(decltype(guard), boost::scope::scope_success< void (*)() noexcept, bool (*)() noexcept >); |
| 357 | } |
| 358 | BOOST_TEST_EQ(g_n, 1); |
| 359 | BOOST_TEST_EQ(g_c, 1); |
| 360 | #endif |
| 361 | |
| 362 | #if !defined(BOOST_NO_CXX17_DEDUCTION_GUIDES) |
| 363 | n = 0; |
| 364 | { |
| 365 | boost::scope::scope_success guard{ normal_func(n) }; |
| 366 | BOOST_TEST(guard.active()); |
| 367 | BOOST_TEST_TRAIT_SAME(decltype(guard), boost::scope::scope_success< normal_func >); |
| 368 | } |
| 369 | BOOST_TEST_EQ(n, 1); |
| 370 | |
| 371 | n = 0; |
| 372 | { |
| 373 | boost::scope::scope_success guard{ normal_func(n), false }; |
| 374 | BOOST_TEST(!guard.active()); |
| 375 | BOOST_TEST_TRAIT_SAME(decltype(guard), boost::scope::scope_success< normal_func >); |
| 376 | } |
| 377 | BOOST_TEST_EQ(n, 0); |
| 378 | |
| 379 | n = 0; |
| 380 | { |
| 381 | int err = 0; |
| 382 | boost::scope::scope_success guard{ normal_func(n), boost::scope::check_error_code(ec&: err) }; |
| 383 | BOOST_TEST(guard.active()); |
| 384 | BOOST_TEST_TRAIT_SAME(decltype(guard), boost::scope::scope_success< normal_func, boost::scope::error_code_checker< int > >); |
| 385 | } |
| 386 | BOOST_TEST_EQ(n, 1); |
| 387 | |
| 388 | n = 0; |
| 389 | { |
| 390 | int err = 0; |
| 391 | boost::scope::scope_success guard{ normal_func(n), boost::scope::error_code_checker(err), true }; |
| 392 | BOOST_TEST(guard.active()); |
| 393 | BOOST_TEST_TRAIT_SAME(decltype(guard), boost::scope::scope_success< normal_func, boost::scope::error_code_checker< int > >); |
| 394 | } |
| 395 | BOOST_TEST_EQ(n, 1); |
| 396 | |
| 397 | g_n = 0; |
| 398 | g_c = 0; |
| 399 | { |
| 400 | boost::scope::scope_success guard{ local::raw_func, local::raw_cond }; |
| 401 | BOOST_TEST(guard.active()); |
| 402 | BOOST_TEST_TRAIT_SAME(decltype(guard), boost::scope::scope_success< void (*)(), bool (*)() >); |
| 403 | } |
| 404 | BOOST_TEST_EQ(g_n, 1); |
| 405 | BOOST_TEST_EQ(g_c, 1); |
| 406 | |
| 407 | #if !defined(BOOST_SCOPE_NO_CXX17_NOEXCEPT_FUNCTION_TYPES) |
| 408 | g_n = 0; |
| 409 | g_c = 0; |
| 410 | { |
| 411 | boost::scope::scope_success guard{ local::raw_func_noexcept, local::raw_cond_noexcept }; |
| 412 | BOOST_TEST(guard.active()); |
| 413 | BOOST_TEST_TRAIT_SAME(decltype(guard), boost::scope::scope_success< void (*)() noexcept, bool (*)() noexcept >); |
| 414 | } |
| 415 | BOOST_TEST_EQ(g_n, 1); |
| 416 | BOOST_TEST_EQ(g_c, 1); |
| 417 | #endif |
| 418 | |
| 419 | n = 0; |
| 420 | { |
| 421 | boost::scope::scope_success guard([&n] { ++n; }); |
| 422 | BOOST_TEST(guard.active()); |
| 423 | } |
| 424 | BOOST_TEST_EQ(n, 1); |
| 425 | |
| 426 | n = 0; |
| 427 | { |
| 428 | int err = 10; |
| 429 | boost::scope::scope_success guard([&n] { ++n; }, [&err]() noexcept { return err < 0; }); |
| 430 | BOOST_TEST(guard.active()); |
| 431 | } |
| 432 | BOOST_TEST_EQ(n, 1); |
| 433 | |
| 434 | n = 0; |
| 435 | { |
| 436 | boost::scope::scope_success guard1{ normal_func(n) }; |
| 437 | boost::scope::scope_success guard2 = std::move(guard1); |
| 438 | BOOST_TEST(guard2.active()); |
| 439 | BOOST_TEST_TRAIT_SAME(decltype(guard2), boost::scope::scope_success< normal_func >); |
| 440 | } |
| 441 | BOOST_TEST_EQ(n, 1); |
| 442 | #endif // !defined(BOOST_NO_CXX17_DEDUCTION_GUIDES) |
| 443 | } |
| 444 | |
| 445 | int main() |
| 446 | { |
| 447 | check_normal(); |
| 448 | check_throw(); |
| 449 | check_cond(); |
| 450 | check_deduction(); |
| 451 | |
| 452 | return boost::report_errors(); |
| 453 | } |
| 454 | |