summaryrefslogtreecommitdiff
path: root/legacy/evil/src/bin/suite.c
blob: 9a1661db1e3c46d8073bbb2c62d0dd5f79caf6a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdlib.h>
#include <stdio.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN

#include "suite.h"
#include "test_memcpy.h"


typedef void(*function)(void);

struct test
{
   const char *name;
   function    fct;
};

struct list
{
  void *data;
  list *next;
};

struct suite
{
   list         *first;
   list         *l;
};


unsigned char *buf1 = NULL;
unsigned char *buf2 = NULL;
size_t         page_size = 0;


suite *
suite_new(void)
{
   suite *s;

   if (!QueryPerformanceFrequency(&freq))
     return NULL;

   s = (suite *)malloc(sizeof(suite));
   if (!s) return NULL;

   s->first = NULL;
   s->l = NULL;

   return s;
}

void
suite_del(suite *s)
{
   list *l;
   list *tmp;

   if (!s) return;

   l = s->first;
   while (l)
     {
        tmp = l->next;
        free(l->data);
        free(l);
        l = tmp;
     }

   free(s);
}

void
suite_time_start()
{
   QueryPerformanceCounter(&start);
}

void
suite_time_stop()
{
   QueryPerformanceCounter(&end);
}

double
suite_time_get()
{
   return (double)(end.QuadPart - start.QuadPart) / (double)freq.QuadPart;
}

void
suite_test_add(suite *s, const char *name, function fct)
{
   test *t;
   list *l;

   t = (test *)malloc(sizeof(test));
   if (!t) return;

   l = (list *)malloc(sizeof(list));
   if (!l)
     {
        free(t);
        return;
     }

   t->name = name;
   t->fct = fct;

   l->data = t;
   l->next = NULL;

   if (!s->first) s->first = l;

   if (!s->l)
     s->l = l;
   else
     {
        s->l->next = l;
        s->l =l;
     }
}

void
suite_run(suite *s)
{
   list *l;

   l = s->first;
   while (l)
     {
        test *t;

        t = (test *)l->data;
        printf("%s test\n", t->name);
        t->fct();
        l = l->next;
     }
}

int
main()
{
   test tests[] = {
     { "memcpy", test_memcpy },
     { NULL,             NULL },
   };
   suite *s;
   int i;

   page_size = 2 * getpagesize();

   buf1 = (unsigned char *)malloc(4 * getpagesize());
   if (!buf1) return EXIT_FAILURE;

   buf2 = (unsigned char *)malloc(4 * getpagesize());
   if (!buf2)
     {
        free(buf1);
        return EXIT_FAILURE;
     }

   memset (buf1, 0xa5, page_size);
   memset (buf2, 0x5a, page_size);

   s = suite_new();
   if (!s)
     {
        free(buf2);
        free(buf1);
        return EXIT_FAILURE;
     }

   for (i = 0; ; ++i)
     {
        if (!tests[i].name)
          break;

        suite_test_add(s, tests[i].name, tests[i].fct);
     }

   suite_run(s);

   suite_del(s);
   free(buf2);
   free(buf1);

   return EXIT_SUCCESS;
}