1 /* GLib testing framework examples and tests
2 * Copyright (C) 2009 Red Hat, Inc.
3 *
4 * SPDX-License-Identifier: LicenseRef-old-glib-tests
5 *
6 * This work is provided "as is"; redistribution and modification
7 * in whole or in part, in any medium, physical or electronic is
8 * permitted without restriction.
9 *
10 * This work is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * In no event shall the authors or contributors be liable for any
15 * direct, indirect, incidental, special, exemplary, or consequential
16 * damages (including, but not limited to, procurement of substitute
17 * goods or services; loss of use, data, or profits; or business
18 * interruption) however caused and on any theory of liability, whether
19 * in contract, strict liability, or tort (including negligence or
20 * otherwise) arising in any way out of the use of this software, even
21 * if advised of the possibility of such damage.
22 */
23
24 #include <glib/glib.h>
25 #include <gio/gio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #define NUM_TRIALS 250000
30
31 struct {
32 const char *order;
33 int expected, seen;
34 } ordering[] = {
35 /* There are 32 legitimate orderings; the result always has to start
36 * with either "fe" (usually) or "ef" (rarely). For the remaining
37 * letters, "cbda" is the most likely, with various other orders
38 * possible, down to "adbc" being the most improbable. However,
39 * almost all "fe" orderings are more likely than almost any "ef"
40 * orderings. The complete probability ordering, from most-likely
41 * to least-likely is something roughly like:
42 */
43 { "fecbda", 0.2468 * NUM_TRIALS, 0},
44 { "febcda", 0.1885 * NUM_TRIALS, 0},
45 { "fecdba", 0.1346 * NUM_TRIALS, 0},
46 { "fedcba", 0.0830 * NUM_TRIALS, 0},
47 { "febdca", 0.0706 * NUM_TRIALS, 0},
48 { "fedbca", 0.0571 * NUM_TRIALS, 0},
49 { "fecbad", 0.0496 * NUM_TRIALS, 0},
50 { "febcad", 0.0374 * NUM_TRIALS, 0},
51 { "fecabd", 0.0185 * NUM_TRIALS, 0},
52 { "fecdab", 0.0136 * NUM_TRIALS, 0},
53 { "fecadb", 0.0110 * NUM_TRIALS, 0},
54 { "febacd", 0.0108 * NUM_TRIALS, 0},
55 { "feacbd", 0.0096 * NUM_TRIALS, 0},
56 { "fedcab", 0.0083 * NUM_TRIALS, 0},
57 { "feabcd", 0.0073 * NUM_TRIALS, 0},
58 { "feacdb", 0.0058 * NUM_TRIALS, 0},
59 { "efcbda", 0.0049 * NUM_TRIALS, 0},
60 { "febdac", 0.0048 * NUM_TRIALS, 0},
61 { "febadc", 0.0043 * NUM_TRIALS, 0},
62 { "fedbac", 0.0038 * NUM_TRIALS, 0},
63 { "efbcda", 0.0038 * NUM_TRIALS, 0},
64 { "feadcb", 0.0036 * NUM_TRIALS, 0},
65 { "fedacb", 0.0035 * NUM_TRIALS, 0},
66 { "feabdc", 0.0029 * NUM_TRIALS, 0},
67 { "feadbc", 0.0026 * NUM_TRIALS, 0},
68 { "fedabc", 0.0026 * NUM_TRIALS, 0},
69 { "efcdba", 0.0026 * NUM_TRIALS, 0},
70 { "efdcba", 0.0017 * NUM_TRIALS, 0},
71 { "efbdca", 0.0014 * NUM_TRIALS, 0},
72 { "efdbca", 0.0011 * NUM_TRIALS, 0},
73 { "efcbad", 0.0010 * NUM_TRIALS, 0},
74 { "efbcad", 0.0008 * NUM_TRIALS, 0},
75 { "efcabd", 0.0004 * NUM_TRIALS, 0},
76 { "efcdab", 0.0003 * NUM_TRIALS, 0},
77 { "efcadb", 0.0002 * NUM_TRIALS, 0},
78 { "efbacd", 0.0002 * NUM_TRIALS, 0},
79 { "efacbd", 0.0002 * NUM_TRIALS, 0},
80 { "efdcab", 0.0002 * NUM_TRIALS, 0},
81 { "efabcd", 0.0002 * NUM_TRIALS, 0},
82 { "efacdb", 0.0001 * NUM_TRIALS, 0},
83 { "efbdac", 0.0001 * NUM_TRIALS, 0},
84 { "efadcb", 0.0001 * NUM_TRIALS, 0},
85 { "efdbac", 0.0001 * NUM_TRIALS, 0},
86 { "efbadc", 0.0001 * NUM_TRIALS, 0},
87 { "efdacb", 0.0001 * NUM_TRIALS, 0},
88 { "efabdc", 0.0001 * NUM_TRIALS, 0},
89 { "efadbc", 0.00005 * NUM_TRIALS, 0},
90 { "efdabc", 0.00005 * NUM_TRIALS, 0}
91 };
92 #define NUM_ORDERINGS G_N_ELEMENTS (ordering)
93
94 static void
95 test_srv_target_ordering (void)
96 {
97 GList *targets, *sorted, *t;
98 char result[7], *p;
99 int i;
100 guint o;
101
102 targets = NULL;
103 /* name, port, priority, weight */
104 targets = g_list_append (targets, g_srv_target_new ("a", 0, 2, 0));
105 targets = g_list_append (targets, g_srv_target_new ("b", 0, 2, 10));
106 targets = g_list_append (targets, g_srv_target_new ("c", 0, 2, 15));
107 targets = g_list_append (targets, g_srv_target_new ("d", 0, 2, 5));
108 targets = g_list_append (targets, g_srv_target_new ("e", 0, 1, 0));
109 targets = g_list_append (targets, g_srv_target_new ("f", 0, 1, 50));
110
111 for (i = 0; i < NUM_TRIALS; i++)
112 {
113 g_random_set_seed (i);
114
115 sorted = g_srv_target_list_sort (g_list_copy (targets));
116
117 for (t = sorted, p = result; t; t = t->next)
118 *(p++) = *g_srv_target_get_hostname (t->data);
119 *p = '\0';
120 g_list_free (sorted);
121
122 for (o = 0; o < NUM_ORDERINGS; o++)
123 {
124 if (!strcmp (result, ordering[o].order))
125 {
126 ordering[o].seen++;
127 break;
128 }
129 }
130
131 /* Assert that @result matched one of the valid orderings */
132 if (o == NUM_ORDERINGS)
133 {
134 char *msg = g_strdup_printf ("result '%s' is invalid", result);
135 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
136 }
137 }
138
139 /* Assert that each ordering appeared roughly the expected
140 * number of times.
141 */
142 for (o = 0; o < NUM_ORDERINGS; o++)
143 {
144 g_assert_cmpint (ordering[o].seen, >, ordering[o].expected / 2);
145 g_assert_cmpint (ordering[o].seen, <, ordering[o].expected * 2);
146 }
147
148 g_resolver_free_targets (targets);
149 }
150
151 int
152 main (int argc, char **argv)
153 {
154 g_test_init (&argc, &argv, NULL);
155
156 g_test_add_func ("/srvtarget/srv-target-ordering", test_srv_target_ordering);
157
158 return g_test_run();
159 }