~vertigo/forthbox

bffc748caa30c8eb25a421d2f2b75a9e027f020d — Samuel A. Falvo II 4 months ago fae0321
Test full queue behavior
1 files changed, 92 insertions(+), 3 deletions(-)

M emulator/test_kia.c
M emulator/test_kia.c => emulator/test_kia.c +92 -3
@@ 42,6 42,22 @@ kia_isnt_empty(KIA_t *pk) {
}

/**
 * @brief Answers true if KIA queue is full.
 */
bool
kia_is_full(KIA_t *pk) {
  return next_slot(pk->write_ptr) == pk->read_ptr;
}

/**
 * @brief Answers true if KIA queue is not full.
 */
bool
kia_isnt_full(KIA_t *pk) {
  return !kia_is_full(pk);
}

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


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

/**


@@ 198,11 216,82 @@ read_ptr_fail:          ++erc;
first_push_data_fail:   ++erc;
}

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

  KIA_t *pk = kia_new();

  kia_push(pk,  1);
  kia_push(pk,  2);
  kia_push(pk,  3);
  kia_push(pk,  4);
  kia_push(pk,  5);
  kia_push(pk,  6);
  kia_push(pk,  7);
  kia_push(pk,  8);
  kia_push(pk,  9);
  kia_push(pk, 10);
  kia_push(pk, 11);
  kia_push(pk, 12);
  kia_push(pk, 13);
  kia_push(pk, 14);
  kia_push(pk, 15);
  kia_push(pk, 16);

  if(kia_peek(pk) != 1) goto general_pop_fail;
  kia_pop(pk); //  1
  if(kia_peek(pk) != 2) goto general_pop_fail;
  kia_pop(pk); //  2
  if(kia_peek(pk) != 3) goto general_pop_fail;
  kia_pop(pk); //  3
  if(kia_peek(pk) != 4) goto general_pop_fail;
  kia_pop(pk); //  4
  if(kia_peek(pk) != 5) goto general_pop_fail;
  kia_pop(pk); //  5
  if(kia_peek(pk) != 6) goto general_pop_fail;
  kia_pop(pk); //  6
  if(kia_peek(pk) != 7) goto general_pop_fail;
  kia_pop(pk); //  7
  if(kia_peek(pk) != 8) goto general_pop_fail;
  kia_pop(pk); //  8
  if(kia_peek(pk) != 9) goto general_pop_fail;
  kia_pop(pk); //  9
  if(kia_peek(pk) != 10) goto general_pop_fail;
  kia_pop(pk); // 10
  if(kia_peek(pk) != 11) goto general_pop_fail;
  kia_pop(pk); // 11
  if(kia_peek(pk) != 12) goto general_pop_fail;
  kia_pop(pk); // 12
  if(kia_peek(pk) != 13) goto general_pop_fail;
  kia_pop(pk); // 13
  if(kia_peek(pk) != 14) goto pop_fail_14;
  kia_pop(pk); // 14
  if(kia_peek(pk) != 15) goto pop_fail_15;
  if(kia_is_empty(pk)) goto not_empty_yet_fail;
  kia_pop(pk); // 15
  if(kia_isnt_empty(pk)) goto now_empty_fail;
  if(kia_peek(pk) != 0) goto pop_fail_16;
  kia_pop(pk); // 16
  if(kia_peek(pk) != 0) goto pop_fail_16;

  return;

pop_fail_16:        ++erc;
now_empty_fail:     ++erc;
not_empty_yet_fail: ++erc;
pop_fail_15:        ++erc;
pop_fail_14:        ++erc;
general_pop_fail:   ++erc;
}

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

  return erc;
}