YAC 3.13.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_mergesort.c
Go to the documentation of this file.
1// Copyright (c) 2024 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <stdlib.h>
6#include <stdio.h>
7
8#include "tests.h"
9#include "utils_core.h"
10
19
20 double dummy;
21 int idx;
22 double dummy_;
23};
24
25static int utest_compare_test_struct(const void * a, const void * b) {
26
27 int idx_a = ((struct test_struct *)a)->idx;
28 int idx_b = ((struct test_struct *)b)->idx;
29
30 return ((idx_a) > (idx_b)) - ((idx_a) < (idx_b));
31}
32
33int main (void) {
34
35 {
36 struct test_struct a[128];
37 struct test_struct ref[128];
38 size_t len = sizeof(a)/sizeof(a[0]);
39
40 for (size_t i = 0; i < len; ++i) {
41 a[i].dummy = -1;
42 a[i].dummy_ = -1;
43 ref[i].dummy = -1;
44 ref[i].dummy_ = -1;
45 }
46
47 for (size_t i = 0; i < len; ++i) {
48 a[i].idx = i;
49 ref[i].idx = i;
50 }
51
52 qsort(ref, len, sizeof(ref[0]), utest_compare_test_struct);
53 yac_mergesort(a, len, sizeof(ref[0]), utest_compare_test_struct);
54
55 for (size_t i = 0; i < len; ++i) if (a[i].idx != ref[i].idx) INC_ERR;
56 }
57
58 {
59 struct test_struct a[128];
60 struct test_struct ref[128];
61
62 size_t len = sizeof(a)/sizeof(a[0]);
63 for (size_t i = 0; i < len; ++i) {
64 a[i].dummy = -1;
65 a[i].dummy_ = -1;
66 ref[i].dummy = -1;
67 ref[i].dummy_ = -1;
68 }
69
70 for (size_t i = 0; i < len; ++i) {
71 a[i].idx = len - i;
72 ref[i].idx = len - i;
73 }
74
75 qsort(ref, len, sizeof(ref[0]), utest_compare_test_struct);
76 yac_mergesort(a, len, sizeof(ref[0]), utest_compare_test_struct);
77
78 for (size_t i = 0; i < len; ++i) if (a[i].idx != ref[i].idx) INC_ERR;
79 }
80
81 {
82 struct test_struct a[128];
83 struct test_struct ref[128];
84
85 size_t len = sizeof(a)/sizeof(a[0]);
86 for (size_t i = 0; i < len; ++i) {
87 a[i].dummy = -1;
88 a[i].dummy_ = -1;
89 ref[i].dummy = -1;
90 ref[i].dummy_ = -1;
91 }
92
93 for (size_t i = 0; i < len; ++i) {
94 a[i].idx = i;
95 ref[i].idx = i;
96 }
97 for (size_t i = 32; i < 64; ++i) {
98 a[i].idx = 63 - i;
99 ref[i].idx = 63 - i;
100 }
101 for (size_t i = 96; i < 128; ++i) {
102 a[i].idx = 127 - i;
103 ref[i].idx = 127 - i;
104 }
105
106 qsort(ref, len, sizeof(ref[0]), utest_compare_test_struct);
107 yac_mergesort(a, len, sizeof(ref[0]), utest_compare_test_struct);
108
109 for (size_t i = 0; i < len; ++i) if (a[i].idx != ref[i].idx) INC_ERR;
110 }
111
112 {
113 struct test_struct a[128];
114 struct test_struct ref[128];
115
116 size_t len = sizeof(a)/sizeof(a[0]);
117 for (size_t i = 0; i < len; ++i) {
118 a[i].dummy = -1;
119 a[i].dummy_ = -1;
120 ref[i].dummy = -1;
121 ref[i].dummy_ = -1;
122 }
123
124 for (size_t i = 0; i < len; ++i) {
125 a[i].idx = 127 - i;
126 ref[i].idx = 127 - i;
127 }
128 for (size_t i = 32; i < 64; ++i) {
129 a[i].idx = i;
130 ref[i].idx = i;
131 }
132 for (size_t i = 96; i < 128; ++i) {
133 a[i].idx = i;
134 ref[i].idx = i;
135 }
136
137 qsort(ref, len, sizeof(ref[0]), utest_compare_test_struct);
138 yac_mergesort(a, len, sizeof(ref[0]), utest_compare_test_struct);
139
140 for (size_t i = 0; i < len; ++i) if (a[i].idx != ref[i].idx) INC_ERR;
141 }
142
143 for (int i = 0; i < 10; ++i) {
144
145 struct test_struct a[128];
146 struct test_struct ref[128];
147
148 size_t len = sizeof(a)/sizeof(a[0]);
149 for (size_t j = 0; j < len; ++j) {
150 a[j].dummy = -1;
151 a[j].dummy_ = -1;
152 ref[j].dummy = -1;
153 ref[j].dummy_ = -1;
154 }
155
156 for (size_t j = 0; j < len; ++j) ref[j].idx = a[j].idx = rand();
157
158 qsort(ref, len, sizeof(ref[0]), utest_compare_test_struct);
159 yac_mergesort(a, len, sizeof(ref[0]), utest_compare_test_struct);
160
161 for (size_t j = 0; j < len; ++j) if (a[j].idx != ref[j].idx) INC_ERR;
162 }
163
164 return TEST_EXIT_CODE;
165}
166
void yac_mergesort(void *base, size_t num, size_t size, int(*compar)(const void *, const void *))
Definition mergesort.c:134
#define TEST_EXIT_CODE
Definition tests.h:14
#define INC_ERR
Definition tests.h:12