Skip to content

Commit

Permalink
Add tests for a few more cases to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mittinatten committed Oct 22, 2017
1 parent 11a5030 commit d474d17
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 34 deletions.
6 changes: 4 additions & 2 deletions src/json.c
Expand Up @@ -10,7 +10,7 @@
#include "freesasa_internal.h"
#include "classifier.h"

/** The functions in JSON-C does not seem to have any documented error
/** The functions in JSON-C do not seem to have any documented error
return values. Therefore these errors are not caught. */

json_object *
Expand Down Expand Up @@ -163,7 +163,9 @@ freesasa_node2json(freesasa_node *node,
freesasa_node *child = freesasa_node_children(node);
if (child) {
if (freesasa_node_type(child) == exclude_type) lowest = 1;
if (!lowest) array = json_object_new_array();
if (!lowest) {
array = json_object_new_array();
}
}

switch (type) {
Expand Down
35 changes: 22 additions & 13 deletions src/selection.c
Expand Up @@ -165,21 +165,21 @@ freesasa_selection_operation(expression_type type,
return e;
}

//for debugging
// for debugging
static void
print_expr(const expression *e,int level)
print_expr(FILE * output, const expression *e, int level)
{
fprintf(stderr,"\n");
for (int i = 0; i < level; ++i) fprintf(stderr," ");
if (e == NULL) fprintf(stderr,"()");
fprintf(output, "\n");
for (int i = 0; i < level; ++i) fprintf(output, " ");
if (e == NULL) fprintf(output, "()");
else {
fprintf(stderr,"(%s ",e_str(e->type));
if (e->value) fprintf(stderr,": %s ",e->value);
print_expr(e->left,level+1);
print_expr(e->right,level+1);
fprintf(stderr,")");
fprintf(output, "(%s ",e_str(e->type));
if (e->value) fprintf(output, ": %s ",e->value);
print_expr(output, e->left, level+1);
print_expr(output, e->right, level+1);
fprintf(output, ")");
}
fflush(stderr);
fflush(output);
}


Expand Down Expand Up @@ -795,8 +795,8 @@ int freesasa_selection_parse_error(expression *e,
yyscan_t scanner,
const char *msg)
{
if (freesasa_get_verbosity() == FREESASA_V_DEBUG) print_expr(e,0);
if (freesasa_get_verbosity() == FREESASA_V_NORMAL) fprintf(stderr,"\n");
if (freesasa_get_verbosity() == FREESASA_V_DEBUG) print_expr(stderr, e, 0);
if (freesasa_get_verbosity() == FREESASA_V_NORMAL) fprintf(stderr, "\n");
return freesasa_fail(msg);
}

Expand Down Expand Up @@ -883,6 +883,14 @@ START_TEST (test_expression)
}
END_TEST

START_TEST (test_debug) // this test just runs the debug output code to not get artificially low coverage
{
FILE *devnull = fopen("/dev/null", "w");
expression *e = get_expression("c1, symbol O+C");
print_expr(devnull, e, 0);
}
END_TEST

struct selection selection_dummy = {.size = 1, .name = NULL, .atom = NULL};

void *freesasa_selection_dummy_ptr = &selection_dummy;
Expand All @@ -901,6 +909,7 @@ test_selection_static()
TCase *tc = tcase_create("selection.c static");
tcase_add_test(tc, test_selection);
tcase_add_test(tc, test_expression);
tcase_add_test(tc, test_debug);

return tc;
}
Expand Down
8 changes: 7 additions & 1 deletion tests/test-cli.in
Expand Up @@ -13,7 +13,7 @@ sharedir=@top_srcdir@/share/
dump=tmp/test-cli.dump # will only contain the latest output
nofile=nonexistent-file
nodir=nonexistent-dir/file
smallpdb=$datadir/rsa/ALA.pdb
smallpdb=$datadir/rsa/GLY.pdb
#global errors
errors=0
use_xml=0
Expand Down Expand Up @@ -132,6 +132,9 @@ assert_pass "grep 'atoms\s\s*: 660' $dump"
assert_pass "grep 'Total\s\s*:\s\s*5656.65' $dump"
assert_pass "$cli -S -n 50 < $datadir/1ubq.pdb > $dump"
assert_fail "$cli -S -n 0 < $datadir/1ubq.pdb > $dump"
assert_fail "$cli -S -n \"-1\" < $datadir/1ubq.pdb > $dump"
assert_fail "$cli -S -t 1000 < $datadir/1ubq.pdb > $dump"
assert_pass "$cli -S -t 16 < $smallpdb > $dump"
echo
echo "== Testing -m -M and -C options =="
# using flags -S and -n 10 to speed things up
Expand Down Expand Up @@ -159,6 +162,9 @@ echo "== Testing L&R =="
assert_pass "$cli -L < $smallpdb > $dump"
assert_pass "$cli -L -n 10 < $smallpdb > $dump"
assert_fail "$cli -L -n 0 < $smallpdb > $dump"
assert_fail "$cli -L -n \"-1\" < $smallpdb > $dump"
assert_fail "$cli -L -t 1000 < $datadir/1ubq.pdb > $dump"
assert_pass "$cli -L -t 16 < $smallpdb > $dump"
echo
echo "== Testing option --chain-groups =="
assert_pass "$cli -g S -S -n 10 $smallpdb > $dump"
Expand Down
10 changes: 5 additions & 5 deletions tests/test_json.c
Expand Up @@ -8,7 +8,7 @@
#include "tools.h"

