<commit_before>#include <errno.h>
#include <time.h>
#include "support.h"

#include "std/c_alloc.h"
#include "std/c_rbtree.h"

typedef struct test_s {
  int key;
  int number;
} test_t;

static c_rbtree_t *tree = NULL;

static int data_cmp(const void *key, const void *data) {
  test_t *a, *b;

  a = (test_t *) key;
  b = (test_t *) data;

  if (a->key < b->key) {
    return -1;
  } else if (a->key > b->key) {
    return 1;
  }

  return 0;
}

static int key_cmp(const void *key, const void *data) {
  int a;
  test_t *b;

  a = POINTER_TO_INT(key);
  b = (test_t *) data;

  if (a < b->key) {
    return -1;
  } else if (a > b->key) {
    return 1;
  }

  return 0;
}

static int visitor(void *obj, void *data) {
  test_t *a = NULL;
  test_t *b = NULL;

  a = (test_t *) obj;
  b = (test_t *) data;

  if (a->key == b->key) {
    a->number = 42;
  }

  return 0;
}

static void destructor(void *data) {
  test_t *freedata = NULL;

  freedata = (test_t *) data;
  SAFE_FREE(freedata);
}

static void setup(void) {
  fail_if(c_rbtree_create(&tree, key_cmp, data_cmp) < 0, "Setup failed");
}

static void setup_complete_tree(void) {
  int i = 0;

  fail_if(c_rbtree_create(&tree, key_cmp, data_cmp) < 0, "Setup failed");
  for (i = 0; i < 100; i++) {
    test_t *testdata = NULL;

    testdata = c_malloc(sizeof(test_t));
    fail_if(testdata == NULL, NULL);

    testdata->key = i;

    fail_if(c_rbtree_insert(tree, (void *) testdata) < 0, "Setup failed");
  }
}

static void teardown(void) {
  c_rbtree_destroy(tree, destructor);
  c_rbtree_free(tree);
  tree = NULL;
  unsetenv("CSYNC_NOMEMORY");
}

START_TEST (check_c_rbtree_create_free)
{
  fail_unless(c_rbtree_create(&tree, key_cmp, data_cmp) == 0, NULL);
  fail_unless(tree->size == 0, NULL);

  fail_unless(c_rbtree_free(tree) == 0, NULL);
  tree = NULL;
}
END_TEST

START_TEST (check_c_rbtree_create_null)
{
  fail_unless(c_rbtree_create(NULL, key_cmp, data_cmp) < 0, NULL);
  fail_unless(c_rbtree_create(&tree, NULL, data_cmp) < 0, NULL);
  fail_unless(c_rbtree_create(&tree, key_cmp, NULL) < 0, NULL);
}
END_TEST

#ifdef CSYNC_MEM_NULL_TESTS
START_TEST (check_c_rbtree_create_nomem)
{
  setenv("CSYNC_NOMEMORY", "1", 1);
  fail_unless(c_rbtree_create(&tree, key_cmp, data_cmp) < 0, NULL);
  unsetenv("CSYNC_NOMEMORY");
}
END_TEST
#endif

START_TEST (check_c_rbtree_free_null)
{
  fail_unless(c_rbtree_free(NULL) < 0, NULL);
}
END_TEST

START_TEST (check_c_rbtree_insert_delete)
{
  c_rbnode_t *node = NULL;
  test_t *testdata = NULL;

  fail_unless(c_rbtree_create(&tree, key_cmp, data_cmp) == 0, NULL);

  testdata = c_malloc(sizeof(test_t));
  testdata->key = 42;

  fail_unless(c_rbtree_insert(tree, (void *) testdata) == 0, NULL);

  node = c_rbtree_head(tree);
  fail_if(node == NULL, NULL);

  testdata = c_rbtree_node_data(node);
  SAFE_FREE(testdata);
  fail_unless(c_rbtree_node_delete(node) == 0, NULL);

  c_rbtree_free(tree);
  tree = NULL;
}
END_TEST

#ifdef CSYNC_MEM_NULL_TESTS
START_TEST (check_c_rbtree_insert_nomem)
{

  c_rbnode_t *node = NULL;
  test_t *testdata = NULL;

  fail_unless(c_rbtree_create(&tree, key_cmp, data_cmp) == 0, NULL);

  testdata = c_malloc(sizeof(test_t));
  testdata->key = 42;

  setenv("CSYNC_NOMEMORY", "1", 1);
  fail_unless(c_rbtree_insert(tree, (void *) testdata) < 0, NULL);
  fail_unless(errno == ENOMEM, NULL);
  unsetenv("CSYNC_NOMEMORY");

  testdata = c_rbtree_node_data(node);
  SAFE_FREE(testdata);

  c_rbtree_free(tree);
  tree = NULL;
}<commit_msg>Fix a memory leak in the rbtree testcase.<commit_after>#include <errno.h>
#include <time.h>
#include "support.h"

