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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#pragma once
#include "backend/common/imageOps.hpp"
#include <cuda/error.hpp>
#include "libvideostitch/config.hpp"
#include <csignal>
#include <iostream>
#include <cstdio>
#include <cassert>
#include <cuda_runtime.h>
#include <sstream>
#include <math.h>
#if defined(_MSC_VER)
#include <Windows.h>
#define sleep(t) Sleep(1000 * (t))
#define backtrace(a, b) 0
#define backtrace_symbols_fd(a, b, c)
#else
#if !defined(__ANDROID__)
#include <execinfo.h>
#else
#include <cstdlib>
#define backtrace(a, b) 0
#define backtrace_symbols_fd(a, b, c) (void*)&b
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-value"
#endif /*(!defined __ANDROID__)*/
#endif
namespace VideoStitch {
namespace Testing {
#ifdef _MSC_VER
// MSVC is dumb here
#pragma warning(push)
#pragma warning(disable : 4101 4189)
#endif
void handler(int sig) {
void* array[100];
const int size = backtrace(array, 100);
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, 2);
exit(1);
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
void initTest() { std::signal(SIGABRT, handler); }
void ENSURE(bool condition, const char* msg = "") {
if (!condition) {
std::cerr << "TEST FAILED: " << msg << std::endl;
std::raise(SIGABRT);
}
}
void ENSURE(Status status, const char* msg = "") {
if (!status.ok()) {
std::cerr << "TEST FAILED: " << msg << std::endl;
std::raise(SIGABRT);
}
}
void ENSURE_CUDA(cudaError success, const char* msg = "") {
if (success != cudaSuccess) {
std::cerr << "TEST FAILED: " << msg << std::endl;
std::raise(SIGABRT);
}
}
void DIE(const char* msg) { ENSURE(false, msg); }
template <typename T>
void ENSURE_EQ(const T& a, const T& b, const char* msg = "") {
if (!(a == b)) {
std::stringstream ss;
ss << "TEST FAILED: Expected '" << a << "', got '" << b << "' " << msg << std::endl;
std::cerr << ss.str();
std::raise(SIGABRT);
}
}
template <typename T>
void ENSURE_NEQ(const T& a, const T& b) {
if (!(a != b)) {
std::cerr << "TEST FAILED: Expected not equal to '" << a << "', got '" << b << "'" << std::endl;
std::raise(SIGABRT);
}
}
template <typename T>
void ENSURE_APPROX_EQ(const T& a, const T& b, const T& eps) {
T err = (T)fabs((double)a - (double)b);
if (err > eps) {
std::cerr << "TEST FAILED: Expected '" << a << "', got '" << b << "' (error=" << err << ">eps=" << eps << ")"
<< std::endl;
std::raise(SIGABRT);
}
}
template <typename T>
void ENSURE_ARRAY_EQ(const T* exp, const T* actual, std::size_t size) {
for (std::size_t i = 0; i < size; ++i) {
if (!(exp[i] == actual[i])) {
std::cerr << "TEST FAILED: At index '" << i << "', expected '" << exp[i] << "', got '" << actual[i] << "'"
<< std::endl;
std::raise(SIGABRT);
}
}
}
template <typename T>
void ENSURE_2D_ARRAY_EQ(const T* exp, const T* actual, std::size_t w, std::size_t h) {
for (std::size_t y = 0; y < h; ++y) {
for (std::size_t x = 0; x < w; ++x) {
const T& expValue = exp[y * w + x];
const T& actualValue = actual[y * w + x];
if (!(expValue == actualValue)) {
std::cerr << "TEST FAILED: At index '(" << x << "," << y << ")', expected '" << expValue << "', got '"
<< actualValue << "'" << std::endl;
std::raise(SIGABRT);
}
}
}
}
template <typename T>
void ENSURE_ARRAY_NEQ(const T* exp, const T* actual, std::size_t size) {
bool eq = true;
for (std::size_t i = 0; i < size; ++i) {
eq = eq && (exp[i] == actual[i]);
}
ENSURE(!eq, "Expected arrays to differ, but they are the same.");
}
void ENSURE_RGBA210_EQ(uint32_t a, uint32_t b) {
ENSURE_EQ(Image::RGB210::a(a), Image::RGB210::a(b));
if (Image::RGB210::a(a)) {
ENSURE_EQ(Image::RGB210::r(a), Image::RGB210::r(b));
ENSURE_EQ(Image::RGB210::g(a), Image::RGB210::g(b));
ENSURE_EQ(Image::RGB210::b(a), Image::RGB210::b(b));
}
}
void ENSURE_RGBA210_ARRAY_EQ(const uint32_t* exp, const uint32_t* actual, std::size_t w, std::size_t h) {
for (std::size_t y = 0; y < h; ++y) {
for (std::size_t x = 0; x < w; ++x) {
const uint32_t& expValue = exp[y * w + x];
const uint32_t& actualValue = actual[y * w + x];
if (Image::RGB210::a(expValue) != Image::RGB210::a(actualValue)) {
std::cerr << "TEST FAILED: At index '(" << x << "," << y << ")', expected alpha=" << Image::RGB210::a(expValue)
<< ", got alpha=" << Image::RGB210::a(actualValue) << std::endl;
ENSURE_EQ(Image::RGB210::a(expValue), Image::RGB210::a(actualValue));
} else if (Image::RGB210::a(expValue)) {
if (!(expValue == actualValue)) {
std::cerr << "TEST FAILED: At index '(" << x << "," << y << ")', expected '(" << Image::RGB210::r(expValue)
<< "," << Image::RGB210::g(expValue) << "," << Image::RGB210::b(expValue) << ")', got '("
<< Image::RGB210::r(actualValue) << "," << Image::RGB210::g(actualValue) << ","
<< Image::RGB210::b(actualValue) << ")'" << std::endl;
ENSURE_RGBA210_EQ(expValue, actualValue);
}
}
}
}
}
void ENSURE_RGBA8888_ARRAY_EQ(const uint32_t* exp, const uint32_t* actual, std::size_t w, std::size_t h) {
for (std::size_t y = 0; y < h; ++y) {
for (std::size_t x = 0; x < w; ++x) {
const uint32_t& expValue = exp[y * w + x];
const uint32_t& actualValue = actual[y * w + x];
if (Image::RGBA::a(expValue) == 0xff) {
if (!(expValue == actualValue)) {
std::cerr << "TEST FAILED: At index '(" << x << "," << y << ")', expected '(" << Image::RGBA::r(expValue)
<< "," << Image::RGBA::g(expValue) << "," << Image::RGBA::b(expValue) << ")', got '("
<< Image::RGBA::r(actualValue) << "," << Image::RGBA::g(actualValue) << ","
<< Image::RGBA::b(actualValue) << ")'" << std::endl;
ENSURE_EQ(expValue, actualValue);
}
}
ENSURE_EQ(Image::RGBA::a(expValue), Image::RGBA::a(actualValue));
}
}
}
#ifdef __CUDACC__
#define DEVICE_RUN(kernelFun) \
{ \
dim3 oneOne(1, 1, 1); \
kernelFun<<<oneOne, oneOne>>>(); \
::VideoStitch::Cuda::debugCudaErrors(cudaStreamSynchronize(0)); \
}
__device__ void DEVICE_ENSURE_EQ(int a, int b) {
if (a != b) {
// printf("TEST FAILED: Expected '%i', got '%i'\n", a, b);
assert(false);
}
}
__device__ void DEVICE_ENSURE_APPROX_EQ(float a, float b, float epsilon) {
if (fabs(a - b) > epsilon) {
// printf("TEST FAILED: Expected '%f', got '%f'\n", a, b);
assert(false);
}
}
#endif // __CUDACC__
} // namespace Testing
} // namespace VideoStitch