<commit_before>#include "m-list.h"
#include "m-array.h"
#include "m-string.h"
#include "m-algo.h"

ARRAY_DEF(int, int)
LIST_DEF(int, int)
LIST_DEF(string, string_t, STRING_OPLIST)

#include "coverage.h"
START_COVERAGE
ALGO_DEF(algo_array, ARRAY_OPLIST(int))

ALGO_DEF(algo_list,  LIST_OPLIST(int))

ALGO_DEF(algo_string, LIST_OPLIST(string, STRING_OPLIST))
END_COVERAGE

static void test_list(void)
{
  list_int_t l;
  list_int_init(l);
  for(int i = 0; i < 100; i++)
    list_int_push_back (l, i);
  assert( algo_list_contains(l, 62) == true);
  assert( algo_list_contains(l, -1) == false);
  
  assert( algo_list_count(l, 1) == 1);
  list_int_push_back (l, 17);
  assert( algo_list_count(l, 17) == 2);
  assert( algo_list_count(l, -1) == 0);

#define f(x) assert((x) >= 0 && (x) < 100);
  ALGO_MAP(l, LIST_OPLIST(int), f);
#define g(y, x) assert((x) >= 0 && (x) < y);
  ALGO_MAP(l, LIST_OPLIST(int), g, 100);

  int *p = algo_list_min(l);
  assert(p != NULL && *p == 0);
  p = algo_list_max(l);
  assert(p != NULL && *p == 99);
  int *p2;
  algo_list_minmax(&p, &p2, l);
  assert(p != NULL && *p == 0);
  assert(p2 != NULL && *p2 == 99);
  
  list_int_push_back (l, 3);
  list_it_int_t it1, it2;
  algo_list_find (it1, l, 3);
  assert (!list_int_end_p (it1));
  algo_list_find_last (it2, l, 3);
  assert (!list_int_end_p (it2));
  assert (!list_int_it_equal_p(it1, it2));
  
  for(int i = -100; i < 100; i+=2)
    list_int_push_back (l, i);

  assert (!algo_list_sort_p(l));
  algo_list_sort(l);
  assert (algo_list_sort_p(l));
  
  list_int_clear(l);

  ALGO_INIT_VA(l, LIST_OPLIST(int), 1, 2, 3, 4, 5);
  assert (list_int_size(l) == 5);
  list_int_clear(l);
}

static void test_array(void)
{
  array_int_t l;
  array_int_init(l);
  for(int i = 0; i < 100; i++)
    array_int_push_back (l, i);
  assert( algo_array_contains(l, 62) == true);
  assert( algo_array_contains(l, -1) == false);

  assert( algo_array_count(l, 1) == 1);
  array_int_push_back (l, 17);
  assert( algo_array_count(l, 17) == 2);
  assert( algo_array_count(l, -1) == 0);

  array_int_clear(l);

  ALGO_INIT_VA(l, ARRAY_OPLIST(int), 1, 2, 3, 4, 5);
  assert (array_int_size(l) == 5);
  array_int_clear(l);
}

static void test_string(void)
{
  list_string_t l;
  list_string_init(l);
  string_t s;
  string_init_set_str(s, "Hello, World, John");
  algo_string_split(l, s, ',');
  list_string_pop_back (&s, l);
  assert (string_equal_str_p(s, "Hello"));
  list_string_pop_back (&s, l);
  assert (string_equal_str_p(s, " World"));
  list_string_pop_back (&s, l);
  assert (string_equal_str_p(s, " John"));
  assert (list_string_empty_p(l));

  list_string_push_back (l, STRING_CTE("John"));
  list_string_push_back (l, STRING_CTE("Who"));
  list_string_push_back (l, STRING_CTE("Is"));
  algo_string_join(s, l, STRING_CTE("-"));
  // NOTE: List is reverse... Is-it really ok?
  assert (string_equal_str_p(s, "Is-Who-John"));

  string_clear(s);
  
  list_string_clear(l);
}

static void test_extract(void)
{
  list_int_t l;
  list_int_init (l);
  for(int i = -100; i < 100; i++)
    list_int_push_back (l, i);
  array_int_t a;
  array_int_init(a);
#define cond(d) ((d) > 0)
  ALGO_EXTRACT(a, ARRAY_OPLIST(int), l, LIST_OPLIST(int), cond);
  assert(array_int_size(a) == 99);
#define cond2(c, d) ((d) > (c))
  ALGO_EXTRACT(a, ARRAY_OPLIST(int), l, LIST_OPLIST(int), cond2, 10);
  assert(array_int_size(a) == 89);

  int dst = 0;
#define inc(d, c) (d) += (c)
  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), inc);
  assert (dst == 100*99/2-10*11/2);
