File: result.cpp

package info (click to toggle)
bpftrace 0.24.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,496 kB
  • sloc: cpp: 60,982; ansic: 10,952; python: 953; yacc: 665; sh: 536; lex: 295; makefile: 22
file content (93 lines) | stat: -rw-r--r-- 1,707 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
88
89
90
91
92
93
#include "util/result.h"
#include "gtest/gtest.h"

namespace bpftrace::test::result {

class NotOK : public ErrorInfo<NotOK> {
public:
  static char ID;
  void log(llvm::raw_ostream &OS) const override
  {
    OS << "Not OK.";
  }
};

class ReallyNotOK : public ErrorInfo<ReallyNotOK> {
public:
  static char ID;
  void log(llvm::raw_ostream &OS) const override
  {
    OS << "Really not OK.";
  }
};

char NotOK::ID;
char ReallyNotOK::ID;

static Result<> alwaysOK()
{
  return OK();
}

static Result<> alwaysNotOK()
{
  return make_error<NotOK>();
}

static Result<bool> maybeOK(bool ok, bool really_bad)
{
  if (!ok) {
    if (really_bad)
      return make_error<ReallyNotOK>();
    return make_error<NotOK>();
  }
  return really_bad; // Arbitrary value.
}

TEST(result, okay)
{
  auto ok = alwaysOK();
  EXPECT_TRUE(bool(ok));
}

TEST(result, not_okay)
{
  auto ok = alwaysNotOK();
  EXPECT_FALSE(bool(ok));
}

TEST(result, values)
{
  auto ok = maybeOK(true, false);
  EXPECT_TRUE(bool(ok));
  EXPECT_FALSE(*ok);
  ok = maybeOK(true, true);
  EXPECT_TRUE(bool(ok));
  EXPECT_TRUE(*ok);
}

TEST(result, handle_err_found)
{
  auto ok = maybeOK(false, false);
  if (!ok) {
    auto nowOk = handleErrors(std::move(ok), [](const NotOK &) {});
    EXPECT_TRUE(bool(nowOk)); // Should now be fine.
  }
}

TEST(result, handle_err_missing)
{
  auto ok = maybeOK(false, true);
  if (!ok) {
    auto nowOk = handleErrors(std::move(ok), [](const NotOK &) {});
    EXPECT_FALSE(bool(nowOk)); // Should still have an error.
  }
}

TEST(result, handle_inline)
{
  auto ok = handleErrors(maybeOK(true, false), [](const NotOK &) {});
  EXPECT_TRUE(bool(ok)); // Handled above.
}

} // namespace bpftrace::test::result