Catch2
if (NOT TARGET Catch2) project(Catch2 CXX) set(CMAKE_CXX_STANDARD 11) add_library(${PROJECT_NAME} INTERFACE) target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_CU ${CMAKE_CURRENT_SOURCE_DIR}/include) # Mimic the library names in Catch2's own CMake files: add_library(Catch2::Catch2 ALIAS Catch2) # This one is historical, left over from when ApprovalTests.cpp cpp was first created. # We could encourage users to move away from it. add_library(catch2 ALIAS Catch2) endif ()0 码力 | 1 页 | 519.00 B | 5 月前3Exceptionally Bad: The Story on the Misuse of Exceptions and How to Do Better
seen in C++ • Look at original goals/ideals of exception handling • Look at the mechanics of throw/catch • Look at Exceptions in both their use and design as seen in real code • Better thinking on the me{"Pete", 21}; apply(me); } catch (const MyStatus& s) { std::cout << "error : " << s << std::endl; } ... } } Error handling Happy Path (clean) Function signature changed } catch (...) { std::cout << Exception hygiene • Throw by value • Catch by (const) reference • Rethrow using throw with no arguments • Catch handlers with derived classes placed before catch handlers with base classes Exception0 码力 | 85 页 | 2.32 MB | 5 月前3C++ Exceptions for Smaller Firmware
throw 5; } int main() { volatile int return_code = 0; try { return_code = start(); } catch (...) { return_code = -1; } return return_code; } 51Barrier #1 Exceptions disabled! 52☠ to convince you That was to show you that something is here. 94C++ Exceptions from throw to catch on GCC ARM 95Things that will NOT be covered here ● Nested exceptions ● Anything other than table based exceptions 96Consider the following 97 struct error {}; void foo() { try { bar(); } catch (error const& p_error) { // end up here... } } void bar() { destructible_t obj; baz();0 码力 | 237 页 | 6.74 MB | 5 月前3Exceptions Under the Spotlight
to throw by value, catch by const ref, and re-throw using throw.• A fail-handling mechanism void bar() { try { foo(); } catch (error_type) { // do stuff, or re-throw; } catch (…) { // do stuff, responsibility 9• A fail-handling mechanism int main() { try { bar(); } catch (error_type) { // do stuff } // don’t catch; } “Re-delegate responsibility” PART 0: WHAT ARE EXCEPTIONS 10PART I: (expensive, un-deterministic mechanism) • C++ does not return tothe throwing code (but to the catch block). Exception handlers are rare compared to function definitions. The formal usage recommendation0 码力 | 53 页 | 2.82 MB | 5 月前3Back to Basics: Exceptions
try { g(); h(); } catch( std::exception const& ex ) { /* Handle exception */ } } Three keywords throw try catch Stack unwinding Objects on the stack are try { g(); h(); } catch( std::exception const& ex ) { /* Handle exception */ } } Three keywords throw try catch Stack unwinding Objects on the stack are try { g(); h(); } catch( std::exception const& ex ) { /* Handle exception */ } } Three keywords throw try catch Stack unwinding Objects on the stack are0 码力 | 111 页 | 4.87 MB | 5 月前3Heterogeneous Modern C++ with SYCL 2020
[=](id<1> i){ out[i] = inA[i] + inB[i]; }); }); gpuQueue.wait_and_throw(); } catch (sycl::exception &e) { /* handle SYCL exception */ } } 23 SYCL 2020 Hello WorldFirst we include [=](id<1> i){ out[i] = inA[i] + inB[i]; }); }); gpuQueue.wait_and_throw(); } catch (sycl::exception &e) { /* handle SYCL exception */ } } 24 SYCL 2020 Hello WorldIn this example [=](id<1> i){ out[i] = inA[i] + inB[i]; }); }); gpuQueue.wait_and_throw(); } catch (sycl::exception &e) { /* handle SYCL exception */ } } 25 SYCL 2020 Hello WorldIn SYCL all0 码力 | 114 页 | 7.94 MB | 5 月前3Quickly Testing Qt Desktop Applications With Approval Tests
from: • Catch2, with a little bit of Qt Test • Tricks needed to use Qt with Catch2 – Useful if using different framework23 Setting up testsuite main() // Demonstrate how to create a Catch main() for #define CATCH_CONFIG_RUNNER #include <Catch.hpp> #includeint main(int argc, char* argv[]) { QApplication app(argc, argv); // -platform offscreen int result = Catch::Session() test our stuff! ☜ ☜ ☜26 ColorWidget setup – QColor object #include "catch.hpp" #include "color_widget.hpp" #include "catch_qt_string_makers.hpp" using namespace ScIDE; TEST_CASE("ColorWidget initial 0 码力 | 77 页 | 6.96 MB | 5 月前3Better Code: Contracts
_first: _second: T0 Tn-1 U0 Un-1 . . . . . . Tn try { _second.push_back(e.second); } catch(...) { throw; }© 2023 Adobe. All Rights Reserved. Three useful guarantees regarding errors The begin()) } { _first.push_back(e.first); try { _second.push_back(e.second); } catch(...) { throw; } } 93 _first: _second: T0 Tn-1 U0 Un-1 . . . . . . Tn© 2023 Adobe. All begin()) } { _first.push_back(e.first); try { _second.push_back(e.second); } catch(...) { _first.pop_back(); throw; } } 94 _first: _second: T0 Tn-1 U0 Un-1 . . . . . .0 码力 | 204 页 | 4.46 MB | 5 月前3Better Code: Exploring Validity
Adobe. All Rights Reserved. struct P { P() { x = new int32_t(); try { y = new int32_t(); } catch(...) { delete x; throw; } } ~P() { delete x; delete y; } private: int32_t * x; int32_t * y; };© 2023 Adobe. All Rights Reserved. struct P { P() { x = new int32_t(); try { y = new int32_t(); } catch(...) { delete x; throw; } } ~P() { delete x; delete y; } private: int32_t * x; int32_t * y; };© 2023 Adobe. All Rights Reserved. struct P { P() { x = new int32_t(); try { y = new int32_t(); } catch(...) { delete x; throw; } } ~P() { delete x; delete y; } private: int32_t * x; int32_t * y; };© 20230 码力 | 117 页 | 6.03 MB | 5 月前3Back to Basics Unit Testing
Modern C++ Testing with Catch2 Catch2 https://github.com/catchorg/Catch2 Frameworks Part 0: Basics 9auto abs(int x) -> int; math.hpp math.cpp test_math.cpp #include <catch2/catch_test_macros.hpp> #include Frameworks Part 0: Basics 10auto abs(int x) -> int; math.hpp math.cpp test_math.cpp #include <catch2/catch_test_macros.hpp> #include "math.hpp" TEST_CASE("Absolute value tests"){ CHECK( abs( 5) == Randomness seeded to: 3662634875 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ output.s is a Catch2 v3.7.0 host application. Run with -? for options ---------------------------------------------------------0 码力 | 109 页 | 4.13 MB | 5 月前3
共 106 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11