File: swap.cpp

package info (click to toggle)
tl-expected 1.0.0~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, bullseye-backports, bullseye-backports-sloppy
  • size: 320 kB
  • sloc: cpp: 3,118; python: 45; makefile: 12
file content (101 lines) | stat: -rw-r--r-- 2,446 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "catch.hpp"
#include <tl/expected.hpp>

struct no_throw {
  no_throw(std::string i) : i(i) {}
  std::string i;
};
struct canthrow_move {
  canthrow_move(std::string i) : i(i) {}
  canthrow_move(canthrow_move const &) = default;
  canthrow_move(canthrow_move &&other) noexcept(false) : i(other.i) {}
  canthrow_move &operator=(canthrow_move &&) = default;
  std::string i;
};

bool should_throw = false;
struct willthrow_move {
  willthrow_move(std::string i) : i(i) {}
  willthrow_move(willthrow_move const &) = default;
  willthrow_move(willthrow_move &&other) : i(other.i) {
    if (should_throw)
      throw 0;
  }
  willthrow_move &operator=(willthrow_move &&) = default;
  std::string i;
};
static_assert(tl::detail::is_swappable<no_throw>::value, "");

template <class T1, class T2> void swap_test() {
  std::string s1 = "abcdefghijklmnopqrstuvwxyz";
  std::string s2 = "zyxwvutsrqponmlkjihgfedcba";

  tl::expected<T1, T2> a{s1};
  tl::expected<T1, T2> b{s2};
  swap(a, b);
  REQUIRE(a->i == s2);
  REQUIRE(b->i == s1);

  a = s1;
  b = tl::unexpected<T2>(s2);
  swap(a, b);
  REQUIRE(a.error().i == s2);
  REQUIRE(b->i == s1);

  a = tl::unexpected<T2>(s1);
  b = s2;
  swap(a, b);
  REQUIRE(a->i == s2);
  REQUIRE(b.error().i == s1);

  a = tl::unexpected<T2>(s1);
  b = tl::unexpected<T2>(s2);
  swap(a, b);
  REQUIRE(a.error().i == s2);
  REQUIRE(b.error().i == s1);

  a = s1;
  b = s2;
  a.swap(b);
  REQUIRE(a->i == s2);
  REQUIRE(b->i == s1);

  a = s1;
  b = tl::unexpected<T2>(s2);
  a.swap(b);
  REQUIRE(a.error().i == s2);
  REQUIRE(b->i == s1);

  a = tl::unexpected<T2>(s1);
  b = s2;
  a.swap(b);
  REQUIRE(a->i == s2);
  REQUIRE(b.error().i == s1);

  a = tl::unexpected<T2>(s1);
  b = tl::unexpected<T2>(s2);
  a.swap(b);
  REQUIRE(a.error().i == s2);
  REQUIRE(b.error().i == s1);
}

TEST_CASE("swap") {

  swap_test<no_throw, no_throw>();
  swap_test<no_throw, canthrow_move>();
  swap_test<canthrow_move, no_throw>();

  std::string s1 = "abcdefghijklmnopqrstuvwxyz";
  std::string s2 = "zyxwvutsrqponmlkjihgfedcbaxxx";
  tl::expected<no_throw, willthrow_move> a{s1};
  tl::expected<no_throw, willthrow_move> b{tl::unexpect, s2};
  should_throw = 1;

  #ifdef _MSC_VER
  //this seems to break catch on GCC and Clang
  REQUIRE_THROWS(swap(a, b));
  #endif

  REQUIRE(a->i == s1);
  REQUIRE(b.error().i == s2);
}