source: mainline/uspace/lib/c/test/qsort.c@ d73d992

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d73d992 was 3f932a7e, checked in by Jiri Svoboda <jiri@…>, 7 years ago

PCUT_INIT declaration also needs a terminating semicolon.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * Copyright (c) 2017 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <pcut/pcut.h>
30#include <qsort.h>
31#include <stdio.h>
32#include <stdlib.h>
33
34enum {
35 /** Length of test number sequences */
36 test_seq_len = 5
37};
38
39/** Test compare function.
40 *
41 * @param a First key
42 * @param b Second key
43 * @return <0, 0, >0 if @a a is less than, equal or greater than @a b
44 */
45static int test_cmp(const void *a, const void *b)
46{
47 int *ia = (int *)a;
48 int *ib = (int *)b;
49
50 return *ia - *ib;
51}
52
53static void bubble_sort(int *seq, size_t nmemb)
54{
55 size_t i;
56 size_t j;
57 int t;
58
59 for (i = 0; i < nmemb - 1; i++)
60 for (j = 0; j < nmemb - 1; j++) {
61 if (seq[j] > seq[j + 1]) {
62 t = seq[j];
63 seq[j] = seq[j + 1];
64 seq[j + 1] = t;
65 }
66 }
67}
68
69PCUT_INIT;
70
71PCUT_TEST_SUITE(qsort);
72
73/** Test sorting already sorted increasing sequence */
74PCUT_TEST(incr_seq)
75{
76 int *seq;
77 int i;
78
79 seq = calloc(test_seq_len, sizeof(int));
80 PCUT_ASSERT_NOT_NULL(seq);
81
82 for (i = 0; i < test_seq_len; i++)
83 seq[i] = i;
84
85 qsort(seq, test_seq_len, sizeof(int), test_cmp);
86
87 for (i = 0; i < test_seq_len; i++) {
88 PCUT_ASSERT_INT_EQUALS(i, seq[i]);
89 }
90
91 free(seq);
92}
93
94/** Test sorting reverse-sorted decreasing sequence. */
95PCUT_TEST(decr_seq)
96{
97 int *seq;
98 int i;
99
100 seq = calloc(test_seq_len, sizeof(int));
101 PCUT_ASSERT_NOT_NULL(seq);
102
103 for (i = 0; i < test_seq_len; i++)
104 seq[i] = test_seq_len - 1 - i;
105
106 qsort(seq, test_seq_len, sizeof(int), test_cmp);
107
108 for (i = 0; i < test_seq_len; i++) {
109 PCUT_ASSERT_INT_EQUALS(i, seq[i]);
110 }
111
112 free(seq);
113}
114
115/** Test sorting reverse-sorted decreasing sequence where each member repeats twice. */
116PCUT_TEST(decr2_seq)
117{
118 int *seq;
119 int i;
120
121 seq = calloc(test_seq_len, sizeof(int));
122 PCUT_ASSERT_NOT_NULL(seq);
123
124 for (i = 0; i < test_seq_len; i++) {
125 seq[i] = (test_seq_len - 1 - i) / 2;
126 }
127
128 qsort(seq, test_seq_len, sizeof(int), test_cmp);
129
130 for (i = 0; i < test_seq_len; i++) {
131 PCUT_ASSERT_INT_EQUALS(i / 2, seq[i]);
132 }
133
134 free(seq);
135}
136
137/** Generate pseudorandom sequence. */
138static int seq_next(int cur)
139{
140 return (cur * 1951) % 1000000;
141}
142
143/** Test sorting pseudorandom sequence. */
144PCUT_TEST(pseudorandom_seq)
145{
146 int *seq, *seq2;
147 int i;
148 int v;
149
150 seq = calloc(test_seq_len, sizeof(int));
151 PCUT_ASSERT_NOT_NULL(seq);
152
153 seq2 = calloc(test_seq_len, sizeof(int));
154 PCUT_ASSERT_NOT_NULL(seq);
155
156 v = 1;
157 for (i = 0; i < test_seq_len; i++) {
158 seq[i] = v;
159 seq2[i] = v;
160 v = seq_next(v);
161 }
162
163 qsort(seq, test_seq_len, sizeof(int), test_cmp);
164 bubble_sort(seq2, test_seq_len);
165
166 for (i = 0; i < test_seq_len; i++) {
167 PCUT_ASSERT_INT_EQUALS(seq2[i], seq[i]);
168 }
169
170 free(seq);
171 free(seq2);
172}
173
174PCUT_EXPORT(qsort);
Note: See TracBrowser for help on using the repository browser.