static void test_list_select_last_matching_not_found(void **state) { Item *list = NULL, *match = NULL, *prev = NULL; bool result; AppendItem(&list, "abc", NULL); AppendItem(&list, "def", NULL); AppendItem(&list, "ghi", NULL); AppendItem(&list, "abc", NULL); result = SelectLastItemMatching("xyz", list, NULL, &match, &prev); assert_false(result); assert_int_equal(match, CF_UNDEFINED_ITEM); assert_int_equal(prev, CF_UNDEFINED_ITEM); DeleteItemList(list); }
static void test_list_select_last_matching_finds_last(void **state) { Item *list = NULL, *match = NULL, *prev = NULL; bool result; AppendItem(&list, "abc", NULL); AppendItem(&list, "def", NULL); AppendItem(&list, "ghi", NULL); AppendItem(&list, "abc", NULL); result = SelectLastItemMatching("abc", list, NULL, &match, &prev); assert_true(result); assert_int_equal(match, list->next->next->next); assert_int_equal(prev, list->next->next); DeleteItemList(list); }
static void test_list_select_last_matching_finds_first(void) { Item *list = NULL, *match = NULL, *prev = NULL; bool result = false; AppendItem(&list, "abc", NULL); AppendItem(&list, "def", NULL); AppendItem(&list, "ghi", NULL); AppendItem(&list, "jkl", NULL); result = SelectLastItemMatching("abc", list, NULL, &match, &prev); assert_true(result); assert_int_equal(match, list); assert_int_equal(prev, CF_UNDEFINED_ITEM); DeleteItemList(list); }
static void test_list_select_last_matching_not_found(void) { EvalContext *ctx = EvalContextNew(); Item *list = NULL, *match = NULL, *prev = NULL; bool result; AppendItem(&list, "abc", NULL); AppendItem(&list, "def", NULL); AppendItem(&list, "ghi", NULL); AppendItem(&list, "abc", NULL); result = SelectLastItemMatching(ctx, "xyz", list, NULL, &match, &prev); assert_false(result); assert_int_equal(match, CF_UNDEFINED_ITEM); assert_int_equal(prev, CF_UNDEFINED_ITEM); DeleteItemList(list); EvalContextDestroy(ctx); }
static void test_list_select_last_matching_finds_last(void) { EvalContext *ctx = EvalContextNew(); Item *list = NULL, *match = NULL, *prev = NULL; bool result; AppendItem(&list, "abc", NULL); AppendItem(&list, "def", NULL); AppendItem(&list, "ghi", NULL); AppendItem(&list, "abc", NULL); result = SelectLastItemMatching(ctx, "abc", list, NULL, &match, &prev); assert_true(result); assert_int_equal(match, list->next->next->next); assert_int_equal(prev, list->next->next); DeleteItemList(list); EvalContextDestroy(ctx); }
int SelectItemMatching(EvalContext *ctx, Item *start, char *regex, Item *begin_ptr, Item *end_ptr, Item **match, Item **prev, char *fl) { Item *ip; int ret = false; *match = CF_UNDEFINED_ITEM; *prev = CF_UNDEFINED_ITEM; if (regex == NULL) { return false; } if (fl && (strcmp(fl, "first") == 0)) { if (SelectNextItemMatching(ctx, regex, begin_ptr, end_ptr, match, prev)) { ret = true; } } else { if (SelectLastItemMatching(ctx, regex, begin_ptr, end_ptr, match, prev)) { ret = true; } } if ((*match != CF_UNDEFINED_ITEM) && (*prev == CF_UNDEFINED_ITEM)) { for (ip = start; (ip != NULL) && (ip != *match); ip = ip->next) { *prev = ip; } } return ret; }