<commit_before>/*
 * Copyright (c) 2017, <NAME>
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * + Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * + Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "m-buffer.h"

// Define a fixed queue of unsigned int
BUFFER_DEF(uint, unsigned int, 10, BUFFER_QUEUE|BUFFER_BLOCKING)

// Define a variable stack of float
BUFFER_DEF(floats, float, 0, BUFFER_STACK|BUFFER_BLOCKING)

// Define a fixed stack of char
BUFFER_DEF(char, char, 10, BUFFER_STACK|BUFFER_UNBLOCKING)

// Define a fixed queue of long long
BUFFER_DEF(llong, long long, 16, BUFFER_QUEUE|BUFFER_THREAD_UNSAFE|BUFFER_UNBLOCKING)

#include <gmp.h>
BUFFER_DEF(mpz, mpz_t, 32, BUFFER_QUEUE, (INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear)))

buffer_uint_t g_buff;

static void conso(void *arg)
{
  unsigned int j;
  assert (arg == NULL);
  for(int i = 0; i < 1000;i++) {
    buffer_uint_pop(&j, g_buff);
    assert (j < 1000);
  }
}

static void prod(void *arg)
{
  assert (arg == NULL);
  for(unsigned int i = 0; i < 1000;i++)
    buffer_uint_push(g_buff, i);
}

static void test_global(void)
{
  m_thread_t idx_p[100];
  m_thread_t idx_c[100];

  buffer_uint_init(g_buff, 10);
  
  for(int i = 0; i < 100; i++) {
    m_thread_create (idx_p[i], conso, NULL);
    m_thread_create (idx_c[i], prod, NULL);
  }
  for(int i = 0; i < 100;i++) {
    m_thread_join(idx_p[i]);
    m_thread_join(idx_c[i]);
  }

  buffer_uint_clear(g_buff);
}

static void test_stack(void)
{
  buffer_floats_t buff;
  buffer_floats_init(buff, 100);
  for(unsigned int i = 0; i < 100; i++) {
    float f = i;
    buffer_floats_push(buff, f);
    assert (buffer_floats_empty_p(buff) == false);
  }
  assert (buffer_floats_full_p(buff) == true);
  for(unsigned int i = 0; i < 100; i++) {
    float j;
    buffer_floats_pop(&j, buff);
    assert(j == 99-i);
    assert (buffer_floats_full_p(buff) == false);
  }
  assert (buffer_floats_empty_p(buff) == true);
  buffer_floats_clear(buff);
}

static void test_stack2(void)
{
  buffer_char_t buff;
  bool b;
  char c;
  buffer_char_init(buff, 10);
  for(int i = 0; i < 10; i++) {
    c = i;
    b = buffer_char_push(buff, c);
    assert (b == true);
    assert (buffer_char_empty_p(buff) == false);
  }
  b = buffer_char_push(buff, c);
  assert (b == false);
  assert (buffer_char_full_p(buff) == true);
  for(int i = 0; i < 10; i++) {
    buffer_char_pop(&c, buff);
    assert(c == 9-i);
    assert (buffer_char_full_p(buff) == false);
  }
  b = buffer_char_pop(&c, buff);
  assert (b == false);
  assert (buffer_char_empty_p(buff) == true);
  buffer_char_clear(buff);
}

static void test_no_thread(void)
{
  buffer_llong_t buff;
  bool b;
  long long c;
  buffer_llong_init(buff, 16);
  for(int i = 0; i < 16; i++) {
    c = i;
    b = buffer_llong_push(buff, c);
    assert (b == true);
    assert (buffer_llong_empty_p(buff) == false);
  }
  b = buffer_llong_push(buff, c);
  assert (b == false);
  assert (buffer_llong_full_p(buff) == true);
  for(int i = 0; i < 16; i++) {
    buffer_llong_pop(&c, buff);
    assert(c == i);
    assert (buffer_llong_full_p(buff) == false);
  }
  b = buffer_llong_pop(&c, buff);
  assert (b == false);
  assert (buffer_llong_empty_p(buff) == true);
  buffer_llong_clear(buff);<commit_msg>Add new test case.<commit_after>/*
 * Copyright (c) 2017, <NAME>
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * + Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * + Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "m-buffer.h"

// Define a fixed queue of unsigned int
BUFFER_DEF(uint, unsigned int, 10, BUFFER_QUEUE|BUFFER_BLOCKING)

// Define a variable stack of float
BUFFER_DEF(floats, float, 0, BUFFER_STACK|BUFFER_BLOCKING)

// Define a fixed stack of char
BUFFER_DEF(char, char, 10, BUFFER_STACK|BUFFER_UNBLOCKING)

// Define a fixed queue of long long
BUFFER_DEF(llong, long long, 16, BUFFER_QUEUE|BUFFER_THREAD_UNSAFE|BUFFER_UNBLOCKING)

#include <gmp.h>
BUFFER_DEF(mpz, mpz_t, 32, BUFFER_QUEUE, (INIT(mpz_init), INIT_SET(mpz_init_set), SET(mpz_set), CLEAR(mpz_clear)))

buffer_uint_t g_buff;

static void conso(void *arg)
{
  unsigned int j;
  assert (arg == NULL);
  for(int i = 0; i < 1000;i++) {
    buffer_uint_pop(&j, g_buff);
    assert (j < 1000);
  }
}

static void prod(void *arg)
{
  assert (arg == NULL);
  for(unsigned int i = 0; i < 1000;i++)
    buffer_uint_push(g_buff, i);
}

static void test_global(void)
{
  m_thread_t idx_p[100];
  m_thread_t idx_c[100];

  buffer_uint_init(g_buff, 10);
  
  for(int i = 0; i < 100; i++) {
    m_thread_create (idx_p[i], conso, NULL);
    m_thread_create (idx_c[i], prod, NULL);
  }
  for(int i = 0; i < 100;i++) {
    m_thread_join(idx_p[i]);
    m_thread_join(idx_c[i]);
  }

  buffer_uint_clear(g_buff);
}

static void test_stack(void)
{
  buffer_floats_t buff;
  buffer_floats_init(buff, 100);
  for(unsigned int i = 0; i < 100; i++) {
    float f = i;
    buffer_floats_push(buff, f);
    assert (buffer_floats_empty_p(buff) == false);
  }
  assert (buffer_floats_full_p(buff) == true);
  for(unsigned int i = 0; i < 100; i++) {
    float j;
    buffer_floats_pop(&j, buff);
    assert(j == 99-i);
    assert (buffer_floats_full_p(buff) == false);
  }
  assert (buffer_floats_empty_p(buff) == true);
  buffer_floats_clear(buff);
}

static void test_stack2(void)
{
  buffer_char_t buff;
  bool b;
  char c;
  buffer_char_init(buff, 10);
  for(int i = 0; i < 10; i++) {
    c = i;
    b = buffer_char_push(buff, c);
    assert (b == true);
    assert (buffer_char_empty_p(buff) == false);
  }
  b = buffer_char_push(buff, c);
  assert (b == false);
  assert (buffer_char_full_p(buff) == true);
  for(int i = 0; i < 10; i++) {
    buffer_char_pop(&c, buff);
    assert(c == 9-i);
    assert (buffer_char_full_p(buff) == false);
  }
  b = buffer_char_pop(&c, buff);
  assert (b == false);
  assert (buffer_char_empty_p(buff) == true);
  buffer_char_clear(buff);
}

static void test_no_thread(void)
{
  buffer_llong_t buff;
  bool b;
  long long c;
  buffer_llong_init(buff, 16);
  for(int i = 0; i < 16; i++) {
    c = i;
    b = buffer_llong_push(buff, c);
    assert (b == true);
    assert (buffer_llong_empty_p(buff) == false);
  }
  b = buffer_llong_push(buff, c);
  assert (b == false);
  assert (buffer_llong_full_p(buff) == true);
  for(int i = 0; i < 16; i++) {
    buffer_llong_pop(&c, buff);
    assert(c == i);
    assert (buffer_llong_full_p(buff) == false);
  }
  b = buffer_llong_pop(&c, buff);
  assert (b == false);
  assert (buffer_llong_empty_p(buff) == true);

  b = buffer_llong_push(buff, c);
  assert (b == true);
  buffer_llong_clean(buff);
  assert (buffer_llong_empty_p(buff) == true);
  
  buffer_llong_clear(buff);
{'fraction_non_alphanumeric': 0.12108072048032022, 'fraction_numerical': 0.01645541472092506, 'mean_word_length': 4.087669683257919, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 7, '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': '618947', 'n_tokens_mistral': 3421, 'n_tokens_neox': 3155, 'n_words': 970}