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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#include "gpu/testing.hpp"
#include <gpu/buffer.hpp>
#include "libvideostitch/status.hpp"
#include <sstream>
namespace VideoStitch {
namespace Testing {
void testStatus() {
ENSURE(Status::OK());
// ensure that ENSURE is working as intended, too
ENSURE(Status::OK().ok());
Status defaultInitialized;
ENSURE(defaultInitialized.ok());
Status fail{Origin::ExternalModule, ErrType::RuntimeError, "Test error message"};
ENSURE(!fail.ok());
Status copy = fail;
ENSURE(!fail.ok());
ENSURE(!copy.ok());
ENSURE(copy.getType() == ErrType::RuntimeError);
ENSURE(copy.getOrigin() == Origin::ExternalModule);
ENSURE_EQ(copy.getErrorMessage(), std::string("Test error message"));
}
void testPotentialManualDelete() {
auto makeIntPotential = [](int *ptr) { return Potential<int>{ptr}; };
Potential<int> pot = makeIntPotential(new int(982448879));
ENSURE(pot.status().ok());
ENSURE(pot.ok());
ENSURE_EQ(*pot.object(), 982448879);
int *ptr = pot.release();
ENSURE_EQ(*ptr, 982448879);
delete ptr;
}
void testPotentialAutoDelete() {
auto makeIntPotential = [](int *ptr) { return Potential<int>{ptr}; };
auto pot = makeIntPotential(new int(982448879));
ENSURE(pot.status().ok());
ENSURE(pot.ok());
ENSURE_EQ(*pot.object(), 982448879);
}
void testPotentialValue() {
auto makeIntPotentialValue = [](int val) { return PotentialValue<int>{val}; };
auto pot = makeIntPotentialValue(982448879);
ENSURE(pot.status().ok());
ENSURE(pot.ok());
ENSURE_EQ(pot.value(), 982448879);
}
void testPotentialImplicitConversion() {
// implicit constructor with pointer
auto makeIntPotentialValid = [](int *ptr) -> Potential<int> { return ptr; };
auto potValid = makeIntPotentialValid(new int(982448879));
ENSURE(potValid.status().ok());
ENSURE(potValid.ok());
ENSURE_EQ(*potValid.object(), 982448879);
// implicit constructor with Status
auto makeIntPotentialError = []() -> Potential<int> {
return {Origin::ExternalModule, ErrType::RuntimeError, "Test error message"};
};
auto potError = makeIntPotentialError();
ENSURE(!potError.status().ok());
ENSURE(!potError.ok());
ENSURE(potError.status().getOrigin() == Origin::ExternalModule);
ENSURE(potError.status().getType() == ErrType::RuntimeError);
ENSURE_EQ(potError.status().getErrorMessage(), std::string{"Test error message"});
}
void testStatusCause() {
const Status a = Status::OK();
ENSURE(!a.hasCause());
ENSURE(&a.getCause() == &a);
// An OK cause is not a cause.
const Status b{Origin::ExternalModule, ErrType::RuntimeError, "Root error message", a};
ENSURE(!b.hasCause());
ENSURE(&b.getCause() == &b);
// Test causes
const Status c{Origin::ExternalModule, ErrType::RuntimeError, "Top error message", b};
ENSURE(c.hasCause());
ENSURE(c.getCause().getErrorMessage() == b.getErrorMessage());
const Status d{Origin::PanoramaConfiguration, ErrType::AlgorithmFailure, "Top error message", c};
ENSURE(d.hasUnderlyingCause(ErrType::AlgorithmFailure));
ENSURE(d.hasUnderlyingCause(ErrType::RuntimeError));
ENSURE(!d.hasUnderlyingCause(ErrType::SetupFailure));
}
void testCustomStatus() {
enum class TestCode {
Ok,
ErrorWithStatus,
CodeA,
CodeB,
};
typedef Result<TestCode> TestStatus;
ENSURE(TestStatus::OK().ok());
ENSURE(!TestStatus::fromCode<TestCode::CodeA>().ok());
ENSURE(TestStatus::fromCode<TestCode::CodeA>().getCode() == TestCode::CodeA);
ENSURE(TestStatus::fromCode<TestCode::CodeB>().getCode() == TestCode::CodeB);
// Result<T> custom states don't have a Status
ENSURE(TestStatus::fromCode<TestCode::CodeA>().getStatus().ok());
ENSURE(TestStatus::fromCode<TestCode::CodeB>().getStatus().ok());
ENSURE(TestStatus(Status::OK()).ok());
const Status someError{Origin::ExternalModule, ErrType::RuntimeError, "Top error message"};
ENSURE(!TestStatus(someError).ok());
ENSURE(TestStatus(someError).getCode() == TestCode::ErrorWithStatus);
}
} // namespace Testing
} // namespace VideoStitch
int main(int argc, char **argv) {
VideoStitch::Testing::initTest();
VideoStitch::Testing::testStatus();
VideoStitch::Testing::testPotentialManualDelete();
VideoStitch::Testing::testPotentialAutoDelete();
VideoStitch::Testing::testPotentialValue();
VideoStitch::Testing::testPotentialImplicitConversion();
VideoStitch::Testing::testStatusCause();
VideoStitch::Testing::testCustomStatus();
return 0;
}