doctest

FORK: The fastest feature-rich C++11/14/17/20 single-header testing framework
git clone https://git.neptards.moe/neptards/doctest.git
Log | Files | Refs | README

decomposition.cpp (1506B)


      1 #include <doctest/doctest.h>
      2 
      3 class MoveOnly {
      4     public:
      5         MoveOnly(int iIn) : i(iIn) { }
      6         MoveOnly(MoveOnly&&) = default;
      7         MoveOnly(const MoveOnly&) = delete;
      8         MoveOnly& operator=(MoveOnly&&) = default;
      9         MoveOnly& operator=(const MoveOnly&) = default;
     10         ~MoveOnly() = default;
     11         // NOLINTNEXTLINE(readability-make-member-function-const)
     12         operator bool() { // NOT const!
     13             return i == 42;
     14         }
     15 
     16     private:
     17         int i;
     18 };
     19 
     20 static MoveOnly genType(bool b) {
     21     return { b ? 42 : 0 };
     22 }
     23 
     24 TEST_CASE("Move Only Type") {
     25     CHECK(genType(true));
     26     CHECK(genType(false));
     27 
     28     MoveOnly a{ 0 };
     29     CHECK(a);
     30 }
     31 
     32 
     33 struct int_pointer {
     34     int* p = nullptr;
     35     int_pointer() = default;
     36 
     37     // non-const ref used in constructor
     38     // we don't want to accidentally construct a dangling pointer from a temporary
     39     int_pointer(int& i) : p(&i) { }
     40 
     41     // NOLINTNEXTLINE(readability-make-member-function-const)
     42     explicit operator bool() { return !!p; }
     43     int val() const { return *p; }
     44 };
     45 
     46 struct int_holder {
     47     int i;
     48     bool operator==(int_pointer p) const {
     49         if (!p) return false;
     50         return i == p.val();
     51     }
     52 };
     53 
     54 DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4866) // eval order
     55 TEST_CASE("Impl cast from non-const value") {
     56     int_holder h{ 8 };
     57     int i = 8;
     58 
     59     // this comparison is safe
     60     // int_pointer can be implicitly constructed from this int here
     61     CHECK(h == i);
     62 }
     63 DOCTEST_MSVC_SUPPRESS_WARNING_POP