endianness independent now

This commit is contained in:
hladu357 2024-06-06 11:49:08 +02:00
parent 19304a2c57
commit 9e91d8f826
1 changed files with 21 additions and 12 deletions

View File

@ -1,11 +1,11 @@
#include "trie.h" #include "trie.h"
#include <assert.h>
#define RULES_FILE "../data/routing-data" #define RULES_FILE "../data/routing-data"
#define TEST_FILE "../data/test-data" #define TEST_FILE "../data/test-data"
#define TEST_ROWS 17217 #define TEST_ROWS 70 /*17217*/
#define READ_MODE "r" #define READ_MODE "r"
#define GET_BIT(key, bit) ( ( key.l[ bit / 64 ] & (1ul << (bit % 64)) ) != 0 ) #define RULE_NOT_FOUND -1
#define GET_BIT(key, bit) ( ( key.s[ bit / 16 ] & (1ul << (15 - bit % 16)) ) != 0 )
trie_node_t create_node() { trie_node_t create_node() {
@ -51,7 +51,7 @@ int trie_search(trie_holder_t t, ipv6_t key) {
trie_node_t iter = t->_root; trie_node_t iter = t->_root;
trie_node_t next; trie_node_t next;
int i = 0; int i = 0;
int ret = -1; int ret = RULE_NOT_FOUND;
for (;i < 128; ++i) { for (;i < 128; ++i) {
if (iter->_rule_valid) if (iter->_rule_valid)
@ -66,9 +66,13 @@ int trie_search(trie_holder_t t, ipv6_t key) {
} }
/*------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/
/* TESTING */
/*--------------------------------------------------------------------------------*/
#include <assert.h>
int parse_line(ipv6_t* key, int* val, int* mask, FILE* f) { /*! assumes only good input */ int parse_line(ipv6_t* key, int* val, int* mask, FILE* f) { /*! assumes only good input */
memset(key, 0, sizeof(ipv6_t)); memset(key, 0, sizeof(ipv6_t));
@ -121,6 +125,7 @@ int load_tests(ipv6_t** rips, int** rref) {
&ips[row].s[0], &ips[row].s[1], &ips[row].s[2], &ips[row].s[3], &ips[row].s[0], &ips[row].s[1], &ips[row].s[2], &ips[row].s[3],
&ips[row].s[4], &ips[row].s[5], &ips[row].s[6], &ips[row].s[7]); &ips[row].s[4], &ips[row].s[5], &ips[row].s[6], &ips[row].s[7]);
while (fgetc(f) != ' '); while (fgetc(f) != ' ');
fscanf(f, "%d", &(ref[row])); fscanf(f, "%d", &(ref[row]));
} }
@ -132,23 +137,27 @@ int load_tests(ipv6_t** rips, int** rref) {
int fire_tests(trie_holder_t t, ipv6_t* ips, int* ref, int num_tests ) { int fire_tests(trie_holder_t t, ipv6_t* ips, int* ref, int num_tests ) {
int row; int row;
int wrong = 0;
for (row = 0; row < TEST_ROWS; ++ row) { for (row = 0; row < TEST_ROWS; ++ row) {
printf("%d %d\n", trie_search(t, ips[row]), ref[row] ); if (trie_search(t, ips[row]) != ref[row]) {
printf("%d %d @ %d\n", trie_search(t, ips[row]), ref[row], row );
++wrong;
}
} }
return 1; return wrong;
} }
int main() { int main() {
ipv6_t* ips; ipv6_t* ips;
int* ref; int* ref;
trie_holder_t t = create_holder(); trie_holder_t t = create_holder();
printf("Loaded %d rules\n", load_input(t) ); printf("Loaded %d rules\n", load_input(t) );
printf("Loaded %d tests\n", load_tests(&ips, &ref) ); printf("Loaded %d tests\n", load_tests(&ips, &ref) );
/* printf("%d tests wrong\n", fire_tests(t, ips, ref, TEST_ROWS) );
fire_tests(t, ips, ref, TEST_ROWS);
*/
return 0; return 0;
} }