summaryrefslogtreecommitdiff
path: root/tmk_core/tool/mbed/mbed-sdk/libraries/tests/mbed/cpp/main.cpp
blob: 48254315a6e43d8b8610896cea434eb4fee91a98 (plain)
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
#include "test_env.h"

#define PATTERN_CHECK_VALUE  0xF0F0ADAD

class Test {

private:
    const char* name;
    const int pattern;

public:
    Test(const char* _name) : name(_name), pattern(PATTERN_CHECK_VALUE)  {
        print("init");
    }

    void print(const char *message) {
        printf("%s::%s\n", name, message);
    }

    bool check_init(void) {
        bool result = (pattern == PATTERN_CHECK_VALUE);
        print(result ? "check_init: OK" : "check_init: ERROR");
        return result;
    }

    void stack_test(void) {
        print("stack_test");
        Test t("Stack");
        t.hello();
    }

    void hello(void) {
        print("hello");
    }

    ~Test() {
        print("destroy");
    }
};

/* Check C++ startup initialisation */
Test s("Static");

/* EXPECTED OUTPUT:
*******************
Static::init
Static::stack_test
Stack::init
Stack::hello
Stack::destroy
Static::check_init: OK
Heap::init
Heap::hello
Heap::destroy
*******************/
int main (void) {
    MBED_HOSTTEST_TIMEOUT(10);
    MBED_HOSTTEST_SELECT(default_auto);
    MBED_HOSTTEST_DESCRIPTION(C++);
    MBED_HOSTTEST_START("MBED_12");

    bool result = true;
    for (;;)
    {
        // Global stack object simple test
        s.stack_test();
        if (s.check_init() == false)
        {
            result = false;
            break;
        }

        // Heap test object simple test
        Test *m = new Test("Heap");
        m->hello();

        if (m->check_init() == false)
        {
            result = false;
        }
        delete m;
        break;
    }

    MBED_HOSTTEST_RESULT(result);
}