diff options
Diffstat (limited to 'tests/vm/child-sort.c')
| -rw-r--r-- | tests/vm/child-sort.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/vm/child-sort.c b/tests/vm/child-sort.c new file mode 100644 index 0000000..dff2c77 --- /dev/null +++ b/tests/vm/child-sort.c | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | /* Reads a 128 kB file into static data and "sorts" the bytes in | ||
| 2 | it, using counting sort, a single-pass algorithm. The sorted | ||
| 3 | data is written back to the same file in-place. */ | ||
| 4 | |||
| 5 | #include <debug.h> | ||
| 6 | #include <syscall.h> | ||
| 7 | #include "tests/lib.h" | ||
| 8 | #include "tests/main.h" | ||
| 9 | |||
| 10 | const char *test_name = "child-sort"; | ||
| 11 | |||
| 12 | unsigned char buf[128 * 1024]; | ||
| 13 | size_t histogram[256]; | ||
| 14 | |||
| 15 | int | ||
| 16 | main (int argc UNUSED, char *argv[]) | ||
| 17 | { | ||
| 18 | int handle; | ||
| 19 | unsigned char *p; | ||
| 20 | size_t size; | ||
| 21 | size_t i; | ||
| 22 | |||
| 23 | quiet = true; | ||
| 24 | |||
| 25 | CHECK ((handle = open (argv[1])) > 1, "open \"%s\"", argv[1]); | ||
| 26 | |||
| 27 | size = read (handle, buf, sizeof buf); | ||
| 28 | for (i = 0; i < size; i++) | ||
| 29 | histogram[buf[i]]++; | ||
| 30 | p = buf; | ||
| 31 | for (i = 0; i < sizeof histogram / sizeof *histogram; i++) | ||
| 32 | { | ||
| 33 | size_t j = histogram[i]; | ||
| 34 | while (j-- > 0) | ||
| 35 | *p++ = i; | ||
| 36 | } | ||
| 37 | seek (handle, 0); | ||
| 38 | write (handle, buf, size); | ||
| 39 | close (handle); | ||
| 40 | |||
| 41 | return 123; | ||
| 42 | } | ||
