Use a temp process for test_uaccess
[akaros.git] / kern / src / ktest / net_ktests.c
1 #include <ip.h>
2 #include <ktest.h>
3 #include <linker_func.h>
4
5 KTEST_SUITE("NET")
6
7 static uint16_t simplesum(const uint8_t *buf, int len)
8 {
9         uint64_t hi = 0, lo = 0, sum;
10         int i;
11
12         for (i = 0; i < len; i++) {
13                 if (i % 2 == 0)
14                         hi += buf[i];
15                 else
16                         lo += buf[i];
17         }
18         sum = (hi << 8) + lo;
19         while (sum >> 16)
20                 sum = (sum >> 16) + (sum & 0xffff);
21         return sum & 0xffff;
22 }
23
24 bool test_ptclbsum(void)
25 {
26         uint16_t csum, expected;
27         uint8_t buf[100];
28         int i, j, len;
29
30         for (i = 0; i < sizeof(buf); i++)
31                 buf[i] = i & 0xff;
32         for (i = 0; i < sizeof(buf); i++) {
33                 for (j = i; j < sizeof(buf); j++) {
34                         len = j - i + 1;
35                         csum = ptclbsum(buf + i, len);
36                         expected = simplesum(buf + i, len);
37                         if (csum != expected) {
38                                 printk("i %d j %d len %d csum %04x expected %04x\n",
39                                            i, j, len, csum, expected);
40                                 return false;
41                         }
42                 }
43         }
44         return true;
45 }
46
47 #define CSUM_BENCH_BUFSIZE 4000
48
49 bool test_simplesum_bench(void)
50 {
51         uint8_t buf[CSUM_BENCH_BUFSIZE];
52         uint16_t csum = 0;
53         int i, j, len;
54
55         for (i = 0; i < sizeof(buf); i++)
56                 buf[i] = i & 0xff;
57         for (i = 0; i < sizeof(buf); i++) {
58                 for (j = i; j < sizeof(buf); j++) {
59                         len = j - i + 1;
60                         csum += simplesum(buf + i, len);
61                 }
62         }
63         return true;
64 }
65
66 bool test_ptclbsum_bench(void)
67 {
68         uint8_t buf[CSUM_BENCH_BUFSIZE];
69         uint16_t csum = 0;
70         int i, j, len;
71
72         for (i = 0; i < sizeof(buf); i++)
73                 buf[i] = i & 0xff;
74         for (i = 0; i < sizeof(buf); i++) {
75                 for (j = i; j < sizeof(buf); j++) {
76                         len = j - i + 1;
77                         csum += ptclbsum(buf + i, len);
78                 }
79         }
80         return true;
81 }
82
83 static struct ktest ktests[] = {
84         KTEST_REG(ptclbsum,                             CONFIG_TEST_ptclbsum),
85         KTEST_REG(simplesum_bench,              CONFIG_TEST_simplesum_bench),
86         KTEST_REG(ptclbsum_bench,               CONFIG_TEST_ptclbsum_bench),
87 };
88
89 static int num_ktests = sizeof(ktests) / sizeof(struct ktest);
90
91 linker_func_1(register_net_ktests)
92 {
93         REGISTER_KTESTS(ktests, num_ktests);
94 }