summaryrefslogtreecommitdiffstats
path: root/files/testlab2.c
diff options
context:
space:
mode:
Diffstat (limited to 'files/testlab2.c')
-rw-r--r--files/testlab2.c195
1 files changed, 195 insertions, 0 deletions
diff --git a/files/testlab2.c b/files/testlab2.c
new file mode 100644
index 0000000..bd97b76
--- /dev/null
+++ b/files/testlab2.c
@@ -0,0 +1,195 @@
1/*
2 * Compile: "gcc testlab2.c"
3 * Run: "./a.out"
4 */
5
6#include <string.h>
7#include <assert.h>
8#include <stdio.h>
9#include <errno.h>
10#include <stdlib.h>
11#define __LIBRARY__
12#include <unistd.h>
13
14_syscall2(int, whoami,char*,name,unsigned int,size);
15_syscall1(int, iam, const char*, name);
16
17#define MAX_NAME_LEN 23
18#define NAMEBUF_SIZE (MAX_NAME_LEN + 1)
19/* truncate a long name to SHORT_NAME_LEN for display */
20#define SHORT_NAME_LEN (MAX_NAME_LEN + 2)
21
22/* name score */
23#define TEST_CASE { \
24 {"x", 10, 1, NAMEBUF_SIZE, 1},\
25 {"sunner", 10, 6, NAMEBUF_SIZE, 6},\
26 {"Twenty-three characters", 5, 23, NAMEBUF_SIZE, 23},\
27 {"123456789009876543211234", 5, -1, 0, -1},\
28 {"abcdefghijklmnopqrstuvwxyz", 5, -1, 0, -1},\
29 {"Linus Torvalds", 5, 14, NAMEBUF_SIZE, 14},\
30 {"", 5, 0, NAMEBUF_SIZE, 0},\
31 {"whoami(0xbalabala, 10)", 5, 22, 10, -1},\
32 {NULL, 0, 0, 0, 0} /* End of cases */ \
33}
34/*改动一:增加size,和rval2*/
35
36int test(const char* name, int max_score, int expected_rval1, int size, int expected_rval2);
37void print_message(const char* msgfmt, const char* name);
38
39struct test_case
40{
41 char *name;
42 int score;
43 int rval1; /* return value of iam() */
44 /*改动2:增加size,和rval2定义*/
45 int size; /*Patch for whoami,2009.11.2*/
46 int rval2; /* return value of whoami() */
47};
48
49int main(void)
50{
51 struct test_case cases[] = TEST_CASE;
52
53 int total_score=0, i=0;
54
55 while (cases[i].score != 0)
56 {
57 int score;
58
59 printf("Test case %d:", i+1);
60
61 /*改动3:增加size,和rval2的参数阿*/
62 score = test( cases[i].name,
63 cases[i].score,
64 cases[i].rval1,
65 cases[i].size,
66 cases[i].rval2 );
67
68 total_score += score;
69 i++;
70 }
71
72 printf("Final result: %d%%\n", total_score);
73 return 0;
74
75}
76 /*改动4:增加size,和rval2的声明*/
77int test(const char* name, int max_score, int expected_rval1, int size, int expected_rval2)
78{
79 int rval;
80 int len;
81 char * gotname;
82 int score=-1;
83
84 assert(name != NULL);
85
86 print_message("name = \"%s\", length = %d...", name);
87
88 /*Test iam()*/
89 len = strlen(name);
90 rval = iam(name);
91 /* printf("Return value = %d\n", rval);*/
92
93/*改动5:增加的expected_rval1*/
94 if (rval == expected_rval1)
95 {
96 if (rval == -1 && errno == EINVAL) /*The system call can detect bad name*/
97 {
98 /* print_message("Long name, %s(%d), detected.\n", name);*/
99 printf("PASS\n");
100 score = max_score;
101 }
102 else if (rval == -1 && errno != EINVAL)
103 {
104 printf("\nERROR iam(): Bad errno %d. It should be %d(EINVAL).\n", errno, EINVAL);
105 score = 0;
106 }
107 /* iam() is good. Test whoami() next. */
108 }
109 else
110 {
111 printf("\nERROR iam(): Return value is %d. It should be %d.\n", rval, expected_rval1);
112 score = 0;
113 }
114
115 if (score != -1)
116 return score;
117
118 /*Test whoami()*/
119 gotname = (char*)malloc(len+1);
120 if (gotname == NULL)
121 exit(-1);
122
123 memset(gotname, 0, len+1);
124
125 /* printf("Get: buffer length = %d.\n", len+1); */
126
127 rval = whoami(gotname, size);
128 /* printf("Return value = %d\n", rval); */
129
130/*改动6:增加的expected_rval2*/
131/*改动++:比较多 ,但还是顺序的改改*/
132
133 if(rval == expected_rval2)
134 {
135 if(rval == -1)
136 {
137 printf("PASS\n");
138 score = max_score;
139 }
140 else
141 {
142 if (strcmp(gotname, name) == 0)
143 {
144 /* print_message("Great! We got %s(%d) finally!\n", gotname); */
145 printf("PASS\n");
146 score = max_score;
147 }
148 else
149 {
150 print_message("\nERROR whoami(): we got %s(%d). ", gotname);
151 print_message("It should be %s(%d).\n", name);
152 score = 0;
153 }
154 }
155 }
156 else if (rval == -1)
157 {
158 printf("\nERROR whoami(): Return value is -1 and errno is %d. Why?\n", errno);
159 score = 0;
160 }
161 else
162 {
163 printf("\nERROR whoami(): Return value should be %d, not %d.\n", expected_rval2, rval);
164 score = 0;
165 }
166
167 free(gotname);
168 assert(score != -1);
169
170 return score;
171}
172
173void print_message(const char* msgfmt, const char* name)
174{
175 char short_name[SHORT_NAME_LEN + 4] = {0};
176 int len;
177
178 len = strlen(name);
179
180 if (len == 0)
181 {
182 strcpy(short_name, "NULL");
183 }
184 else if (len <= SHORT_NAME_LEN)
185 {
186 strcpy(short_name, name);
187 }
188 else
189 {
190 memset(short_name, '.', SHORT_NAME_LEN+3);
191 memcpy(short_name, name, SHORT_NAME_LEN);
192 }
193
194 printf(msgfmt, short_name, len);
195}