summaryrefslogtreecommitdiff
path: root/test/CursorName.c
blob: a3f8aae97fcf7c9372fd09f0d3c1ff0cb5cd361a (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
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (c) 2023, Oracle and/or its affiliates.
 *
 * 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.
 */

/* Test code for XmuCursorNameToIndex() in src/CursorName.c */
#include "config.h"
#include <X11/Xmu/CurUtil.h>
#include <X11/Xmu/CharSet.h>
#include <glib.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SKIP_WHITESPACE(p)  while (isspace(*p)) p++

/* Looks up each entry from <X11/cursorfont.h> to verify value returned */
static void
test_CursorNameToIndex_goodnames(void)
{
    FILE *cursorfont;
    char line[256];
    int cursorschecked = 0;
    int cursorsexpected = 0;

    cursorfont = fopen("/usr/include/X11/cursorfont.h", "r");
    if (cursorfont == NULL) {
        g_test_skip("Could not open /usr/include/X11/cursorfont.h");
        return;
    }

    while (fgets(line, sizeof(line), cursorfont) != NULL) {
        char *p = line;

        /* skip lines that don't start with "#define" */
        if (strncmp(p, "#define", 7) != 0)
            continue;
        else
            p += 7;

        /* skip over whitespace after #define */
        SKIP_WHITESPACE(p);

        /* skip #define _X11_CURSORFONT_H_ */
        if (strncmp(p, "XC_", 3) != 0)
            continue;
        else
            p += 3;

        if (strncmp(p, "num_glyphs", 10) == 0) {
            /* Use #define XC_num_glyphs to record the number we expect */
            g_assert_cmpint(cursorsexpected, ==, 0);
            p += strlen("num_glyphs");
            SKIP_WHITESPACE(p);
            cursorsexpected = (int) strtol(p, NULL, 0) / 2;
            g_test_message("cursors expected = %d", cursorsexpected);
            continue;
        }
        else {
            /* Should be a cursor name then */
            char *name = p;
            int expected_id, returned_id;
            char upper_name[32];

            while (!isspace(*p))
                p++;
            *p++ = '\0';
            SKIP_WHITESPACE(p);
            expected_id = (int) strtol(p, NULL, 0);

            g_test_message("%s = %d", name, expected_id);

            returned_id = XmuCursorNameToIndex(name);
            g_assert_cmpint(returned_id, ==, expected_id);

            XmuNCopyISOLatin1Uppered(upper_name, name, sizeof(upper_name));
            returned_id = XmuCursorNameToIndex(upper_name);
            g_assert_cmpint(returned_id, ==, expected_id);

            cursorschecked++;
        }
    }

    fclose(cursorfont);

    g_assert_cmpint(cursorschecked, ==, cursorsexpected);
}

static void
test_CursorNameToIndex_badnames(void)
{
    const char *badnames[] = {
        "does-not-exist",
        "starts-with-a-good-name", /* starts with "star" */
        "num_glyphs",
        ""
    };
#define NUM_BAD_NAMES (sizeof(badnames) / sizeof(badnames[0]))

    for (unsigned int i = 0; i < NUM_BAD_NAMES; i++) {
        int returned_id = XmuCursorNameToIndex(badnames[i]);
        g_test_message("%s", badnames[i]);
        g_assert_cmpint(returned_id, ==, -1);
    }
}

int
main(int argc, char** argv)
{
    g_test_init(&argc, &argv, NULL);
    g_test_bug_base(PACKAGE_BUGREPORT);

    g_test_add_func("/CursorName/XmuCursorNameToIndex/good-names",
                    test_CursorNameToIndex_goodnames);

    g_test_add_func("/CursorName/XmuCursorNameToIndex/bad-names",
                    test_CursorNameToIndex_badnames);

    return g_test_run();
}