int test_red_black_tree_1(void)
{
	red_black_tree_t* tree_1 = init_red_black_tree(cmp_rb_node_value, 
								destruct_rb_node_value, 
								copy_rb_node_value);


	red_black_tree_t* tree_2 = init_red_black_tree(cmp_rb_node_value, 
								destruct_rb_node_value, 
								copy_rb_node_value);

	int i;
	int n;

	void* node = NULL;

	int* sorted_values = malloc(MAX_TREE_VALUES * sizeof(int));
	int* values = malloc(MAX_TREE_VALUES * sizeof(int));

	printf("Test red black tree ... \n");

	assert(tree_1);
	assert(tree_2);

	if (!tree_1 || !tree_2)
		return 1;

	if (!values)
		return 1;

	if (!sorted_values)
		return 1;

	node = rb_max_value(tree_1);

	assert(node == NULL);

	node = rb_min_value(tree_1);

	assert(node == NULL);

	srand ( SEED_VALUE );
	
	for (i = 0; i < MAX_TREE_VALUES; i++)
	{
		n = rand();
		values[i] = n;
		sorted_values[i] = n;

		rb_insert_value(tree_1, new_rb_node_value(n));
		rb_insert_value(tree_2, new_rb_node_value(n));
	}

	qsort(sorted_values, MAX_TREE_VALUES, sizeof(int), cmp_integer);

	n = get_rb_node_value_from_node(rb_max_value(tree_1));

	assert(n == sorted_values[MAX_TREE_VALUES - 1]);

	n = get_rb_node_value_from_node(rb_min_value(tree_1));

	assert(n == sorted_values[0]);

	for (i = 0; i < MAX_TREE_VALUES - 1; i++)
	{
		rb_delete_min_element(tree_1);

		n = get_rb_node_value_from_node(rb_min_value(tree_1));
		assert(sorted_values[i + 1] == n);

		n = get_rb_node_value_from_node(rb_max_value(tree_1));

		assert(n == sorted_values[MAX_TREE_VALUES - 1]);
	}

	for (i = 0; i < MAX_TREE_VALUES - 1; i++)
	{
		rb_delete_max_element(tree_2);
		n = get_rb_node_value_from_node(rb_max_value(tree_2));
		assert(sorted_values[MAX_TREE_VALUES - i - 2] == n);

		n = get_rb_node_value_from_node(rb_min_value(tree_2));

		assert(n == sorted_values[0]);
	}

	rb_delete_tree(tree_1);
	rb_delete_tree(tree_2);

	free(values);
	free(sorted_values);

	printf("Test red black tree [OK] \n");

	return 0;
}
Esempio n. 2
0
static void * find_right_min(MTrace * m){
    return rb_min_value(m->rb2);
}