#include "std/c_alloc.h"
#include "std/c_rbtree.h"

typedef struct test_s {
  int key;
  int number;
} test_t;

static c_rbtree_t *tree = NULL;

static int data_cmp(const void *key, const void *data) {
  test_t *a, *b;

  a = (test_t *) key;
  b = (test_t *) data;

  if (a->key < b->key) {
    return -1;
  } else if (a->key > b->key) {
    return 1;
  }

  return 0;
}

static int key_cmp(const void *key, const void *data) {
  int a;
  test_t *b;

  a = POINTER_TO_INT(key);
  b = (test_t *) data;

  if (a < b->key) {
    return -1;
  } else if (a > b->key) {
    return 1;
  }

  return 0;
}

static int visitor(void *obj, void *data) {
  test_t *a = NULL;
  test_t *b = NULL;

  a = (test_t *) obj;
  b = (test_t *) data;

  if (a->key == b->key) {
    a->number = 42;
  }

  return 0;
}

static void destructor(void *data) {
  test_t *freedata = NULL;

  freedata = (test_t *) data;
  SAFE_FREE(freedata);
}

static void setup(void) {
  fail_if(c_rbtree_create(&tree, key_cmp, data_cmp) < 0, "Setup failed");
}

static void setup_complete_tree(void) {
  int i = 0;

  fail_if(c_rbtree_create(&tree, key_cmp, data_cmp) < 0, "Setup failed");
  for (i = 0; i < 100; i++) {
    test_t *testdata = NULL;

    testdata = c_malloc(sizeof(test_t));
    fail_if(testdata == NULL, NULL);

    testdata->key = i;

    fail_if(c_rbtree_insert(tree, (void *) testdata) < 0, "Setup failed");
  }
}

static void teardown(void) {
  c_rbtree_destroy(tree, destructor);
  c_rbtree_free(tree);
  tree = NULL;
  unsetenv("CSYNC_NOMEMORY");
}

START_TEST (check_c_rbtree_create_free)
{
  fail_unless(c_rbtree_create(&tree, key_cmp, data_cmp) == 0, NULL);
  fail_unless(tree->size == 0, NULL);

  fail_unless(c_rbtree_free(tree) == 0, NULL);
  tree = NULL;
}
END_TEST

START_TEST (check_c_rbtree_create_null)
{
  fail_unless(c_rbtree_create(NULL, key_cmp, data_cmp) < 0, NULL);
  fail_unless(c_rbtree_create(&tree, NULL, data_cmp) < 0, NULL);
  fail_unless(c_rbtree_create(&tree, key_cmp, NULL) < 0, NULL);
}
END_TEST

#ifdef CSYNC_MEM_NULL_TESTS
START_TEST (check_c_rbtree_create_nomem)
{
  setenv("CSYNC_NOMEMORY", "1", 1);
  fail_unless(c_rbtree_create(&tree, key_cmp, data_cmp) < 0, NULL);
  unsetenv("CSYNC_NOMEMORY");
}
END_TEST
#endif

START_TEST (check_c_rbtree_free_null)
{
  fail_unless(c_rbtree_free(NULL) < 0, NULL);
}
END_TEST

START_TEST (check_c_rbtree_insert_delete)
{
  c_rbnode_t *node = NULL;
  test_t *testdata = NULL;

  fail_unless(c_rbtree_create(&tree, key_cmp, data_cmp) == 0, NULL);

  testdata = c_malloc(sizeof(test_t));
  testdata->key = 42;

  fail_unless(c_rbtree_insert(tree, (void *) testdata) == 0, NULL);

  node = c_rbtree_head(tree);
  fail_if(node == NULL, NULL);

  testdata = c_rbtree_node_data(node);
  SAFE_FREE(testdata);
  fail_unless(c_rbtree_node_delete(node) == 0, NULL);

  c_rbtree_free(tree);
  tree = NULL;
}
END_TEST

#ifdef CSYNC_MEM_NULL_TESTS
START_TEST (check_c_rbtree_insert_nomem)
{

  c_rbnode_t *node = NULL;
  test_t *testdata = NULL;

  fail_unless(c_rbtree_create(&tree, key_cmp, data_cmp) == 0, NULL);

  testdata = c_malloc(sizeof(test_t));
  testdata->key = 42;

  setenv("CSYNC_NOMEMORY", "1", 1);
  fail_unless(c_rbtree_insert(tree, (void *) testdata) < 0, NULL);
  fail_unless(errno == ENOMEM, NULL);
  unsetenv("CSYNC_NOMEMORY");

  SAFE_FREE(testdata);

  c_rbtree_free(tree);
  tree = NULL;
}
{'fraction_non_alphanumeric': 0.14833501513622604, 'fraction_numerical': 0.010955744558166355, 'mean_word_length': 4.280060882800609, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4537615', 'n_tokens_mistral': 3132, 'n_tokens_neox': 2986, 'n_words': 603}