Commit 0fef5c19 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Some paranoid refactoring and testing

parent 98babff7
......@@ -80,6 +80,8 @@ TESTCC = gcc -Wall -std=c99
test:
$(TESTCC) -o midi-test midi-test.c midi.c
./midi-test
$(TESTCC) -o list-test list-test.c list.c
./list-test
clean:
rm -rf *.o $(PRG).elf *.eps *.png *.pdf *.bak
......
File added
#include <stdio.h>
#include "test.h"
#include "list.h"
#define TEST_ARRAY_SIZE 10
uint8_t test_array[TEST_ARRAY_SIZE];
struct list l = {
.array = test_array,
.sup = TEST_ARRAY_SIZE,
};
void
test_flush(void)
{
desc("test l_flush()");
l_flush(&l);
assert(l.head == l.sup);
assert(l.sup == TEST_ARRAY_SIZE);
}
void
test_empty(void)
{
desc("test l_empty()");
l_flush(&l);
assert(l_empty(&l));
l_push(&l, 5);
assert(!l_empty(&l));
l_delete(&l, 5);
assert(l_empty(&l));
}
void
test_push(void)
{
desc("test l_push(), l_first(), l_last()");
l_flush(&l);
l_push(&l, 2);
l_push(&l, 5);
l_push(&l, 3);
assert(l_first(&l) == 3);
assert(l_last(&l) == 2);
l_delete(&l, 5);
assert(l_first(&l) == 3);
assert(l_last(&l) == 2);
l_delete(&l, 2);
assert(l_first(&l) == 3);
assert(l_last(&l) == 3);
l_push(&l, 1);
l_push(&l, 9);
l_push(&l, 9);
assert(l_first(&l) == 9);
l_delete(&l, 9);
assert(l_first(&l) == 1);
assert(l_last(&l) == 3);
l_delete(&l, 3);
assert(l_first(&l) == 1);
assert(l_last(&l) == 1);
l_delete(&l, 1);
assert(l_empty(&l));
}
int
main(int argc, char** argv)
{
test_flush();
test_empty();
test_push();
return 0;
}
#include "list.h"
uint8_t
succ(struct list *l, uint8_t x)
{
return l->array[x];
}
void
l_push(struct list *l, uint8_t x)
{
if (x >= l->sup) {
return;
}
l_delete(l, x);
l->array[x] = l->head;
l->head = x;
}
......@@ -10,28 +21,38 @@ l_push(struct list *l, uint8_t x)
void
l_delete(struct list *l, uint8_t x)
{
if (l_empty(l) || x >= l->sup) {
return;
}
if (l->head == x) {
l->head = l->array[x];
l->head = succ(l, x);
return;
}
for (uint8_t i = l->head; l->array[i] != l->sup; i = l->array[i]) {
if (l->array[i] == x) {
l->array[i] = l->array[x];
for (uint8_t i = l->head; i != l->sup; i = succ(l, i)) {
if (succ(l, i) == x) {
l->array[i] = succ(l, x);
return;
}
}
}
uint8_t
l_first(struct list *l)
{
return l->head;
}
uint8_t
l_last(struct list *l)
{
uint8_t x = l->head;
if (x == l->sup) {
return x;
if (l_empty(l)) {
return l->sup;
}
for (; l->array[x] != l->sup; x = l->array[x]) {
uint8_t x = l->head;
for (; succ(l, x) != l->sup; x = succ(l, x)) {
// walk the list
}
return x;
......@@ -42,3 +63,9 @@ l_flush(struct list *l)
{
l->head = l->sup;
}
uint8_t
l_empty(struct list *l)
{
return l->head == l->sup;
}
......@@ -11,7 +11,9 @@ struct list {
void l_push(struct list *l, uint8_t x);
void l_delete(struct list *l, uint8_t x);
uint8_t l_first(struct list *l);
uint8_t l_last(struct list *l);
void l_flush(struct list *l);
uint8_t l_empty(struct list *l);
#endif
#ifndef midi_test_h
#define midi_test_h
#include "test.h"
// Access to midi.c internals
void status_init(void);
void midi_parse(uint8_t);
uint8_t midi_available(void);
#define desc(s) printf("TEST: %s\n", s)
#define assert_status(status, avail, data) assert(midi_status() == status); \
assert(!midi_available() == !avail); \
if (avail) assert(midi_data() == data)
......
......@@ -2,26 +2,21 @@
#include "dac.h"
#include "gate.h"
#include "voice.h"
uint8_t current_note;
#include "list.h"
#define NUM_NOTES 128
uint8_t notes[NUM_NOTES];
uint8_t playing_note;
uint8_t notes_array[NUM_NOTES];
struct list notes = {
.array = notes_array,
.sup = NUM_NOTES,
};
void set_pitch(uint8_t n);
void
mono_init(void)
{
playing_note = NUM_NOTES;
}
void
push_note(uint8_t n)
{
notes[n] = playing_note;
playing_note = n;
l_flush(&notes);
}
void
......@@ -31,7 +26,7 @@ mono_note_on(uint8_t n)
return;
}
push_note(n);
l_push(&notes, n);
set_pitch(n);
for (uint8_t v = 0; v < NUM_VOICES; v++) {
......@@ -39,22 +34,6 @@ mono_note_on(uint8_t n)
}
}
void
drop_note(uint8_t n)
{
if (playing_note == n) {
playing_note = notes[playing_note];
return;
}
for (uint8_t i = playing_note; notes[i] != NUM_NOTES; i = notes[i]) {
if (notes[i] == n) {
notes[i] = notes[n];
return;
}
}
}
void
set_pitch(uint8_t n)
{
......@@ -70,14 +49,14 @@ mono_note_off(uint8_t n)
return;
}
drop_note(n);
l_delete(&notes, n);
if (playing_note == NUM_NOTES) {
if (l_empty(&notes)) {
for (uint8_t v = 0; v < NUM_VOICES; v++) {
gate_off(v);
}
return;
}
set_pitch(playing_note);
set_pitch(l_first(&notes));
}
#ifndef _test_h
#define _test_h
#include <assert.h>
#define desc(s) printf("TEST: %s\n", s)
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment