/*
 * ringbuf_test.c
 *
 *  Created on: Jul 14, 2022
 *      Author: tom
 */

#include <assert.h>
#include <stdio.h>
#include "ringbuf.h"
#include <malloc.h>


void ringbuf_test(void) {
	size_t usable_size = 0;
	fprintf(stderr, "\n");

	malloc_stats();


	assert(1);
	// assert(0); // ... assertion "0" failed: ...blabla bla
	//1. Test Initialization
	hring rb1 = ringbuf_create(512, RINGBUF_PARAM_NONE);
	usable_size = malloc_usable_size(rb1);
	fprintf(stdout, "Malloc Usable Size: %d\n", usable_size);

	hring rb2 = ringbuf_create(512, RINGBUF_ALLOWOVERWRITE);
	assert(rb1 != NULL);
	assert(rb2 != NULL);
	hring rb3 = ringbuf_create(512, RINGBUF_ALLOWOVERWRITE);
	hring rb4 = ringbuf_create(512, RINGBUF_ALLOWOVERWRITE);


	printf("Malloc Usable Size of rb1: %d\n", malloc_usable_size(rb1));
	printf("Malloc Usable Size of rb2: %d\n", malloc_usable_size(rb2));
	printf("Malloc Usable Size of rb3: %d\n", malloc_usable_size(rb3));
	printf("Malloc Usable Size of rb4: %d\n", malloc_usable_size(rb4));

	ringbuf_destroy(rb4);
	malloc_stats();
	ringbuf_destroy(rb3);
	malloc_stats();

	malloc_stats();
	ringbuf_destroy(rb2);
	malloc_stats();
	ringbuf_destroy(rb1);
	malloc_stats();

	printf("rb1 after destroy is: 0x%08X\n", (unsigned int) rb1);


}

#if 0
  volatile int ret = 0;
  ring = ringbuf_create(64,RINGBUF_ALLOWOVERWRITE);
  if (ring == NULL)
	  printf("we have some problems ...\n");

  uint8_t data[1024];
  char str[1024]={0};

  strcpy(str, "KW4NZ");

  ret = ringbuf_dump(ring);
  ret = ringbuf_write(ring, str);
  printf("Writing string: %s\n", str);
  ret = ringbuf_dump(ring);
  ret = ringbuf_push(ring, (uint8_t *)"Ich gehe spazieren.", sizeof("Ich gehe spazieren."));
  ret = ringbuf_dump(ring);
  ret = ringbuf_push(ring, (uint8_t *)"NOCHMALS GEHE ICH RAUS.", sizeof("NOCHMALS GEHE ICH RAUS."));
  ret = ringbuf_dump(ring);

  ret = ringbuf_read(ring, str);
  printf("STRING: %s\n", str);
  ret = ringbuf_dump(ring);

  ret = ringbuf_read(ring, str);
  printf("STRING: %s\n", str);
  ret = ringbuf_dump(ring);

  ret = ringbuf_read(ring, str);
  printf("STRING: %s\n", str);
  ret = ringbuf_dump(ring);

  strcpy(str, "OE3TKT,OE1TKT,OE7TKT");
  ret = ringbuf_write(ring, str);
  ret = ringbuf_dump(ring);

  ret = ringbuf_read(ring, str);
  printf("STRING: %s\n", str);
  ret = ringbuf_dump(ring);

  strcpy(str, "OE1TKT,OE3TKT,OE7TKT");
  ret = ringbuf_write(ring, str);
  strcpy(str, "KW4NZ");
  ret = ringbuf_write(ring, str);
  ret = ringbuf_dump(ring);

  ret = ringbuf_read(ring, str);
  printf("STRING: %s (OE1TKT,OE3TKT,OE7TKT)\n", str);
  ret = ringbuf_read(ring, str);
  printf("STRING: %s (KW4NZ)\n", str);

  ret = ringbuf_pull(ring, data, 1024);
  printf("Read %d bytes...\n", ret);
  for (int i = 0; i < ret; i++)
	  putchar(data[i]);
  puts("");

  ringbuf_dump(ring);

  ret = ringbuf_push(ring, (uint8_t *)"Eine 128-tägige Reise ist zu gewinnen!", sizeof("Eine 128-tägige Reise ist zu gewinnen!"));
  ringbuf_dump(ring);

//  ret = ringbuf_push(ring, (uint8_t *)"Eine 100-tägige Reise ist zu gewinnen!", sizeof("Eine 128-tägige Reise ist zu gewinnen!"));
//  ret = ringbuf_push(ring, (uint8_t *)"Eine 90-tägige Reise ist zu gewinnen!", sizeof("Eine 90-tägige Reise ist zu gewinnen!"));
  ret = ringbuf_push(ring, (uint8_t *)"ENDENDENDENDEND", sizeof("ENDENDENDENDEND"));
  ringbuf_dump(ring);
  ret = ringbuf_push(ring, (uint8_t *)"Vereinbarungen treffen zu.", sizeof("Vereinbarungen treffen zu."));
  ringbuf_dump(ring);
  ret = ringbuf_read(ring, str);
  printf("String: %s (length: %d)\n", str, ret);
  ret = ringbuf_dump(ring);
  ret = ringbuf_read(ring, str);
  printf("String: %s (length: %d)\n", str, ret);
  ret = ringbuf_read(ring, str);
  printf("String: %s (length: %d)\n", str, ret);

  ret = ringbuf_pull(ring, data, 1024);
  printf("Read %d bytes...\n", ret);
  for (int i = 0; i < ret; i++)
	  putchar(data[i]);
  puts("");
  ringbuf_dump(ring);

  ringbuf_clear(ring);
  ringbuf_dump(ring);
#if RING_STATISTICS_ENABLED
  ringbuf_statistics(ring);
#endif
#endif
