pelib  2.0.0
src/test_sort.c
Go to the documentation of this file.
00001 /*
00002  * test_array.c
00003  *
00004  *  Created on: 13 Jan 2012
00005  *  Copyright 2012 Nicolas Melot
00006  *
00007  * This file is part of pelib.
00008  * 
00009  *     pelib is free software: you can redistribute it and/or modify
00010  *     it under the terms of the GNU General Public License as published by
00011  *     the Free Software Foundation, either version 3 of the License, or
00012  *     (at your option) any later version.
00013  * 
00014  *     pelib is distributed in the hope that it will be useful,
00015  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *     GNU General Public License for more details.
00018  * 
00019  *     You should have received a copy of the GNU General Public License
00020  *     along with pelib. If not, see <http://www.gnu.org/licenses/>.
00021  * 
00022  */
00023 
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <math.h>
00027 
00028 #include <pelib/unit.h>
00029 #include <pelib/integer.h>
00030 #include <pelib/sort.h>
00031 
00032 #define LENGTH 1024
00033 #define RANDOM_SEED     0
00034 #define MAX_VALUE LENGTH
00035 
00036 array_t(int) *prand, *increasing, *unsorted;
00037 int value;
00038 
00039 int
00040 pseudo_randomize(int i, int max)
00041 {
00042         double res;
00043 
00044         res = (sin((double) (i * max) / (double) (max * 2 * M_PI * 0.7)) + 1) / 2
00045                         * max;
00046 
00047         return (int) res;
00048 }
00049 
00050 void
00051 setup()
00052 {
00053         // do nothing
00054 }
00055 
00056 void
00057 teardown()
00058 {
00059         // do nothing
00060 }
00061 
00062 void
00063 init()
00064 {
00065         int i;
00066 
00067         // Initialize pseudo-random array
00068         srandom(RANDOM_SEED);
00069         prand = pelib_alloc_collection(array_t(int))(LENGTH);
00070         for (i = 0; i < LENGTH; i++)
00071         {
00072                 pelib_array_append(int)(prand, random() % MAX_VALUE);
00073         }
00074 
00075         // Initialize increasing sorted array
00076         increasing = pelib_alloc_collection(array_t(int))(LENGTH);
00077         for (i = 0; i < LENGTH; i++)
00078         {
00079                 pelib_array_append(int)(increasing, i);
00080         }
00081 
00082         // Initialize increasing sorted array
00083         unsorted = pelib_alloc_collection(array_t(int))(LENGTH);
00084         for (i = 0; i < LENGTH; i++)
00085         {
00086                 pelib_array_append(int)(unsorted, pseudo_randomize(i, MAX_VALUE));
00087         }
00088 }
00089 
00090 void
00091 cleanup()
00092 {
00093         pelib_free(array_t(int))(prand);
00094         pelib_free(array_t(int))(increasing);
00095         pelib_free(array_t(int))(unsorted);
00096 }
00097 
00098 static void
00099 test_is_increasing()
00100 {
00101         assert(pelib_is_increasing(increasing));
00102         assert_false(pelib_is_increasing(unsorted));
00103 }
00104 
00105 static void
00106 test_quicksort()
00107 {
00108         pelib_quicksort(unsorted);
00109         assert(pelib_is_increasing(unsorted));
00110 }
00111 
00112 /*
00113 static void
00114 test_insertsort()
00115 {
00116         // do nothing
00117 }
00118 */
00119 
00120 void
00121 run()
00122 {
00123         test(test_is_increasing);
00124         test(test_quicksort);
00125         //test(test_insertsort);
00126 }