Debian Codegraph @

tests/resolve_table.c

raw symbols SCIP
1 /* liblouis Braille Translation and Back-Translation Library
2
3 Copyright (C) 2014 Swiss Library for the Blind, Visually Impaired and Print Disabled
4
5 Copying and distribution of this file, with or without modification,
6 are permitted in any medium without royalty provided the copyright
7 notice and this notice are preserved. This file is offered as-is,
8 without any warranty. */
9
10 #include <config.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include "liblouis.h"
16 #include "resolve_table.h"
17
18 #define ASSERT(test) \
19 do { \
20 if (!(test)) \
21 result = 1; \
22 } while(0) \
23
24 int
25 main(int argc, char **argv)
26 {
27
28 /* ====================================================================== *
29 * `-- resolve_table *
30 * |-- dir_1 *
31 * | |-- dir_1.1 *
32 * | | `-- table_1.1.1 *
33 * | |-- table_1.1 *
34 * | |-- table_1.2 ------------------------------ *
35 * | `-- table_1.3 --> | include dir_1.1/table_1.1.1 | *
36 * |-- dir_2 `----------------------------- *
37 * | |-- table_2.1 ------------------ *
38 * | `-- table_2.2 --> | include table_1 | *
39 * |-- table_1 `----------------- *
40 * |-- table_2 ------------------ *
41 * |-- table_3 --> | include table_2 | *
42 * | `----------------- *
43 * | -------------------------- *
44 * |-- table_4 --> | include dir_1/table_1.3 | *
45 * | `------------------------- *
46 * | -------------------------- *
47 * |-- table_5 --> | include dir_2/table_2.2 | *
48 * | `------------------------- *
49 * | ---------------------- *
50 * `-- table_6 --> | dir_1.1/table_1.1.1 | *
51 * `--------------------- *
52 * ====================================================================== */
53
54 int result = 0;
55
56 // this test relies on being in the test dir, so that it can test
57 // finding tables by relative path
58 if (chdir(TEST_SRC_DIR)) return 1;
59
60 // Full path
61 setenv ("LOUIS_TABLEPATH", "", 1);
62 ASSERT (lou_getTable ("tables/resolve_table/table_1"));
63
64 // File name not on LOUIS_TABLEPATH
65 ASSERT (!lou_getTable ("table_1"));
66
67 // File name on LOUIS_TABLEPATH
68 setenv ("LOUIS_TABLEPATH", "tables/resolve_table", 1);
69 ASSERT (lou_getTable ("table_1"));
70
71 // First is full path, second is in same directory
72 setenv ("LOUIS_TABLEPATH", "", 1);
73 ASSERT (lou_getTable ("tables/resolve_table/table_1,"
74 "table_2"));
75
76 // First is full path, second is not in same directory
77 ASSERT (!lou_getTable ("tables/resolve_table/table_1,"
78 "table_1.1.1"));
79
80 // Two full paths
81 ASSERT (lou_getTable ("tables/resolve_table/dir_1/table_1.1,"
82 "tables/resolve_table/dir_2/table_2.1"));
83
84 // First is full path, second is on LOUIS_TABLEPATH, third is in same
85 // directory as first
86 setenv ("LOUIS_TABLEPATH", "tables/resolve_table/dir_2", 1);
87 ASSERT (lou_getTable ("tables/resolve_table/dir_1/table_1.1,"
88 "table_2.1,"
89 "table_1.2"));
90
91 // First is full path, second is in subdirectory
92 setenv ("LOUIS_TABLEPATH", "", 1);
93 ASSERT (lou_getTable ("tables/resolve_table/table_1,"
94 "dir_1/table_1.1"));
95
96 // Two file names in different directories, but both on LOUIS_TABLEPATH
97 setenv ("LOUIS_TABLEPATH", "tables/resolve_table/dir_1,"
98 "tables/resolve_table/dir_2", 1);
99 ASSERT (lou_getTable ("table_1.2,"
100 "table_2.1"));
101
102 // First is file name on LOUIS_TABLEPATH, second is full path, third is in
103 // same directory as second
104 setenv ("LOUIS_TABLEPATH", "tables/resolve_table", 1);
105 ASSERT (!lou_getTable ("table_1,"
106 "tables/resolve_table/dir_1/table_1.1,"
107 "table_1.2"));
108
109 // Full path, include table in same directory
110 setenv ("LOUIS_TABLEPATH", "", 1);
111 ASSERT (lou_getTable ("tables/resolve_table/table_3"));
112
113 // Full path, include table in subdirectory
114 ASSERT (lou_getTable ("tables/resolve_table/dir_1/table_1.3"));
115
116 // Full path, include table in subdirectory, from there include table in
117 // sub-subdirectory
118 ASSERT (lou_getTable ("tables/resolve_table/table_4"));
119
120 // Full path, include table in subdirectory, from there include table in
121 // first directory
122 ASSERT (!lou_getTable ("tables/resolve_table/table_5"));
123
124 // Full path, include table in subdirectory, from there include table on
125 // LOUIS_TABLEPATH
126 setenv ("LOUIS_TABLEPATH", "tables/resolve_table", 1);
127 ASSERT (lou_getTable ("tables/resolve_table/table_5"));
128
129 // Full path, include table in subdirectory of LOUIS_TABLEPATH
130 setenv ("LOUIS_TABLEPATH", "tables/resolve_table/dir_1", 1);
131 ASSERT (lou_getTable ("tables/resolve_table/table_6"));
132
133 // Overlong LOUIS_TABLEPATH: the search path cannot be constructed, but
134 // this must not crash, and full paths must still resolve (see issue #1999)
135 {
136 char longPath[4096];
137 memset (longPath, 'a', sizeof(longPath) - 1);
138 longPath[sizeof(longPath) - 1] = '\0';
139 setenv ("LOUIS_TABLEPATH", longPath, 1);
140 lou_free(); // clear the table cache so resolution actually runs
141 ASSERT (!lou_getTable ("table_1"));
142 ASSERT (lou_getTable ("tables/resolve_table/table_1"));
143 }
144
145 lou_free();
146
147 return result;
148
149 }