Skip to content

Commit

Permalink
#7 Add Lab 2 test
Browse files Browse the repository at this point in the history
  • Loading branch information
vityaman committed Sep 16, 2024
1 parent 6bdc0a2 commit 14075a2
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/UserTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:

- name: Test Lab 2
if: ${{ github.head_ref == 'lab-2' }}
run: echo "Not implemented" || exit 1
run: sh ./ci/test/run.sh alloctest usertests

- name: Test Lab 3
if: ${{ github.head_ref == 'lab-3' }}
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ UPROGS=\
$U/_zombie\
$U/_dumptests\
$U/_dump2tests\
$U/_alloctest\

fs.img: mkfs/mkfs README $(UPROGS)
mkfs/mkfs fs.img README $(UPROGS)
Expand Down
3 changes: 2 additions & 1 deletion ci/test/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from argparse import ArgumentParser

from suite.usertests import Xv6UserTestSuite
from suite.custom import DUMPTESTS, DUMP2TESTS
from suite.custom import DUMPTESTS, DUMP2TESTS, ALLOCTEST
from test import assert_eq
from qemu import Qemu

Expand All @@ -13,6 +13,7 @@
suite.name: suite for suite in (
DUMPTESTS,
DUMP2TESTS,
ALLOCTEST,
)
}

Expand Down
24 changes: 24 additions & 0 deletions ci/test/suite/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,27 @@
"4 tests were run. 4 tests passed",
]
)

ALLOCTEST = SimpleSuite(
name = "alloctest",
prologue = [],
tests = [
PatternTest(
name = "filetest",
timeout = timedelta(seconds = 2),
patterns = [
"filetest: start",
"filetest: OK",
],
),
PatternTest(
name = "memtest",
timeout = timedelta(seconds = 16),
patterns = [
"memtest: start",
"memtest: OK",
],
),
],
epilogue = [],
)
5 changes: 4 additions & 1 deletion ci/test/suite/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ def __init__(self, name: str, prologue: list[str], tests: list[Test], epilogue:
self.tests = tests
self.epilogue = epilogue

self.prologue[0] = f"(\\$ )+{self.prologue[0]}"
if len(self.prologue) != 0:
self.prologue[0] = f"(\\$ )+{self.prologue[0]}"
else:
self.tests[0].patterns[0] = f"(\\$ )+{self.tests[0].patterns[0]}"

def start(self, stream: RWStream):
Xv6(stream).run(self.name)
Expand Down
112 changes: 112 additions & 0 deletions user/alloctest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "kernel/param.h"
#include "kernel/types.h"
#include "kernel/stat.h"
#include "kernel/riscv.h"
#include "kernel/fcntl.h"
#include "kernel/memlayout.h"
#include "user/user.h"

void test0()
{
enum { NCHILD = 50, NFD = 10};
int i, j;
int fd;

printf("filetest: start\n");

if(NCHILD*NFD < NFILE) {
printf("test setup is wrong\n");
exit(1);
}

for (i = 0; i < NCHILD; i++) {
int pid = fork();
if(pid < 0){
printf("fork failed");
exit(1);
}
if(pid == 0){
for(j = 0; j < NFD; j++) {
if ((fd = open("README", O_RDONLY)) < 0) {
// the open() failed; exit with -1
exit(1);
}
}
sleep(10);
exit(0); // no errors; exit with 0.
}
}

int all_ok = 1;
for(int i = 0; i < NCHILD; i++){
int xstatus;
wait(&xstatus);
if(xstatus != 0) {
if(all_ok == 1)
printf("filetest: FAILED\n");
all_ok = 0;
}
}

if(all_ok)
printf("filetest: OK\n");
}

// Allocate all free memory and count how it is
void test1()
{
void *a;
int tot = 0;
char buf[1];
int fds[2];

printf("memtest: start\n");
if(pipe(fds) != 0){
printf("pipe() failed\n");
exit(1);
}
int pid = fork();
if(pid < 0){
printf("fork failed");
exit(1);
}
if(pid == 0){
close(fds[0]);
while(1) {
a = sbrk(PGSIZE);
if (a == (char*)0xffffffffffffffffL)
exit(0);
*(int *)(a+4) = 1;
if (write(fds[1], "x", 1) != 1) {
printf("write failed");
exit(1);
}
}
exit(0);
}
close(fds[1]);
while(1) {
if (read(fds[0], buf, 1) != 1) {
break;
} else {
tot += 1;
}
}
//int n = (PHYSTOP-KERNBASE)/PGSIZE;
//printf("allocated %d out of %d pages\n", tot, n);
if(tot < 31950) {
printf("expected to allocate at least 31950, only got %d\n", tot);
printf("memtest: FAILED\n");
} else {
printf("memtest: OK\n");
}
}

int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
test0();
test1();
exit(0);
}

0 comments on commit 14075a2

Please sign in to comment.