Add a library of common test functions
[platform/upstream/libxkbcommon.git] / test / common.c
1 /*
2 Copyright 2009 Dan Nicholson
3 Copyright © 2012 Daniel Stone
4 Copyright © 2012 Ran Benita
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23 Except as contained in this notice, the names of the authors or their
24 institutions shall not be used in advertising or otherwise to promote the
25 sale, use or other dealings in this Software without prior written
26 authorization from the authors.
27 */
28
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38
39 #include "xkbcommon/xkbcommon.h"
40 #include "test.h"
41
42 const char *
43 test_get_path(const char *path_rel)
44 {
45     static char path[PATH_MAX];
46     const char *srcdir = getenv("srcdir");
47
48     snprintf(path, PATH_MAX - 1,
49              "%s/test/data/%s", srcdir ? srcdir : ".", path_rel);
50
51     return path;
52 }
53
54 char *
55 test_read_file(const char *path_rel)
56 {
57     struct stat info;
58     char *ret, *tmp;
59     int fd, count, remaining;
60
61     fd = open(test_get_path(path_rel), O_RDONLY);
62     if (fd < 0)
63         return NULL;
64
65     if (fstat(fd, &info) != 0) {
66         close(fd);
67         return NULL;
68     }
69
70     ret = malloc(info.st_size + 1);
71     if (!ret) {
72         close(fd);
73         return NULL;
74     }
75
76     remaining = info.st_size;
77     tmp = ret;
78     while ((count = read(fd, tmp, remaining))) {
79         remaining -= count;
80         tmp += count;
81     }
82     ret[info.st_size] = '\0';
83     close(fd);
84
85     if (remaining != 0) {
86         free(ret);
87         return NULL;
88     }
89
90     return ret;
91 }
92
93 struct xkb_context *
94 test_get_context(void)
95 {
96     return xkb_context_new(0);
97 }
98
99 struct xkb_keymap *
100 test_compile_file(struct xkb_context *context, const char *path_rel)
101 {
102     struct xkb_keymap *keymap;
103     FILE *file;
104     const char *path = test_get_path(path_rel);
105
106     file = fopen(path, "r");
107     if (!file) {
108         fprintf(stderr, "Failed to open path: %s\n", path);
109         return NULL;
110     }
111     assert(file != NULL);
112
113     keymap = xkb_map_new_from_file(context, file,
114                                    XKB_KEYMAP_FORMAT_TEXT_V1, 0);
115     fclose(file);
116
117     if (!keymap) {
118         fprintf(stderr, "Failed to compile path: %s\n", path);
119         return NULL;
120     }
121
122     fprintf(stderr, "Successfully compiled path: %s\n", path);
123
124     return keymap;
125 }
126
127 struct xkb_keymap *
128 test_compile_string(struct xkb_context *context, const char *string)
129 {
130     struct xkb_keymap *keymap;
131
132     keymap = xkb_map_new_from_string(context, string,
133                                      XKB_KEYMAP_FORMAT_TEXT_V1, 0);
134     if (!keymap) {
135         fprintf(stderr, "Failed to compile string\n");
136         return NULL;
137     }
138
139     return keymap;
140 }
141
142 struct xkb_keymap *
143 test_compile_rules(struct xkb_context *context, const char *rules,
144                    const char *model, const char *layout,
145                    const char *variant, const char *options)
146 {
147     struct xkb_keymap *keymap;
148     struct xkb_rule_names rmlvo = {
149         .rules = rules,
150         .model = model,
151         .layout = layout,
152         .variant = variant,
153         .options = options
154     };
155
156     keymap = xkb_map_new_from_names(context, &rmlvo, 0);
157     if (!keymap) {
158         fprintf(stderr,
159                 "Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
160                 rules, model, layout, variant, options);
161         return NULL;
162     }
163
164     return keymap;
165 }