~vertigo/forthbox

4caed6fd700f01a15d18db5e5e789a73addbce01 — Samuel A. Falvo II 4 months ago 8d0fda4
Test pushing a byte to an empty KIA queue
2 files changed, 70 insertions(+), 13 deletions(-)

M emulator/CMakeLists.txt
M emulator/test_kia.c
M emulator/CMakeLists.txt => emulator/CMakeLists.txt +4 -0
@@ 1,10 1,14 @@
include(./FindSDL2.cmake)
include(CTest)

include_directories(${SDL2_INCLUDE_DIRS})

add_executable(fbemu main.c)
target_link_libraries(fbemu lib65816a ${SDL2_LIBRARIES})

add_executable(test_kia test_kia.c)
add_test(NAME test_kia COMMAND test_kia)

install(TARGETS fbemu)

add_custom_target(

M emulator/test_kia.c => emulator/test_kia.c +66 -13
@@ 1,26 1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

static int erc = 0;
#define KIA_QUEUE_DEPTH 16

typedef struct {
  int     read_ptr;
  int     write_ptr;
  uint8_t queue;
} KIA_t;

void
kia_reset(KIA_t *kp) {
kia_reset(KIA_t *pk) {
  memset((void *)pk, 0, sizeof(KIA_t));
}

KIA_t *
kia_new(void) {
  KIA_t *kp = (KIA_t *)malloc(sizeof(KIA_t));
  if(kp) kia_reset(kp);
  return kp;
  KIA_t *pk = (KIA_t *)malloc(sizeof(KIA_t));
  if(pk) kia_reset(pk);
  return pk;
}

void
kia_test_get_pointers(int *p_read_pointer, int *p_write_pointer) {
  *p_read_pointer = 0;
  *p_write_pointer = 0;
kia_test_get_pointers(KIA_t *pk, int *p_read_pointer, int *p_write_pointer) {
  *p_read_pointer = pk->read_ptr;
  *p_write_pointer = pk->write_ptr;
}

void


@@ 28,29 34,76 @@ kia_pop(void) {
}

void
kia_push(KIA_t *pk, uint8_t byte) {
  ++pk->write_ptr;
  pk->queue = byte;
}

uint8_t
kia_peek(KIA_t *pk) {
  return pk->queue;
}

static int erc = 0;

void
test_pop_empty() {
  if(erc) return;
  printf("test_pop_empty\n");

  KIA_t * pk = kia_new();

  if(!pk) goto null_ptr;

  int read_pointer, write_pointer;
  kia_test_get_pointers(&read_pointer, &write_pointer);
  int old_read, old_write;
  kia_test_get_pointers(pk, &old_read, &old_write);

  kia_pop();

  int new_read, new_write;
  kia_test_get_pointers(&new_read, &new_write);
  kia_test_get_pointers(pk, &new_read, &new_write);

  if(read_pointer != new_read) goto fail;
  if(write_pointer != new_write) goto fail;
  if(old_read != new_read) goto fail;
  if(old_write != new_write) goto fail;
  return;

null_ptr: ++erc;
fail:     ++erc;
}

void
test_push_byte() {
  if(erc) return;
  printf("test_push_byte\n");

  const uint8_t expected_byte = 0xCC;

  KIA_t *pk = kia_new();

  int old_read, old_write;
  kia_test_get_pointers(pk, &old_read, &old_write);

  kia_push(pk, expected_byte);

  int new_read, new_write;
  kia_test_get_pointers(pk, &new_read, &new_write);

  if(new_read != old_read) goto read_ptr_fail;
  if(new_write != ((old_write + 1) % KIA_QUEUE_DEPTH)) goto write_ptr_fail;

  if(kia_peek(pk) != expected_byte) goto push_data_fail;

  return;

push_data_fail: ++erc;
write_ptr_fail: ++erc;
read_ptr_fail:  ++erc;
}

int
main() {
  test_pop_empty();
  test_push_byte();

  return erc;
}