| 1 | /* |
| 2 | * Copyright © 2020 Benjamin Otte |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include <locale.h> |
| 19 | |
| 20 | #include <gtk/gtk.h>cxx . . $ `<file>/gtk/gtk.h`/ |
| 21 | |
| 22 | #include "gtk/timsort/gtktimsortprivate.h"cxx . . $ `<file>/gtk/timsort/gtktimsortprivate.h`/ |
| 23 | |
| 24 | #define assert_sort_equalcxx . . $ `testsuite/gtk/timsort.c:24:9`!(a, b, size, n) \ |
| 25 | g_assert_cmpmemcxx . . $ `<external>/15287032793271088244/gtestutils.h:105:9`! (a, sizeof (size) * n, b, sizeof (size) * n) |
| 26 | |
| 27 | static int |
| 28 | compare_intcxx . . $ compare_int(b8f62d2ccbc12f7d). (gconstpointercxx . . $ gconstpointer# a, |
| 29 | gconstpointercxx . . $ gconstpointer# b, |
| 30 | gpointercxx . . $ gpointer# unused) |
| 31 | { |
| 32 | int ia = *(const int *) a; |
| 33 | int ib = *(const int *) b; |
| 34 | |
| 35 | return ia < ib ? -1 : (ia > ib); |
| 36 | } |
| 37 | |
| 38 | static int |
| 39 | compare_pointercxx . . $ compare_pointer(b8f62d2ccbc12f7d). (gconstpointercxx . . $ gconstpointer# a, |
| 40 | gconstpointercxx . . $ gconstpointer# b, |
| 41 | gpointercxx . . $ gpointer# unused) |
| 42 | { |
| 43 | gpointercxx . . $ gpointer# pa = *(const gpointercxx . . $ gpointer# *) a; |
| 44 | gpointercxx . . $ gpointer# pb = *(const gpointercxx . . $ gpointer# *) b; |
| 45 | |
| 46 | return pa < pb ? -1 : (pa > pb); |
| 47 | } |
| 48 | G_GNUC_UNUSEDcxx . . $ `<external>/12741050725406041920/gmacros.h:647:9`! static void |
| 49 | dumpcxx . . $ dump(b05cc78aca5bb3fc). (int *a, |
| 50 | gsizecxx . . $ gsize# n) |
| 51 | { |
| 52 | gsizecxx . . $ gsize# i; |
| 53 | |
| 54 | for (i = 0; i < n; i++) |
| 55 | { |
| 56 | if (i) |
| 57 | g_printcxx . . $ g_print(326aeb464d16410c).(", "); |
| 58 | g_printcxx . . $ g_print(326aeb464d16410c). ("%d", a[i]); |
| 59 | } |
| 60 | g_printcxx . . $ g_print(326aeb464d16410c). ("\n"); |
| 61 | } |
| 62 | |
| 63 | static void |
| 64 | run_comparisoncxx . . $ run_comparison(f6cd6bfde2f94d8a). (gpointercxx . . $ gpointer# a, |
| 65 | gsizecxx . . $ gsize# n, |
| 66 | gsizecxx . . $ gsize# element_size, |
| 67 | GCompareDataFunccxx . . $ GCompareDataFunc# compare_func, |
| 68 | gpointercxx . . $ gpointer# data) |
| 69 | { |
| 70 | gint64cxx . . $ gint64# start, mid, end; |
| 71 | gpointercxx . . $ gpointer# b; |
| 72 | |
| 73 | g_assert_cmpintcxx . . $ `<external>/15287032793271088244/gtestutils.h:53:9`! (n, <=, G_MAXSIZEcxx . . $ `<external>/10505296616216143549/glibconfig.h:89:9`! / element_size); |
| 74 | |
| 75 | b = g_memdup2cxx . . $ g_memdup2(7ca5e6a2fd1baf31). (a, element_size * n); |
| 76 | |
| 77 | start = g_get_monotonic_timecxx . . $ g_get_monotonic_time(da6335ded91d79c2). (); |
| 78 | gtk_tim_sortcxx . . $ gtk_tim_sort(f6cd6bfde2f94d8a). (a, n, element_size, compare_func, data); |
| 79 | mid = g_get_monotonic_timecxx . . $ g_get_monotonic_time(da6335ded91d79c2). (); |
| 80 | g_sort_arraycxx . . $ g_sort_array(6cb3f3d114016b6). (b, n, element_size, compare_func, data); |
| 81 | end = g_get_monotonic_timecxx . . $ g_get_monotonic_time(da6335ded91d79c2). (); |
| 82 | |
| 83 | g_test_messagecxx . . $ g_test_message(326aeb464d16410c). ("%zu items in %uus vs %uus (%u%%)", |
| 84 | n, |
| 85 | (guintcxx . . $ guint#) (mid - start), |
| 86 | (guintcxx . . $ guint#) (end - mid), |
| 87 | (guintcxx . . $ guint#) (100 * (mid - start) / MAXcxx . . $ `<external>/12741050725406041920/gmacros.h:975:9`! (1, end - mid))); |
| 88 | assert_sort_equalcxx . . $ `testsuite/gtk/timsort.c:24:9`! (a, b, int, n); |
| 89 | |
| 90 | g_freecxx . . $ g_free(5d22bdacc48458e8). (b); |
| 91 | } |
| 92 | |
| 93 | static void |
| 94 | test_integerscxx . . $ test_integers(49f6e7a06ebc5aa8). (void) |
| 95 | { |
| 96 | int *a; |
| 97 | gsizecxx . . $ gsize# i, n, run; |
| 98 | |
| 99 | a = g_newcxx . . $ `<external>/1123956534522462559/gmem.h:318:9`! (int, 1000); |
| 100 | |
| 101 | for (run = 0; run < 10; run++) |
| 102 | { |
| 103 | n = g_test_rand_int_rangecxx . . $ g_test_rand_int_range(9b79fb6aee4c0440). (0, 1000); |
| 104 | for (i = 0; i < n; i++) |
| 105 | a[i] = g_test_rand_intcxx . . $ g_test_rand_int(b126dc7c1de90089). (); |
| 106 | |
| 107 | run_comparisoncxx . . $ run_comparison(f6cd6bfde2f94d8a). (a, n, sizeof (int), compare_intcxx . . $ compare_int(b8f62d2ccbc12f7d)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 108 | } |
| 109 | |
| 110 | g_freecxx . . $ g_free(5d22bdacc48458e8). (a); |
| 111 | } |
| 112 | |
| 113 | static void |
| 114 | test_integers_runscxx . . $ test_integers_runs(49f6e7a06ebc5aa8). (void) |
| 115 | { |
| 116 | int *a; |
| 117 | gsizecxx . . $ gsize# i, j, n, run; |
| 118 | |
| 119 | a = g_newcxx . . $ `<external>/1123956534522462559/gmem.h:318:9`! (int, 1000); |
| 120 | |
| 121 | for (run = 0; run < 10; run++) |
| 122 | { |
| 123 | n = g_test_rand_int_rangecxx . . $ g_test_rand_int_range(9b79fb6aee4c0440). (0, 1000); |
| 124 | for (i = 0; i < n; i++) |
| 125 | { |
| 126 | a[i] = g_test_rand_intcxx . . $ g_test_rand_int(b126dc7c1de90089). (); |
| 127 | j = i + g_test_rand_int_rangecxx . . $ g_test_rand_int_range(9b79fb6aee4c0440). (0, 20); |
| 128 | j = MINcxx . . $ `<external>/12741050725406041920/gmacros.h:978:9`! (n, j); |
| 129 | if (g_test_rand_bitcxx . . $ `<external>/15287032793271088244/gtestutils.h:571:10`! ()) |
| 130 | { |
| 131 | for (i++; i < j; i++) |
| 132 | a[i] = a[i - 1] + 1; |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | for (i++; i < j; i++) |
| 137 | a[i] = a[i - 1] - 1; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | run_comparisoncxx . . $ run_comparison(f6cd6bfde2f94d8a). (a, n, sizeof (int), compare_intcxx . . $ compare_int(b8f62d2ccbc12f7d)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 142 | } |
| 143 | |
| 144 | g_freecxx . . $ g_free(5d22bdacc48458e8). (a); |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | test_integers_hugecxx . . $ test_integers_huge(49f6e7a06ebc5aa8). (void) |
| 149 | { |
| 150 | int *a; |
| 151 | gsizecxx . . $ gsize# i, n; |
| 152 | |
| 153 | n = g_test_rand_int_rangecxx . . $ g_test_rand_int_range(9b79fb6aee4c0440). (2 * 1000 * 1000, 5 * 1000 * 1000); |
| 154 | |
| 155 | a = g_newcxx . . $ `<external>/1123956534522462559/gmem.h:318:9`! (int, n); |
| 156 | for (i = 0; i < n; i++) |
| 157 | a[i] = g_test_rand_intcxx . . $ g_test_rand_int(b126dc7c1de90089). (); |
| 158 | |
| 159 | run_comparisoncxx . . $ run_comparison(f6cd6bfde2f94d8a). (a, n, sizeof (int), compare_intcxx . . $ compare_int(b8f62d2ccbc12f7d)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 160 | |
| 161 | g_freecxx . . $ g_free(5d22bdacc48458e8). (a); |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | test_pointerscxx . . $ test_pointers(49f6e7a06ebc5aa8). (void) |
| 166 | { |
| 167 | gpointercxx . . $ gpointer# *a; |
| 168 | gsizecxx . . $ gsize# i, n, run; |
| 169 | |
| 170 | a = g_newcxx . . $ `<external>/1123956534522462559/gmem.h:318:9`! (gpointer, 1000); |
| 171 | |
| 172 | for (run = 0; run < 10; run++) |
| 173 | { |
| 174 | n = g_test_rand_int_rangecxx . . $ g_test_rand_int_range(9b79fb6aee4c0440). (0, 1000); |
| 175 | for (i = 0; i < n; i++) |
| 176 | a[i] = GINT_TO_POINTERcxx . . $ `<external>/10505296616216143549/glibconfig.h:106:9`! (g_test_rand_int ()); |
| 177 | |
| 178 | run_comparisoncxx . . $ run_comparison(f6cd6bfde2f94d8a). (a, n, sizeof (gpointercxx . . $ gpointer#), compare_pointercxx . . $ compare_pointer(b8f62d2ccbc12f7d)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 179 | } |
| 180 | |
| 181 | g_freecxx . . $ g_free(5d22bdacc48458e8). (a); |
| 182 | } |
| 183 | |
| 184 | static void |
| 185 | test_pointers_hugecxx . . $ test_pointers_huge(49f6e7a06ebc5aa8). (void) |
| 186 | { |
| 187 | gpointercxx . . $ gpointer# *a; |
| 188 | gsizecxx . . $ gsize# i, n; |
| 189 | |
| 190 | n = g_test_rand_int_rangecxx . . $ g_test_rand_int_range(9b79fb6aee4c0440). (2 * 1000 * 1000, 5 * 1000 * 1000); |
| 191 | |
| 192 | a = g_newcxx . . $ `<external>/1123956534522462559/gmem.h:318:9`! (gpointer, n); |
| 193 | for (i = 0; i < n; i++) |
| 194 | a[i] = GINT_TO_POINTERcxx . . $ `<external>/10505296616216143549/glibconfig.h:106:9`! (g_test_rand_int ()); |
| 195 | |
| 196 | run_comparisoncxx . . $ run_comparison(f6cd6bfde2f94d8a). (a, n, sizeof (gpointercxx . . $ gpointer#), compare_pointercxx . . $ compare_pointer(b8f62d2ccbc12f7d)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 197 | |
| 198 | g_freecxx . . $ g_free(5d22bdacc48458e8). (a); |
| 199 | } |
| 200 | |
| 201 | static void |
| 202 | test_stepscxx . . $ test_steps(49f6e7a06ebc5aa8). (void) |
| 203 | { |
| 204 | GtkTimSortRuncxx . . $ GtkTimSortRun# change; |
| 205 | GtkTimSortcxx . . $ GtkTimSort# sort; |
| 206 | int *a, *b; |
| 207 | gsizecxx . . $ gsize# i, n; |
| 208 | |
| 209 | n = g_test_rand_int_rangecxx . . $ g_test_rand_int_range(9b79fb6aee4c0440). (20 * 1000, 50 * 1000); |
| 210 | |
| 211 | a = g_newcxx . . $ `<external>/1123956534522462559/gmem.h:318:9`! (int, n); |
| 212 | for (i = 0; i < n; i++) |
| 213 | a[i] = g_test_rand_intcxx . . $ g_test_rand_int(b126dc7c1de90089). (); |
| 214 | b = g_memdup2cxx . . $ g_memdup2(7ca5e6a2fd1baf31). (a, sizeof (int) * n); |
| 215 | |
| 216 | gtk_tim_sort_initcxx . . $ gtk_tim_sort_init(42e5fc7dfac18a70). (&sort, a, n, sizeof (int), compare_intcxx . . $ compare_int(b8f62d2ccbc12f7d)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 217 | gtk_tim_sort_set_max_merge_sizecxx . . $ gtk_tim_sort_set_max_merge_size(9dcb2f516e86b67b). (&sort, g_test_rand_int_rangecxx . . $ g_test_rand_int_range(9b79fb6aee4c0440). (512, 2048)); |
| 218 | |
| 219 | while (gtk_tim_sort_stepcxx . . $ gtk_tim_sort_step(a3cd5f8650ca4faf). (&sort, &change)) |
| 220 | { |
| 221 | if (change.lencxx . . $ _GtkTimSortRun#len.) |
| 222 | { |
| 223 | int *a_start = change.basecxx . . $ _GtkTimSortRun#base.; |
| 224 | int *b_start = b + ((int *) change.basecxx . . $ _GtkTimSortRun#base. - a); |
| 225 | g_assert_cmpintcxx . . $ `<external>/15287032793271088244/gtestutils.h:53:9`! (a_start[0], !=, b_start[0]); |
| 226 | g_assert_cmpintcxx . . $ `<external>/15287032793271088244/gtestutils.h:53:9`! (a_start[change.len - 1], !=, b_start[change.len - 1]); |
| 227 | memcpycxx . . $ memcpy(cc817cb175f282d). (b_start, a_start, change.lencxx . . $ _GtkTimSortRun#len. * sizeof (int)); |
| 228 | } |
| 229 | |
| 230 | assert_sort_equalcxx . . $ `testsuite/gtk/timsort.c:24:9`! (a, b, int, n); |
| 231 | } |
| 232 | |
| 233 | g_sort_arraycxx . . $ g_sort_array(6cb3f3d114016b6). (b, n, sizeof (int), compare_intcxx . . $ compare_int(b8f62d2ccbc12f7d)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 234 | assert_sort_equalcxx . . $ `testsuite/gtk/timsort.c:24:9`! (a, b, int, n); |
| 235 | |
| 236 | gtk_tim_sort_finishcxx . . $ gtk_tim_sort_finish(66cb275ad66ec7e0). (&sort); |
| 237 | g_freecxx . . $ g_free(5d22bdacc48458e8). (b); |
| 238 | g_freecxx . . $ g_free(5d22bdacc48458e8). (a); |
| 239 | } |
| 240 | |
| 241 | int |
| 242 | maincxx . . $ main(afcd3168fc1188a4). (int argc, char *argv[]) |
| 243 | { |
| 244 | (g_test_initcxx . . $ g_test_init(b91bcc5cc1349d78).) (&argc, &argv, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 245 | setlocalecxx . . $ setlocale(10ed28bce9115888). (LC_ALLcxx . . $ `<external>/3736975272097309885/locale.h:41:9`!, "C"); |
| 246 | |
| 247 | g_test_add_funccxx . . $ g_test_add_func(18fee6cbb4bb18a4). ("/timsort/integers", test_integerscxx . . $ test_integers(49f6e7a06ebc5aa8).); |
| 248 | g_test_add_funccxx . . $ g_test_add_func(18fee6cbb4bb18a4). ("/timsort/integers/runs", test_integers_runscxx . . $ test_integers_runs(49f6e7a06ebc5aa8).); |
| 249 | g_test_add_funccxx . . $ g_test_add_func(18fee6cbb4bb18a4). ("/timsort/integers/huge", test_integers_hugecxx . . $ test_integers_huge(49f6e7a06ebc5aa8).); |
| 250 | g_test_add_funccxx . . $ g_test_add_func(18fee6cbb4bb18a4). ("/timsort/pointers", test_pointerscxx . . $ test_pointers(49f6e7a06ebc5aa8).); |
| 251 | g_test_add_funccxx . . $ g_test_add_func(18fee6cbb4bb18a4). ("/timsort/pointers/huge", test_pointers_hugecxx . . $ test_pointers_huge(49f6e7a06ebc5aa8).); |
| 252 | g_test_add_funccxx . . $ g_test_add_func(18fee6cbb4bb18a4). ("/timsort/steps", test_stepscxx . . $ test_steps(49f6e7a06ebc5aa8).); |
| 253 | |
| 254 | return g_test_runcxx . . $ g_test_run(b126dc7c1de90089). (); |
| 255 | } |