Skip to content

Commit

Permalink
Add some tests, working queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Jade Meskill and Roy van de Water authored and Roy van de Water committed Dec 9, 2013
1 parent aa1c25e commit ad40258
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/TestStatus.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void setup()
{
last_time = millis();
pulse_interval = 0;
read_buffer = new CharQueue();

LEDS.addLeds<WS2811, PIN_SIGNAL, GRB>(leds, LED_COUNT); // this configures the BlinkyBoard - leave as is.
LEDS.showColor(CRGB(0, 0, 0));
Expand Down Expand Up @@ -120,8 +121,9 @@ void set_rgb(int r, int g, int b) {
}

void test_sequence() {
int interval = 254 / LED_COUNT;
for(int i=0; i < LED_COUNT; i += 1) {
leds[i] = CRGB(0, 0, 254);
leds[i] = CRGB(254 - (i * interval), 0, (i * interval));
}
LEDS.show();
}
Expand All @@ -131,7 +133,9 @@ void read() {
char character = Serial.read();

if(character == 't') {
return;
read_buffer->clear();
else if (character == 'e') {
Serial.println(read_buffer->to_string());
} else {
read_buffer->push(character);
}
Expand Down
9 changes: 9 additions & 0 deletions src/char.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
#ifndef _CHAR
#define _CHAR

#include <stdio.h>

class Char {
private:
bool last;
char character;
Char *next;

public:
Char() {
last = true;
next = NULL;
character = '\0';
}

Char(char pcharacter) {
last = true;
character = pcharacter;
next = 0;
}

char get_character() {
Expand Down
47 changes: 32 additions & 15 deletions src/char_queue.h
Original file line number Diff line number Diff line change
@@ -1,39 +1,56 @@
#ifndef _CHAR_QUEUE
#define _CHAR_QUEUE

#include <string>
#include "char.h"

class CharQueue {
private:
int mSize;
// Char *head;
Char *mHead;
Char *mTail;

public:
CharQueue() {
mSize = 0;
mHead = new Char();
mTail = mHead;
}

int size() {
return mSize;
void clear() {
mSize = 0;
mHead = new Char();
mTail = mHead;
}

void pop() {
// char character = head->get_character();
// head = head->get_next();
// mSize--;
mSize--;
// return character;
char pop() {
Char *realHead = mHead->get_next();
char character = '\0';

if(realHead) {
character = realHead->get_character();
mHead->set_next(realHead->get_next());
mSize--;
}

return character;
}

void push(char character) {
// Char *next = new Char(character);
// Char *current = head;
// while (mSize > 0 && !current->last()) {
// current = current->get_next();
// }
// current->set_next(next);
Char *next = new Char(character);
mTail->set_next(next);
mTail = next;

mSize++;
}

int size() {
return mSize;
}

std::string to_string() {
return "";
}
};

#endif /* _CHAR_QUEUE */
5 changes: 5 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
all:
g++ -o run_all run_all.cpp

test: all
./run_all
Binary file added test/char_queue_test
Binary file not shown.
61 changes: 61 additions & 0 deletions test/char_queue_test.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../src/char_queue.h"
#include <stdio.h>
#include <assert.h>

class CharQueueTest {
Expand All @@ -22,6 +23,24 @@ class CharQueueTest {
char_queue->push('h');
}

void test_pop_with_nothing() {
setup();
assert( !char_queue->pop() );
}

void test_pop_after_push() {
setup();
char_queue->push('h');
assert( char_queue->pop() == 'h' );
}

void test_pop_after_two_pushes() {
setup();
char_queue->push('x');
char_queue->push('y');
assert( char_queue->pop() == 'x' );
}

void test_size_when_empty() {
setup();
assert( char_queue->size() == 0 );
Expand Down Expand Up @@ -54,16 +73,58 @@ class CharQueueTest {
char_queue->pop();
assert( char_queue->size() == 1 );
}

void test_size_after_a_pop() {
setup();
char_queue->pop();
assert( char_queue->size() == 0 );
}

void test_double_pop_after_double_push() {
setup();
char_queue->push('f');
char_queue->push('b');
assert( char_queue->pop() == 'f' );
assert( char_queue->pop() == 'b' );
}

void test_clear_with_nothing() {
setup();

char_queue->clear();
assert( char_queue->pop() == '\0' );
assert( char_queue->size() == 0 );
}

void test_to_string_with_nothing() {
setup();

assert(char_queue->to_string() == "");
}

void test_to_string_after_push() {
setup();
char_queue->push('c');
assert(char_queue->to_string() == "c");
}
};


void char_queue_test(){
CharQueueTest *test = new CharQueueTest();
test->test_constructor();
test->test_push();
test->test_pop_with_nothing();
test->test_pop_after_push();
test->test_pop_after_two_pushes();
test->test_size_when_empty();
test->test_size_with_one_item();
test->test_size_with_two_items();
test->test_size_after_a_push_and_a_pop();
test->test_size_after_two_pushes_and_a_pop();
test->test_size_after_a_pop();
test->test_double_pop_after_double_push();
test->test_clear_with_nothing();
test->test_to_string_with_nothing();
test->test_to_string_after_push();
}
Binary file added test/run_all
Binary file not shown.

0 comments on commit ad40258

Please sign in to comment.