Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_sort_lists.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32
33# include <gsl/gsl_rng.h>
34# include <cassert>
35# include <cerrno>
36# include <climits>
37# include <cstdlib>
38# include <iostream>
39# include <ahSort.H>
40# include <tpl_sort_utils.H>
41
42# include <ctime>
43using namespace std;
44
46
47template <template <typename T> class List,
48 typename T = long>
50{
52 for (int i = 0; i < n; ++i)
54
55 return ret_val;
56}
57
58template <template <typename T> class List,
59 typename T>
60bool verify_sort(const List<T> & list)
61{
62 T value = numeric_limits<T>::min();
63 return list.all([&value] (const T & item)
64 {
65 bool ret = value <= item;
66 value = item;
67 return ret;
68 });
69}
70
71template <template <typename T> class List,
72 typename T = long>
74{
76 for (int i = 0; i < n; ++i)
78
79 return ret_val;
80}
81
82template <template <typename T> class List,
83 typename T >
84bool verify_sort(const List<T*> & list)
85{
86 T value = numeric_limits<T>::min();
87 return list.all([&value] (T * ptr)
88 {
89 bool ret = value <= *ptr;
90 value = *ptr;
91 return ret;
92 });
93}
94
95template <template <typename T> class List, typename T>
97{
98 list.for_each([] (T * ptr)
99 {
100 delete ptr;
101 });
102}
103
104int main(int argc, char *argv[])
105{
106 unsigned long n = 1000;
107 if (argc > 1)
108 {
109 errno = 0;
110 char * endptr = nullptr;
111 const unsigned long parsed = strtoul(argv[1], &endptr, 10);
112 if (errno != 0 or endptr == argv[1] or *endptr != '\0' or
113 parsed > static_cast<unsigned long>(INT_MAX))
114 {
115 cerr << "Invalid n: must be a non-negative integer <= "
116 << INT_MAX << endl;
117 return 1;
118 }
119 n = parsed;
120 }
121
122 unsigned int t = std::time(NULL);
123 if (argc > 2)
124 {
125 errno = 0;
126 char * endptr = nullptr;
127 const unsigned long parsed_t = strtoul(argv[2], &endptr, 10);
128 if (errno != 0 or endptr == argv[2] or *endptr != '\0' or
130 {
131 cerr << "Invalid t: " << argv[2] << endl;
132 return 1;
133 }
134 t = static_cast<unsigned int>(parsed_t);
135 }
136
137 cout << argv[0] << " " << n << " " << t << endl;
138
140 gsl_rng_set(r, t % gsl_rng_max(r));
141
142 {
143 cout << "Testing quicksort on single lists" << endl
144 << "Building list ... " << endl;
146 cout << "sorting it ..." << endl;
147 quicksort(l);
148 cout << "done! " << endl
149 << "Verifying ... " << endl;
151 assert(l.length() == n);
152 cout << "done!" << endl
153 << endl;
154 }
155
156 {
157 cout << "Testing quicksort on single lists of pointers" << endl
158 << "Building list ... " << endl;
160 cout << "sorting it ..." << endl;
161 quicksort(l, [] (long * x, long * y)
162 {
163 return *x < *y;
164 });
165 cout << "done! " << endl
166 << "Verifying ... " << endl;
168 assert(l.length() == n);
169 cout << "done!" << endl
170 << endl;
172 }
173
174 {
175 cout << "Testing mergesort on single lists" << endl
176 << "Building list ... " << endl;
178 cout << "sorting it ..." << endl;
179 mergesort(l);
180 cout << "done! " << endl
181 << "Verifying ... " << endl;
183 assert(l.length() == n);
184 cout << "done!" << endl
185 << endl;
186 }
187
188 {
189 cout << "Testing mergesort on single lists of pointers" << endl
190 << "Building list ... " << endl;
192 cout << "sorting it ..." << endl;
193 mergesort(l, [] (long * x, long * y)
194 {
195 return *x < *y;
196 });
197 cout << "done! " << endl
198 << "Verifying ... " << endl;
200 assert(l.length() == n);
201 cout << "done!" << endl
202 << endl;
204 }
205
206 {
207 cout << "Testing default sort method on single lists" << endl
208 << "Building list ... " << endl;
210 cout << "sorting it ..." << endl;
211 auto sorted = sort(l);
212 cout << "done! " << endl
213 << "Verifying ... " << endl;
215 assert(sorted.length() == n);
216 cout << "done!" << endl
217 << endl;
218 }
219
220 gsl_rng_free(r);
221 return 0;
222}
High-level sorting functions for Aleph containers.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1155
size_t length() const noexcept
Count the number of elements of a container.
Definition ah-dry.H:1725
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
static mpfr_t y
Definition mpfr_mul_d.c:3
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:234
void quicksort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using iterative quicksort with optimizations.
void mergesort(T *a, const long l, const long r, Array< T > &buf, Compare cmp)
Sort an array using merge sort with a reusable buffer.
STL namespace.
Dnode< unsigned > List
Definition testMerge.C:39
gsl_rng * r
List< T > build_int_list(int n)
List< T * > build_ptr_list(int n)
void free_ptr_list(List< T * > &list)
bool verify_sort(const List< T > &list)
Comprehensive sorting algorithms and search utilities for Aleph-w.
DynList< int > l