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