Branch data Line data Source code
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 : : gdouble expected;
34 : : unsigned int seen;
35 : : } ordering[] = {
36 : : /* There are 32 legitimate orderings; the result always has to start
37 : : * with either "fe" (usually) or "ef" (rarely). For the remaining
38 : : * letters, "cbda" is the most likely, with various other orders
39 : : * possible, down to "adbc" being the most improbable. However,
40 : : * almost all "fe" orderings are more likely than almost any "ef"
41 : : * orderings. The complete probability ordering, from most-likely
42 : : * to least-likely is something roughly like:
43 : : */
44 : : { "fecbda", 0.2468 * NUM_TRIALS, 0},
45 : : { "febcda", 0.1885 * NUM_TRIALS, 0},
46 : : { "fecdba", 0.1346 * NUM_TRIALS, 0},
47 : : { "fedcba", 0.0830 * NUM_TRIALS, 0},
48 : : { "febdca", 0.0706 * NUM_TRIALS, 0},
49 : : { "fedbca", 0.0571 * NUM_TRIALS, 0},
50 : : { "fecbad", 0.0496 * NUM_TRIALS, 0},
51 : : { "febcad", 0.0374 * NUM_TRIALS, 0},
52 : : { "fecabd", 0.0185 * NUM_TRIALS, 0},
53 : : { "fecdab", 0.0136 * NUM_TRIALS, 0},
54 : : { "fecadb", 0.0110 * NUM_TRIALS, 0},
55 : : { "febacd", 0.0108 * NUM_TRIALS, 0},
56 : : { "feacbd", 0.0096 * NUM_TRIALS, 0},
57 : : { "fedcab", 0.0083 * NUM_TRIALS, 0},
58 : : { "feabcd", 0.0073 * NUM_TRIALS, 0},
59 : : { "feacdb", 0.0058 * NUM_TRIALS, 0},
60 : : { "efcbda", 0.0049 * NUM_TRIALS, 0},
61 : : { "febdac", 0.0048 * NUM_TRIALS, 0},
62 : : { "febadc", 0.0043 * NUM_TRIALS, 0},
63 : : { "fedbac", 0.0038 * NUM_TRIALS, 0},
64 : : { "efbcda", 0.0038 * NUM_TRIALS, 0},
65 : : { "feadcb", 0.0036 * NUM_TRIALS, 0},
66 : : { "fedacb", 0.0035 * NUM_TRIALS, 0},
67 : : { "feabdc", 0.0029 * NUM_TRIALS, 0},
68 : : { "feadbc", 0.0026 * NUM_TRIALS, 0},
69 : : { "fedabc", 0.0026 * NUM_TRIALS, 0},
70 : : { "efcdba", 0.0026 * NUM_TRIALS, 0},
71 : : { "efdcba", 0.0017 * NUM_TRIALS, 0},
72 : : { "efbdca", 0.0014 * NUM_TRIALS, 0},
73 : : { "efdbca", 0.0011 * NUM_TRIALS, 0},
74 : : { "efcbad", 0.0010 * NUM_TRIALS, 0},
75 : : { "efbcad", 0.0008 * NUM_TRIALS, 0},
76 : : { "efcabd", 0.0004 * NUM_TRIALS, 0},
77 : : { "efcdab", 0.0003 * NUM_TRIALS, 0},
78 : : { "efcadb", 0.0002 * NUM_TRIALS, 0},
79 : : { "efbacd", 0.0002 * NUM_TRIALS, 0},
80 : : { "efacbd", 0.0002 * NUM_TRIALS, 0},
81 : : { "efdcab", 0.0002 * NUM_TRIALS, 0},
82 : : { "efabcd", 0.0002 * NUM_TRIALS, 0},
83 : : { "efacdb", 0.0001 * NUM_TRIALS, 0},
84 : : { "efbdac", 0.0001 * NUM_TRIALS, 0},
85 : : { "efadcb", 0.0001 * NUM_TRIALS, 0},
86 : : { "efdbac", 0.0001 * NUM_TRIALS, 0},
87 : : { "efbadc", 0.0001 * NUM_TRIALS, 0},
88 : : { "efdacb", 0.0001 * NUM_TRIALS, 0},
89 : : { "efabdc", 0.0001 * NUM_TRIALS, 0},
90 : : { "efadbc", 0.00005 * NUM_TRIALS, 0},
91 : : { "efdabc", 0.00005 * NUM_TRIALS, 0}
92 : : };
93 : : #define NUM_ORDERINGS G_N_ELEMENTS (ordering)
94 : :
95 : : static void
96 : 1 : test_srv_target_ordering (void)
97 : : {
98 : : GList *targets, *sorted, *t;
99 : : char result[7], *p;
100 : : int i;
101 : : guint o;
102 : :
103 : 1 : targets = NULL;
104 : : /* name, port, priority, weight */
105 : 1 : targets = g_list_append (targets, g_srv_target_new ("a", 0, 2, 0));
106 : 1 : targets = g_list_append (targets, g_srv_target_new ("b", 0, 2, 10));
107 : 1 : targets = g_list_append (targets, g_srv_target_new ("c", 0, 2, 15));
108 : 1 : targets = g_list_append (targets, g_srv_target_new ("d", 0, 2, 5));
109 : 1 : targets = g_list_append (targets, g_srv_target_new ("e", 0, 1, 0));
110 : 1 : targets = g_list_append (targets, g_srv_target_new ("f", 0, 1, 50));
111 : :
112 : 250001 : for (i = 0; i < NUM_TRIALS; i++)
113 : : {
114 : 250000 : g_random_set_seed (i);
115 : :
116 : 250000 : sorted = g_srv_target_list_sort (g_list_copy (targets));
117 : :
118 : 1750000 : for (t = sorted, p = result; t; t = t->next)
119 : 1500000 : *(p++) = *g_srv_target_get_hostname (t->data);
120 : 250000 : *p = '\0';
121 : 250000 : g_list_free (sorted);
122 : :
123 : 1204522 : for (o = 0; o < NUM_ORDERINGS; o++)
124 : : {
125 : 1204522 : if (!strcmp (result, ordering[o].order))
126 : : {
127 : 250000 : ordering[o].seen++;
128 : 250000 : break;
129 : : }
130 : : }
131 : :
132 : : /* Assert that @result matched one of the valid orderings */
133 : 250000 : if (o == NUM_ORDERINGS)
134 : : {
135 : 0 : char *msg = g_strdup_printf ("result '%s' is invalid", result);
136 : 0 : g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
137 : : }
138 : : }
139 : :
140 : : /* Assert that each ordering appeared roughly the expected
141 : : * number of times.
142 : : */
143 : 49 : for (o = 0; o < NUM_ORDERINGS; o++)
144 : : {
145 : 48 : g_assert_cmpuint (ordering[o].seen, >, (guint64) (ordering[o].expected / 2));
146 : 48 : g_assert_cmpuint (ordering[o].seen, <, (guint64) (ordering[o].expected * 2));
147 : : }
148 : :
149 : 1 : g_resolver_free_targets (targets);
150 : 1 : }
151 : :
152 : : int
153 : 1 : main (int argc, char **argv)
154 : : {
155 : 1 : g_test_init (&argc, &argv, NULL);
156 : :
157 : 1 : g_test_add_func ("/srvtarget/srv-target-ordering", test_srv_target_ordering);
158 : :
159 : 1 : return g_test_run();
160 : : }
|