diff --git a/tests/core/Sconscript b/tests/core/Sconscript index 0e8dd92f..c15f76cd 100644 --- a/tests/core/Sconscript +++ b/tests/core/Sconscript @@ -24,11 +24,11 @@ res += [ CoreTest("idle", "idle.cpp") ] res += SConscript("tags/Sconscript") +res += [ CoreTest("queue", "queue.cpp") ] + Return("res") # -#objs += [ get_test_obj("tags/tags", "core") ] -#test( "queue" ) #test_env.UsePackage("taglib") #test( "library" ) #test( "playlist" ) diff --git a/tests/core/queue.cpp b/tests/core/queue.cpp index 66d67381..a6ee7380 100644 --- a/tests/core/queue.cpp +++ b/tests/core/queue.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include "test.h" unsigned int count_add = 0; @@ -35,8 +35,6 @@ public: std::vector get_sorder() { return _sort_order; }; }; -TestQueue *Q = NULL, *R = NULL; - void test_default() { @@ -84,98 +82,77 @@ void test_flags() } -unsigned int _test_add_loop(unsigned int i) -{ - Track *track = tags :: get_track(i % 24); - unsigned int index = Q->add(track); - expected += track->length(); - if (index != i) - return LOOP_FAILED; - if ((index + 1) != count_add) - return LOOP_FAILED; - return LOOP_PASSED; -} - -unsigned int _test_del_loop(unsigned int i) -{ - Track *track = tags :: get_track(i); - unsigned int j = (i + 1) * 2; - - Q->del(track); - expected -= track->length() * 2; - if (count_del != j) - return LOOP_FAILED; - return LOOP_PASSED; -} - -unsigned int _test_del_middle_loop(unsigned int i) -{ - unsigned int j = 23 - i; - expected -= (*Q)[j]->length(); - Q->del(j); - return (count_del == (i + 1)) ? LOOP_PASSED : LOOP_FAILED; -} - -unsigned int _test_del_front_loop(unsigned int i) -{ - expected -= (*Q)[0]->length(); - Q->del((unsigned int)0); - return (count_del == (i + 1)) ? LOOP_PASSED : LOOP_FAILED; -} - void test_add_remove() { TestQueue q(0); - Q = &q; + Track *track; + + test_cp_data_dir(); + tags :: init(); test_equal(q.length(), expected); test_equal(q.size(), (unsigned)0); /* Add tracks */ - test_for_each(0, 24, 1, _test_add_loop); + for (unsigned int i = 0; i < 24; i++) { + track = tags :: get_track(i); + expected += track->length(); + test_loop_equal(q.add(track), i, i); + test_loop_equal(count_add, i + 1, i); + } test_loop_passed(); test_equal(q.length(), expected); test_equal(q.size(), (unsigned)24); /* Add everything again */ - test_for_each(24, 48, 1, _test_add_loop); + for (unsigned int i = 24; i < 48; i++) { + track = tags :: get_track(i - 24); + expected += track->length(); + test_loop_equal(q.add(track), i, i); + test_loop_equal(count_add, i + 1, i); + } test_loop_passed(); test_equal(q.length(), expected); test_equal(q.size(), (unsigned)48); /* Test removing multiple tracks at once */ count_del = 0; - test_for_each(0, 12, 1, _test_del_loop); + for (unsigned int i = 0; i < 12; i++) { + track = tags :: get_track(i); + q.del(track); + expected -= track->length() * 2; + test_loop_equal(count_del, (i + 1) * 2, i); + } test_equal(q.length(), expected); test_equal(q.size(), (unsigned)24); /* Test removing tracks one at a time */ count_del = 0; - test_for_each(0, 12, 1, _test_del_middle_loop); + for (unsigned int i = 0; i < 12; i++) { + expected -= q[23 - i]->length(); + q.del((unsigned int)(23 - i)); + test_loop_equal(count_del, (i + 1), i); + } test_loop_passed(); test_equal(q.length(), expected); test_equal(q.size(), (unsigned)12); /* Remove remaining tracks */ count_del = 0; - test_for_each(0, 12, 1, _test_del_front_loop); + for (unsigned int i = 0; i < 12; i++) { + expected -= q[0]->length(); + q.del((unsigned int)0); + test_loop_equal(count_del, i + 1, i); + } test_loop_passed(); test_equal(q.length(), (unsigned)0); test_equal(q.size(), (unsigned)0); } -static void test_fill_q() +static void test_fill_q(TestQueue *q) { for (unsigned int i = 0; i < 24; i++) - Q->add(tags :: get_track(i)); -} - -unsigned int _test_update_loop(unsigned int i) -{ - Q->updated(tags :: get_track(i)); - if (last_update != i) - return LOOP_FAILED; - return ((i+1) == count_updated) ? LOOP_PASSED : LOOP_FAILED; + q->add(tags :: get_track(i)); } void test_updated() @@ -183,10 +160,13 @@ void test_updated() Track *track; TestQueue q(0); - Q = &q; + test_fill_q(&q); - test_fill_q(); - test_for_each(0, 24, 1, _test_update_loop); + for (unsigned int i = 0; i < 24; i++) { + q.updated(tags :: get_track(i)); + test_loop_equal(last_update, i, i); + test_loop_equal(count_updated, i + 1, i); + } test_loop_passed(); track = tags :: get_track(0); q.add(track); @@ -195,34 +175,22 @@ void test_updated() test_equal(count_updated, (unsigned)24 + 3); } + unsigned int expected_rand[] = { 1, 4, 8, 13, 19, 3, 14, 16, 20, 2, 11, 17, 23, 6, 12, 18, 0, 5, 9, 10, 15, 21, 22, 7 }; - -unsigned int _test_next_loop(unsigned int i) -{ - Track *track = Q->next(); - if (track == TRACK_NULL) - return LOOP_FAILED; - return (track->index() == (i % 24)) ? LOOP_PASSED : LOOP_FAILED; -} - -unsigned int _test_next_rand_loop(unsigned int i) -{ - Track *track = Q->next(); - if (track == TRACK_NULL) - return LOOP_FAILED; - return (track->index() == expected_rand[i]) ? LOOP_PASSED : LOOP_FAILED; -} - void test_next() { + Track *track; TestQueue q(0); - Q = &q; - test_fill_q(); + test_fill_q(&q); - test_for_each(0, 24, 1, _test_next_loop); + for (unsigned int i = 0; i < 24; i++) { + track = q.next(); + test_loop_not_equal(track, NULL, i); + test_loop_equal(track->index(), i % 24, i); + } test_loop_passed(); test_equal(q.size(), (unsigned)0); test_equal(q.length(), 0); test_equal(q.next(), TRACK_NULL); @@ -230,9 +198,13 @@ void test_next() q.set_flag(Q_RANDOM); random_seed(0); - test_fill_q(); + test_fill_q(&q); - test_for_each(0, 24, 1, _test_next_rand_loop); + for (unsigned int i = 0; i < 24; i++) { + track = q.next(); + test_loop_not_equal(track, NULL, i); + test_loop_equal(track->index(), expected_rand[i], i); + } test_loop_passed(); test_equal(q.size(), (unsigned)0); test_equal(q.length(), 0); test_equal(q.next(), TRACK_NULL); @@ -240,9 +212,13 @@ void test_next() q.set_flag(Q_REPEAT); q.unset_flag(Q_RANDOM); - test_fill_q(); + test_fill_q(&q); - test_for_each(0, 48, 1, _test_next_loop); + for (unsigned int i = 0; i < 48; i++) { + track = q.next(); + test_loop_not_equal(track, NULL, i); + test_loop_equal(track->index(), i % 24, i); + } test_loop_passed(); test_equal(q.size(), (unsigned)24); } @@ -250,8 +226,7 @@ void test_select() { TestQueue q(0); - Q = &q; - test_fill_q(); + test_fill_q(&q); test_equal(q.size(), (unsigned)24); q.track_selected(10); @@ -270,75 +245,45 @@ unsigned int exp_sort_title[] = { 1, 18, 19, 16, 20, 8, 2, 9, 23, 10, 17, 11, unsigned int exp_sort_ye_ti[] = { 0, 3, 2, 1, 7, 6, 5, 4, 11, 10, 9, 8, 17, 16, 19, 18, 22, 21, 23, 20, 15, 14, 13, 12 }; -unsigned int _test_sort_title(unsigned int i) -{ - if ((*Q)[i]->index() == exp_sort_title[i]) - return LOOP_PASSED; - return LOOP_FAILED; -} - -unsigned int _test_sort_title_reverse(unsigned int i) -{ - if ((*Q)[i]->index() == exp_sort_title[23 - i]) - return LOOP_PASSED; - return LOOP_FAILED; -} - -unsigned int _test_sort_index(unsigned int i) -{ - if ((*Q)[i]->index() == i) - return LOOP_PASSED; - return LOOP_FAILED; -} - -unsigned int _test_sort_ye_ti(unsigned int i) -{ - if ((*Q)[i]->index() != exp_sort_ye_ti[i]) - return LOOP_FAILED; - return LOOP_PASSED; -} - void test_sorting() { TestQueue q(0); - Q = &q; q.sort(SORT_TITLE, true); test_equal(q.get_sorder().size(), (size_t)1); - test_fill_q(); - test_for_each(0, 24, 1, _test_sort_title); + test_fill_q(&q); + for (unsigned int i = 0; i < 24; i++) + test_loop_equal(q[i]->index(), exp_sort_title[i], i); + test_loop_passed(); q.sort(SORT_TITLE, false); test_equal(q.get_sorder().size(), (size_t)1); - test_for_each(0, 24, 1, _test_sort_title_reverse); + for (unsigned int i = 0; i < 24; i++) + test_loop_equal(q[i]->index(), exp_sort_title[23 - i], i); + test_loop_passed(); q.sort(SORT_LENGTH, true); - test_for_each(0, 1, 24, _test_sort_index); + for (unsigned int i = 0; i < 24; i++) + test_loop_equal(q[i]->index(), i, i); + test_loop_passed(); q.sort(SORT_YEAR, true); q.sort(SORT_TITLE, false); q.sort(SORT_TITLE, false); test_equal(q.get_sorder().size(), (size_t)2); - test_for_each(0, 24, 1, _test_sort_ye_ti); + for (unsigned int i = 0; i < 24; i++) + test_loop_equal(q[i]->index(), exp_sort_ye_ti[i], i); + test_loop_passed(); } -unsigned int _test_saving_loop(unsigned int i) -{ - if ((*Q)[i]->index() == (*R)[i]->index()) - return LOOP_PASSED; - return LOOP_FAILED; -} - void test_saving() { TestQueue q(Q_RANDOM); TestQueue r(0); File f("test.q", 0); - Q = &q; - R = &r; - test_fill_q(); + test_fill_q(&q); f.open(OPEN_WRITE); q.write(f); @@ -351,22 +296,20 @@ void test_saving() test_equal(r.has_flag(Q_RANDOM), q.has_flag(Q_RANDOM)); test_equal(r.size(), q.size()); test_equal(r.length(), q.length()); - test_for_each(0, 24, 1, _test_saving_loop); + + for (unsigned int i = 0; i < 24; i++) + test_loop_equal(q[i]->index(), r[i]->index(), i); + test_loop_passed(); } -int main(int argc, char **argv) -{ - test :: cp_data_dir(); - tags :: init(); - - test :: run("Queue Default Constructor Test", test_default); - test :: run("Queue Constructor Test", test_constructor); - test :: run("Queue Flag Test", test_flags); - test :: run("Queue Add and Remove Test", test_add_remove); - test :: run("Queue Track Updated Test", test_updated); - test :: run("Queue Pick Next Test", test_next); - test :: run("Queue Select Track Test", test_select); - test :: run("Queue Sorting Test", test_sorting); - test :: run("Queue Save and Load Test", test_saving); - return 0; -} +DECLARE_UNIT_TESTS( + UNIT_TEST("Queue Default Constructor", test_default), + UNIT_TEST("Queue Constructor", test_constructor), + UNIT_TEST("Queue Flags", test_flags), + UNIT_TEST("Queue Add and Remove", test_add_remove), + UNIT_TEST("Queue Track Updated", test_updated), + UNIT_TEST("Queue Pick Next Track", test_next), + UNIT_TEST("Queue Select Track", test_select), + UNIT_TEST("Queue Sorting", test_sorting), + UNIT_TEST("Queue Save and Load", test_saving), +); diff --git a/tests/core/tags/Sconscript b/tests/core/tags/Sconscript index 07476052..9d002032 100644 --- a/tests/core/tags/Sconscript +++ b/tests/core/tags/Sconscript @@ -17,4 +17,6 @@ res += [ TagTest("album", "album.cpp") ] res += [ TagTest("genre", "genre.cpp") ] res += [ TagTest("library", "library.cpp") ] res += [ TagTest("track", "track.cpp") ] + +core_objs += [ env.Object("../../../core/tags/tags.cpp") ] Return("res")