#define sqr(d, c) (d) = (c)*(c)
  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), inc, sqr);
  assert (dst == 327965);
#define sqr2(d, f, c) (d) = (f) * (c)
  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), inc, sqr2, 4);
  assert (dst == (100*99/2-10*11/2) *4 );

  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), sum);
  assert (dst == 100*99/2-10*11/2);
  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), add);
  assert (dst == 100*99/2-10*11/2);
  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), and);
  assert (dst == 0);
  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), or);
  assert (dst == 127);

  array_int_clear(a);
  list_int_clear(l);
}

int main(void)
{
  test_list();
  test_array();
  test_string();
  test_extract();
  exit(0);
}
<commit_msg>Improve coverage<commit_after>#include "m-list.h"
#include "m-array.h"
#include "m-string.h"
#include "m-algo.h"

ARRAY_DEF(int, int)
LIST_DEF(int, int)
LIST_DEF(string, string_t, STRING_OPLIST)

#include "coverage.h"
START_COVERAGE
ALGO_DEF(algo_array, ARRAY_OPLIST(int))

ALGO_DEF(algo_list,  LIST_OPLIST(int))

ALGO_DEF(algo_string, LIST_OPLIST(string, STRING_OPLIST))
END_COVERAGE

int g_min, g_max, g_count;
static void g_f(int n)
{
  assert (g_min <= n && n <= g_max);
  g_count++;
}

static void test_list(void)
{
  list_int_t l;
  list_int_init(l);
  for(int i = 0; i < 100; i++)
    list_int_push_back (l, i);
  assert( algo_list_contains(l, 62) == true);
  assert( algo_list_contains(l, -1) == false);
  
  assert( algo_list_count(l, 1) == 1);
  list_int_push_back (l, 17);
  assert( algo_list_count(l, 17) == 2);
  assert( algo_list_count(l, -1) == 0);

#define f(x) assert((x) >= 0 && (x) < 100);
  ALGO_MAP(l, LIST_OPLIST(int), f);
#define g(y, x) assert((x) >= 0 && (x) < y);
  ALGO_MAP(l, LIST_OPLIST(int), g, 100);

  int *p = algo_list_min(l);
  assert(p != NULL && *p == 0);
  p = algo_list_max(l);
  assert(p != NULL && *p == 99);
  int *p2;
  algo_list_minmax(&p, &p2, l);
  assert(p != NULL && *p == 0);
  assert(p2 != NULL && *p2 == 99);
  
  list_int_push_back (l, 3);
  list_it_int_t it1, it2;
  algo_list_find (it1, l, 3);
  assert (!list_int_end_p (it1));
  algo_list_find_last (it2, l, 3);
  assert (!list_int_end_p (it2));
  assert (!list_int_it_equal_p(it1, it2));
  
  for(int i = -100; i < 100; i+=2)
    list_int_push_back (l, i);

  assert (!algo_list_sort_p(l));
  algo_list_sort(l);
  assert (algo_list_sort_p(l));
  
  list_int_clear(l);

  ALGO_INIT_VA(l, LIST_OPLIST(int), 1, 2, 3, 4, 5);
  assert (list_int_size(l) == 5);
  list_int_clear(l);
}

