Make tests public. Rewrite most tests. Add tests.
[pintos-anon] / src / tests / vm / mmap-remove.c
1 #include <string.h>
2 #include <syscall.h>
3 #include "tests/vm/sample.inc"
4 #include "tests/lib.h"
5 #include "tests/main.h"
6
7 void
8 test_main (void)
9 {
10   char *actual = (char *) 0x10000000;
11   int handle;
12   mapid_t map;
13   size_t i;
14
15   /* Map file. */
16   CHECK ((handle = open ("sample.txt")) > 1, "open \"sample.txt\"");
17   CHECK ((map = mmap (handle, actual)) != MAP_FAILED, "mmap \"sample.txt\"");
18
19   /* Close file and delete it. */
20   close (handle);
21   CHECK (remove ("sample.txt"), "remove \"sample.txt\"");
22   CHECK (open ("sample.txt") == -1, "try to open \"sample.txt\"");
23
24   /* Create a new file in hopes of overwriting data from the old
25      one, in case the file system has incorrectly freed the
26      file's data. */
27   CHECK (create ("another", 4096 * 10), "create \"another\"");
28
29   /* Check that mapped data is correct. */
30   if (memcmp (actual, sample, strlen (sample)))
31     fail ("read of mmap'd file reported bad data");
32
33   /* Verify that data is followed by zeros. */
34   for (i = strlen (sample); i < 4096; i++)
35     if (actual[i] != 0)
36       fail ("byte %zu of mmap'd region has value %02hhx (should be 0)",
37             i, actual[i]);
38
39   munmap (map);
40 }