File: observers.cpp

package info (click to toggle)
librepcb 1.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 58,484 kB
  • sloc: cpp: 267,986; python: 12,100; ansic: 6,899; xml: 234; sh: 215; makefile: 115; perl: 73
file content (35 lines) | stat: -rw-r--r-- 998 bytes parent folder | download | duplicates (2)
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
#include "catch.hpp"
#include "optional.hpp"

struct move_detector {
  move_detector() = default;
  move_detector(move_detector &&rhs) { rhs.been_moved = true; }
  bool been_moved = false;
};

TEST_CASE("Observers", "[observers]") {
  tl::optional<int> o1 = 42;
  tl::optional<int> o2;
  const tl::optional<int> o3 = 42;

  REQUIRE(*o1 == 42);
  REQUIRE(*o1 == o1.value());
  REQUIRE(o2.value_or(42) == 42);
  REQUIRE(o3.value() == 42);
  auto success = std::is_same<decltype(o1.value()), int &>::value;
  REQUIRE(success);
  success = std::is_same<decltype(o3.value()), const int &>::value;
  REQUIRE(success);
  success = std::is_same<decltype(std::move(o1).value()), int &&>::value;
  REQUIRE(success);

  #ifndef TL_OPTIONAL_NO_CONSTRR
  success = std::is_same<decltype(std::move(o3).value()), const int &&>::value;
  REQUIRE(success);
  #endif

  tl::optional<move_detector> o4{tl::in_place};
  move_detector o5 = std::move(o4).value();
  REQUIRE(o4->been_moved);
  REQUIRE(!o5.been_moved);
}