dff2c772050167e4227f314e6fdea2e34b5c4437
[pintos-anon] / src / tests / vm / child-sort.c
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 }