-
Notifications
You must be signed in to change notification settings - Fork 0
/
my-p2-test.c
100 lines (90 loc) · 2.38 KB
/
my-p2-test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
A basic test suite for Portland State University CS333 Operating Systems Project 2.
Created by Joe Coleman
*/
#ifdef CS333_P2
#include "types.h"
#include "user.h"
// comment out tests for features the student doesn't have implemented
// Note the CPUTIME_TEST requires GETPROCS_TEST
#define GETPROCS_TEST
#ifdef GETPROCS_TEST
#include "uproc.h"
#endif
#ifdef GETPROCS_TEST
// Fork to 64 process and then make sure we get all when passing table array
// of sizes 1, 16, 64, 72. NOTE: caller does all forks.
static int
testprocarray(int max, int expected_ret){
struct uproc * table;
int ret, success = 0;
table = malloc(sizeof(struct uproc) * max); // bad code, assumes success
if (!table) {
printf(2, "Error: malloc() call failed. %s at line %d\n", __FUNCTION__, __LINE__);
exit();
}
ret = getprocs(max, table);
sleep(2000);
if (ret != expected_ret){
printf(2, "FAILED: getprocs(%d) returned %d, expected %d\n", max, ret, expected_ret);
success = -1;
}
else{
printf(2, "getprocs() was asked for %d processes and returned %d. SUCCESS\n", max, expected_ret);
}
free(table);
return success;
}
static int
testinvalidarray(void){
struct uproc * table;
int ret;
table = malloc(sizeof(struct uproc));
if (!table) {
printf(2, "Error: malloc() call failed. %s at line %d\n", __FUNCTION__, __LINE__);
exit();
}
ret = getprocs(1024, table);
free(table);
if(ret >= 0){
printf(2, "FAILED: called getprocs with max way larger than table and returned %d, not error\n", ret);
return -1;
}
return 0;
}
static void
testgetprocs(){
int ret, success;
printf(1, "\n----------\nRunning GetProcs Test\n----------\n");
printf(1, "Filling the proc[] array with dummy processes\n");
// Fork until no space left in ptable
ret = fork();
if (ret == 0){
while((ret = fork()) == 0);
if(ret > 0){
wait();
exit();
}
// Only return left is -1, which is no space left in ptable
success = testinvalidarray();
success |= testprocarray( 1, 1);
success |= testprocarray(16, 16);
success |= testprocarray(64, 64);
success |= testprocarray(72, 64);
if (success == 0)
printf(1, "** All Tests Passed **\n");
exit();
}
wait();
}
#endif
int
main(int argc, char *argv[])
{
#ifdef GETPROCS_TEST
testgetprocs(); // no need to pass argv[0]
#endif
printf(1, "\n** End of Tests **\n");
exit();
}
#endif