📄 usertests.c
字号:
#include "types.h"#include "stat.h"#include "user.h"#include "fs.h"#include "fcntl.h"char buf[2048];char name[3];char *echo_args[] = { "echo", "ALL", "TESTS", "PASSED", 0 };char *cat_args[] = { "cat", "README", 0 };int stdout = 1;// simple file system testsvoidopentest(void){ int fd; printf(stdout, "open test\n"); fd = open("echo", 0); if(fd < 0){ printf(stdout, "open echo failed!\n"); exit(); } close(fd); fd = open("doesnotexist", 0); if(fd >= 0){ printf(stdout, "open doesnotexist succeeded!\n"); exit(); } printf(stdout, "open test ok\n");}voidwritetest(void){ int fd; int i; printf(stdout, "small file test\n"); fd = open("small", O_CREATE|O_RDWR); if(fd >= 0){ printf(stdout, "creat small succeeded; ok\n"); } else { printf(stdout, "error: creat small failed!\n"); exit(); } for(i = 0; i < 100; i++) { if(write(fd, "aaaaaaaaaa", 10) != 10) { printf(stdout, "error: write aa %d new file failed\n", i); exit(); } if(write(fd, "bbbbbbbbbb", 10) != 10) { printf(stdout, "error: write bb %d new file failed\n", i); exit(); } } printf(stdout, "writes ok\n"); close(fd); fd = open("small", O_RDONLY); if(fd >= 0){ printf(stdout, "open small succeeded ok\n"); } else { printf(stdout, "error: open small failed!\n"); exit(); } i = read(fd, buf, 2000); if(i == 2000) { printf(stdout, "read succeeded ok\n"); } else { printf(stdout, "read failed\n"); exit(); } close(fd); if(unlink("small") < 0) { printf(stdout, "unlink small failed\n"); exit(); } printf(stdout, "small file test ok\n");}voidwritetest1(void){ int i, fd, n; printf(stdout, "big files test\n"); fd = open("big", O_CREATE|O_RDWR); if(fd < 0){ printf(stdout, "error: creat big failed!\n"); exit(); } for(i = 0; i < MAXFILE; i++) { ((int*) buf)[0] = i; if(write(fd, buf, 512) != 512) { printf(stdout, "error: write big file failed\n", i); exit(); } } close(fd); fd = open("big", O_RDONLY); if(fd < 0){ printf(stdout, "error: open big failed!\n"); exit(); } n = 0; for(;;) { i = read(fd, buf, 512); if(i == 0) { if(n == MAXFILE - 1) { printf(stdout, "read only %d blocks from big", n); exit(); } break; } else if(i != 512) { printf(stdout, "read failed %d\n", i); exit(); } if(((int*)buf)[0] != n) { printf(stdout, "read content of block %d is %d\n", n, ((int*)buf)[0]); exit(); } n++; } close(fd); if(unlink("big") < 0) { printf(stdout, "unlink big failed\n"); exit(); } printf(stdout, "big files ok\n");}voidcreatetest(void){ int i, fd; printf(stdout, "many creates, followed by unlink test\n"); name[0] = 'a'; name[2] = '\0'; for(i = 0; i < 52; i++) { name[1] = '0' + i; fd = open(name, O_CREATE|O_RDWR); close(fd); } name[0] = 'a'; name[2] = '\0'; for(i = 0; i < 52; i++) { name[1] = '0' + i; unlink(name); } printf(stdout, "many creates, followed by unlink; ok\n");}void dirtest(void){ printf(stdout, "mkdir test\n"); if(mkdir("dir0") < 0) { printf(stdout, "mkdir failed\n"); exit(); } if(chdir("dir0") < 0) { printf(stdout, "chdir dir0 failed\n"); exit(); } if(chdir("..") < 0) { printf(stdout, "chdir .. failed\n"); exit(); } if(unlink("dir0") < 0) { printf(stdout, "unlink dir0 failed\n"); exit(); } printf(stdout, "mkdir test\n");}voidexectest(void){ printf(stdout, "exec test\n"); if(exec("echo", echo_args) < 0) { printf(stdout, "exec echo failed\n"); exit(); }}// simple fork and pipe read/writevoidpipe1(void){ int fds[2], pid; int seq = 0, i, n, cc, total; if(pipe(fds) != 0){ printf(1, "pipe() failed\n"); exit(); } pid = fork(); if(pid == 0){ close(fds[0]); for(n = 0; n < 5; n++){ for(i = 0; i < 1033; i++) buf[i] = seq++; if(write(fds[1], buf, 1033) != 1033){ printf(1, "pipe1 oops 1\n"); exit(); } } exit(); } else if(pid > 0){ close(fds[1]); total = 0; cc = 1; while((n = read(fds[0], buf, cc)) > 0){ for(i = 0; i < n; i++){ if((buf[i] & 0xff) != (seq++ & 0xff)){ printf(1, "pipe1 oops 2\n"); return; } } total += n; cc = cc * 2; if(cc > sizeof(buf)) cc = sizeof(buf); } if(total != 5 * 1033) printf(1, "pipe1 oops 3 total %d\n", total); close(fds[0]); wait(); } else { printf(1, "fork() failed\n"); exit(); } printf(1, "pipe1 ok\n");}// meant to be run w/ at most two CPUsvoidpreempt(void){ int pid1, pid2, pid3; int pfds[2]; pid1 = fork(); if(pid1 == 0) for(;;) ; pid2 = fork(); if(pid2 == 0) for(;;) ; pipe(pfds); pid3 = fork(); if(pid3 == 0){ close(pfds[0]); if(write(pfds[1], "x", 1) != 1) printf(1, "preempt write error"); close(pfds[1]); for(;;) ; } close(pfds[1]); if(read(pfds[0], buf, sizeof(buf)) != 1){ printf(1, "preempt read error"); return; } close(pfds[0]); kill(pid1); kill(pid2); kill(pid3); wait(); wait(); wait(); printf(1, "preempt ok\n");}// try to find any races between exit and waitvoidexitwait(void){ int i, pid; for(i = 0; i < 100; i++){ pid = fork(); if(pid < 0){ printf(1, "fork failed\n"); return; } if(pid){ if(wait() != pid){ printf(1, "wait wrong pid\n"); return; } } else { exit(); } } printf(1, "exitwait ok\n");}voidmem(void){ void *m1, *m2; int pid; if((pid = fork()) == 0){ m1 = 0; while((m2 = malloc(10001)) != 0) { *(char**) m2 = m1; m1 = m2; } while(m1) { m2 = *(char**)m1; free(m1); m1 = m2; } m1 = malloc(1024*20); if(m1 == 0) { printf(1, "couldn't allocate mem?!!\n"); exit(); } free(m1); printf(1, "mem ok\n"); exit(); } else { wait(); }}// More file system tests// two processes write to the same file descriptor// is the offset shared? does inode locking work?voidsharedfd(){ int fd, pid, i, n, nc, np; char buf[10]; unlink("sharedfd"); fd = open("sharedfd", O_CREATE|O_RDWR); if(fd < 0){ printf(1, "fstests: cannot open sharedfd for writing"); return; } pid = fork(); memset(buf, pid==0?'c':'p', sizeof(buf)); for(i = 0; i < 1000; i++){ if(write(fd, buf, sizeof(buf)) != sizeof(buf)){ printf(1, "fstests: write sharedfd failed\n"); break; } } if(pid == 0) exit(); else wait(); close(fd); fd = open("sharedfd", 0); if(fd < 0){ printf(1, "fstests: cannot open sharedfd for reading\n"); return; } nc = np = 0; while((n = read(fd, buf, sizeof(buf))) > 0){ for(i = 0; i < sizeof(buf); i++){ if(buf[i] == 'c') nc++; if(buf[i] == 'p') np++; } } close(fd); unlink("sharedfd"); if(nc == 10000 && np == 10000) printf(1, "sharedfd ok\n"); else printf(1, "sharedfd oops %d %d\n", nc, np);}// two processes write two different files at the same// time, to test block allocation.voidtwofiles(){ int fd, pid, i, j, n, total; char *fname; printf(1, "twofiles test\n"); unlink("f1"); unlink("f2"); pid = fork(); if(pid < 0){ printf(1, "fork failed\n"); return; } fname = pid ? "f1" : "f2"; fd = open(fname, O_CREATE | O_RDWR); if(fd < 0){ printf(1, "create failed\n"); exit(); } memset(buf, pid?'p':'c', 512); for(i = 0; i < 12; i++){ if((n = write(fd, buf, 500)) != 500){ printf(1, "write failed %d\n", n); exit(); } } close(fd); if(pid) wait(); else exit(); for(i = 0; i < 2; i++){ fd = open(i?"f1":"f2", 0); total = 0; while((n = read(fd, buf, sizeof(buf))) > 0){ for(j = 0; j < n; j++){ if(buf[j] != (i?'p':'c')){ printf(1, "wrong char\n"); exit(); } } total += n; } close(fd); if(total != 12*500){ printf(1, "wrong length %d\n", total); exit(); } } unlink("f1"); unlink("f2"); printf(1, "twofiles ok\n");}// two processes create and delete files in same directoryvoidcreatedelete(){ int pid, i, fd; int n = 20; char name[32]; printf(1, "createdelete test\n"); pid = fork(); if(pid < 0){ printf(1, "fork failed\n"); exit(); } name[0] = pid ? 'p' : 'c'; name[2] = '\0'; for(i = 0; i < n; i++){ name[1] = '0' + i; fd = open(name, O_CREATE | O_RDWR); if(fd < 0){ printf(1, "create failed\n"); exit(); } close(fd); if(i > 0 && (i % 2 ) == 0){ name[1] = '0' + (i / 2); if(unlink(name) < 0){ printf(1, "unlink failed\n"); exit(); } } } if(pid==0) exit(); // else //exit(); for(i = 0; i < n; i++){ name[0] = 'p'; name[1] = '0' + i; fd = open(name, 0); if((i == 0 || i >= n/2) && fd < 0){ printf(1, "oops createdelete %s didn't exist\n", name); exit(); } else if((i >= 1 && i < n/2) && fd >= 0){ printf(1, "oops createdelete %s did exist\n", name); exit(); } if(fd >= 0) close(fd); name[0] = 'c'; name[1] = '0' + i; fd = open(name, 0); if((i == 0 || i >= n/2) && fd < 0){ printf(1, "oops createdelete %s didn't exist\n", name); exit(); } else if((i >= 1 && i < n/2) && fd >= 0){ printf(1, "oops createdelete %s did exist\n", name); exit(); } if(fd >= 0) close(fd); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -