#include #include #include #include #include #include "ordq.h" #include "unity.h" #include "utils.h" static int long_cmp(void* ctx, void* k1, void* k2) { if ((long)k1 < (long)k2) { return -1; } else if ((long)k1 > (long)k2) { return 1; } return 0; } static void test_ordq_get(void) { int rc; ordq_t q; ordq_item_t* it; int keys[8] = { 9, 8, 1, 4, 7, 2, 6, 2 }; int val[8] = { 1, 2, 2, 4, 6, 7, 8, 9 }; int i; rc = ordq_init(&q, NULL, (ordq_cmp_fn_t)long_cmp); TEST_ASSERT_TRUE(rc >= 0); for (i = 0; i < 8; i++) { ordq_put(&q, (void*)(long)(keys[i])); } for (i = 0; i < 8; i++) { it = ordq_get(&q, NULL); TEST_ASSERT_TRUE((it != NULL) && ((int)(long)it->data == val[i])); free(it); } ordq_fini(&q); return; } static int test_match(void* data, va_list ap) { int val = (int)(long)data; return (val % 2 == 0); } static void test_ordq_get_match(void) { int rc; ordq_t q; ordq_item_t* it; int keys[8] = { 9, 8, 1, 4, 7, 2, 6, 2 }; int val[8] = { 1, 2, 2, 4, 6, 7, 8, 9 }; int i, j, nc; rc = ordq_init(&q, NULL, (ordq_cmp_fn_t)long_cmp); TEST_ASSERT_TRUE(rc >= 0); for (i = 0; i < 8; i++) { ordq_put(&q, (void*)(long)(keys[i])); } j = 0; nc = 0; for (i = 0; i < 5; i++) { it = ordq_get(&q, test_match); if (it == NULL) { break; } nc++; while ((j < 8) && (val[j] % 2 != 0)) { j++; } TEST_ASSERT_TRUE((int)(long)it->data == (val[j])); free(it); j++; } TEST_ASSERT_TRUE(nc == 5); ordq_fini(&q); return; } static void test_ordq_peek(void) { int rc; ordq_t q; ordq_item_t* it; int keys[8] = { 9, 8, 1, 4, 7, 2, 6, 2 }; int val[8] = { 1, 2, 2, 4, 6, 7, 8, 9 }; int i; rc = ordq_init(&q, NULL, (ordq_cmp_fn_t)long_cmp); TEST_ASSERT_TRUE(rc >= 0); for (i = 0; i < 8; i++) { ordq_put(&q, (void*)(long)(keys[i])); } for (i = 0; i < 8; i++) { it = ordq_peek(&q, NULL); /* ordq_peek() will not remove item from q */ TEST_ASSERT_TRUE((it != NULL) && ((int)(long)it->data == (val[0]))); } ordq_fini(&q); return; } static int test_match2(void* data, va_list ap) { int val = (int)(long)data; return (val % 3 == 0); } static void test_ordq_peek_match(void) { int rc; ordq_t q; ordq_item_t* it; int keys[8] = { 9, 8, 1, 4, 7, 2, 6, 2 }; int val[8] = { 1, 2, 2, 4, 6, 7, 8, 9 }; int i; rc = ordq_init(&q, NULL, (ordq_cmp_fn_t)long_cmp); TEST_ASSERT_TRUE(rc >= 0); for (i = 0; i < 8; i++) { ordq_put(&q, (void*)(long)(keys[i])); } for (i = 0; i < 8; i++) { it = ordq_peek( &q, test_match2); /* ordq_peek() will not remove item from q */ TEST_ASSERT_TRUE((it != NULL) && ((int)(long)it->data == val[4])); } ordq_fini(&q); return; } static void test_ordq_del(void) { int rc; ordq_t q; ordq_item_t* it; int keys[8] = { 9, 8, 1, 4, 7, 2, 6, 2 }; int i; int count; rc = ordq_init(&q, NULL, (ordq_cmp_fn_t)long_cmp); TEST_ASSERT_TRUE(rc >= 0); for (i = 0; i < 8; i++) { ordq_put(&q, (void*)(long)(keys[i])); } count = ordq_del(&q, test_match2); TEST_ASSERT_TRUE(count == 2); while ((it = ordq_get(&q, NULL)) != NULL) { TEST_ASSERT_TRUE((int)(long)it->data % 3 != 0); free(it); } ordq_fini(&q); return; } void test_ordq(void) { printf("\n>> ordq test\n\n"); RUN_TEST(test_ordq_get); RUN_TEST(test_ordq_peek); RUN_TEST(test_ordq_get_match); RUN_TEST(test_ordq_peek_match); RUN_TEST(test_ordq_del); }