t_sorts.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_sorts                                                        *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include <basis/chaos.h>
00016 #include <basis/function.h>
00017 #include <basis/guards.h>
00018 #include <basis/shell_sort.h>
00019 #include <opsystem/application_shell.h>
00020 #include <loggers/console_logger.h>
00021 #include <data_struct/static_memory_gremlin.h>
00022 
00023 const int MAX_ELEMENTS = 1200;
00024 
00025 const int MAX_VALUE = 28000;
00026 
00027 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s)
00028 
00029 class test_sorts : public application_shell
00030 {
00031 public:
00032   test_sorts() : application_shell(class_name()) {}
00033   IMPLEMENT_CLASS_NAME("test_sorts");
00034   virtual int execute();
00035 };
00036 
00037 int test_sorts::execute()
00038 {
00039   FUNCDEF("execute");
00040 
00041   int *list = new int[MAX_ELEMENTS];
00042   for (int i = 0; i < MAX_ELEMENTS; i++)
00043     list[i] = randomizer().inclusive(0, MAX_VALUE);
00044 
00045 //istring ret;
00046 //for (int i = 0; i < MAX_ELEMENTS; i++) ret += isprintf("%d ", list[i]);
00047 //LOG(ret);
00048 //LOG("-------------");
00049 
00050   // check a normal sort.
00051   shell_sort(list, MAX_ELEMENTS);
00052   int last = -1;
00053   for (int j = 0; j < MAX_ELEMENTS; j++) {
00054     if (list[j] < last)
00055       deadly_error("t_sorts", "ordering check", "list is not ordered");
00056     last = list[j];
00057   }
00058 
00059   // re-randomize the list.
00060   for (int i = 0; i < MAX_ELEMENTS; i++)
00061     list[i] = randomizer().inclusive(0, MAX_VALUE);
00062 
00063   // check a reversed sort.
00064   shell_sort(list, MAX_ELEMENTS, true);
00065   last = MAX_VALUE + 100;  // past the maximum we'll include in the list.
00066   for (int j = 0; j < MAX_ELEMENTS; j++) {
00067     if (list[j] > last)
00068       deadly_error("t_sorts", "ordering check", "list is not ordered");
00069     last = list[j];
00070   }
00071 
00072   // clean up now.
00073   delete [] list;
00074 
00075   guards::alert_message("shell_sort:: works for those functions tested.");
00076   return 0;
00077 }
00078 
00079 HOOPLE_MAIN(test_sorts, )
00080 

Generated on Fri Nov 21 04:30:05 2008 for HOOPLE Libraries by  doxygen 1.5.1