/* * Search enum_name array with in prefixed and postfixed uppercase */ int alg_enum_search_ppfix (enum_names *ed, const char *prefix , const char *postfix, const char *str , int str_len) { char buf[64]; char *ptr; int ret; int len=sizeof(buf)-1; /* reserve space for final \0 */ for (ptr=buf; *prefix; *ptr++=*prefix++, len--); while (str_len--&&len--&&*str) { *ptr++=toupper(*str++); } while (len--&&*postfix) { *ptr++=*postfix++; } *ptr=0; DBG(DBG_CRYPT, DBG_log("enum_search_ppfixi () " "calling enum_search(%p, \"%s\")", ed, buf)); ret=enum_search(ed, buf); return ret; }
void do_test(enum_names *enum_test, int max) { int i = 0; for(i;i<256;i++) { int found; const char *name = enum_name(enum_test, i); if (name != NULL) { found = enum_search(enum_test, name); fprintf(stdout,"%s: [%5d] -> %s -> %d\n", found == i ? "OK" : "ERROR", i, name, found); } } fprintf(stdout,"\n"); fflush(stdout); }
static void test_enum(enum_names *enum_test, int i, enum where where) { /* find a name, if any, for this value */ const char *name = enum_name(enum_test, i); switch (where) { case OPTIONAL: if (name == NULL) { return; } break; case PRESENT: if (name == NULL) { printf("name for %d missing (should be present)\n", i); return; } break; case ABSENT: if (name != NULL) { printf("name for %d present (should be absent)\n", i); } return; } printf(" %3d -> %s\n", i, name); /* * So that it is easy to see what was tested, print something * for every comparison. */ if (i < 0) { /* we are cheating: don't do the other checks */ return; } LSWBUF(buf) { printf(PREFIX "lswlog_enum %d: ", i); lswlog_enum(buf, enum_test, i); if (streq(name, buf->array)) { printf("OK\n"); } else { printf("ERROR\n"); } } { printf(PREFIX "search %s: ", name); int e = enum_search(enum_test, name); if (e != i) { printf("%d ERROR\n", e); } else { printf("OK\n"); } } { printf(PREFIX "match %s: ", name); int e = enum_match(enum_test, shunk1(name)); if (e != i) { printf("%d ERROR\n", e); } else { printf("OK\n"); } } if (strchr(name, '(') != NULL) { char *clone = clone_str(name, "trunc_name"); shunk_t trunc_name = shunk2(clone, strcspn(clone, "(")); passert(clone[trunc_name.len] == '('); clone[trunc_name.len] = '*'; printf(PREFIX "match "PRI_SHUNK" [trunc]: ", PRI_shunk(trunc_name)); int e = enum_match(enum_test, trunc_name); pfree(clone); if (e != i) { printf("%d ERROR\n", e); } else { printf("OK\n"); } } printf(PREFIX "short_name %d: ", i); const char *short_name = enum_short_name(enum_test, i); if (short_name == NULL) { printf("ERROR\n"); return; } else { printf(" OK\n"); } LSWBUF(buf) { printf(PREFIX "lswlog_enum_short %d: ", i); lswlog_enum_short(buf, enum_test, i); if (streq(short_name, buf->array)) { printf("OK\n"); } else { printf("ERROR\n"); } } if (streq(short_name, name)) { /* remaining tests redundant */ return; } { printf(PREFIX "match %s [short]: ", short_name); int e = enum_match(enum_test, shunk1(short_name)); if (e != i) { printf("%d ERROR\n", e); } else { printf("OK\n"); } } if (strchr(short_name, '(') != NULL) { char *trunc_short_name = clone_str(short_name, "trunc_short_name"); *strchr(trunc_short_name, '(') = '\0'; printf(PREFIX "match %s [short+trunc]: ", trunc_short_name); int e = enum_match(enum_test, shunk1(trunc_short_name)); pfree(trunc_short_name); if (e != i) { printf("%d ERROR\n", e); } else { printf("OK\n"); } } }