first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / tests_algorithms / test_sorts.cpp
1 /*
2 *  Name   : test_sorts
3 *  Author : Chris Koeritz
4 **
5 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
6 * redistribute it and/or modify it under the terms of the GNU General Public  *
7 * License as published by the Free Software Foundation; either version 2 of   *
8 * the License or (at your option) any later version.  This is online at:      *
9 *     http://www.fsf.org/copyleft/gpl.html                                    *
10 * Please send any updates to: fred@gruntose.com                               *
11 */
12
13 #include <algorithms/shell_sort.h>
14 #include <application/hoople_main.h>
15 #include <basis/functions.h>
16 #include <basis/guards.h>
17 #include <loggers/console_logger.h>
18 #include <mathematics/chaos.h>
19 #include <structures/static_memory_gremlin.h>
20 #include <unit_test/unit_base.h>
21
22 using namespace algorithms;
23 using namespace application;
24 using namespace basis;
25 using namespace loggers;
26 using namespace mathematics;
27 using namespace structures;
28 using namespace textual;
29 using namespace timely;
30 using namespace unit_test;
31
32 const int MAX_ELEMENTS = 1200;
33
34 const int MAX_VALUE = 28000;
35
36 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger().get(), to_print)
37
38 class test_sorts : virtual public unit_base, virtual public application_shell
39 {
40 public:
41   test_sorts() : application_shell() {}
42   DEFINE_CLASS_NAME("test_sorts");
43   virtual int execute();
44 };
45
46 int test_sorts::execute()
47 {
48   FUNCDEF("execute");
49
50   int *list = new int[MAX_ELEMENTS];
51   for (int i = 0; i < MAX_ELEMENTS; i++)
52     list[i] = randomizer().inclusive(0, MAX_VALUE);
53
54 //astring ret;
55 //for (int i = 0; i < MAX_ELEMENTS; i++) ret += a_sprintf("%d ", list[i]);
56 //LOG(ret);
57 //LOG("-------------");
58
59   // check a normal sort.
60   shell_sort(list, MAX_ELEMENTS);
61   int last = -1;
62   for (int j = 0; j < MAX_ELEMENTS; j++) {
63     ASSERT_FALSE(list[j] < last, "ordering check - list should be ordered at first check");
64     last = list[j];
65   }
66
67   // re-randomize the list.
68   for (int i = 0; i < MAX_ELEMENTS; i++)
69     list[i] = randomizer().inclusive(0, MAX_VALUE);
70
71   // check a reversed sort.
72   shell_sort(list, MAX_ELEMENTS, true);
73   last = MAX_VALUE + 100;  // past the maximum we'll include in the list.
74   for (int j = 0; j < MAX_ELEMENTS; j++) {
75     ASSERT_FALSE(list[j] > last, "ordering check - list should be ordered at second check");
76     last = list[j];
77   }
78
79   // clean up now.
80   delete [] list;
81
82   return final_report();
83 }
84
85 HOOPLE_MAIN(test_sorts, )
86