00001 #include <cc++/thread.h>
00002 #include <cstdio>
00003
00004 #ifdef CCXX_NAMESPACES
00005 using namespace ost;
00006 #endif
00007
00008
00009
00010
00011 class ThreadTest: public Thread
00012 {
00013 public:
00014 ThreadTest();
00015 void run();
00016 };
00017
00018 volatile int n = 0;
00019
00020 bool WaitNValue(int value)
00021 {
00022 for(int i=0;; ++i) {
00023 if (n == value)
00024 break;
00025 if (i >= 100)
00026 return false;
00027 Thread::sleep(10);
00028 }
00029 return true;
00030 }
00031
00032 bool WaitChangeNValue(int value)
00033 {
00034 for(int i=0;; ++i) {
00035 if (n != value)
00036 break;
00037 if (i >= 100)
00038 return false;
00039 Thread::sleep(10);
00040 }
00041 return true;
00042 }
00043
00044 ThreadTest::ThreadTest()
00045 {
00046 }
00047
00048 void ThreadTest::run()
00049 {
00050 setCancel(Thread::cancelDeferred);
00051 n = 1;
00052
00053
00054 if (!WaitNValue(2)) return;
00055
00056
00057 for(;;) {
00058 yield();
00059 n = n+1;
00060 }
00061 }
00062
00063 bool TestChange(bool shouldChange)
00064 {
00065 if (shouldChange)
00066 printf("- thread should change n...");
00067 else
00068 printf("- thread should not change n...");
00069 if (WaitChangeNValue(n) == shouldChange) {
00070 printf("ok\n");
00071 return true;
00072 }
00073 printf("ko\n");
00074 return false;
00075 }
00076
00077 #undef ERROR
00078 #undef OK
00079 #define ERROR {printf("ko\n"); return 1; }
00080 #define OK {printf("ok\n"); }
00081
00082 #define TEST_CHANGE(b) if (!TestChange(b)) return 1;
00083
00084 int main(int argc, char* argv[])
00085 {
00086 ThreadTest test;
00087
00088
00089 printf("***********************************************\n");
00090 printf("* Testing class Thread without syncronization *\n");
00091 printf("***********************************************\n");
00092
00093 printf("Testing thread creation\n\n");
00094 n = 0;
00095 test.start();
00096
00097
00098 printf("- thread should set n to 1...");
00099 if (WaitNValue(1)) OK
00100 else ERROR;
00101
00102
00103 printf("\nTesting thread is working\n\n");
00104 n = 2;
00105 TEST_CHANGE(true);
00106 TEST_CHANGE(true);
00107
00108
00109 printf("\nTesting suspend & resume\n\n");
00110 test.suspend();
00111 TEST_CHANGE(false);
00112 TEST_CHANGE(false);
00113
00114
00115 test.resume();
00116 TEST_CHANGE(true);
00117 TEST_CHANGE(true);
00118
00119 printf("\nTesting recursive suspend & resume\n\n");
00120 test.suspend();
00121 test.suspend();
00122 TEST_CHANGE(false);
00123 TEST_CHANGE(false);
00124
00125 test.resume();
00126 TEST_CHANGE(false);
00127 TEST_CHANGE(false);
00128 test.resume();
00129 TEST_CHANGE(true);
00130 TEST_CHANGE(true);
00131
00132 printf("\nTesting no suspend on resume\n\n");
00133 test.resume();
00134 TEST_CHANGE(true);
00135 TEST_CHANGE(true);
00136
00137
00138 printf("\nTesting resuspend\n\n");
00139 test.suspend();
00140 TEST_CHANGE(false);
00141 TEST_CHANGE(false);
00142
00143 printf("\nNow program should finish... :)\n");
00144 test.resume();
00145
00146 return 0;
00147 }