Ejemplo n.º 1
0
int
main ()
{
  int i;
  int sample_list[10] = {8, 10, 4, 6, 7, 9, 5, 3, 1, 2};

  node *r = NULL;

  for (i = 0; i < 5; i++) {
    printf ("node insert %d\n", sample_list[i]);
    r = root_insert_node (r, sample_list[i]);
  }

  printf ("\nshow tree:\n");
  show (r, 0);
  printf ("\nvalidated?: %s\n", validate_tree (r) ? "yes\n" : "no\n");

  for (i = 5; i < 10; i++) {
    printf ("node insert %d\n", sample_list[i]);
    r = root_insert_node (r, sample_list[i]);
  }

  printf ("\nshow tree:\n");
  show (r, 0);
  printf ("\nvalidated?: %s\n", validate_tree (r) ? "yes\n" : "no\n");

  return 0;
}
Ejemplo n.º 2
0
/*
 * This function adds words in different orders and amounts to the AVL-tree. 
 * After adding, it validates the AVL-tree. Function prints the results to stdout. 
 */
void run_test() {

    node* root;
    FILE* out;
    root = NULL;
    int i;
    char* test_strings[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"};

    if ((out = fopen("tree_test_result.txt", "w")) == NULL) {
        fprintf(stderr, "Cannot write to output file tree_test_result\n");
        exit(1);
    }
  
    /*Add words in increasing order*/
    printf("\nAdd words in increasing order:\n");
    for (i = 0; i < 11; i++) {
        root = add_word(root, test_strings[i]);
    }
    printf("Run validation\n");
    validate_tree(root);
    printf("Test passed\n");
    print_words(root, out);
    destroy(root);

    /*Add words in decreasing order*/
    root = NULL;
    printf("\nAdd words in decreasing order:\n");
    for (i = 10; i >= 0; i--) {
        root = add_word(root, test_strings[i]);
    }
    printf("Run validation\n");
    validate_tree(root);
    printf("Test passed\n");
    print_words(root, out);
    destroy(root);

    /*Add only one word*/
    root = NULL;
    printf("\nAdd single word:\n");
    root = add_word(root, test_strings[0]);
    printf("Run validation\n");
    validate_tree(root);
    printf("Test passed\n");
    print_words(root, out);
    destroy(root);
    fclose(out);
    
    exit(0);


}
Ejemplo n.º 3
0
void nghttp2_stream_dep_remove_subtree(nghttp2_stream *stream) {
  nghttp2_stream *next, *dep_prev;

  DEBUGF("stream: dep_remove_subtree stream(%p)=%d\n", stream,
         stream->stream_id);

  assert(stream->dep_prev);

  dep_prev = stream->dep_prev;

  if (stream->sib_prev) {
    link_sib(stream->sib_prev, stream->sib_next);
  } else {
    next = stream->sib_next;

    link_dep(dep_prev, next);

    if (next) {
      next->sib_prev = NULL;
    }
  }

  dep_prev->sum_dep_weight -= stream->weight;

  if (stream->queued) {
    stream_obq_remove(stream);
  }

  validate_tree(dep_prev);

  stream->sib_prev = NULL;
  stream->sib_next = NULL;
  stream->dep_prev = NULL;
}
Ejemplo n.º 4
0
int nghttp2_stream_dep_add_subtree(nghttp2_stream *dep_stream,
                                   nghttp2_stream *stream) {
  int rv;

  DEBUGF("stream: dep_add_subtree dep_stream(%p)=%d stream(%p)=%d\n",
         dep_stream, dep_stream->stream_id, stream, stream->stream_id);

  dep_stream->sum_dep_weight += stream->weight;

  if (dep_stream->dep_next) {
    insert_link_dep(dep_stream, stream);
  } else {
    link_dep(dep_stream, stream);
  }

  if (stream_subtree_active(stream)) {
    rv = stream_obq_push(dep_stream, stream);
    if (rv != 0) {
      return rv;
    }
  }

  validate_tree(dep_stream);

  return 0;
}
Ejemplo n.º 5
0
/* validate if tree follows order rules */
bool
validate_tree (struct node * leaf)
{
  if (leaf) {
    if (leaf->left && leaf->key < leaf->left->key)
        return false;
    if (leaf->right && leaf->key > leaf->right->key)
        return false;

    if (!validate_tree (leaf->left))
      return false;
    if (!validate_tree (leaf->right))
      return false;
  }

  return true;
}
Ejemplo n.º 6
0
/*
 * This function validates the AVL-tree by checking if the height difference between 
 * child-nodes is adequate and the word-order in the tree is correct.
 * Parameter: node* n
 */
void validate_tree(node *n) {

    if (n != NULL) {
         
        if (n->right != NULL && n->left != NULL) {
            if (abs(get_height(n->right) - get_height(n->left)) >= 2) {
                printf("Height difference too big");
                exit(EXIT_FAILURE);
            }
            if (strcmp(n->word, n->left->word) < 0) {
                printf("inappropriate word-order, left child");
                exit(EXIT_FAILURE);
            }

            if (strcmp(n->word, n->right->word) > 0) {
                printf("inappropriate word-order, right child");
                exit(EXIT_FAILURE);
            }
            validate_tree(n->left);
            validate_tree(n->right);
        } else if (n->right == NULL && n->left != NULL) {
            if (abs(get_height(n->right) - get_height(n->left)) >= 2) {
                printf("Height difference too big");
                exit(EXIT_FAILURE);
            }
            if (strcmp(n->word, n->left->word) < 0) {
                printf("inappropriate word-order, left child");
                exit(EXIT_FAILURE);
            }
            validate_tree(n->left);


        } else if (n->right != NULL && n->left == NULL) {
            if (abs(get_height(n->right) - get_height(n->left)) >= 2) {
                printf("Height difference too big");
                exit(EXIT_FAILURE);
            }
            if (strcmp(n->word, n->right->word) > 0) {
                printf("inappropriate word-order, right child");
                exit(EXIT_FAILURE);
            }
            validate_tree(n->right);
        }
    }
}
Ejemplo n.º 7
0
static int stream_update_dep_on_detach_item(nghttp2_stream *stream) {
  if (nghttp2_pq_empty(&stream->obq)) {
    stream_obq_remove(stream);
  }

  validate_tree(stream);

  return 0;
}
Ejemplo n.º 8
0
static int stream_update_dep_on_attach_item(nghttp2_stream *stream) {
  int rv;

  rv = stream_obq_push(stream->dep_prev, stream);
  if (rv != 0) {
    return rv;
  }

  validate_tree(stream);
  return 0;
}
Ejemplo n.º 9
0
TEST(symlinks_are_loaded_as_files, IF(not_windows))
{
	/* symlink() is not available on Windows, but other code is fine. */
#ifndef _WIN32
	assert_success(symlink(TEST_DATA_PATH, SANDBOX_PATH "/link"));
#endif

	assert_success(flist_load_tree(&lwin, SANDBOX_PATH));
	assert_int_equal(1, lwin.list_rows);
	assert_int_equal(0, lwin.filtered);
	validate_tree(&lwin);

	(void)filter_set(&lwin.local_filter.filter, "a");

	assert_success(flist_load_tree(&lwin, SANDBOX_PATH));
	assert_int_equal(1, lwin.list_rows);
	assert_int_equal(1, lwin.filtered);
	validate_tree(&lwin);

	assert_success(remove(SANDBOX_PATH "/link"));
}
Ejemplo n.º 10
0
int
main ()
{
  node *a = NULL;
  node *b = NULL;

  insert_node (&a, 8);
  insert_node (&a, 10);
  insert_node (&a, 6);
  insert_node (&a, 4);
  insert_node (&a, 7);

  insert_node (&b, 5);
  insert_node (&b, 1);
  insert_node (&b, 9);
  insert_node (&b, 3);
  insert_node (&b, 2);

  printf ("show tree A:\n");
  show (a, 0);

  printf ("show tree B:\n");
  show (b, 0);

  printf ("validated A?: %s\n", validate_tree (a) ? "yes" : "no");
  printf ("validated B?: %s\n\n", validate_tree (b) ? "yes" : "no");

  join_trees (b, a);

  printf ("show joint tree:\n");
  show (a, 0);

  printf ("validated?: %s\n", validate_tree (a) ? "yes\n" : "no\n");

  destroy_tree (a);
  destroy_tree (b);

  return 0;
}
Ejemplo n.º 11
0
int nghttp2_stream_dep_remove(nghttp2_stream *stream) {
  nghttp2_stream *dep_prev, *si;
  int32_t sum_dep_weight_delta;
  int rv;

  DEBUGF("stream: dep_remove stream(%p)=%d\n", stream, stream->stream_id);

  /* Distribute weight of |stream| to direct descendants */
  sum_dep_weight_delta = -stream->weight;

  for (si = stream->dep_next; si; si = si->sib_next) {
    si->weight = nghttp2_stream_dep_distributed_weight(stream, si->weight);

    sum_dep_weight_delta += si->weight;

    if (si->queued) {
      rv = stream_obq_move(stream->dep_prev, stream, si);
      if (rv != 0) {
        return rv;
      }
    }
  }

  assert(stream->dep_prev);

  dep_prev = stream->dep_prev;

  dep_prev->sum_dep_weight += sum_dep_weight_delta;

  if (stream->queued) {
    stream_obq_remove(stream);
  }

  if (stream->sib_prev) {
    unlink_sib(stream);
  } else {
    unlink_dep(stream);
  }

  stream->sum_dep_weight = 0;

  stream->dep_prev = NULL;
  stream->dep_next = NULL;
  stream->sib_prev = NULL;
  stream->sib_next = NULL;

  validate_tree(dep_prev);

  return 0;
}
Ejemplo n.º 12
0
int nghttp2_stream_dep_insert_subtree(nghttp2_stream *dep_stream,
                                      nghttp2_stream *stream) {
  nghttp2_stream *last_sib;
  nghttp2_stream *dep_next;
  nghttp2_stream *si;
  int rv;

  DEBUGF("stream: dep_insert_subtree dep_stream(%p)=%d stream(%p)=%d\n",
         dep_stream, dep_stream->stream_id, stream, stream->stream_id);

  stream->sum_dep_weight += dep_stream->sum_dep_weight;
  dep_stream->sum_dep_weight = stream->weight;

  if (dep_stream->dep_next) {
    dep_next = dep_stream->dep_next;

    link_dep(dep_stream, stream);

    if (stream->dep_next) {
      last_sib = stream_last_sib(stream->dep_next);

      link_sib(last_sib, dep_next);
    } else {
      link_dep(stream, dep_next);
    }

    for (si = dep_next; si; si = si->sib_next) {
      si->dep_prev = stream;
      if (si->queued) {
        rv = stream_obq_move(stream, dep_stream, si);
        if (rv != 0) {
          return rv;
        }
      }
    }
  } else {
    link_dep(dep_stream, stream);
  }

  if (stream_subtree_active(stream)) {
    rv = stream_obq_push(dep_stream, stream);
    if (rv != 0) {
      return rv;
    }
  }

  validate_tree(dep_stream);

  return 0;
}
Ejemplo n.º 13
0
void nghttp2_stream_dep_add(nghttp2_stream *dep_stream,
                            nghttp2_stream *stream) {
  DEBUGF("stream: dep_add dep_stream(%p)=%d, stream(%p)=%d\n", dep_stream,
         dep_stream->stream_id, stream, stream->stream_id);

  dep_stream->sum_dep_weight += stream->weight;

  if (dep_stream->dep_next == NULL) {
    link_dep(dep_stream, stream);
  } else {
    insert_link_dep(dep_stream, stream);
  }

  validate_tree(stream);
}
Ejemplo n.º 14
0
void test_avl_tree_remove(void)
{
	AVLTree *tree;
	int i;
	int x, y, z;
	int value;
	int expected_entries;

	tree = create_tree();

	/* Try removing invalid entries */

	i = NUM_TEST_VALUES + 100;
	assert(avl_tree_remove(tree, &i) == 0);
	i = -1;
	assert(avl_tree_remove(tree, &i) == 0);

	/* Delete the nodes from the tree */

	expected_entries = NUM_TEST_VALUES;

	/* This looping arrangement causes nodes to be removed in a
	 * randomish fashion from all over the tree. */

	for (x=0; x<4; ++x) {
		for (y=0; y<4; ++y) {
			for (z=0; z<8; ++z) {
				value = z * 16 + (3 - y) * 4 + x;
				assert(avl_tree_remove(tree, &value) != 0);
				validate_tree(tree);
				expected_entries -= 1;
				assert(avl_tree_num_entries(tree)
				       == expected_entries);
			}
		}
	}

	/* All entries removed, should be empty now */

	assert(avl_tree_root_node(tree) == NULL);

	avl_tree_free(tree);
}
Ejemplo n.º 15
0
void test_avl_tree_insert_lookup(void)
{
	AVLTree *tree;
	AVLTreeNode *node;
	int i;
	int *value;

	/* Create a tree containing some values. Validate the
	 * tree is consistent at all stages. */

	tree = avl_tree_new((AVLTreeCompareFunc) int_compare);

	for (i=0; i<NUM_TEST_VALUES; ++i) {
		test_array[i] = i;
		avl_tree_insert(tree, &test_array[i], &test_array[i]);

		assert(avl_tree_num_entries(tree) == i + 1);
		validate_tree(tree);
	}

	assert(avl_tree_root_node(tree) != NULL);

	/* Check that all values can be read back again */

	for (i=0; i<NUM_TEST_VALUES; ++i) {
		node = avl_tree_lookup_node(tree, &i);
		assert(node != NULL);
		value = avl_tree_node_key(node);
		assert(*value == i);
		value = avl_tree_node_value(node);
		assert(*value == i);
	}

	/* Check that invalid nodes are not found */

	i = -1;
	assert(avl_tree_lookup_node(tree, &i) == NULL);
	i = NUM_TEST_VALUES + 100;
	assert(avl_tree_lookup_node(tree, &i) == NULL);

	avl_tree_free(tree);
}
Ejemplo n.º 16
0
int nghttp2_stream_dep_insert(nghttp2_stream *dep_stream,
                              nghttp2_stream *stream) {
  nghttp2_stream *si;
  int rv;

  DEBUGF("stream: dep_insert dep_stream(%p)=%d, stream(%p)=%d\n", dep_stream,
         dep_stream->stream_id, stream, stream->stream_id);

  stream->sum_dep_weight = dep_stream->sum_dep_weight;
  dep_stream->sum_dep_weight = stream->weight;

  if (dep_stream->dep_next) {
    for (si = dep_stream->dep_next; si; si = si->sib_next) {
      si->dep_prev = stream;
      if (si->queued) {
        rv = stream_obq_move(stream, dep_stream, si);
        if (rv != 0) {
          return rv;
        }
      }
    }

    if (stream_subtree_active(stream)) {
      rv = stream_obq_push(dep_stream, stream);
      if (rv != 0) {
        return rv;
      }
    }

    stream->dep_next = dep_stream->dep_next;
  }

  dep_stream->dep_next = stream;
  stream->dep_prev = dep_stream;

  validate_tree(stream);

  return 0;
}
Ejemplo n.º 17
0
void test_rb_tree_to_array (void) {
  RBTree* tree;
  int entries[] = { 89, 23, 42, 4, 16, 15, 8, 99, 50, 30 };
  int sorted[]  = { 4, 8, 15, 16, 23, 30, 42, 50, 89, 99 };
  int num_entries = sizeof (entries) / sizeof (int);
  int i;
  int** array;

  /* Add all entries to the tree */

  tree = rb_tree_new ( (RBTreeCompareFunc) int_compare);

  for (i = 0; i < num_entries; ++i) {
    rb_tree_insert (tree, &entries[i], NULL);
    }

  assert (rb_tree_num_entries (tree) == num_entries);

  /* Convert to an array and check the contents */

  array = (int**) rb_tree_to_array (tree);

  for (i = 0; i < num_entries; ++i) {
    assert (*array[i] == sorted[i]);
    }

  free (array);

  /* Test out of memory scenario */

  alloc_test_set_limit (0);

  array = (int**) rb_tree_to_array (tree);
  assert (array == NULL);
  validate_tree (tree);

  rb_tree_free (tree);
  }
Ejemplo n.º 18
0
void test_out_of_memory (void) {
  RBTree* tree;
  RBTreeNode* node;
  int i;

  /* Create a tree */

  tree = create_tree();

  /* Set a limit to stop any more entries from being added. */

  alloc_test_set_limit (0);

  /* Try to add some more nodes and verify that this fails. */

  for (i = 10000; i < 20000; ++i) {
    node = rb_tree_insert (tree, &i, &i);
    assert (node == NULL);
    validate_tree (tree);
    }

  rb_tree_free (tree);
  }
static void
validate_accessible (Accessible *accessible,
		     gboolean    has_parent,
		     gboolean    recurse_down)
{
	Accessible          *tmp;
	char                *name, *descr;
	AccessibleRole       role;
	AccessibleRelation **relations;
	char                *role_name;
	GString             *item_str = g_string_new ("");
	int                  i;

	name = Accessible_getName (accessible);
	g_assert (name != NULL);
	
	descr = Accessible_getDescription (accessible);
	g_assert (descr != NULL);

	role = Accessible_getRole (accessible);
	g_assert (role != SPI_ROLE_INVALID);
	role_name = Accessible_getRoleName (accessible);
	g_assert (role_name != NULL);

	relations = Accessible_getRelationSet (accessible);
	g_assert (relations != NULL);

	for (i = 0; relations [i]; i++) {
		AccessibleRelationType type;
		int                    targets;

		fprintf (stderr, "relation %d\n", i);

		type = AccessibleRelation_getRelationType (relations [i]);
		g_assert (type != SPI_RELATION_NULL);

		targets = AccessibleRelation_getNTargets (relations [i]);
		g_assert (targets != -1);

		AccessibleRelation_unref (relations [i]);
		relations [i] = NULL;
	}
	free (relations);

	if (print_tree) {
		int i;

		for (i = 0; i < print_tree_depth; i++)
			fputc (' ', stderr);
		fputs ("|-> [ ", stderr);
	}

	if (Accessible_isAction (accessible)) {
		tmp = Accessible_getAction (accessible);
		g_assert (tmp != NULL);
		if (print_tree)
			fprintf (stderr, "At");
		else
			test_action (tmp);
		AccessibleAction_unref (tmp);
	}

	if (Accessible_isApplication (accessible)) {
		tmp = Accessible_getApplication (accessible);
		if (print_tree)
			fprintf (stderr, "Ap");
		else
			test_application (tmp);
		AccessibleApplication_unref (tmp);
	}

	if (Accessible_isComponent (accessible)) {
		tmp = Accessible_getComponent (accessible);
		g_assert (tmp != NULL);
		if (print_tree)
			fprintf (stderr, "Co");
		else
			test_component (tmp);
		AccessibleComponent_unref (tmp);
	}

	if (Accessible_isEditableText (accessible)) {
		tmp = Accessible_getEditableText (accessible);
		g_assert (tmp != NULL);
		if (print_tree)
			fprintf (stderr, "Et");
		else
			test_editable_text (tmp);
		AccessibleEditableText_unref (tmp);
	}

	if (Accessible_isHypertext (accessible)) {
		tmp = Accessible_getHypertext (accessible);
		g_assert (tmp != NULL);
		if (print_tree)
			fprintf (stderr, "Ht");
		AccessibleHypertext_unref (tmp);
	}

	if (Accessible_isImage (accessible)) {
		tmp = Accessible_getImage (accessible);
		g_assert (tmp != NULL);
		if (print_tree) {
			char *desc;

			fprintf (stderr, "Im");

			desc = AccessibleImage_getImageDescription (tmp);
			g_string_append_printf (
				item_str, " image descr: '%s'", desc);
			SPI_freeString (desc);
		} else
			test_image (tmp);

		AccessibleImage_unref (tmp);
	}

	if (Accessible_isSelection (accessible)) {
		tmp = Accessible_getSelection (accessible);
		g_assert (tmp != NULL);
		if (print_tree)
			fprintf (stderr, "Se");
		AccessibleSelection_unref (tmp);
	}

	if (Accessible_isTable (accessible)) {
		tmp = Accessible_getTable (accessible);
		g_assert (tmp != NULL);
		if (print_tree)
			fprintf (stderr, "Ta");
		else
			test_table (tmp);
		AccessibleTable_unref (tmp);
	}

	if (Accessible_isText (accessible)) {
		tmp = Accessible_getText (accessible);
		g_assert (tmp != NULL);
		if (print_tree)
			fprintf (stderr, "Te");
		else {
			if (strcmp (name, TEST_STRING_A_OBJECT) == 0)	
				test_text (tmp);
		}
		AccessibleText_unref (tmp);
	}

	if (Accessible_isValue (accessible)) {
		tmp = Accessible_getValue (accessible);
		g_assert (tmp != NULL);
		if (print_tree)
			fprintf (stderr, "Va");
		else
			test_value (tmp); 
		AccessibleValue_unref (tmp);
	}

	if (print_tree)
		fprintf (stderr, " ] '%s' (%s) - %s: %s\n",
			 name, descr, role_name, item_str->str);

	SPI_freeString (name);
	SPI_freeString (descr);
	SPI_freeString (role_name);
	g_string_free (item_str, TRUE);

	validate_tree (accessible, has_parent, recurse_down);
}