extern json_object *
freesasa_node2json(freesasa_node *node, int exclude_type);
freesasa_node2json(freesasa_node *node, int exclude_type, int options);

static int
compare_nodearea(json_object *obj, const freesasa_nodearea *ref, int is_abs)
Expand Down Expand Up @@ -56,7 +56,7 @@ int
test_atom(freesasa_node *node)
{
ck_assert_ptr_ne(node, NULL);
json_object *atom = freesasa_node2json(node, FREESASA_NODE_NONE);
json_object *atom = freesasa_node2json(node, FREESASA_NODE_NONE, 0);
ck_assert_ptr_ne(atom, NULL);

struct json_object_iterator it = json_object_iter_begin(atom),
Expand Down Expand Up @@ -93,7 +93,7 @@ int
test_residue(freesasa_node *node)
{
ck_assert_ptr_ne(node, NULL);
json_object *residue = freesasa_node2json(node, FREESASA_NODE_NONE);
json_object *residue = freesasa_node2json(node, FREESASA_NODE_NONE, 0);
ck_assert_ptr_ne(residue, NULL);
const freesasa_nodearea *resarea = freesasa_node_area(node);
struct json_object_iterator it = json_object_iter_begin(residue),
Expand Down Expand Up @@ -133,7 +133,7 @@ int
test_chain(freesasa_node *node, const freesasa_result *result)
{
ck_assert_ptr_ne(node, NULL);
json_object *chain = freesasa_node2json(node, FREESASA_NODE_NONE);
json_object *chain = freesasa_node2json(node, FREESASA_NODE_NONE, 0);
const freesasa_nodearea *chain_area = freesasa_node_area(node);
ck_assert_ptr_ne(chain, NULL);
ck_assert(float_eq(chain_area->total, result->total, 1e-10));
Expand Down Expand Up @@ -176,7 +176,7 @@ test_structure(freesasa_node *node)
.side_chain = 3689.8982162353718,
.main_chain = 1114.157424906374
};
json_object *jstruct = freesasa_node2json(node, FREESASA_NODE_NONE);
json_object *jstruct = freesasa_node2json(node, FREESASA_NODE_NONE, 0);
ck_assert_ptr_ne(jstruct, NULL);

struct json_object_iterator it = json_object_iter_begin(jstruct),
Expand Down
56 changes: 43 additions & 13 deletions tests/test_xml.c
Expand Up @@ -98,19 +98,37 @@ int xmlTextWriterEndDocument(xmlTextWriterPtr a) {
return real_twed(a);
}

START_TEST (test_libxmlerr)
{
FILE *pdb = fopen(DATADIR "1ubq.pdb", "r"), *devnull = fopen("/dev/null", "w");
freesasa_structure *ubq =
freesasa_structure_from_pdb(pdb, &freesasa_default_classifier, 0);
static FILE *pdb, *devnull;
static freesasa_structure *ubq;
static freesasa_result *result;
static freesasa_node *tree, *structure_node;
static freesasa_selection *selection;


static void setup(void) {
pdb = fopen(DATADIR "1ubq.pdb", "r");
devnull = fopen("/dev/null", "w");
ubq = freesasa_structure_from_pdb(pdb, &freesasa_default_classifier, 0);
fclose(pdb);
freesasa_result *result = freesasa_calc_structure(ubq, NULL);
freesasa_node *tree = freesasa_tree_new(), *structure_node;
freesasa_selection *selection = freesasa_selection_new("ala, resn ala", ubq, result);
int ret;
result = freesasa_calc_structure(ubq, NULL);
tree = freesasa_tree_new();
selection = freesasa_selection_new("ala, resn ala", ubq, result);

freesasa_tree_add_result(tree, result, ubq, "test");
structure_node = freesasa_node_children(freesasa_node_children(tree));
freesasa_node_structure_add_selection(structure_node, selection);
}

static void teardown(void) {
freesasa_node_free(tree);
freesasa_result_free(result);
freesasa_selection_free(selection);
freesasa_structure_free(ubq);
}

START_TEST (test_libxmlerr)
{
int ret;

freesasa_set_verbosity(FREESASA_V_SILENT);
for (int i = 1; i < 100; ++i) {
Expand All @@ -126,17 +144,29 @@ START_TEST (test_libxmlerr)
ck_assert_int_eq(ret, FREESASA_FAIL);
}
freesasa_set_verbosity(FREESASA_V_NORMAL);
freesasa_node_free(tree);
freesasa_result_free(result);
freesasa_selection_free(selection);
freesasa_structure_free(ubq);
}
END_TEST

START_TEST (test_memerr) {
int ret;

freesasa_set_verbosity(FREESASA_V_SILENT);
for (int i = 1; i < 35; ++i) {
set_fail_after(i);
ret = freesasa_write_xml(devnull, tree, FREESASA_OUTPUT_ATOM);
set_fail_after(0);
ck_assert_int_eq(ret, FREESASA_FAIL);
}
freesasa_set_verbosity(FREESASA_V_NORMAL);
}
END_TEST

Suite* xml_suite() {
Suite *s = suite_create("XML");
TCase *tc_core = tcase_create("Core");
tcase_add_checked_fixture(tc_core,setup, teardown);
tcase_add_test(tc_core, test_libxmlerr);
tcase_add_test(tc_core, test_memerr);

suite_add_tcase(s, tc_core);

Expand Down

0 comments on commit d474d17

Please sign in to comment.