File: Test_Result.cpp

package info (click to toggle)
pymol 3.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 74,084 kB
  • sloc: cpp: 482,660; python: 89,328; ansic: 29,512; javascript: 6,792; sh: 84; makefile: 25
file content (87 lines) | stat: -rw-r--r-- 2,150 bytes parent folder | download
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
#include "Result.h"
#include "Test.h"
using namespace pymol::test;

static
pymol::Result<int> sum(int i, int j)
{
  return i + j;
}

TEST_CASE("Result simple", "[Result]")
{
  auto ans = sum(3, 5);
  static_assert(
      std::is_same<int, decltype(ans)::type>::value, "Not correct result type");
  static_assert(std::is_same<decltype(ans), pymol::Result<int>>::value,
      "Not correct result type");
  REQUIRE(ans);
  REQUIRE(ans.result() == 8);
}

template <typename T, typename U>
static
pymol::Result<std::common_type_t<T, U>> sum(T i, U j)
{
  return i + j;
}

TEST_CASE("Result template", "[Result]")
{
  auto ans = sum(3, 5.);
  static_assert(std::is_same<double, decltype(ans)::type>::value,
      "Not correct result type");
  static_assert(std::is_same<decltype(ans), pymol::Result<double>>::value,
      "Not correct result type");
  REQUIRE(ans);
  REQUIRE(pymol::almost_equal(ans.result(), 8.));
}

static
pymol::Result<int> sumError(int i, int j)
{
  return pymol::Error{"Values cannot be summed."};
}

TEST_CASE("Err Message", "[Result]")
{
  auto ans = sumError(4, 5);
  static_assert(
      std::is_same<int, decltype(ans)::type>::value, "Not correct result type");
  static_assert(std::is_same<decltype(ans), pymol::Result<int>>::value,
      "Not correct result type");
  REQUIRE(!ans);
  REQUIRE(ans.error().what() == "Values cannot be summed.");
}

static pymol::Result<> returnEmptyBrackets()
{
  return {};
}

TEST_CASE("Default constructor", "[Result]")
{
  auto ans = returnEmptyBrackets();
  REQUIRE(ans);
}

TEST_CASE("Error codes", "[Result]")
{
  auto error1 = pymol::Error();
  REQUIRE(error1.code() == pymol::Error::DEFAULT);
  auto error2 = pymol::Error::make<pymol::Error::QUIET>();
  REQUIRE(error2.code() == pymol::Error::QUIET);
}

TEST_CASE("Result from error code", "[Result]")
{
  // error
  pymol::Result<int> result1 = pymol::Error::QUIET;
  REQUIRE(!bool(result1));
  REQUIRE(result1.error().code() == pymol::Error::QUIET);

  // no error
  pymol::Result<int> result2 = static_cast<int>(pymol::Error::QUIET);
  REQUIRE(bool(result2));
  REQUIRE(result2.result() == static_cast<int>(pymol::Error::QUIET));
}