static void test_array(void)
{
  array_int_t l;
  array_int_init(l);
  for(int i = 0; i < 100; i++)
    array_int_push_back (l, i);
  assert( algo_array_contains(l, 62) == true);
  assert( algo_array_contains(l, -1) == false);
  assert( algo_array_sort_p(l) == true);

  assert( algo_array_count(l, 1) == 1);
  array_int_push_back (l, 17);
  assert( algo_array_count(l, 17) == 2);
  assert( algo_array_count(l, -1) == 0);
  assert( algo_array_sort_p(l) == false);

  array_it_int_t it;
  algo_array_find_last(it, l, 17);
  assert (!array_int_end_p (it));
  assert (array_int_last_p (it));
  algo_array_find_last(it, l, 1742);
  assert (array_int_end_p (it));
  algo_array_find(it, l, 1742);
  assert (array_int_end_p (it));

#define f(x) assert((x) >= 0 && (x) < 100);
  ALGO_MAP(l, ARRAY_OPLIST(int), f);

  g_min = 0;
  g_max = 99;
  g_count = 0;
  algo_array_map(l, g_f);
  assert(g_count == 101);

  // FIXME: reduce functions don't have the right prototype to work with integers as the 1st argument is an int instead of a int*

  int *min, *max;
  assert (*algo_array_min(l) == 0);
  assert (*algo_array_max(l) == 99);
  algo_array_minmax(&min, &max, l);
  assert (*min == 0);
  assert (*max == 99);
  array_int_push_back (l, 1742);
  array_int_push_back (l, -17);
  assert (*algo_array_min(l) == -17);
  assert (*algo_array_max(l) == 1742);
  algo_array_minmax(&min, &max, l);
  assert (*min == -17);
  assert (*max == 1742);
  assert( algo_array_sort_p(l) == false);

  algo_array_sort(l);
  assert( algo_array_sort_p(l) == true);
  assert (array_int_size(l) == 103);
  algo_array_uniq(l);
  assert (array_int_size(l) == 102);
  assert( algo_array_sort_p(l) == true);

  array_int_clean(l);
  assert (algo_array_min(l) == NULL);
  assert (algo_array_max(l) == NULL);
  algo_array_minmax(&min, &max, l);
  assert (min == NULL);
  assert (max == NULL);
  assert (algo_array_sort_p(l) == true);
  algo_array_uniq(l);
  assert (array_int_size(l) == 0);
  assert( algo_array_sort_p(l) == true);

  array_int_clear(l);

  ALGO_INIT_VA(l, ARRAY_OPLIST(int), 1, 2, 3, 4, 5);
  assert (array_int_size(l) == 5);
  array_int_clear(l);
}

static void test_string(void)
{
  list_string_t l;
  list_string_init(l);
  string_t s;
  string_init_set_str(s, "Hello, World, John");
  algo_string_split(l, s, ',');
  list_string_pop_back (&s, l);
  assert (string_equal_str_p(s, "Hello"));
  list_string_pop_back (&s, l);
  assert (string_equal_str_p(s, " World"));
  list_string_pop_back (&s, l);
  assert (string_equal_str_p(s, " John"));
  assert (list_string_empty_p(l));

  list_string_push_back (l, STRING_CTE("John"));
  list_string_push_back (l, STRING_CTE("Who"));
  list_string_push_back (l, STRING_CTE("Is"));
  algo_string_join(s, l, STRING_CTE("-"));
  // NOTE: List is reverse... Is-it really ok?
  assert (string_equal_str_p(s, "Is-Who-John"));

  string_clear(s);
  
  list_string_clear(l);
}

static void test_extract(void)
{
  list_int_t l;
  list_int_init (l);
  for(int i = -100; i < 100; i++)
    list_int_push_back (l, i);
  array_int_t a;
  array_int_init(a);
#define cond(d) ((d) > 0)
  ALGO_EXTRACT(a, ARRAY_OPLIST(int), l, LIST_OPLIST(int), cond);
  assert(array_int_size(a) == 99);
#define cond2(c, d) ((d) > (c))
  ALGO_EXTRACT(a, ARRAY_OPLIST(int), l, LIST_OPLIST(int), cond2, 10);
  assert(array_int_size(a) == 89);

  int dst = 0;
#define inc(d, c) (d) += (c)
  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), inc);
  assert (dst == 100*99/2-10*11/2);
#define sqr(d, c) (d) = (c)*(c)
  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), inc, sqr);
  assert (dst == 327965);
#define sqr2(d, f, c) (d) = (f) * (c)
  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), inc, sqr2, 4);
  assert (dst == (100*99/2-10*11/2) *4 );

  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), sum);
  assert (dst == 100*99/2-10*11/2);
  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), add);
  assert (dst == 100*99/2-10*11/2);
  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), and);
  assert (dst == 0);
  ALGO_REDUCE(dst, a, ARRAY_OPLIST(int), or);
  assert (dst == 127);

  array_int_clear(a);
  list_int_clear(l);
}

int main(void)
{
  test_list();
  test_array();
  test_string();
  test_extract();
  exit(0);
}

{'fraction_non_alphanumeric': 0.1839669095922789, 'fraction_numerical': 0.03574945834154028, 'mean_word_length': 4.6073992269464386, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 12, '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': '3054626', 'n_tokens_mistral': 4873, 'n_tokens_neox': 4605, 'n_words': 952}