00001 #include <kernel.h>
00002 #include <proc.h>
00003 #include <semaphore.h>
00004 #include <stdio.h>
00005
00006 extern void testPass(const char *);
00007 extern void testFail(const char *);
00008
00009 void test_semWaiter(semaphore s, int times, uchar *testResult)
00010 {
00011 while (times > 0)
00012 {
00013 wait(s);
00014 (*testResult)++;
00015 times--;
00016 }
00017 }
00018
00019 bool test_checkSemCount(semaphore s, short c)
00020 {
00021 char msg[50];
00022
00023 if (c != semtab[s].count)
00024 {
00025 sprintf(msg, "count = %d, not %d", semtab[s].count, c);
00026 testFail(msg);
00027 return FALSE;
00028 }
00029 return TRUE;
00030 }
00031
00032 bool test_checkProcState(ushort pid, uchar state)
00033 {
00034 char msg[50];
00035
00036 if (state != proctab[pid].state)
00037 {
00038 sprintf(msg, "pid %d state %d, not %d",
00039 pid, proctab[pid].state, state);
00040 testFail(msg);
00041 return FALSE;
00042 }
00043 return TRUE;
00044 }
00045
00046 bool test_checkResult(uchar testResult, uchar expected)
00047 {
00048 char msg[80];
00049
00050 if (expected != testResult)
00051 {
00052 sprintf(msg, "process didn't seem to wait, expected %d, saw %d",
00053 expected, testResult);
00054 testFail(msg);
00055 return FALSE;
00056 }
00057 return TRUE;
00058 }
00059
00060 int test_semaphore(int argc, char **argv)
00061 {
00062 int apid;
00063 bool passed = TRUE;
00064 semaphore s;
00065 uchar testResult = 0;
00066 char msg[50];
00067
00068 printf("Test Suite: Single Semaphore\n");
00069
00070 printf(" Semaphore creation: ");
00071 s = newsem(0);
00072 if (isbadsem(s))
00073 {
00074 passed = FALSE;
00075 sprintf(msg, "%d", s);
00076 testFail(msg);
00077 }
00078 else if (test_checkSemCount(s, 0))
00079 { testPass(""); }
00080 else
00081 { passed = FALSE; }
00082
00083 ready(apid = create((void *)test_semWaiter, INITSTK, 31, "SEMAPHORE-A",
00084 3, s, 1, &testResult), RESCHED_YES);
00085
00086 printf(" Wait on semaphore: ");
00087 if ( test_checkProcState(apid, PRWAIT)
00088 && test_checkSemCount(s, -1)
00089 && test_checkResult(testResult, 0) )
00090 { testPass(""); }
00091 else
00092 { passed = FALSE; }
00093
00094 signal(s);
00095
00096 printf(" Signal semaphore: ");
00097 if ( test_checkProcState(apid, PRFREE)
00098 && test_checkSemCount(s, 0)
00099 && test_checkResult(testResult, 1) )
00100 { testPass(""); }
00101 else
00102 { passed = FALSE; }
00103
00104 if (TRUE == passed)
00105 { testPass(""); }
00106 else
00107 { testFail(""); }
00108
00109
00110 kill(apid);
00111 freesem(s);
00112
00113 return OK;
00114 }
00115