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 "inpututils.h"
30 #include "inputstr.h"
31 #include "assert.h"
32 
33 #include "protocol-common.h"
34 
35 ClientRec client_window;
36 
37 static void
xi2mask_test(void)38 xi2mask_test(void)
39 {
40     XI2Mask *xi2mask = NULL, *mergemask = NULL;
41     unsigned char *mask;
42     DeviceIntRec dev;
43     DeviceIntRec all_devices, all_master_devices;
44     int i;
45 
46     all_devices.id = XIAllDevices;
47     inputInfo.all_devices = &all_devices;
48     all_master_devices.id = XIAllMasterDevices;
49     inputInfo.all_master_devices = &all_master_devices;
50 
51     /* size >= nmasks * 2 for the test cases below */
52     xi2mask = xi2mask_new_with_size(MAXDEVICES + 2, (MAXDEVICES + 2) * 2);
53     assert(xi2mask);
54     assert(xi2mask->nmasks > 0);
55     assert(xi2mask->mask_size > 0);
56 
57     assert(xi2mask_mask_size(xi2mask) == xi2mask->mask_size);
58     assert(xi2mask_num_masks(xi2mask) == xi2mask->nmasks);
59 
60     mask = calloc(1, xi2mask_mask_size(xi2mask));
61     /* ensure zeros */
62     for (i = 0; i < xi2mask_num_masks(xi2mask); i++) {
63         const unsigned char *m = xi2mask_get_one_mask(xi2mask, i);
64 
65         assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0);
66     }
67 
68     /* set various bits */
69     for (i = 0; i < xi2mask_num_masks(xi2mask); i++) {
70         const unsigned char *m;
71 
72         xi2mask_set(xi2mask, i, i);
73 
74         dev.id = i;
75         assert(xi2mask_isset(xi2mask, &dev, i));
76 
77         m = xi2mask_get_one_mask(xi2mask, i);
78         SetBit(mask, i);
79         assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0);
80         ClearBit(mask, i);
81     }
82 
83     /* ensure zeros one-by-one */
84     for (i = 0; i < xi2mask_num_masks(xi2mask); i++) {
85         const unsigned char *m = xi2mask_get_one_mask(xi2mask, i);
86 
87         assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) != 0);
88         xi2mask_zero(xi2mask, i);
89         assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0);
90     }
91 
92     /* re-set, zero all */
93     for (i = 0; i < xi2mask_num_masks(xi2mask); i++)
94         xi2mask_set(xi2mask, i, i);
95     xi2mask_zero(xi2mask, -1);
96 
97     for (i = 0; i < xi2mask_num_masks(xi2mask); i++) {
98         const unsigned char *m = xi2mask_get_one_mask(xi2mask, i);
99 
100         assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0);
101     }
102 
103     for (i = 0; i < xi2mask_num_masks(xi2mask); i++) {
104         const unsigned char *m;
105 
106         SetBit(mask, i);
107         xi2mask_set_one_mask(xi2mask, i, mask, xi2mask_mask_size(xi2mask));
108         m = xi2mask_get_one_mask(xi2mask, i);
109         assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0);
110         ClearBit(mask, i);
111     }
112 
113     mergemask = xi2mask_new_with_size(MAXDEVICES + 2, (MAXDEVICES + 2) * 2);
114     for (i = 0; i < xi2mask_num_masks(mergemask); i++) {
115         dev.id = i;
116         xi2mask_set(mergemask, i, i * 2);
117     }
118 
119     /* xi2mask still has all i bits set, should now also have all i * 2 bits */
120     xi2mask_merge(xi2mask, mergemask);
121     for (i = 0; i < xi2mask_num_masks(mergemask); i++) {
122         const unsigned char *m = xi2mask_get_one_mask(xi2mask, i);
123 
124         SetBit(mask, i);
125         SetBit(mask, i * 2);
126         assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0);
127         ClearBit(mask, i);
128         ClearBit(mask, i * 2);
129     }
130 
131     xi2mask_free(&xi2mask);
132     assert(xi2mask == NULL);
133 
134     xi2mask_free(&mergemask);
135     assert(mergemask == NULL);
136     free(mask);
137 }
138 
139 int
xi2_test(void)140 xi2_test(void)
141 {
142     xi2mask_test();
143 
144     return 0;
145 }
146