misc.c revision 5a7dfde8
1/**
2 * Copyright © 2011 Red Hat, Inc.
3 *
4 *  Permission is hereby granted, free of charge, to any person obtaining a
5 *  copy of this software and associated documentation files (the "Software"),
6 *  to deal in the Software without restriction, including without limitation
7 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 *  and/or sell copies of the Software, and to permit persons to whom the
9 *  Software is furnished to do so, subject to the following conditions:
10 *
11 *  The above copyright notice and this permission notice (including the next
12 *  paragraph) shall be included in all copies or substantial portions of the
13 *  Software.
14 *
15 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 *  DEALINGS IN THE SOFTWARE.
22 */
23
24#ifdef HAVE_DIX_CONFIG_H
25#include <dix-config.h>
26#endif
27
28#include <stdint.h>
29#include "misc.h"
30#include "scrnintstr.h"
31#include "dix.h"
32#include "dixstruct.h"
33
34#include "tests-common.h"
35
36extern ScreenInfo screenInfo;
37
38static void
39dix_version_compare(void)
40{
41    int rc;
42
43    rc = version_compare(0, 0, 1, 0);
44    assert(rc < 0);
45    rc = version_compare(1, 0, 0, 0);
46    assert(rc > 0);
47    rc = version_compare(0, 0, 0, 0);
48    assert(rc == 0);
49    rc = version_compare(1, 0, 1, 0);
50    assert(rc == 0);
51    rc = version_compare(1, 0, 0, 9);
52    assert(rc > 0);
53    rc = version_compare(0, 9, 1, 0);
54    assert(rc < 0);
55    rc = version_compare(1, 0, 1, 9);
56    assert(rc < 0);
57    rc = version_compare(1, 9, 1, 0);
58    assert(rc > 0);
59    rc = version_compare(2, 0, 1, 9);
60    assert(rc > 0);
61    rc = version_compare(1, 9, 2, 0);
62    assert(rc < 0);
63}
64
65static void
66dix_update_desktop_dimensions(void)
67{
68    int i;
69    int x, y, w, h;
70    int w2, h2;
71    ScreenRec screens[MAXSCREENS];
72
73    for (i = 0; i < MAXSCREENS; i++)
74        screenInfo.screens[i] = &screens[i];
75
76    x = 0;
77    y = 0;
78    w = 10;
79    h = 5;
80    w2 = 35;
81    h2 = 25;
82
83#define assert_dimensions(_x, _y, _w, _h) \
84    update_desktop_dimensions();          \
85    assert(screenInfo.x == _x);           \
86    assert(screenInfo.y == _y);           \
87    assert(screenInfo.width == _w);       \
88    assert(screenInfo.height == _h);
89
90#define set_screen(idx, _x, _y, _w, _h)   \
91    screenInfo.screens[idx]->x = _x;      \
92    screenInfo.screens[idx]->y = _y;      \
93    screenInfo.screens[idx]->width = _w;  \
94    screenInfo.screens[idx]->height = _h; \
95
96    /* single screen */
97    screenInfo.numScreens = 1;
98    set_screen(0, x, y, w, h);
99    assert_dimensions(x, y, w, h);
100
101    /* dualhead rightof */
102    screenInfo.numScreens = 2;
103    set_screen(1, w, 0, w2, h2);
104    assert_dimensions(x, y, w + w2, h2);
105
106    /* dualhead belowof */
107    screenInfo.numScreens = 2;
108    set_screen(1, 0, h, w2, h2);
109    assert_dimensions(x, y, w2, h + h2);
110
111    /* triplehead L shape */
112    screenInfo.numScreens = 3;
113    set_screen(1, 0, h, w2, h2);
114    set_screen(2, w2, h2, w, h);
115    assert_dimensions(x, y, w + w2, h + h2);
116
117    /* quadhead 2x2 */
118    screenInfo.numScreens = 4;
119    set_screen(1, 0, h, w, h);
120    set_screen(2, w, h, w, h2);
121    set_screen(3, w, 0, w2, h);
122    assert_dimensions(x, y, w + w2, h + h2);
123
124    /* quadhead horiz line */
125    screenInfo.numScreens = 4;
126    set_screen(1, w, 0, w, h);
127    set_screen(2, 2 * w, 0, w, h);
128    set_screen(3, 3 * w, 0, w, h);
129    assert_dimensions(x, y, 4 * w, h);
130
131    /* quadhead vert line */
132    screenInfo.numScreens = 4;
133    set_screen(1, 0, h, w, h);
134    set_screen(2, 0, 2 * h, w, h);
135    set_screen(3, 0, 3 * h, w, h);
136    assert_dimensions(x, y, w, 4 * h);
137
138    /* x overlap */
139    screenInfo.numScreens = 2;
140    set_screen(0, 0, 0, w2, h2);
141    set_screen(1, w, 0, w2, h2);
142    assert_dimensions(x, y, w2 + w, h2);
143
144    /* y overlap */
145    screenInfo.numScreens = 2;
146    set_screen(0, 0, 0, w2, h2);
147    set_screen(1, 0, h, w2, h2);
148    assert_dimensions(x, y, w2, h2 + h);
149
150    /* negative origin */
151    screenInfo.numScreens = 1;
152    set_screen(0, -w2, -h2, w, h);
153    assert_dimensions(-w2, -h2, w, h);
154
155    /* dualhead negative origin, overlap */
156    screenInfo.numScreens = 2;
157    set_screen(0, -w2, -h2, w2, h2);
158    set_screen(1, -w, -h, w, h);
159    assert_dimensions(-w2, -h2, w2, h2);
160}
161
162static int
163dix_request_fixed_size_overflow(ClientRec *client)
164{
165    xReq req = { 0 };
166
167    client->req_len = req.length = 1;
168    REQUEST_FIXED_SIZE(req, SIZE_MAX);
169    return Success;
170}
171
172static int
173dix_request_fixed_size_match(ClientRec *client)
174{
175    xReq req = { 0 };
176
177    client->req_len = req.length = 9;
178    REQUEST_FIXED_SIZE(req, 30);
179    return Success;
180}
181
182static void
183dix_request_size_checks(void)
184{
185    ClientRec client = { 0 };
186    int rc;
187
188    rc = dix_request_fixed_size_overflow(&client);
189    assert(rc == BadLength);
190
191    rc = dix_request_fixed_size_match(&client);
192    assert(rc == Success);
193}
194
195static void
196bswap_test(void)
197{
198    const uint16_t test_16 = 0xaabb;
199    const uint16_t expect_16 = 0xbbaa;
200    const uint32_t test_32 = 0xaabbccdd;
201    const uint32_t expect_32 = 0xddccbbaa;
202    const uint64_t test_64 = 0x11223344aabbccddull;
203    const uint64_t expect_64 = 0xddccbbaa44332211ull;
204    uint16_t result_16;
205    uint32_t result_32;
206    uint64_t result_64;
207
208    assert(bswap_16(test_16) == expect_16);
209    assert(bswap_32(test_32) == expect_32);
210    assert(bswap_64(test_64) == expect_64);
211
212    result_16 = test_16;
213    swaps(&result_16);
214    assert(result_16 == expect_16);
215
216    result_32 = test_32;
217    swapl(&result_32);
218    assert(result_32 == expect_32);
219
220    result_64 = test_64;
221    swapll(&result_64);
222    assert(result_64 == expect_64);
223}
224
225int
226misc_test(void)
227{
228    dix_version_compare();
229    dix_update_desktop_dimensions();
230    dix_request_size_checks();
231    bswap_test();
232
233    return 0;
234}
235