<commit_before>#include "test.h"
#include <simple-tiles/list.h>


int frees = 0;

void
freed(){
  frees++;
}

typedef struct {
  int val;
} wrap_t;


wrap_t*
wrap_new(int val){
  wrap_t* wrap;
  if(!(wrap = malloc(sizeof(*wrap))))
    return NULL;
  wrap->val = val;
  return wrap;
}

static simplet_list_t*
build_list(){
  simplet_list_t *list;
  if(!(list = simplet_list_new()))
    assert(0);
  
  wrap_t *test = wrap_new(5);
  wrap_t *test2 = wrap_new(6);
  wrap_t *test3 = wrap_new(7);

  simplet_list_push(list, test);
  simplet_list_push(list, test2);
  simplet_list_push(list, test3);
  return list;
}


static void
test_push(){
  simplet_list_t *list;
  if(!(list = simplet_list_new()))
    assert(0);
  assert(list->length == 0);
  wrap_t *test = wrap_new(5);
  simplet_list_push(list, &test);
  assert(list->head->value == &test);
  assert(list->tail->value == &test);
  assert(list->length == 1);
  simplet_list_free(list);
}

static void
test_pop(){
  simplet_list_t *list = build_list();
  wrap_t *ret;
  ret = simplet_list_pop(list);
  assert(ret->val == 7);
  ret = simplet_list_pop(list);
  assert(ret->val == 6);
  ret = simplet_list_pop(list);
  assert(ret->val == 5);
  simplet_list_free(list);
}

static void
test_destroy(){
  simplet_list_t *list = build_list();

  list->free = freed;

  simplet_list_free(list);
  assert(frees == 3);
}

static void
test_iter(){
  simplet_list_t *list = build_list();
  simplet_listiter_t *iter = simplet_get_list_iter(list);
  wrap_t *ret;
  assert((ret = simplet_list_next(iter))->val == 5);
  assert((ret = simplet_list_next(iter))->val == 6);
  assert((ret = simplet_list_next(iter))->val == 7);
  assert(simplet_list_next(iter) == NULL);

  simplet_list_free(list);
}

TASK(list) {
  test(push);
  test(pop);
  test(destroy);
  test(iter);
}<commit_msg>fixe mem leaks in list tests<commit_after>#include "test.h"
#include <simple-tiles/list.h>


int frees = 0;

void
freed(wrap_t *value){
  frees++;
  free_wrap(value);
}

void
free_wrap(wrap_t *value){
  free(value);
}

typedef struct {
  int val;
} wrap_t;


wrap_t*
wrap_new(int val){
  wrap_t* wrap;
  if(!(wrap = malloc(sizeof(*wrap))))
    return NULL;
  wrap->val = val;
  return wrap;
}

static simplet_list_t*
build_list(){
  simplet_list_t *list;
  if(!(list = simplet_list_new()))
    assert(0);
  
  wrap_t *test = wrap_new(5);
  wrap_t *test2 = wrap_new(6);
  wrap_t *test3 = wrap_new(7);

  simplet_list_push(list, test);
  simplet_list_push(list, test2);
  simplet_list_push(list, test3);
  return list;
}


static void
test_push(){
  simplet_list_t *list;
  if(!(list = simplet_list_new()))
    assert(0);
  assert(list->length == 0);
  wrap_t *test = wrap_new(5);
  simplet_list_push(list, &test);
  assert(list->head->value == &test);
  assert(list->tail->value == &test);
  assert(list->length == 1);
  list->free = free_wrap;
  simplet_list_free(list);
}

static void
test_pop(){
  simplet_list_t *list = build_list();
  wrap_t *ret;
  ret = simplet_list_pop(list);
  assert(ret->val == 7);
  ret = simplet_list_pop(list);
  assert(ret->val == 6);
  ret = simplet_list_pop(list);
  assert(ret->val == 5);
  list->free = free_wrap;
  simplet_list_free(list);
}

static void
test_destroy(){
  simplet_list_t *list = build_list();
  list->free = freed;
  simplet_list_free(list);
  assert(frees == 3);
}

static void
test_iter(){
  simplet_list_t *list = build_list();
  simplet_listiter_t *iter = simplet_get_list_iter(list);
  wrap_t *ret;
  assert((ret = simplet_list_next(iter))->val == 5);
  assert((ret = simplet_list_next(iter))->val == 6);
  assert((ret = simplet_list_next(iter))->val == 7);
  assert(simplet_list_next(iter) == NULL);
  list->free = free_wrap;
  simplet_list_free(list);
}

TASK(list) {
  test(push);
  test(pop);
  test(destroy);
  test(iter);
}
{'fraction_non_alphanumeric': 0.16728037874802734, 'fraction_numerical': 0.010520778537611783, 'mean_word_length': 4.568081991215227, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 36, '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': '183276', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1539, 'n_words': 287}