Example #1
0
int test_prepend_five()
{
	char *test_name = "test_prepend_five";
	struct llist *list_p;
	struct list_elem *elem;

	list_p = create_llist();
	prepend_element(list_p, "one");
	prepend_element(list_p, "two");
	prepend_element(list_p, "three");
	prepend_element(list_p, "four");
	prepend_element(list_p, "five");
	/* note that adding to head reverses list */
	elem = list_p->head;
	if (strcmp(elem->data, "five") != 0) {
		printf ("%s: expected 'five', got %s.\n", test_name,
				(char *) elem->data);
		return 1;
	}
	elem = elem->next;
	if (strcmp(elem->data, "four") != 0) {
		printf ("%s: expected 'five', got %s.\n", test_name,
				(char *) elem->data);
		return 1;
	}
	elem = elem->next;
	if (strcmp(elem->data, "three") != 0) {
		printf ("%s: expected 'five', got %s.\n", test_name,
				(char *) elem->data);
		return 1;
	}
	elem = elem->next;
	if (strcmp(elem->data, "two") != 0) {
		printf ("%s: expected 'five', got %s.\n", test_name,
				(char *) elem->data);
		return 1;
	}
	elem = elem->next;
	if (strcmp(elem->data, "one") != 0) {
		printf ("%s: expected 'five', got %s.\n", test_name,
				(char *) elem->data);
		return 1;
	}
	if(list_p->count != 5) {
		printf ("%s: count should be 5.\n", test_name);
		return 1;
	}
	printf("%s ok.\n", test_name);
	return 0;
}
Example #2
0
int test_add_struct()
{
	const int num_edges = 10;
	struct llist *list_p;
	int i;
	struct test_data *datap;
	struct list_elem *elem_p;

	list_p = create_llist();

	/* populate list */
	for (i = 0; i < num_edges; i++) {
		if (NULL == (datap = malloc(sizeof(struct test_data)))) {
			perror(NULL);
			return 1;
		}
		datap->height = (double) 2.5 * i;
		datap->length = (double) i;
		snprintf(datap->name, 10, "elem %d", i);
		prepend_element(list_p, (void *) datap);
	}
	
	/* check elements */
	for (elem_p=list_p->head, i=9; NULL != elem_p; elem_p = elem_p->next, i--) {
		datap = (struct test_data *) elem_p->data;
		if (datap->length != (double) i) {
			printf ("Expected edge length %.2f, got %.2f.",
					(double) i, datap->length);
			return 1;
		}
	}
	printf("test_add_struct ok.\n");
	return 0;
}
Example #3
0
int test_prepend_element()
{
	char *test_name = "test_prepend_element";
	struct llist *list_p;
	char *data = "blah";
	list_p = create_llist();
	prepend_element(list_p, data);
	if (NULL == list_p->head) {
		printf ("%s: head should not be NULL.\n", test_name);
		return 1;
	}
	if (list_p->head != list_p->tail) {
		printf("%s: tail should be the same as head\n", test_name);
		return 1;
	}
	if (strcmp(list_p->head->data, data) != 0) {
		printf ("%s: data should be '%s'.\n", test_name, data);
		return 1;
	}
	if(list_p->count != 1) {
		printf ("%s: count should be 1.\n", test_name);
		return 1;
	}
	printf ("%s ok.\n", test_name);
	return 0;
}
Example #4
0
int test_clear()
{
	const char *test_name = "test_clear";
	const int num_elements = 100000;
	struct llist *list_p;
	int i;

	list_p = create_llist();

	for (i = 0; i < num_elements; i++)
		prepend_element(list_p, &i);
	if (list_p->count != num_elements) {
		printf("count wrong: %d, expected %d.\n", list_p->count, num_elements);
		return 1;
	}
	clear_llist(list_p);
	if (0 != list_p->count ) {
		printf ("%s: count wrong: expected 0, got %d\n", 
				test_name, list_p->count);
		return 1;
	}
	if (NULL != list_p->head) {
		printf ("%s: expected NULL head.\n", test_name);
		return 1;
	}
	if (NULL != list_p->tail) {
		printf ("%s: expected NULL tail.\n", test_name);
		return 1;
	}

	printf("%s ok.\n", test_name);
	return 0;
}	
Example #5
0
int test_destroy()
{
	char *test_name = "test_destroy";
	struct llist *list_p;

	list_p = create_llist();
	prepend_element(list_p, "one");
	prepend_element(list_p, "two");
	prepend_element(list_p, "three");
	prepend_element(list_p, "four");
	prepend_element(list_p, "five");

	destroy_llist(list_p);

	printf("%s ok.\n", test_name);
	return 0;
}
Example #6
0
struct llist *llist_reverse(struct llist *list)
{
	struct llist *result;
	struct list_elem *elem;

	result = create_llist();
	if (NULL == result) return NULL;

	for (elem = list->head; NULL != elem; elem = elem->next) 
		if (! prepend_element(result, elem->data))
			return NULL;

	return result;
}
Example #7
0
int test_add_many()
{
	const int num_elements = 100000;
	struct llist *list_p;
	int i;

	list_p = create_llist();

	for (i = 0; i < num_elements; i++)
		prepend_element(list_p, &i);
	if (list_p->count != num_elements) {
		printf("count wrong: %d, expected %d.\n", list_p->count, num_elements);
		return 1;
	}
	printf("test_add_many ok.\n");
	return 0;
}	
Example #8
0
void *reduce(struct llist *list, void* (*func)(void *, void*))
{
	void *func_result;
	void *top, *second;

	while (list->count > 1) {
		assert(NULL != list->head);
		top = shift(list);
		assert(NULL != list->head);
		second = shift(list);
		func_result = func(top, second);
		if (NULL == func_result) return NULL;
		if (! prepend_element(list, func_result)) return NULL;
	}

	return shift(list);
}
Example #9
0
struct chk_context *
chk_context_open(struct chk_conditions *cond)
{
	assert(cond != NULL);

	struct chk_context *ctx = malloc(sizeof(*ctx));
	if (ctx == NULL)
		err(EXIT_FAILURE, "malloc chk_context");
	if ((ctx->cd = iconv_open("", "UTF-8")) == (iconv_t) (-1))
		err(EXIT_FAILURE, "iconv_open");
	ctx->cond = cond;
	ctx->regexlist = NULL;

	for (struct element *e = cond->regexlist; e != NULL; e = e->next) {
		struct regex *re = malloc(sizeof(*re));
		if (re == NULL)
			err(EXIT_FAILURE, "malloc regex");

		struct chk_expression *cx = e->payload;
		re->invert = cx->invert;
		re->pattern = cx->expression;
		if ((re->regex = malloc(sizeof(*(re->regex))))== NULL)
			err(EXIT_FAILURE, "malloc regex_t");

		int flags = REG_EXTENDED;
		if (!cond->noignorecase)
			flags |= REG_ICASE;

		int errcode = regcomp(re->regex, re->pattern, flags);
		if (errcode != 0) {
			char errstr[128];
			regerror(errcode, re->regex, errstr, sizeof(errstr));
			errx(EXIT_FAILURE, "regcomp \"%s\": %s", re->pattern, errstr);
		}

		struct element *ne = create_element(re);
		if (ne == NULL)
			err(EXIT_FAILURE, "create_element regex");
		ctx->regexlist = prepend_element(ne, ctx->regexlist);
	}

	return (ctx);
}