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 extern bool test_checkSemCount(semaphore s, short c);
00010 extern bool test_checkProcState(ushort pid, uchar state);
00011 extern bool test_checkResult(uchar testResult, uchar expected);
00012
00013 extern void test_semWaiter(semaphore s, int times, uchar *testResult);
00014
00015 int test_semaphore2(int argc, char **argv)
00016 {
00017 ushort apid, bpid;
00018 bool passed = TRUE;
00019 semaphore s;
00020 uchar testResult = 0;
00021 char msg[50];
00022
00023 printf("Test Suite: Multiple Semaphores\n");
00024
00025 printf(" Semaphore creation: ");
00026 s = newsem(0);
00027 if (isbadsem(s))
00028 {
00029 passed = FALSE;
00030 sprintf(msg, "%d", s);
00031 testFail(msg);
00032 }
00033 else if (test_checkSemCount(s, 0))
00034 { testPass(""); }
00035 else
00036 { passed = FALSE; }
00037
00038 ready(apid = create((void *)test_semWaiter, INITSTK, 31, "SEMAPHORE-A",
00039 3, s, 1, &testResult), RESCHED_NO);
00040 ready(bpid = create((void *)test_semWaiter, INITSTK, 31, "SEMAPHORE-B",
00041 3, s, 1, &testResult), RESCHED_YES);
00042
00043
00044 printf(" Wait on semaphore: ");
00045 if ( test_checkProcState(apid, PRWAIT)
00046 && test_checkProcState(bpid, PRWAIT)
00047 && test_checkSemCount(s, -2)
00048 && test_checkResult(testResult, 0) )
00049 { testPass(""); }
00050 else
00051 { passed = FALSE; }
00052
00053 signal(s);
00054
00055
00056 printf(" Signal first semaphore: ");
00057 if ( test_checkProcState(apid, PRFREE)
00058 && test_checkProcState(bpid, PRWAIT)
00059 && test_checkSemCount(s, -1)
00060 && test_checkResult(testResult, 1) )
00061 { testPass(""); }
00062 else
00063 { passed = FALSE; }
00064
00065 signal(s);
00066
00067
00068 printf(" Signal second semaphore: ");
00069 if ( test_checkProcState(bpid, PRFREE)
00070 && test_checkSemCount(s, 0)
00071 && test_checkResult(testResult, 2) )
00072 { testPass(""); }
00073 else
00074 { passed = FALSE; }
00075
00076 if (TRUE == passed)
00077 { testPass(""); }
00078 else
00079 { testFail(""); }
00080
00081
00082 kill(apid);
00083 kill(bpid);
00084 freesem(s);
00085
00086 return OK;
00087 }
00088