-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
61 lines (48 loc) · 1.37 KB
/
test.c
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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <string.h>
#include "test.h"
#include "trie.h"
void test(trie_t *top, char * testfile){
FILE * tests = fopen(testfile, "r");
char buf[128];
int passed = 0;
int failed = 0;
while(!feof(tests)){
if(fgets(buf, 128, tests) == NULL){
if(ferror(tests)){
perror("file");
exit(1);
} else {
break;
}
}
if(strlen(buf) < 7){
break;
}
char * input_temp;
char * dest_temp;
input_temp = strtok(buf, "\t");
dest_temp = strtok(NULL, "\n");
uint32_t dest, input;
inet_pton(AF_INET, input_temp, &input);
inet_pton(AF_INET, dest_temp, &dest);
input = ntohl(input);
dest = ntohl(dest);
uint32_t res;
res = trie_lookup(top, input);
if(dest != res){
char ipbuf[INET_ADDRSTRLEN];
res = htonl(res);
inet_ntop(AF_INET, &res, ipbuf, INET_ADDRSTRLEN);
printf("lookup(%s)=\"%s\", expected \"%s\"\n", input_temp, ipbuf, dest_temp );
failed++;
} else {
passed++;
}
}
fclose(tests);
printf("%d of %d tests passed, %d tests failed.\n", passed, passed+failed, failed);
}