From 9e91d8f82629df8b0f25edf0d159d88215b9f0c7 Mon Sep 17 00:00:00 2001 From: hladu357 Date: Thu, 6 Jun 2024 11:49:08 +0200 Subject: [PATCH] endianness independent now --- trie/trie.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/trie/trie.c b/trie/trie.c index 1c542b5..e162be0 100644 --- a/trie/trie.c +++ b/trie/trie.c @@ -1,11 +1,11 @@ #include "trie.h" -#include #define RULES_FILE "../data/routing-data" #define TEST_FILE "../data/test-data" -#define TEST_ROWS 17217 +#define TEST_ROWS 70 /*17217*/ #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() { @@ -51,7 +51,7 @@ int trie_search(trie_holder_t t, ipv6_t key) { trie_node_t iter = t->_root; trie_node_t next; int i = 0; - int ret = -1; + int ret = RULE_NOT_FOUND; for (;i < 128; ++i) { if (iter->_rule_valid) @@ -66,9 +66,13 @@ int trie_search(trie_holder_t t, ipv6_t key) { } -/*------------------------------------------------------------*/ +/*--------------------------------------------------------------------------------*/ +/* TESTING */ +/*--------------------------------------------------------------------------------*/ +#include + int parse_line(ipv6_t* key, int* val, int* mask, FILE* f) { /*! assumes only good input */ 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[4], &ips[row].s[5], &ips[row].s[6], &ips[row].s[7]); + while (fgetc(f) != ' '); 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 row; + int wrong = 0; + 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() { ipv6_t* ips; - int* ref; - + int* ref; + trie_holder_t t = create_holder(); printf("Loaded %d rules\n", load_input(t) ); printf("Loaded %d tests\n", load_tests(&ips, &ref) ); - /* - fire_tests(t, ips, ref, TEST_ROWS); - */ + printf("%d tests wrong\n", fire_tests(t, ips, ref, TEST_ROWS) ); + return 0; } \ No newline at end of file