Debian Codegraph @

tests/test_selection.c

raw
1 #include <check.h>
2 #include <freesasa_internal.h>
3 #include <math.h>
4 #include <selection.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 #include "tools.h"
9
10 #define N 11
11
12 freesasa_structure *structure;
13 freesasa_result *result;
14 // clang-format off
15 double radii[N] = { 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6};
16 const char *name[N] = { " CA ", " CA ", " CA ", " O ", " N ", " SD ", " CB ", " OXT", "SE ", " P ", " O5'"};
17 const char *resn[N] = { "ALA", "ALA", "ALA", "ALA", "ARG", "MET", "VAL", "GLU", "SEC", " DC", " DA"};
18 const char *resi[N] = { " -2", " -1", " 1", " 1", " 2", " 3", " 4", " 1", " 2", " 4", " 5"};
19 const char chain[N] = { 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'};
20 const int symb_O[N] = { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1};
21 const int symb_C[N] = { 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0};
22 const int symb_N[N] = { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0};
23 const int symb_S[N] = { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0};
24 const int symb_SE[N]= { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
25 const int symb_P[N] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0};
26 const int resn_A[N] = { 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0};
27 const int resn_R[N] = { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0};
28 const int resn_M[N] = { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0};
29 const int resn_V[N] = { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0};
30 const int resn_Q[N] = { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0};
31 const int resn_U[N] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
32 const int resn_DC[N]= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0};
33 const int name_CA[N]= { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
34 const int name_O[N] = { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0};
35 const int name_OXT[N]={ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0};
36 const int name_O5p[N]={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
37 const int resi_1r4[N]={ 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0};
38 const int resi_1[N] = { 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0};
39 const int resi_2r4[N]={ 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0};
40 const int resi_m2r1[N]={ 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0};
41 const int resi_m2rm1[N]={ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
42 const int chain_A[N]= { 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
43 const int chain_B[N]= { 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
44 const int all[N]= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
45 // clang-format on
46 char selection_name[100][FREESASA_MAX_SELECTION_NAME + 1];
47 double value[100];
48
49 static void
50 test_select(const char **command, int n_commands)
51 {
52 freesasa_selection *sel;
53 for (int i = 0; i < n_commands; ++i) {
54 ck_assert_int_eq(freesasa_select_area(command[i], selection_name[i], value + i, structure, result),
55 FREESASA_SUCCESS);
56 sel = freesasa_selection_new(command[i], structure, result);
57 ck_assert_ptr_ne(sel, NULL);
58 ck_assert_str_eq(freesasa_selection_command(sel), command[i]);
59 ck_assert_str_eq(freesasa_selection_name(sel), selection_name[i]);
60 ck_assert(float_eq(freesasa_selection_area(sel), value[i], 1e-10));
61 freesasa_selection_free(sel);
62 }
63 }
64
65 static void setup(void)
66 {
67 structure = freesasa_structure_new();
68 for (int i = 0; i < N; ++i) {
69 freesasa_structure_add_atom(structure, name[i], resn[i], resi[i], chain[i], i * 10, 0, 0);
70 }
71 result = freesasa_calc_structure(structure, NULL);
72 }
73
74 static void teardown(void)
75 {
76 freesasa_structure_free(structure);
77 freesasa_result_free(result);
78 }
79
80 static double addup(const int *sel, const freesasa_result *res)
81 {
82 double sum = 0;
83 for (int i = 0; i < res->n_atoms; ++i)
84 sum += sel[i] * res->sasa[i];
85 return sum;
86 }
87
88 START_TEST(test_selection_name)
89 {
90 freesasa_select_area("c, name ca", selection_name[0], value, structure, result);
91 ck_assert_str_eq(selection_name[0], "c");
92 freesasa_select_area("1, name ca", selection_name[0], value, structure, result);
93 ck_assert_str_eq(selection_name[0], "1");
94 freesasa_select_area("c1, name ca", selection_name[0], value, structure, result);
95 ck_assert_str_eq(selection_name[0], "c1");
96 freesasa_select_area("1c, name ca", selection_name[0], value, structure, result);
97 ck_assert_str_eq(selection_name[0], "1c");
98 freesasa_select_area("-1, name ca", selection_name[0], value, structure, result);
99 ck_assert_str_eq(selection_name[0], "-1");
100 freesasa_select_area("-1+2_abc, name ca", selection_name[0], value, structure, result);
101 ck_assert_str_eq(selection_name[0], "-1+2_abc");
102 }
103 END_TEST
104
105 START_TEST(test_name)
106 {
107 const char *commands[] = {"c1, name ca+o",
108 "c2, name ca",
109 "c3, name oxt",
110 "c4, name ca AND name o",
111 "c5, name ca OR name o",
112 "c6, name ca+o+oxt",
113 "c7, name o5'",
114 "c8, name o5'+ca"};
115 test_select(commands, 7);
116 ck_assert(value[0] > 5); // check that it's non-zero
117 ck_assert(float_eq(value[0], addup(name_CA, result) + addup(name_O, result), 1e-10));
118 ck_assert(float_eq(value[0], value[4], 1e-10));
119 ck_assert(float_eq(value[1], addup(name_CA, result), 1e-10));
120 ck_assert(float_eq(value[2], addup(name_OXT, result), 1e-10));
121 ck_assert(float_eq(value[3], 0, 1e-10));
122 ck_assert(float_eq(value[5],
123 addup(name_CA, result) + addup(name_O, result) + addup(name_OXT, result),
124 1e-10));
125 ck_assert(float_eq(value[6], addup(name_O5p, result), 1e-10));
126
127 freesasa_set_verbosity(FREESASA_V_SILENT);
128 ck_assert_int_eq(freesasa_select_area("c1, name ABCDE", selection_name[0], value, structure, result),
129 FREESASA_WARN);
130 freesasa_set_verbosity(FREESASA_V_NORMAL);
131 }
132 END_TEST
133
134 START_TEST(test_symbol)
135 {
136 const char *commands[] = {"c1, symbol o+c",
137 "c2, symbol O",
138 "c3, symbol C",
139 "c4, symbol O AND symbol C",
140 "c5, symbol O OR symbol C",
141 "c6, symbol O+C+SE",
142 "c7, symbol SE",
143 "c8, symbol O+C+SE and not symbol se"};
144 test_select(commands, 8);
145 ck_assert_str_eq(selection_name[0], "c1");
146 ck_assert_str_eq(selection_name[1], "c2");
147 ck_assert_str_eq(selection_name[2], "c3");
148 ck_assert(value[0] > 5); //just to check that it's non-zero
149 ck_assert(float_eq(value[0], addup(symb_O, result) + addup(symb_C, result), 1e-10));
150 ck_assert(float_eq(value[1], addup(symb_O, result), 1e-10));
151 ck_assert(float_eq(value[2], addup(symb_C, result), 1e-10));
152 ck_assert(float_eq(value[3], 0, 1e-10));
153 ck_assert(float_eq(value[4], value[0], 1e-10));
154 ck_assert(float_eq(value[5],
155 addup(symb_O, result) + addup(symb_C, result) + addup(symb_SE, result),
156 1e-10));
157 ck_assert(float_eq(value[6], addup(symb_SE, result), 1e-10));
158 ck_assert(float_eq(value[7], value[0], 1e-10));
159
160 freesasa_set_verbosity(FREESASA_V_SILENT);
161 ck_assert_int_eq(freesasa_select_area("c1, symbol ABC", selection_name[0], value, structure, result),
162 FREESASA_WARN);
163 ck_assert_int_eq(freesasa_select_area("c1, symbol 1", selection_name[0], value, structure, result),
164 FREESASA_WARN);
165 ck_assert_int_eq(freesasa_select_area("c1, symbol &%", selection_name[0], value, structure, result),
166 FREESASA_FAIL);
167 freesasa_set_verbosity(FREESASA_V_NORMAL);
168 }
169 END_TEST
170
171 START_TEST(test_resn)
172 {
173 const char *commands[] = {"c1, resn ala+arg",
174 "c2, resn ala",
175 "c3, resn arg",
176 "c4, resn ala AND resn arg",
177 "c5, resn ala OR resn arg",
178 "c6, resn ala+arg AND NOT resn arg"};
179 test_select(commands, 6);
180 ck_assert(value[0] > 5);
181 ck_assert(float_eq(value[0], addup(resn_A, result) + addup(resn_R, result), 1e-10));
182 ck_assert(float_eq(value[1], addup(resn_A, result), 1e-10));
183 ck_assert(float_eq(value[2], addup(resn_R, result), 1e-10));
184 ck_assert(float_eq(value[3], 0, 1e-10));
185 ck_assert(float_eq(value[4], value[0], 1e-10));
186 ck_assert(float_eq(value[5], value[1], 1e-10));
187
188 freesasa_set_verbosity(FREESASA_V_SILENT);
189 ck_assert_int_eq(freesasa_select_area("c1, resn ABCD", selection_name[0], value, structure, result),
190 FREESASA_WARN);
191 freesasa_set_verbosity(FREESASA_V_NORMAL);
192 }
193 END_TEST
194
195 START_TEST(test_resi)
196 {
197 const char *commands[] = {
198 "c1, resi \\-2-5",
199 "c2, resi 2-4",
200 "c3, resi 1",
201 "c4, resi \\-2 AND resi \\-1-5",
202 "c5, resi \\-2 OR resi \\-1-5",
203 "c6, resi \\-2-2+2-5",
204 "c7, resi \\-1+\\-2-4+5",
205 "c8, resi \\-2-2+7+9+3-5+100",
206 "c9, resi 1-4 AND NOT resi 2-4",
207 "c10,resi \\-2-",
208 "c11,resi -5",
209 "c12,resi \\-2-2+2-5",
210 "c13,resi -5 AND NOT resi \\-2+\\-1+1+5",
211 "c14,resi 1-2+3- AND NOT resi 5",
212 "c15,resi 2- AND NOT resi 5",
213 };
214 freesasa_set_verbosity(FREESASA_V_SILENT);
215 test_select(commands, 15);
216 freesasa_set_verbosity(FREESASA_V_NORMAL);
217 ck_assert(value[0] > 5);
218 ck_assert(float_eq(value[0], addup(all, result), 1e-10));
219 ck_assert(float_eq(value[1], addup(resi_2r4, result), 1e-10));
220 ck_assert(float_eq(value[2], addup(resi_1, result), 1e-10));
221 ck_assert(float_eq(value[3], 0, 1e-10));
222 ck_assert(float_eq(value[4], value[0], 1e-10));
223 ck_assert(float_eq(value[5], value[0], 1e-10));
224 ck_assert(float_eq(value[6], value[0], 1e-10));
225 ck_assert(float_eq(value[7], value[0], 1e-10));
226 ck_assert(float_eq(value[8], value[2], 1e-10));
227 ck_assert(float_eq(value[9], value[0], 1e-10));
228 ck_assert(float_eq(value[10], value[0], 1e-10));
229 ck_assert(float_eq(value[11], value[0], 1e-10));
230 ck_assert(float_eq(value[12], value[1], 1e-10));
231 ck_assert(float_eq(value[13], value[1] + value[2], 1e-10));
232 ck_assert(float_eq(value[14], value[1], 1e-10));
233
234 freesasa_set_verbosity(FREESASA_V_SILENT);
235 ck_assert_int_eq(freesasa_select_area("c1, resi 1A", selection_name[0], value, structure, result),
236 FREESASA_SUCCESS);
237 ck_assert_int_eq(freesasa_select_area("c1, resi 1a", selection_name[0], value, structure, result),
238 FREESASA_SUCCESS);
239 ck_assert_int_eq(freesasa_select_area("c1, resi A", selection_name[0], value, structure, result),
240 FREESASA_WARN);
241 ck_assert_int_eq(freesasa_select_area("c1, resi A1", selection_name[0], value, structure, result),
242 FREESASA_WARN);
243 ck_assert_int_eq(freesasa_select_area("c1, resi 1AA", selection_name[0], value, structure, result),
244 FREESASA_WARN);
245 ck_assert_int_eq(freesasa_select_area("c1, resi 1aa", selection_name[0], value, structure, result),
246 FREESASA_WARN);
247 ck_assert_int_eq(freesasa_select_area("c1, resi 1-A", selection_name[0], value, structure, result),
248 FREESASA_WARN);
249 ck_assert_int_eq(freesasa_select_area("c1, resi 1A-2", selection_name[0], value, structure, result),
250 FREESASA_WARN);
251 freesasa_set_verbosity(FREESASA_V_NORMAL);
252 }
253 END_TEST
254
255 START_TEST(test_chain)
256 {
257 const char *commands[] = {"c1, chain A+B",
258 "c2, chain A",
259 "c3, chain B",
260 "c4, chain A AND chain B",
261 "c5, chain A OR chain B",
262 "c6, chain A-B",
263 "c7, chain A-B AND NOT chain A"};
264 test_select(commands, 7);
265 ck_assert(value[0] > 5);
266 ck_assert(float_eq(value[0], addup(chain_A, result) + addup(chain_B, result), 1e-10));
267 ck_assert(float_eq(value[0], value[4], 1e-10));
268 ck_assert(float_eq(value[0], value[5], 1e-10));
269 ck_assert(float_eq(value[1], addup(chain_A, result), 1e-10));
270 ck_assert(float_eq(value[2], addup(chain_B, result), 1e-10));
271 ck_assert(float_eq(value[3], 0, 1e-10));
272 ck_assert(float_eq(value[6], addup(chain_B, result), 1e-10));
273
274 freesasa_set_verbosity(FREESASA_V_SILENT);
275 ck_assert_int_eq(freesasa_select_area("c1, chain AA", selection_name[0], value, structure, result),
276 FREESASA_WARN);
277 ck_assert_int_eq(freesasa_select_area("c1, chain A-1", selection_name[0], value, structure, result),
278 FREESASA_WARN);
279 ck_assert_int_eq(freesasa_select_area("c1, chain &", selection_name[0], value, structure, result),
280 FREESASA_FAIL);
281 freesasa_set_verbosity(FREESASA_V_NORMAL);
282 }
283 END_TEST
284
285 START_TEST(test_syntax_error)
286 {
287 double a;
288 int i;
289 char s[FREESASA_MAX_SELECTION_NAME + 1];
290 char s2[2 * FREESASA_MAX_SELECTION_NAME + 1], c = 'a';
291 const char *err[] = {
292 "",
293 "a",
294 "a,",
295 "a,b",
296 // no selection arg
297 "a,resi",
298 "a,resn",
299 "a,name",
300 "a,symbol",
301 "a,chain",
302 // no name of selection
303 ",resn ala",
304 ",resi 1",
305 ",name ca",
306 ", symbol c",
307 ",chain a",
308 "resn ala",
309 "resi 1",
310 "name ca",
311 "symbol c",
312 "chain a",
313 // comma wrong place, no name
314 "resn, ala",
315 "resi, 1",
316 "name, ca",
317 "symbol, c",
318 "chain, a",
319 // ranges (-) used where not allowed
320 "a, resn ala-arg",
321 "a, name ca-cb",
322 "a, symbol c-o",
323 // illegal range syntax
324 "a, resi 1-2-3",
325 "a, resi -1-2",
326 "a, resi 1-2-",
327 "a, chain A-",
328 // trailing +
329 "a, resn ala+",
330 "a, resn ala+arg+",
331 "a, resi 1-2+",
332 "a, resi 1+2-5+",
333 // boolean operators
334 "a, (resn ala) AND",
335 "a,(resn ala) OR",
336 "a,(resn ala) OR NOT",
337 "a, (resn ala) AND arg",
338 "a,(resn ala) OR arg",
339 "a, (resn ala) OR NOT arg",
340 "a, ala OR resn arg",
341 };
342 int n = sizeof(err) / sizeof(char *);
343 freesasa_set_verbosity(FREESASA_V_SILENT);
344 for (i = 0; i < n; ++i) {
345 ck_assert_int_eq(freesasa_select_area(err[i], s, &a, structure, result), FREESASA_FAIL);
346 }
347
348 // check that really long strings are truncated properly
349 for (i = 0; i < FREESASA_MAX_SELECTION_NAME + 5; ++i) {
350 s2[i] = c + (i % 25);
351 }
352 strcpy(s2 + i, ", resn ala");
353 ck_assert_int_eq(freesasa_select_area(s2, s, &a, structure, result), FREESASA_SUCCESS);
354 ck_assert_int_eq(strlen(s), FREESASA_MAX_SELECTION_NAME);
355 ck_assert(strncmp(s2, s, strlen(s)) == 0);
356 freesasa_set_verbosity(FREESASA_V_NORMAL);
357 }
358 END_TEST
359
360 // check that some complex constructs don't cause errors, but no checks for valid selection
361 START_TEST(test_complex_syntax)
362 {
363 double a;
364 char s[FREESASA_MAX_SELECTION_NAME + 1];
365 const char *stmt[] = {"a, (resn ala AND resi 1-3) OR (NOT chain A+B AND (symbol C OR symbol O))",
366 "a, NOT symbol SE+C AND NOT resi 5-7+1+\\-6-\\-2+100+200+10-11"};
367 int n = sizeof(stmt) / sizeof(char *);
368 freesasa_set_verbosity(FREESASA_V_SILENT);
369 for (int i = 0; i < n; ++i) {
370 ck_assert_int_eq(freesasa_select_area(stmt[i], s, &a, structure, result), FREESASA_SUCCESS);
371 }
372 freesasa_set_verbosity(FREESASA_V_NORMAL);
373 }
374 END_TEST
375
376 extern void *freesasa_selection_dummy_ptr;
377
378 extern int
379 freesasa_wrap_select_atoms(void *selection,
380 const expression *expr,
381 const freesasa_structure *structure);
382
383 extern freesasa_selection *
384 freesasa_selection_alloc(const char *name, const char *command);
385
386 START_TEST(test_memerr)
387 {
388 void *ptr1, *ptr2, *ptr3, *ptr4;
389 freesasa_set_verbosity(FREESASA_V_SILENT);
390 set_fail_after(1);
391 ptr1 = freesasa_selection_atom(E_SYMBOL, "C");
392 ptr2 = freesasa_selection_create(NULL, "bla");
393 ptr3 = freesasa_selection_selector(E_SYMBOL, NULL);
394 ptr4 = freesasa_selection_operation(E_AND, NULL, NULL);
395 set_fail_after(0);
396 ck_assert_ptr_eq(ptr1, NULL);
397 ck_assert_ptr_eq(ptr2, NULL);
398 ck_assert_ptr_eq(ptr3, NULL);
399 ck_assert_ptr_eq(ptr4, NULL);
400 set_fail_after(2);
401 ptr1 = freesasa_selection_atom(E_SYMBOL, "C");
402 set_fail_after(2);
403 ptr2 = freesasa_selection_create(NULL, "bla");
404 set_fail_after(2);
405 ptr3 = freesasa_selection_atom(E_NEGNUM, "-1");
406 set_fail_after(0);
407 ck_assert_ptr_eq(ptr1, NULL);
408 ck_assert_ptr_eq(ptr2, NULL);
409 ck_assert_ptr_eq(ptr3, NULL);
410
411 expression e = {.left = NULL, .right = NULL, .type = E_OR, .value = NULL};
412
413 for (int i = 1; i < 5; ++i) {
414 set_fail_after(i);
415 int ret = freesasa_wrap_select_atoms(freesasa_selection_dummy_ptr, &e, structure);
416 set_fail_after(0);
417 ck_assert_int_eq(ret, FREESASA_FAIL);
418 }
419
420 freesasa_selection *sel, *sel2;
421
422 // the following lines fail with some compilers (at least gcc-5.4 on Ubuntu 16.04)
423 // todo: figure out why and fix it
424
425 // can't go deeper here, because our malloc mocking doesn't work with flex/bison.
426 // set_fail_after(1);
427 // sel = freesasa_selection_new("a, atom 1", structure, result);
428 // set_fail_after(0);
429 // ck_assert_ptr_eq(sel, NULL);
430
431 for (int i = 1; i < 4; ++i) {
432 set_fail_after(i);
433 sel = freesasa_selection_alloc("bla", "bla");
434 set_fail_after(0);
435 ck_assert_ptr_eq(sel, NULL);
436
437 // this one should be valid
438 sel = freesasa_selection_alloc("bla", "bla");
439 set_fail_after(i);
440 sel2 = freesasa_selection_clone(sel);
441 set_fail_after(0);
442 ck_assert_ptr_eq(sel2, NULL);
443 }
444 freesasa_set_verbosity(FREESASA_V_NORMAL);
445 }
446 END_TEST
447
448 extern TCase *test_selection_static();
449
450 Suite *selector_suite()
451 {
452 Suite *s = suite_create("Selector");
453
454 TCase *tc_core = tcase_create("Core");
455 tcase_add_checked_fixture(tc_core, setup, teardown);
456 tcase_add_test(tc_core, test_selection_name);
457 tcase_add_test(tc_core, test_name);
458 tcase_add_test(tc_core, test_symbol);
459 tcase_add_test(tc_core, test_resn);
460 tcase_add_test(tc_core, test_resi);
461 tcase_add_test(tc_core, test_chain);
462 tcase_add_test(tc_core, test_memerr);
463
464 TCase *tc_static = test_selection_static();
465
466 TCase *tc_syntax = tcase_create("Syntax");
467 // just to avoid passing NULL pointers
468 tcase_add_checked_fixture(tc_syntax, setup, teardown);
469 tcase_add_test(tc_syntax, test_syntax_error);
470 tcase_add_test(tc_syntax, test_complex_syntax);
471
472 suite_add_tcase(s, tc_core);
473 suite_add_tcase(s, tc_syntax);
474 suite_add_tcase(s, tc_static);
475
476 return s;
477 }