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 | #include "gate/tests.hpp"
#include "gate/functions.hpp"
#include "gate/callstacks.hpp"
using namespace gate;
GATEXX_TEST_UNIT(CallstackTrace)
{
Callstack::Address addrbuffer[32];
Result<gate::size_t> traceResult = Callstack::trace(addrbuffer, sizeof(addrbuffer) / sizeof(addrbuffer[0]));
if (traceResult.hasError() && (traceResult.error() == results::NotSupported))
{
/* trace feature not support on platform, tests are meaningless */
}
else
{
GATEXX_TEST_REQUIRE(traceResult.hasValue());
gate::size_t addrbuffer_used = traceResult.value();
GATEXX_TEST_REQUIRE(addrbuffer_used > 0);
StringBuilder sb;
VoidResult printResult = Callstack::print(addrbuffer[0], sb);
GATEXX_TEST_CHECK(!printResult.hasError());
}
}
static int cs_state = 0;
static Callstack::Context main_context;
static Callstack::Context other_context;
class GATE_API_LOCAL OtherStackCode : public IRunnable
{
public:
void run() override
{
cs_state = 1;
Callstack::switchTo(other_context, main_context);
}
};
GATEXX_TEST_UNIT(CallstackRun)
{
Callstack cs(4096);
OtherStackCode otherCode;
cs_state = 0;
Result<result_t> result = cs.run(main_context, otherCode);
if (result.hasError() && (result.error() == results::NotSupported))
{
/* stack switching is not supported on this platform, further tests are meaningless*/
}
else
{
GATEXX_TEST_REQUIRE(!result.hasError());
GATEXX_TEST_CHECK(result.value() == results::Ok);
GATEXX_TEST_CHECK(cs_state == 1);
}
}
static int fork_state = 0;
class GATE_API_LOCAL MainForkCode : public IRunnable
{
public:
Callstack::Anchor anchor;
void run() override
{
GATEXX_TEST_CHECK(fork_state == 0);
fork_state = 1;
Callstack::jumpToBranch(this->anchor);
}
};
class GATE_API_LOCAL BranchCode : public IRunnable
{
public:
void run() override
{
GATEXX_TEST_CHECK(fork_state == 1);
fork_state = 2;
}
};
GATEXX_TEST_UNIT(CallstackFork)
{
MainForkCode maincode;
BranchCode branchcode;
fork_state = 0;
VoidResult result = Callstack::fork(maincode.anchor, maincode, branchcode);
GATEXX_TEST_CHECK(!result.hasError());
GATEXX_TEST_CHECK(fork_state == 2);
}
static int test_func_sargs(signed char a, short b, int c, long d, long long e)
{
return a + b + c + d + static_cast<int>(e);
}
static unsigned int test_func_uargs(unsigned char a, unsigned short b, unsigned int c, unsigned long d, unsigned long long e)
{
return a + b + c + d + static_cast<unsigned>(e);
}
GATEXX_TEST_UNIT(GenericFunction)
{
{ // signed int
FunctionArgument args[5] = {
static_cast<char>(1),
static_cast<short>(2),
static_cast<int>(3),
static_cast<long>(4),
static_cast<long long>(5)
};
FunctionArgument retval = Function::invokeGeneric((gate_funcptr_t)&test_func_sargs, 0, args, 5);
retval.getType();
int iretval = *static_cast<int const*>(retval.getValuePtr());
GATEXX_TEST_CHECK_EQUAL(iretval, 15);
}
{ // unsigned int
FunctionArgument args[5] = {
static_cast<unsigned char>(1),
static_cast<unsigned short>(2),
static_cast<unsigned int>(3),
static_cast<unsigned long>(4),
static_cast<unsigned long long>(5)
};
FunctionArgument retval = Function::invokeGeneric((gate_funcptr_t)&test_func_uargs, 0, args, 5);
retval.getType();
int iretval = *static_cast<int const*>(retval.getValuePtr());
GATEXX_TEST_CHECK_EQUAL(iretval, 15);
}
}
class GATE_CORE_CPP_API GuardedCodeTarget : public gate::IRunnable, public gate::IQuitable
{
public:
result_t exitResult;
GuardedCodeTarget(result_t exit_result = results::Ok)<--- Class 'GuardedCodeTarget' has a constructor with 1 argument that is not explicit. [+]Class 'GuardedCodeTarget' has a constructor with 1 argument that is not explicit. Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.
: exitResult(exit_result)
{
}
virtual ~GuardedCodeTarget() noexcept
{
}
virtual void run() override
{
if (this->exitResult == results::UnknownException)
{
throw 123;
}
GATEXX_CHECK_EXCEPTION(this->exitResult);
}
virtual void quit() override
{
}
};
GATEXX_TEST_UNIT(InvokeGuarded)
{
{
GuardedCodeTarget noThrowCode(results::Ok);
GATEXX_TEST_CHECK_NOTHROW(Function::invokeGuarded(noThrowCode));
noThrowCode.quit();
}
{
GuardedCodeTarget throwCode(results::Failed);
GATEXX_TEST_CHECK_THROW(Function::invokeGuarded(throwCode));
}
{
GuardedCodeTarget foreignCxxThrowCode(results::UnknownException);
GATEXX_TEST_CHECK_THROW(Function::invokeGuarded(foreignCxxThrowCode));
}
}
|