/** Add residue to config. If the residue already exists, it returns the index of that residue, else it returns the index of the new residue. Returns FREESASA_FAILURE if realloc/strdup fails. */ static int add_residue(struct classifier_config *config, const char* name) { char **rn = config->residue_name; struct classifier_residue **cr = config->residue; int res = find_string(config->residue_name, name, config->n_residues); if (res >= 0) return res; res = config->n_residues + 1; if ((config->residue_name = realloc(rn, sizeof(char*) * res)) == NULL) { config->residue_name = rn; return mem_fail(); } if ((config->residue = realloc(cr, sizeof(struct classifier_residue *) * res)) == NULL) { config->residue = cr; return mem_fail(); } if ((config->residue[res-1] = classifier_residue_new(name)) == NULL) { return mem_fail(); } ++config->n_residues; config->residue_name[res-1] = config->residue[res-1]->name; return res-1; }
END_TEST START_TEST (test_classifier) { freesasa_set_verbosity(FREESASA_V_SILENT); set_fail_freq(1); ck_assert_ptr_eq(classifier_types_new(),NULL); ck_assert_ptr_eq(classifier_residue_new("A"),NULL); ck_assert_ptr_eq(classifier_config_new(),NULL); for (int i = 1; i < 5; ++i) { set_fail_freq(10000); struct classifier_types *types = classifier_types_new(); struct classifier_residue *res = classifier_residue_new("ALA"); struct classifier_config *cfg = classifier_config_new(); if (i < 3) { set_fail_freq(i); ck_assert_int_eq(add_class(types,"A"),FREESASA_FAIL); set_fail_freq(i); ck_assert_int_eq(add_type(types,"a","A",1.0),FREESASA_FAIL); } set_fail_freq(i); ck_assert_int_eq(add_atom(res,"A",1.0,0),FREESASA_FAIL); set_fail_freq(i); ck_assert_int_eq(add_residue(cfg,"A"),FREESASA_FAIL); classifier_types_free(types); classifier_residue_free(res); classifier_config_free(cfg); } // don't test all levels, but make sure errors in low level // allocation propagates to the interface FILE *config = fopen(SHAREDIR "naccess.config","r"); ck_assert_ptr_ne(config, NULL); for (int i = 1; i < 50; ++i) { set_fail_freq(i); ck_assert_ptr_eq(freesasa_classifier_from_file(config),NULL); rewind(config); } fclose(config); freesasa_set_verbosity(FREESASA_V_NORMAL); }