Annotation of embedaddon/bird2/nest/a-set_test.c, revision 1.1
1.1 ! misho 1: /*
! 2: * BIRD -- Set/Community-list Operations Tests
! 3: *
! 4: * (c) 2015 CZ.NIC z.s.p.o.
! 5: *
! 6: * Can be freely distributed and used under the terms of the GNU GPL.
! 7: */
! 8:
! 9: #include "test/birdtest.h"
! 10: #include "test/bt-utils.h"
! 11:
! 12: #include "lib/net.h"
! 13: #include "nest/route.h"
! 14: #include "nest/attrs.h"
! 15: #include "lib/resource.h"
! 16:
! 17: #define SET_SIZE 10
! 18: static const struct adata *set_sequence; /* <0; SET_SIZE) */
! 19: static const struct adata *set_sequence_same; /* <0; SET_SIZE) */
! 20: static const struct adata *set_sequence_higher; /* <SET_SIZE; 2*SET_SIZE) */
! 21: static const struct adata *set_random;
! 22:
! 23: #define BUFFER_SIZE 1000
! 24: static byte buf[BUFFER_SIZE] = {};
! 25:
! 26: #define SET_SIZE_FOR_FORMAT_OUTPUT 10
! 27:
! 28: struct linpool *lp;
! 29:
! 30: enum set_type
! 31: {
! 32: SET_TYPE_INT,
! 33: SET_TYPE_EC
! 34: };
! 35:
! 36: static void
! 37: generate_set_sequence(enum set_type type, int len)
! 38: {
! 39: struct adata empty_as_path = {};
! 40: set_sequence = set_sequence_same = set_sequence_higher = set_random = &empty_as_path;
! 41: lp = lp_new_default(&root_pool);
! 42:
! 43: int i;
! 44: for (i = 0; i < len; i++)
! 45: {
! 46: if (type == SET_TYPE_INT)
! 47: {
! 48: set_sequence = int_set_add(lp, set_sequence, i);
! 49: set_sequence_same = int_set_add(lp, set_sequence_same, i);
! 50: set_sequence_higher = int_set_add(lp, set_sequence_higher, i + SET_SIZE);
! 51: set_random = int_set_add(lp, set_random, bt_random());
! 52: }
! 53: else if (type == SET_TYPE_EC)
! 54: {
! 55: set_sequence = ec_set_add(lp, set_sequence, i);
! 56: set_sequence_same = ec_set_add(lp, set_sequence_same, i);
! 57: set_sequence_higher = ec_set_add(lp, set_sequence_higher, i + SET_SIZE);
! 58: set_random = ec_set_add(lp, set_random, (bt_random() << 32 | bt_random()));
! 59: }
! 60: else
! 61: bt_abort_msg("This should be unreachable");
! 62: }
! 63: }
! 64:
! 65: /*
! 66: * SET INT TESTS
! 67: */
! 68:
! 69: static int
! 70: t_set_int_contains(void)
! 71: {
! 72: int i;
! 73:
! 74: resource_init();
! 75: generate_set_sequence(SET_TYPE_INT, SET_SIZE);
! 76:
! 77: bt_assert(int_set_get_size(set_sequence) == SET_SIZE);
! 78:
! 79: for (i = 0; i < SET_SIZE; i++)
! 80: bt_assert(int_set_contains(set_sequence, i));
! 81: bt_assert(int_set_contains(set_sequence, -1) == 0);
! 82: bt_assert(int_set_contains(set_sequence, SET_SIZE) == 0);
! 83:
! 84: int *data = int_set_get_data(set_sequence);
! 85: for (i = 0; i < SET_SIZE; i++)
! 86: bt_assert_msg(data[i] == i, "(data[i] = %d) == i = %d)", data[i], i);
! 87:
! 88: rfree(lp);
! 89: return 1;
! 90: }
! 91:
! 92: static int
! 93: t_set_int_union(void)
! 94: {
! 95: resource_init();
! 96: generate_set_sequence(SET_TYPE_INT, SET_SIZE);
! 97:
! 98: const struct adata *set_union;
! 99: set_union = int_set_union(lp, set_sequence, set_sequence_same);
! 100: bt_assert(int_set_get_size(set_union) == SET_SIZE);
! 101: bt_assert(int_set_format(set_union, 0, 2, buf, BUFFER_SIZE) == 0);
! 102:
! 103: set_union = int_set_union(lp, set_sequence, set_sequence_higher);
! 104: bt_assert_msg(int_set_get_size(set_union) == SET_SIZE*2, "int_set_get_size(set_union) %d, SET_SIZE*2 %d", int_set_get_size(set_union), SET_SIZE*2);
! 105: bt_assert(int_set_format(set_union, 0, 2, buf, BUFFER_SIZE) == 0);
! 106:
! 107: rfree(lp);
! 108: return 1;
! 109: }
! 110:
! 111: static int
! 112: t_set_int_format(void)
! 113: {
! 114: resource_init();
! 115: generate_set_sequence(SET_TYPE_INT, SET_SIZE_FOR_FORMAT_OUTPUT);
! 116:
! 117: bt_assert(int_set_format(set_sequence, 0, 0, buf, BUFFER_SIZE) == 0);
! 118: bt_assert(strcmp(buf, "0.0.0.0 0.0.0.1 0.0.0.2 0.0.0.3 0.0.0.4 0.0.0.5 0.0.0.6 0.0.0.7 0.0.0.8 0.0.0.9") == 0);
! 119:
! 120: bzero(buf, BUFFER_SIZE);
! 121: bt_assert(int_set_format(set_sequence, 0, 2, buf, BUFFER_SIZE) == 0);
! 122: bt_assert(strcmp(buf, "0.0.0.2 0.0.0.3 0.0.0.4 0.0.0.5 0.0.0.6 0.0.0.7 0.0.0.8 0.0.0.9") == 0);
! 123:
! 124: bzero(buf, BUFFER_SIZE);
! 125: bt_assert(int_set_format(set_sequence, 1, 0, buf, BUFFER_SIZE) == 0);
! 126: bt_assert(strcmp(buf, "(0,0) (0,1) (0,2) (0,3) (0,4) (0,5) (0,6) (0,7) (0,8) (0,9)") == 0);
! 127:
! 128: rfree(lp);
! 129: return 1;
! 130: }
! 131:
! 132: static int
! 133: t_set_int_delete(void)
! 134: {
! 135: resource_init();
! 136: generate_set_sequence(SET_TYPE_INT, SET_SIZE);
! 137:
! 138: const struct adata *deleting_sequence = set_sequence;
! 139: u32 i;
! 140: for (i = 0; i < SET_SIZE; i++)
! 141: {
! 142: deleting_sequence = int_set_del(lp, deleting_sequence, i);
! 143: bt_assert_msg(int_set_get_size(deleting_sequence) == (int) (SET_SIZE-1-i),
! 144: "int_set_get_size(deleting_sequence) %d == SET_SIZE-1-i %d",
! 145: int_set_get_size(deleting_sequence),
! 146: SET_SIZE-1-i);
! 147: }
! 148:
! 149: bt_assert(int_set_get_size(set_sequence) == SET_SIZE);
! 150:
! 151: return 1;
! 152: }
! 153:
! 154: /*
! 155: * SET EC TESTS
! 156: */
! 157:
! 158: static int
! 159: t_set_ec_contains(void)
! 160: {
! 161: u32 i;
! 162:
! 163: resource_init();
! 164: generate_set_sequence(SET_TYPE_EC, SET_SIZE);
! 165:
! 166: bt_assert(ec_set_get_size(set_sequence) == SET_SIZE);
! 167:
! 168: for (i = 0; i < SET_SIZE; i++)
! 169: bt_assert(ec_set_contains(set_sequence, i));
! 170: bt_assert(ec_set_contains(set_sequence, -1) == 0);
! 171: bt_assert(ec_set_contains(set_sequence, SET_SIZE) == 0);
! 172:
! 173: // int *data = ec_set_get_data(set_sequence);
! 174: // for (i = 0; i < SET_SIZE; i++)
! 175: // bt_assert_msg(data[i] == (SET_SIZE-1-i), "(data[i] = %d) == ((SET_SIZE-1-i) = %d)", data[i], SET_SIZE-1-i);
! 176:
! 177: rfree(lp);
! 178: return 1;
! 179: }
! 180:
! 181: static int
! 182: t_set_ec_union(void)
! 183: {
! 184: resource_init();
! 185: generate_set_sequence(SET_TYPE_EC, SET_SIZE);
! 186:
! 187: const struct adata *set_union;
! 188: set_union = ec_set_union(lp, set_sequence, set_sequence_same);
! 189: bt_assert(ec_set_get_size(set_union) == SET_SIZE);
! 190: bt_assert(ec_set_format(set_union, 0, buf, BUFFER_SIZE) == 0);
! 191:
! 192: set_union = ec_set_union(lp, set_sequence, set_sequence_higher);
! 193: bt_assert_msg(ec_set_get_size(set_union) == SET_SIZE*2, "ec_set_get_size(set_union) %d, SET_SIZE*2 %d", ec_set_get_size(set_union), SET_SIZE*2);
! 194: bt_assert(ec_set_format(set_union, 0, buf, BUFFER_SIZE) == 0);
! 195:
! 196: rfree(lp);
! 197: return 1;
! 198: }
! 199:
! 200: static int
! 201: t_set_ec_format(void)
! 202: {
! 203: resource_init();
! 204:
! 205: const struct adata empty_as_path = {};
! 206: set_sequence = set_sequence_same = set_sequence_higher = set_random = &empty_as_path;
! 207: lp = lp_new_default(&root_pool);
! 208:
! 209: u64 i = 0;
! 210: set_sequence = ec_set_add(lp, set_sequence, i);
! 211: for (i = 1; i < SET_SIZE_FOR_FORMAT_OUTPUT; i++)
! 212: set_sequence = ec_set_add(lp, set_sequence, i + ((i%2) ? ((u64)EC_RO << 48) : ((u64)EC_RT << 48)));
! 213:
! 214: bt_assert(ec_set_format(set_sequence, 0, buf, BUFFER_SIZE) == 0);
! 215: bt_assert_msg(strcmp(buf, "(unknown 0x0, 0, 0) (ro, 0, 1) (rt, 0, 2) (ro, 0, 3) (rt, 0, 4) (ro, 0, 5) (rt, 0, 6) (ro, 0, 7) (rt, 0, 8) (ro, 0, 9)") == 0,
! 216: "ec_set_format() returns '%s'", buf);
! 217:
! 218: rfree(lp);
! 219: return 1;
! 220: }
! 221:
! 222: static int
! 223: t_set_ec_delete(void)
! 224: {
! 225: resource_init();
! 226: generate_set_sequence(SET_TYPE_EC, SET_SIZE);
! 227:
! 228: const struct adata *deleting_sequence = set_sequence;
! 229: u32 i;
! 230: for (i = 0; i < SET_SIZE; i++)
! 231: {
! 232: deleting_sequence = ec_set_del(lp, deleting_sequence, i);
! 233: bt_assert_msg(ec_set_get_size(deleting_sequence) == (int) (SET_SIZE-1-i),
! 234: "ec_set_get_size(deleting_sequence) %d == SET_SIZE-1-i %d",
! 235: ec_set_get_size(deleting_sequence), SET_SIZE-1-i);
! 236: }
! 237:
! 238: bt_assert(ec_set_get_size(set_sequence) == SET_SIZE);
! 239:
! 240: return 1;
! 241: }
! 242:
! 243: int
! 244: main(int argc, char *argv[])
! 245: {
! 246: bt_init(argc, argv);
! 247:
! 248: bt_test_suite(t_set_int_contains, "Testing sets of integers: contains, get_data");
! 249: bt_test_suite(t_set_int_format, "Testing sets of integers: format");
! 250: bt_test_suite(t_set_int_union, "Testing sets of integers: union");
! 251: bt_test_suite(t_set_int_delete, "Testing sets of integers: delete");
! 252:
! 253: bt_test_suite(t_set_ec_contains, "Testing sets of Extended Community values: contains, get_data");
! 254: bt_test_suite(t_set_ec_format, "Testing sets of Extended Community values: format");
! 255: bt_test_suite(t_set_ec_union, "Testing sets of Extended Community values: union");
! 256: bt_test_suite(t_set_ec_delete, "Testing sets of Extended Community values: delete");
! 257:
! 258: return bt_exit_value();
! 259: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>