~vertigo/forthbox

fae032160f425225c9bd3899c708f584901a2b02 — Samuel A. Falvo II 4 months ago b45fe7c
Test pushing multiple bytes into queue
1 files changed, 64 insertions(+), 10 deletions(-)

M emulator/test_kia.c
M emulator/test_kia.c => emulator/test_kia.c +64 -10
@@ 2,6 2,7 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>

/**
 * @brief The KIA queue depth.


@@ 10,6 11,12 @@
 */
#define KIA_QUEUE_DEPTH 16

typedef struct {
  int     read_ptr;
  int     write_ptr;
  uint8_t queue[KIA_QUEUE_DEPTH];
} KIA_t;

/**
 * @brief Answers with the slot index after the one provided.
 */


@@ 18,11 25,21 @@ next_slot(int slot) {
  return (slot + 1) % KIA_QUEUE_DEPTH;
}

typedef struct {
  int     read_ptr;
  int     write_ptr;
  uint8_t queue;
} KIA_t;
/**
 * @brief Answers true if the queue is empty.
 */
bool
kia_is_empty(KIA_t *pk) {
  return (pk->read_ptr) == (pk->write_ptr);
}

/**
 * @brief Answers true if the queue is not empty.
 */
bool
kia_isnt_empty(KIA_t *pk) {
  return !kia_is_empty(pk);
}

/**
 * @brief Resets the KIA_t instance to its empty-queue condition.


@@ 61,7 78,8 @@ kia_test_get_pointers(
 * @brief Pops the input queue.
 */
void
kia_pop(void) {
kia_pop(KIA_t *pk) {
  if(kia_isnt_empty(pk)) pk->read_ptr = next_slot(pk->read_ptr);
}

/**


@@ 71,8 89,9 @@ kia_pop(void) {
 */
void
kia_push(KIA_t *pk, uint8_t byte) {
  ++pk->write_ptr;
  pk->queue = byte;
  int wp = pk->write_ptr;
  pk->write_ptr = next_slot(wp);
  pk->queue[wp] = byte;
}

/**


@@ 84,7 103,7 @@ kia_push(KIA_t *pk, uint8_t byte) {
 */
uint8_t
kia_peek(KIA_t *pk) {
  return pk->queue;
  return pk->queue[pk->read_ptr];
}

static int erc = 0;


@@ 101,7 120,7 @@ test_pop_empty() {
  int old_read, old_write;
  kia_test_get_pointers(pk, &old_read, &old_write);

  kia_pop();
  kia_pop(pk);

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


@@ 145,10 164,45 @@ write_ptr_fail: ++erc;
read_ptr_fail:  ++erc;
}

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

  KIA_t *pk = kia_new();

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

  kia_push(pk, 0x11);
  kia_push(pk, 0x22);
  if(kia_peek(pk) != 0x11) goto first_push_data_fail;

  kia_pop(pk);

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

  if(new_read != next_slot(old_read)) goto read_ptr_fail;
  if(new_write != next_slot(next_slot(old_write))) {
    goto write_ptr_fail;
  }

  if(kia_peek(pk) != 0x22) goto second_push_data_fail;

  return;

second_push_data_fail:  ++erc;
write_ptr_fail:         ++erc;
read_ptr_fail:          ++erc;
first_push_data_fail:   ++erc;
}

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

  return erc;
}