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