<commit_before>#include <pthread.h>
#include <stdint.h>
#include "ringbuffer.h"

#define TWD_RINGBUFFER_SIZE 4096 // 1 memory page on most systems

#define handle_error_en(en, msg) \
  do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

#define handle_warning_en(en, msg) \
  do { errno = en; perror(msg); } while (0)

#define handle_error(msg) \
  do { perror(msg); exit(EXIT_FAILURE); } while (0)

struct acks {
  uint64_t timestamp;
  uint32_t seqnum;
  uint32_t flags;
};

typedef struct acks ack_t;

const char SUCCESS[] = "SUCCESS";
const char FAIL[] = "FAIL";

ack_t test_data_struct[TWD_RINGBUFFER_SIZE * 2] = {0};

static int test_basic(ringbuffer__s *rbuf) {
  int a, b, c;
  a = 1;
  b = 2;
  ringbuffer_write(rbuf,&a,sizeof(a));
  ringbuffer_write(rbuf,&b,sizeof(b));
  if(ringbuffer_read(rbuf,&c,sizeof(c)) != sizeof(c)) return -1;
  if(c!=a) return -2;
  if(ringbuffer_read(rbuf,&c,sizeof(c)) != sizeof(c)) return -1;
  if(c!=b) return -2;
  if(ringbuffer_read(rbuf,&c,sizeof(c)) != 0) return -3;
  if(a != 1) return -4; // fail. we scribbled on a somehow
  if(b != 2) return -4; // fail. we scribbled on b somehow
  return 0;
}

/* PTHREADED TESTS */

struct test_control {
  pthread_t thread_id; /* ID returned by pthread_create() */
  int thread_num;      /* Application-defined thread # */
  ringbuffer__s *ringbuf;

  size_t fd;
  size_t cookie;
  size_t seqno_start;
  size_t limit;

};

static void *
thread_reader(void *arg)
{
  struct test_control *tinfo = arg;
  int count;
  ack_t *a;
  printf("Reader started\n");
  while(!(count = ringbuffer_read(tinfo->ringbuf,&a,sizeof(a))))
    sched_yield();
  while(a->flags != 1) {
    printf("Seqnum: %d; flags: %d\n", a->seqnum, a->flags);
    while(!(count = ringbuffer_read(tinfo->ringbuf,&a,sizeof(a))))
      sched_yield();
  }
  printf("Done reading!\n");
  return SUCCESS;
}

static void *
thread_writer(void *arg)
{
  struct test_control *tinfo = arg;
  int i;
  printf("Writer started\n");
  for(i = 0; i<2*TWD_RINGBUFFER_SIZE; i++) { 
    test_data_struct[i].seqnum = i;
    test_data_struct[i].flags = 0;
    if(!ringbuffer_write(tinfo->ringbuf,&test_data_struct[i],sizeof(ack_t)))
      sched_yield();
  }
  printf("Done writing i!\n");
  test_data_struct[i].seqnum = i;
  test_data_struct[i].flags = 1;
  while(!ringbuffer_write(tinfo->ringbuf,&test_data_struct[i],sizeof(ack_t)))
    sched_yield();
  printf("Done writing!\n");
  return FAIL;
}

static int test_pthread1(ringbuffer__s *rbuf) {
  int s, tnum, num_threads;
  pthread_attr_t attr;
  int stack_size = 0;
  void *res;
  int i;

  num_threads = 2;

  /* Allocate memory for pthread_create() arguments in c99 */

  {
    struct test_control tinfo[num_threads *
			     sizeof(struct test_control)];
    memset(tinfo,num_threads*sizeof(struct test_control),0);<commit_msg>test_ringbuffer.c: now we are into the "interesting" crash range<commit_after>#include <pthread.h>
#include <stdint.h>
#include "ringbuffer.h"

#define TWD_RINGBUFFER_SIZE 4096 // 1 memory page on most systems

#define handle_error_en(en, msg) \
  do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

#define handle_warning_en(en, msg) \
  do { errno = en; perror(msg); } while (0)

#define handle_error(msg) \
  do { perror(msg); exit(EXIT_FAILURE); } while (0)

struct acks {
  uint64_t timestamp;
  uint32_t seqnum;
  uint32_t flags;
};

typedef struct acks ack_t;

const char SUCCESS[] = "SUCCESS";
const char FAIL[] = "FAIL";

ack_t test_data_struct[TWD_RINGBUFFER_SIZE * 2] = {0};

static int test_basic(ringbuffer__s *rbuf) {
  int a, b, c;
  a = 1;
  b = 2;
  ringbuffer_write(rbuf,&a,sizeof(a));
  ringbuffer_write(rbuf,&b,sizeof(b));
  if(ringbuffer_read(rbuf,&c,sizeof(c)) != sizeof(c)) return -1;
  if(c!=a) return -2;
  if(ringbuffer_read(rbuf,&c,sizeof(c)) != sizeof(c)) return -1;
  if(c!=b) return -2;
  if(ringbuffer_read(rbuf,&c,sizeof(c)) != 0) return -3;
  if(a != 1) return -4; // fail. we scribbled on a somehow
  if(b != 2) return -4; // fail. we scribbled on b somehow
  return 0;
}

/* PTHREADED TESTS */

struct test_control {
  pthread_t thread_id; /* ID returned by pthread_create() */
  int thread_num;      /* Application-defined thread # */
  ringbuffer__s *ringbuf;

  size_t fd;
  size_t cookie;
  size_t seqno_start;
  size_t limit;

};

static void *
thread_reader(void *arg)
{
  struct test_control *tinfo = arg;
  int count;
  ack_t a;
  printf("Reader started\n");
  while(!(count = ringbuffer_read(tinfo->ringbuf,&a,sizeof(a))))
    sched_yield();
  while(a.flags != 1) {
    printf("Seqnum: %d; flags: %d\n", a.seqnum, a.flags);
    while(!(count = ringbuffer_read(tinfo->ringbuf,&a,sizeof(a))))
      sched_yield();
  }
  printf("Done reading!\n");
  return SUCCESS;
}

static void *
thread_writer(void *arg)
{
  struct test_control *tinfo = arg;
  int i;
  printf("Writer started\n");
  for(i = 0; i<2*TWD_RINGBUFFER_SIZE; i++) { 
    test_data_struct[i].seqnum = i;
    test_data_struct[i].flags = 0;
    //    printf("Writing i:%d!\n",i);
    if(!ringbuffer_write(tinfo->ringbuf,&test_data_struct[i],sizeof(ack_t)))
      sched_yield();
  }
  printf("Done writing i!\n");
  test_data_struct[i].seqnum = i;
  test_data_struct[i].flags = 1;
  while(!ringbuffer_write(tinfo->ringbuf,&test_data_struct[i],sizeof(ack_t)))
    sched_yield();
  printf("Done writing!\n");
  return FAIL;
}

static int test_pthread1(ringbuffer__s *rbuf) {
  int s, tnum, num_threads;
  pthread_attr_t attr;
  int stack_size = 0;
  void *res;
  int i;

  num_threads = 2;

  /* Allocate memory for pthread_create() arguments in c99 */

  {
    struct test_control tinfo[num_threads *
			     sizeof(struct test_control)];
    memset(tinfo,num_threads*sizeof(struct test_control),0);
{'fraction_non_alphanumeric': 0.15451691663760028, 'fraction_numerical': 0.013951866062085804, 'mean_word_length': 4.171325518485122, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 18, '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': '720245', 'n_tokens_mistral': 2297, 'n_tokens_neox': 2212, 'n_words': 495}