root/lang/objective-cplusplus/i3/trunk/src/mil/src/Test1.cpp @ 34447

Revision 34447, 18.5 kB (checked in by saturday06, 4 years ago)

iojasdfaodfd

Line 
1#include <mil/PrecompiledHeaders.h>
2#include <mil/GuiModule.h>
3#include <quicktest/quicktest.h>
4
5#include "Test.h"
6
7using namespace mil;
8using namespace mil::Test;
9
10int global = 0;
11// class TestGlobal {
12//     mil::Atomic<int> data;
13// public:
14//     void operator+=(int op) {
15//         data.store(data.load() + op);
16//     }
17//     void operator-=(int op) {
18//         data.store(data.load() - op);
19//     }
20//     int operator+(int op) {
21//         return data.load() + op;
22//     }
23//     int operator-(int op) {
24//         return data.load() - op;
25//     }
26//     int operator=(int op) {
27//         data.store(op);
28//         return data.load();
29//     }
30//     int operator++() {
31//         data.store(data.load() + 1);
32//         return data.load();
33//     }
34//     int operator--() {
35//         data.store(data.load() - 1);
36//         return data.load();
37//     }
38//     bool operator==(int op) {
39//         return (data.load() == op);
40//     }
41// } aaaaaglobal;
42
43void create_thread(void (*func)(void*), void* data);
44
45#ifdef MIL_OS_WINDOWS
46
47struct ThreadData {
48    typedef void (*FuncType)(void*);
49    FuncType func;
50    void* data;
51};
52
53DWORD WINAPI ThreadProc(LPVOID data) {
54    ThreadData* t = (ThreadData*)data;
55    t->func(t->data);
56    delete t;
57    return 0;
58}
59
60void create_thread(void (*func)(void*), void* data) {
61    ThreadData* t = new ThreadData;
62    t->func = func;
63    t->data = data;
64    DWORD id = 0;
65    CreateThread(NULL, 0, ThreadProc, (LPVOID)t, 0, &id);
66}
67
68#else
69void create_thread(void (*func)(void*), void* data) {
70}
71#endif
72
73/* ---------------------------------------
74   select test
75   --------------------------------------- */
76
77//#define def_test_FMTQueue
78//#define def_test_TSTL
79//#define def_test_fast_pool_allocator
80#define def_test_Module
81#define def_test_Thread
82
83/* ---------------------------------------
84   test cases
85   --------------------------------------- */
86
87
88#ifdef def_test_Module
89
90#include <mil/Module.h>
91
92#if defined(MIL_GUI_WINDOWS)
93#  include <mil/gui-windows/GuiModule.h>
94#elif defined(MIL_GUI_COCOA)
95#  include <mil/gui-cocoa/GuiModule.h>
96#else
97#  include <mil/gui-cocoa/GuiModule.h>
98#endif
99
100using namespace mil;
101
102
103namespace Test1 {
104struct DummyModule : Module<DummyModule> {
105};
106}
107
108using namespace Test1;
109
110namespace ns_test_Module {
111
112struct FooEvent {
113    int value;
114};
115
116struct BarEvent {
117    int value;
118    int padding;
119};
120
121struct InfinityIncEvent {
122};
123
124struct Plus5 {
125    int pad[5];
126};
127
128struct Minus3 {
129};
130
131struct Plus5_3Times {
132    int count;
133    Plus5_3Times() : count(0) {
134    }
135};
136
137struct Destroy {
138};
139
140struct AppendString {
141    std::string* str;
142    void* pad[5];
143};
144
145struct FooModule : mil::Module<FooModule> {
146    void run() {
147        loop();
148    }
149    std::string str;
150    void execute(AppendString& e) {
151        str += *(e.str);
152        delete e.str;
153    }
154
155    void execute(FooEvent& e) {
156        global = e.value;
157        repostEvent();
158    }
159    void execute(BarEvent& e) {
160        global = e.value;
161    }
162    void execute(InfinityIncEvent& i) {
163        global++;
164        repostEvent();
165    }
166    void execute(Plus5& e) {
167        global += 5;
168    }
169    void execute(Minus3& e) {
170        global -= 3;
171    }
172    void execute(Plus5_3Times& e) {
173        e.count++;
174        if (e.count <= 3) {
175            global += 5;
176            repostEvent();
177        }
178    }
179    void execute(Destroy& e) {
180        destroy();
181    }
182};
183struct FooGuiModule : mil::GuiModule<FooGuiModule> {
184    void run() {
185        createUI();
186        loop();
187    }
188    std::string str;
189    void execute(AppendString& e) {
190        str += *(e.str);
191        delete e.str;
192    }
193
194    void createUI() {
195#ifdef MIL_GUI_WINDOWS
196        HINSTANCE hInstance = GetModuleHandle(NULL);
197        const TCHAR ClassName[] = _T("GuiModuleTestajiodjrfau89a");
198        int nCmdShow = SW_SHOW;
199
200        WNDCLASSEX wc;
201        wc.cbSize        = sizeof(WNDCLASSEX);
202        wc.style         = CS_HREDRAW | CS_VREDRAW | WS_OVERLAPPED;
203        wc.lpfnWndProc   = DefWindowProc;
204        wc.cbClsExtra    = 0;
205        wc.cbWndExtra    = 0;
206        wc.hInstance     = hInstance;
207        wc.hIcon         = NULL;
208        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
209        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
210        wc.lpszMenuName  = NULL;
211        wc.lpszClassName = ClassName;
212        wc.hIconSm       = NULL;
213
214        RegisterClassEx(&wc);
215
216        HWND hWnd = CreateWindowEx(
217                        0, ClassName, _T("GuiModule Test"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
218                        CW_USEDEFAULT,200,50,NULL,NULL,hInstance,NULL
219                    );
220        assert(IsWindow(hWnd));
221        setWindow(hWnd);
222
223        ShowWindow(hWnd, nCmdShow);
224        UpdateWindow(hWnd);
225        wndproc.set(hWnd);
226#endif
227    }
228
229    void execute(FooEvent& e) {
230        global = e.value;
231        repostEvent();
232    }
233
234    void execute(BarEvent& e) {
235        global = e.value;
236    }
237    void execute(InfinityIncEvent& i) {
238        global++;
239        repostEvent();
240    }
241    void execute(Plus5& e) {
242        global += 5;
243    }
244    void execute(Minus3& e) {
245        global -= 3;
246    }
247    void execute(Plus5_3Times& e) {
248        e.count++;
249        if (e.count <= 3) {
250            global += 5;
251            repostEvent();
252        }
253    }
254    void execute(Destroy& e) {
255        destroy();
256    }
257};
258
259struct FooSingleThreadedGuiModule : public FooGuiModule {
260    FooSingleThreadedGuiModule() {
261        setWindow(create_window());
262    }
263};
264
265template <typename TestModuleType>
266void test() {
267    DummyModule dummy;
268    {
269        global = 0;
270        TestModuleType m;
271        FooEvent e = {100};
272        m.post(e, dummy);
273        m.execute_front();
274        QT_CHECK(global == 100);
275    }
276
277    {
278        global = 0;
279        TestModuleType m;
280        FooEvent e = {50};
281        m.post(e, dummy);
282        m.execute_front();
283        QT_CHECK_EQUAL(global, 50);
284    }
285
286    {
287        global = 0;
288        TestModuleType m;
289        FooEvent e = {-1};
290        m.post(e, dummy);
291        m.execute_front();
292        QT_CHECK(global == -1);
293    }
294
295    {
296        global = 0;
297        TestModuleType m;
298        InfinityIncEvent i;
299        m.post(i, dummy);
300        m.execute_front();
301        QT_CHECK(global == 1);
302    }
303
304    {
305        TestModuleType m;
306        FooEvent e = {100};
307        m.post(e, dummy);
308        global = 0;
309        m.execute_front();
310        QT_CHECK(global == 100);
311        global = 0;
312        m.execute_front();
313        QT_CHECK(global == 100);
314    }
315
316    {
317        global = 0;
318        TestModuleType m;
319        InfinityIncEvent i;
320        m.post(i, dummy);
321        m.execute_front();
322        m.execute_front();
323        m.execute_front();
324        QT_CHECK(global == 3);
325    }
326    {
327        global = 0;
328        TestModuleType foo;
329        Plus5 p;
330        InfinityIncEvent i;
331        Minus3 m;
332        foo.post(p, dummy);
333        foo.post(p, dummy);
334        foo.post(m, dummy);
335        foo.post(i, dummy);
336        foo.post(p, dummy);
337        foo.post(i, dummy);
338
339        foo.execute_front();
340        QT_CHECK(global == 5);
341        foo.execute_front();
342        QT_CHECK(global == 10);
343        foo.execute_front();
344        QT_CHECK(global == 7);
345        foo.execute_front();
346        QT_CHECK(global == 8);
347        foo.execute_front();
348        QT_CHECK(global == 13);
349        foo.execute_front();
350        QT_CHECK(global == 14);
351        foo.execute_front();
352        QT_CHECK(global == 15);
353        foo.execute_front();
354        QT_CHECK(global == 16);
355        foo.execute_front();
356        QT_CHECK(global == 17);
357    }
358    {
359        global = 3;
360        TestModuleType foo;
361
362        AppendString s1;
363        s1.str = new std::string("foo");
364        foo.post(s1, dummy);
365        foo.execute_front();
366        QT_CHECK(foo.str == "foo");
367        Plus5_3Times p5_3;
368        foo.post(p5_3, dummy);
369        QT_CHECK(global == 3);
370        foo.execute_front();
371        QT_CHECK(global == 8);
372        foo.execute_front();
373        QT_CHECK(global == 13);
374        foo.execute_front();
375        QT_CHECK(global == 18);
376        foo.execute_front();
377        QT_CHECK(global == 18);
378        foo.execute_front();
379        QT_CHECK(global == 18);
380        AppendString s2;
381        s2.str = new std::string("bar");
382        foo.post(s2, dummy);
383        foo.execute_front();
384        QT_CHECK(foo.str == "foobar");
385    }
386
387}
388
389}
390QT_TEST(test_Module) {
391    using namespace ns_test_Module;
392    test<FooModule>();
393    test<FooSingleThreadedGuiModule>();
394}
395
396
397#endif
398
399#ifdef def_test_Thread
400#include <mil/Thread.h>
401
402namespace ns_test_Thread {
403
404template <typename TestModuleType>
405void test() {
406    using namespace mil;
407    using namespace ns_test_Module;
408
409    DummyModule dummy;
410
411    {
412        TestModuleType m;
413        FooEvent e = {100};
414        m.post(e, dummy);
415        global = 0;
416        m.execute_front();
417        QT_CHECK(global == 100);
418        global = 0;
419        m.execute_front();
420        QT_CHECK(global == 100);
421    }
422    {
423        global = 0;
424        TestModuleType m;
425        InfinityIncEvent i;
426        m.post(i, dummy);
427        m.execute_front();
428        m.execute_front();
429        m.execute_front();
430        QT_CHECK(global == 3);
431    }
432    {
433        global = 0;
434        TestModuleType foo;
435        Plus5 p;
436        InfinityIncEvent i;
437        Minus3 m;
438        foo.post(p, dummy);
439        foo.post(p, dummy);
440        foo.post(m, dummy);
441        foo.post(i, dummy);
442        foo.post(p, dummy);
443        foo.post(i, dummy);
444
445        foo.execute_front();
446        QT_CHECK(global == 5);
447        foo.execute_front();
448        QT_CHECK(global == 10);
449        foo.execute_front();
450        QT_CHECK(global == 7);
451        foo.execute_front();
452        QT_CHECK(global == 8);
453        foo.execute_front();
454        QT_CHECK(global == 13);
455        foo.execute_front();
456        QT_CHECK(global == 14);
457        foo.execute_front();
458        QT_CHECK(global == 15);
459        foo.execute_front();
460        QT_CHECK(global == 16);
461        foo.execute_front();
462        QT_CHECK(global == 17);
463    }
464    {
465        mil::Tls tls(0);
466        global = 3;
467        TestModuleType foo;
468        Plus5_3Times p5_3;
469        foo.post(p5_3, dummy);
470        QT_CHECK(global == 3);
471        foo.execute_front();
472        QT_CHECK(global == 8);
473        foo.execute_front();
474        QT_CHECK(global == 13);
475        foo.execute_front();
476        QT_CHECK(global == 18);
477        foo.execute_front();
478        QT_CHECK(global == 18);
479        foo.execute_front();
480        QT_CHECK(global == 18);
481    }
482}
483
484
485
486template <typename TestModuleType>
487void ttest2() {
488    using namespace mil;
489    using namespace ns_test_Module;
490
491    TestModuleType poster;
492
493    {
494        TestModuleType t;
495    }
496
497    {
498        TestModuleType t;
499        t.start();
500    }
501
502    {
503        global = 0;
504        TestModuleType t;
505        Plus5 e;
506        t.start();
507        sleep(1);
508        t.post(e, poster);
509        sleep(1);
510        QT_CHECK_EQUAL(global, 5);
511    }
512
513    {
514        global = 0;
515        TestModuleType t;
516        Plus5 e;
517        t.start();
518        sleep(1);
519        for (int i = 0; i < 50; i++) {
520            t.post(e, poster);
521        }
522        sleep(1);
523        QT_CHECK_EQUAL(global, 50 * 5);
524    }
525
526    {
527        global = 0;
528        TestModuleType t1;
529        TestModuleType t2;
530        Plus5 e;
531        t1.start();
532        sleep(1);
533        for (int i = 0; i < 50; i++) {
534            t1.post(e, poster);
535        }
536        sleep(1);
537        QT_CHECK_EQUAL(global, 50 * 5);
538        global = 0;
539        t2.start();
540        sleep(1);
541        for (int i = 0; i < 60; i++) {
542            t2.post(e, poster);
543        }
544        sleep(1);
545        QT_CHECK_EQUAL(global, 60 * 5);
546    }
547
548    {
549        Plus5 e;
550        TestModuleType foo[7];
551        for (int i = 0; i < _countof(foo); i++) {
552            foo[i].start();
553        }
554        {
555            global = 0;
556            TestModuleType bar[_countof(foo)];
557            for (int i = 0; i < _countof(foo); i++) {
558                bar[i].start();
559            }
560            sleep(1);
561            for (int i = 0; i < 50; i++) {
562                foo[i % _countof(foo)].post(e, poster);
563                bar[i % _countof(foo)].post(e, poster);
564            }
565            sleep(1);
566            QT_CHECK_EQUAL(global, 50 * 2 * 5);
567        }
568
569        {
570            global = 0;
571            TestModuleType bar[_countof(foo)];
572            TestModuleType baz[_countof(foo)];
573            for (int i = 0; i < _countof(foo); i++) {
574                bar[i].start();
575                baz[i].start();
576            }
577            sleep(2);
578            for (int i = 0; i < 3000; i++) {
579                foo[(i + 1) % _countof(foo)].post(e, poster);
580                bar[(i + 1) % _countof(foo)].post(e, poster);
581                baz[(i + 3) % _countof(foo)].post(e, poster);
582            }
583            sleep(2);
584            QT_CHECK_EQUAL(global, 3000 * 2 * 5);
585        }
586    }
587}
588
589template <typename TestModuleType>
590void ttest4() {
591    using namespace mil;
592    using namespace boost;
593    using namespace ns_test_Module;
594
595    TestModuleType poster;
596    {
597        shared_ptr<TestModuleType> t(new TestModuleType);
598    }
599
600    {
601        shared_ptr<TestModuleType> t(new TestModuleType);
602        t->start();
603    }
604
605    {
606        global = 0;
607        shared_ptr<TestModuleType> t(new TestModuleType);
608        Plus5 e;
609        t->start();
610        sleep(1);
611        t->post(e, poster);
612        sleep(1);
613        QT_CHECK_EQUAL(global, 5);
614    }
615
616    {
617        global = 0;
618        shared_ptr<TestModuleType> t(new TestModuleType);
619        Plus5 e;
620        t->start();
621        sleep(1);
622        for (int i = 0; i < 50; i++) {
623            t->post(e, poster);
624        }
625        sleep(1);
626        QT_CHECK_EQUAL(global, 50 * 5);
627    }
628
629    {
630        global = 0;
631        shared_ptr<TestModuleType> t1(new TestModuleType);
632        shared_ptr<TestModuleType> t2(new TestModuleType);
633        Plus5 e;
634        t1->start();
635        sleep(1);
636        for (int i = 0; i < 50; i++) {
637            t1->post(e, poster);
638        }
639        sleep(1);
640        QT_CHECK_EQUAL(global, 50 * 5);
641        global = 0;
642        t2->start();
643        sleep(1);
644        for (int i = 0; i < 60; i++) {
645            t2->post(e, poster);
646        }
647        sleep(1);
648        QT_CHECK_EQUAL(global, 60 * 5);
649    }
650
651    {
652        Plus5 e;
653        int foo[7] = {};
654
655        {
656            global = 0;
657            shared_ptr<TestModuleType> bar[_countof(foo)];
658            shared_ptr<TestModuleType> baz[_countof(foo)];
659            for (int i = 0; i < _countof(foo); i++) {
660                bar[i] = shared_ptr<TestModuleType>(new TestModuleType);
661                baz[i] = shared_ptr<TestModuleType>(new TestModuleType);
662                bar[i]->start();
663                baz[i]->start();
664            }
665            sleep(2);
666            for (int i = 0; i < 3000; i++) {
667                bar[(i + 1) % _countof(foo)]->post(e, poster);
668                baz[(i + 3) % _countof(foo)]->post(e, poster);
669            }
670            sleep(2);
671            QT_CHECK_EQUAL(global, 3000 * 2 * 5);
672        }
673    }
674}
675
676
677
678template <typename TestModuleType>
679void ttest3() {
680    using namespace mil;
681    using namespace ns_test_Module;
682
683    DummyModule dummy;
684    global = 0;
685    TestModuleType t;
686    TestModuleType t2;
687    Plus5 e;
688    t.start();
689    sleep(1);
690    for (int i = 0; i < 50; i++) {
691        t.post(e, dummy);
692    }
693    sleep(1);
694    QT_CHECK_EQUAL(global, 50 * 5);
695}
696
697QT_TEST(test_Thread) {
698    ns_test_Thread::test<ns_test_Module::FooModule>();
699}
700
701QT_TEST(test_Thread0909) {
702    ns_test_Thread::ttest2<ns_test_Module::FooModule>();
703}
704
705QT_TEST(test_Thread0809) {
706    ns_test_Thread::ttest3<ns_test_Module::FooModule>();
707}
708
709QT_TEST(test_Thread080889) {
710    ns_test_Thread::ttest4<ns_test_Module::FooModule>();
711}
712
713QT_TEST(test_Thread888) {
714    ns_test_Thread::test<ns_test_Module::FooSingleThreadedGuiModule>();
715}
716
717QT_TEST(test_Thread576) {
718    ns_test_Thread::ttest2<ns_test_Module::FooGuiModule>();
719}
720
721QT_TEST(test_Thread57688) {
722    ns_test_Thread::ttest3<ns_test_Module::FooGuiModule>();
723}
724
725QT_TEST(test_Thread5768adfadf8) {
726    ns_test_Thread::ttest4<ns_test_Module::FooGuiModule>();
727}
728
729}
730
731#endif
732
733#ifdef def_test_fast_pool_allocator
734#include <boost/thread.hpp>
735#include <boost/thread/mutex.hpp>
736#include <boost/intrusive/slist.hpp>
737#include <boost/intrusive/slist_hook.hpp>
738#include <boost/intrusive/list.hpp>
739#include <boost/intrusive/list_hook.hpp>
740#include <boost/pool/object_pool.hpp>
741#include <boost/pool/pool.hpp>
742#include <boost/pool/singleton_pool.hpp>
743#include <boost/pool/pool_alloc.hpp>
744
745boost::details::pool::default_mutex m;
746boost::mutex m2;
747
748class Foo : public boost::intrusive::slist_base_hook<> {
749public:
750    int value;
751};
752class MyObject
753            : public boost::intrusive::slist_base_hook<> {
754public:
755    MyObject(int v) : value(v) {}
756    int value;
757};
758QT_TEST(test_fast_pool_allocator)
759//int main()
760{
761    //std::list<int, boost::fast_pool_allocator<int> > l;
762    //l.push_back(100);
763    //QT_CHECK(l.front() == 100);
764    //int i = rand();
765    //l.push_back(i);
766    //QT_CHECK(l.back() == i);
767    //QT_CHECK(l.front() == 100);
768    //l.pop_back();
769    //QT_CHECK(l.back() == 100);
770
771    MyObject* pa;
772    {
773        boost::details::pool::guard<boost::details::pool::default_mutex> g(m);
774        //boost::mutex::scoped_lock lk(m2);
775        pa = new MyObject(100);
776    }
777    {
778        boost::intrusive::slist<MyObject> l;
779        l.push_front(*pa);
780        //l.push_back(*f);
781        char aaa[sizeof(l)+1] = {};
782        memcpy(aaa, (char*)&l, sizeof(l));
783        printf("TEST %s TEST\n", aaa);
784    }
785    {
786        //boost::mutex::scoped_lock lk(m2);
787        boost::details::pool::guard<boost::details::pool::default_mutex> g(m);
788        delete pa;
789    }
790}
791#endif
792
793#ifdef def_test_TSTL
794
795#include <TSTL/TSTL.h>
796#include <TSTL/queue.h>
797
798QT_TEST(test_TSTL) {
799    Queue<int> q;
800}
801#endif
802
803#ifdef def_test_FMTQueue
804
805#include <fecti/config.h>
806#include <fecti/Utility/FMTQueue.h>
807
808QT_TEST(test_FMTQueue) {
809    fecti::util::FMTQueue<int> queue(10);
810    queue.push(100);
811    int j = 0;
812    queue.pop(&j);
813    QT_CHECK(j == 100);
814
815    int i = rand();
816    queue.push(i);
817    queue.pop(&j);
818    QT_CHECK(j == i);
819}
820
821#endif
822
Note: See TracBrowser for help on using the browser.