aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/test/xi2/xi2.c
blob: 5143caff8358c130d092e59761bb973c9d2c6b50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
 * Copyright © 2011 Red Hat, Inc.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice (including the next
 *  paragraph) shall be included in all copies or substantial portions of the
 *  Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 *  DEALINGS IN THE SOFTWARE.
 */

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <stdint.h>
#include "inpututils.h"
#include "inputstr.h"
#include "assert.h"

static void xi2mask_test(void)
{
    XI2Mask *xi2mask = NULL,
            *mergemask = NULL;
    unsigned char *mask;
    DeviceIntRec dev;
    int i;

    /* size >= nmasks * 2 for the test cases below */
    xi2mask = xi2mask_new_with_size(MAXDEVICES + 2, (MAXDEVICES + 2) * 2);
    assert(xi2mask);
    assert(xi2mask->nmasks > 0);
    assert(xi2mask->mask_size > 0);

    assert(xi2mask_mask_size(xi2mask) == xi2mask->mask_size);
    assert(xi2mask_num_masks(xi2mask) == xi2mask->nmasks);

    mask = calloc(1, xi2mask_mask_size(xi2mask));
    /* ensure zeros */
    for (i = 0; i < xi2mask_num_masks(xi2mask); i++) {
        const unsigned char *m = xi2mask_get_one_mask(xi2mask, i);
        assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0);
    }

    /* set various bits */
    for (i = 0; i < xi2mask_num_masks(xi2mask); i++) {
        const unsigned char *m;
        xi2mask_set(xi2mask, i, i);

        dev.id = i;
        assert(xi2mask_isset(xi2mask, &dev, i));

        m = xi2mask_get_one_mask(xi2mask, i);
        SetBit(mask, i);
        assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0);
        ClearBit(mask, i);
    }

    /* ensure zeros one-by-one */
    for (i = 0; i < xi2mask_num_masks(xi2mask); i++) {
        const unsigned char *m = xi2mask_get_one_mask(xi2mask, i);
        assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) != 0);
        xi2mask_zero(xi2mask, i);
        assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0);
    }

    /* re-set, zero all */
    for (i = 0; i < xi2mask_num_masks(xi2mask); i++)
        xi2mask_set(xi2mask, i, i);
    xi2mask_zero(xi2mask, -1);

    for (i = 0; i < xi2mask_num_masks(xi2mask); i++) {
        const unsigned char *m = xi2mask_get_one_mask(xi2mask, i);
        assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0);
    }

    for (i = 0; i < xi2mask_num_masks(xi2mask); i++) {
        const unsigned char *m;
        SetBit(mask, i);
        xi2mask_set_one_mask(xi2mask, i, mask, xi2mask_mask_size(xi2mask));
        m = xi2mask_get_one_mask(xi2mask, i);
        assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0);
        ClearBit(mask, i);
    }

    mergemask = xi2mask_new_with_size(MAXDEVICES + 2, (MAXDEVICES + 2) * 2);
    for (i = 0; i < xi2mask_num_masks(mergemask); i++) {
        dev.id = i;
        xi2mask_set(mergemask, i, i * 2);
    }

    /* xi2mask still has all i bits set, should now also have all i * 2 bits */
    xi2mask_merge(xi2mask, mergemask);
    for (i = 0; i < xi2mask_num_masks(mergemask); i++) {
        const unsigned char *m = xi2mask_get_one_mask(xi2mask, i);
        SetBit(mask, i);
        SetBit(mask, i * 2);
        assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0);
        ClearBit(mask, i);
        ClearBit(mask, i * 2);
    }

    xi2mask_free(&xi2mask);
    assert(xi2mask == NULL);

    xi2mask_free(&mergemask);
    assert(mergemask == NULL);
    free(mask);
}


int main(int argc, char** argv)
{
    xi2mask_test();

    return 0;
}