Skip to content

Commit 04dd49c

Browse files
author
DreamAndDead
committed
improve 9.14 test code
1 parent 6d5dbeb commit 04dd49c

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

chapter9/code/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
9.14
22
9.17
33
9.18
4+
hello.txt
45

chapter9/code/9.14.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,29 @@
22
* 9.14.c
33
*/
44
#include <stdio.h>
5+
#include <assert.h>
56
#include "vm/csapp.h"
67

8+
int test(char* filename, char* content) {
9+
int fd;
10+
char buf[20];
11+
fd = Open(filename, O_RDONLY, 0);
12+
Read(fd, buf, strlen(content));
13+
return !strncmp(buf, content, strlen(content));
14+
}
15+
16+
int touch(char* filename, char* content) {
17+
int fd;
18+
umask(DEF_UMASK);
19+
fd = Open(filename, O_WRONLY|O_CREAT|O_TRUNC, DEF_MODE);
20+
Write(fd, content, strlen(content));
21+
Close(fd);
22+
}
23+
724
int main(int argc, char* argv[]) {
25+
touch("hello.txt", "Hello, world!");
26+
assert(test("hello.txt", "Hello, world!"));
27+
828
struct stat stat;
929
int fd;
1030
char* bufp;
@@ -18,6 +38,7 @@ int main(int argc, char* argv[]) {
1838
*bufp = 'J';
1939
Munmap(bufp, size);
2040

41+
assert(test("hello.txt", "Jello, world!"));
2142
return 0;
2243
}
2344

chapter9/code/hello.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

chapter9/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test way:
2323
|9.11|----|----|
2424
|9.12|----|----|
2525
|9.13|----|----|
26-
|9.14|9.14.c|check hello.txt|
26+
|9.14|9.14.c|assert|
2727
|9.15|----|----|
2828
|9.16|----|----|
2929
|9.17|vm/(mm.9.17, 9.17.c)|malloc and free|

review.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
|6|----|----|
1212
|7|----|----|
1313
|8|----|----|
14-
|9|test 9.14, 9.17, 9.18; more detailed reason for 9.19; 9.20 is awful, sub with more professional way(such as segrated fit method) |ing|
14+
|9|test 9.17, 9.18; more detailed reason for 9.19; 9.20 is awful, sub with more professional way(such as segrated fit method) |ing|
1515
|10|----|----|
1616
|11|----|----|
1717
|12|----|----|

0 commit comments

Comments
 (0)