X-Git-Url: https://feistymeow.org/gitweb/?a=blobdiff_plain;f=nucleus%2Flibrary%2Ftests_algorithms%2Ftest_sorts.cpp;h=0edc61bb9e7a16d0ce59f389850bd124d3779504;hb=0cc8bcdf18d075ad1297a054e571570e75624e84;hp=9cec59a30c6cc8ac0fa5f9eca705b9cc8eaf2643;hpb=d7e8a05960951d8ce67e36c587726a851e2f5165;p=feisty_meow.git diff --git a/nucleus/library/tests_algorithms/test_sorts.cpp b/nucleus/library/tests_algorithms/test_sorts.cpp index 9cec59a3..0edc61bb 100644 --- a/nucleus/library/tests_algorithms/test_sorts.cpp +++ b/nucleus/library/tests_algorithms/test_sorts.cpp @@ -1,14 +1,14 @@ /* -* Name : test_sorts -* Author : Chris Koeritz -** -* Copyright (c) 1992-$now By Author. This program is free software; you can * -* redistribute it and/or modify it under the terms of the GNU General Public * -* License as published by the Free Software Foundation; either version 2 of * -* the License or (at your option) any later version. This is online at: * -* http://www.fsf.org/copyleft/gpl.html * -* Please send any updates to: fred@gruntose.com * -*/ + * Name : test_sorts + * Author : Chris Koeritz + ** + * Copyright (c) 1992-$now By Author. This program is free software; you can * + * redistribute it and/or modify it under the terms of the GNU General Public * + * License as published by the Free Software Foundation; either version 2 of * + * the License or (at your option) any later version. This is online at: * + * http://www.fsf.org/copyleft/gpl.html * + * Please send any updates to: fred@gruntose.com * + */ #include #include @@ -29,7 +29,8 @@ using namespace textual; using namespace timely; using namespace unit_test; -const int MAX_ELEMENTS = 1200; +const int MAX_ELEMENTS = 30; +//1200 const int MAX_VALUE = 28000; @@ -38,14 +39,21 @@ const int MAX_VALUE = 28000; class test_sorts : virtual public unit_base, virtual public application_shell { public: - test_sorts() : application_shell() {} - DEFINE_CLASS_NAME("test_sorts"); + test_sorts() + : application_shell() + { + } + DEFINE_CLASS_NAME("test_sorts") + ; - int *populate_random_c_array(int size); - basis::array populate_random_array(int size); - int test_shell_sort(int *list, int size); + int *populate_random_c_array(int size); + basis::array populate_random_array(int size); - virtual int execute(); + void test_shell_sort(int *list, int size); + void test_heap_sort(int *list, int size); + void test_merge_sort(basis::array &list); + + virtual int execute(); }; int *test_sorts::populate_random_c_array(int size) @@ -64,56 +72,115 @@ basis::array test_sorts::populate_random_array(int size) return to_return; } -int test_sorts::test_shell_sort(int *list, int size) +//hmmm: this pattern is very silly. it's nearly cookie cutter, so why not implement a templated version? +// one diff is the C array versus basis array usage. + +void test_sorts::test_shell_sort(int *list, int size) +{ + FUNCDEF("test_shell_sort"); + + // check a normal sort. + shell_sort(list, size); + int last = -1; + for (int j = 0; j < size; j++) { + ASSERT_FALSE(list[j] < last, "ordering check - list should be ordered at first check"); + last = list[j]; + } + + // re-randomize the list. + for (int i = 0; i < size; i++) + list[i] = randomizer().inclusive(0, MAX_VALUE); + + // check a reversed sort. + shell_sort(list, size, true); + last = MAX_VALUE + 100; // past the maximum we'll include in the list. + for (int j = 0; j < size; j++) { + ASSERT_FALSE(list[j] > last, "ordering check - list should be ordered at second check"); + last = list[j]; + } + + // clean up now. + delete[] list; +} + +void test_sorts::test_heap_sort(int *list, int size) +{ + FUNCDEF("test_heap_sort"); + + // check a normal sort. + heap_sort(list, size); + + int last = -1; + for (int j = 0; j < size; j++) { + ASSERT_FALSE(list[j] < last, "ordering check - list should be ordered at first check"); + last = list[j]; + } + + // re-randomize the list. + for (int i = 0; i < size; i++) + list[i] = randomizer().inclusive(0, MAX_VALUE); + + // check a reversed sort. + heap_sort(list, size, true); + + last = MAX_VALUE + 100; // past the maximum we'll include in the list. + for (int j = 0; j < size; j++) { + ASSERT_FALSE(list[j] > last, "ordering check - list should be ordered at second check"); + last = list[j]; + } + + // clean up now. + delete[] list; +} + +void test_sorts::test_merge_sort(basis::array &list) { - FUNCDEF("test_shell_sort"); - - // check a normal sort. - shell_sort(list, size); - int last = -1; - for (int j = 0; j < size; j++) { - ASSERT_FALSE(list[j] < last, "ordering check - list should be ordered at first check"); - last = list[j]; - } - - // re-randomize the list. - for (int i = 0; i < size; i++) - list[i] = randomizer().inclusive(0, MAX_VALUE); - - // check a reversed sort. - shell_sort(list, size, true); - last = MAX_VALUE + 100; // past the maximum we'll include in the list. - for (int j = 0; j < size; j++) { - ASSERT_FALSE(list[j] > last, "ordering check - list should be ordered at second check"); - last = list[j]; - } - - // clean up now. - delete [] list; - - //hmmm: wait, what if it fails above? - return true; + FUNCDEF("test_merge_sort"); + + // check a normal sort. + basis::array ret = merge_sort(list); + +// LOG(a_sprintf("list has %d elems", ret.length())); +// LOG(astring("list has ") + dump_list(ret.observe(), ret.length())); + + int last = -1; + for (int j = 0; j < list.length(); j++) { + ASSERT_FALSE(ret[j] < last, "ordering check - list should be ordered at first check"); + last = ret[j]; + } + + // re-randomize the list. + for (int i = 0; i < list.length(); i++) + list[i] = randomizer().inclusive(0, MAX_VALUE); + + // check a reversed sort. + ret = merge_sort(list, true); + + last = MAX_VALUE + 100; // past the maximum we'll include in the list. + for (int j = 0; j < list.length(); j++) { + ASSERT_FALSE(ret[j] > last, "ordering check - list should be ordered at second check"); + last = ret[j]; + } } int test_sorts::execute() { - FUNCDEF("execute"); + FUNCDEF("execute"); + + int size = MAX_ELEMENTS; - int size = MAX_ELEMENTS; + test_shell_sort(populate_random_c_array(size), size); -//astring ret; -//for (int i = 0; i < size; i++) ret += a_sprintf("%d ", list[i]); -//LOG(ret); -//LOG("-------------"); + test_heap_sort(populate_random_c_array(size), size); - test_shell_sort(populate_random_c_array(size), size); + basis::array testarray = populate_random_array(size); + test_merge_sort(testarray); -// test_quick_sort(populate_random_list(size), size); + // test_quick_sort(populate_random_array(size), size); -// test_merge_sort(populate_random_array(size)); - return final_report(); + return final_report(); } -HOOPLE_MAIN(test_sorts, ) +HOOPLE_MAIN(test_sorts,)