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
220
221
#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);
}

static int test_func_float(float a, double b, void* c)
{
    double* const ptr = static_cast<double*>(c);
    GATEXX_TEST_CHECK(ptr != NULL);
    if(ptr)
    {
        /* prohibit segfault on param error */
        *ptr = a + b;
        return static_cast<int>(*ptr);
    }
    return 0;
}


GATEXX_TEST_UNIT(GenericFunction)
{
    {   // signed int
        FunctionArgument args[5] = {
            static_cast<signed char>(1),
            static_cast<signed short>(2),
            static_cast<signed int>(3),
            static_cast<signed long>(4),
            static_cast<signed 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);

        // test copy construction
        FunctionArgument t0 = args[0];
        FunctionArgument t1 = args[1];
        FunctionArgument t2 = args[2];
        FunctionArgument t3 = args[3];
        FunctionArgument t4 = args[4];
    }

    /*
    // NOTICE: floating point args are not working on register-based calling convention, 
    // tests will fail on X64
    {   // float + ptr
        double dbl_result = 0.0;
        FunctionArgument args[3] = {
            static_cast<double>(1.0),
            static_cast<float>(2.0),
            static_cast<void*>(&dbl_result)
        };

        FunctionArgument retval = Function::invokeGeneric((gate_funcptr_t)&test_func_float, 0, args, 3);
        int iretval = *static_cast<int const*>(retval.getValuePtr());
        GATEXX_TEST_CHECK_EQUAL(iretval, 3);
    }
    */
}

class GATE_CORE_CPP_API GuardedCodeTarget : public gate::IRunnable, public gate::IQuitable
{
public:
    result_t exitResult;

    GuardedCodeTarget(result_t exit_result = results::Ok)
        : 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));
    }
}