size_t lswlog_enum_lset_short(struct lswlog *buf, enum_names *en, const char *separator, lset_t val) { unsigned int e; /* if nothing gets filled in, default to "none" rather than "" */ if (val == LEMPTY) { return lswlogs(buf, "none"); } size_t size = 0; const char *sep = ""; for (e = 0; val != 0; e++) { lset_t bit = LELEM(e); if (val & bit) { size += lswlogs(buf, sep); sep = separator; size += lswlog_enum_short(buf, en, e); val -= bit; } } return size; }
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"); } } }