diff --git a/src/TestStatus.ino b/src/TestStatus.ino index 579ff72..bed403e 100644 --- a/src/TestStatus.ino +++ b/src/TestStatus.ino @@ -19,6 +19,7 @@ void setup() { last_time = millis(); pulse_interval = 0; + read_buffer = new CharQueue(); LEDS.addLeds(leds, LED_COUNT); // this configures the BlinkyBoard - leave as is. LEDS.showColor(CRGB(0, 0, 0)); @@ -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(); } @@ -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); } diff --git a/src/char.h b/src/char.h index a5efc16..7e98567 100644 --- a/src/char.h +++ b/src/char.h @@ -1,6 +1,8 @@ #ifndef _CHAR #define _CHAR +#include + class Char { private: bool last; @@ -8,9 +10,16 @@ class Char { Char *next; public: + Char() { + last = true; + next = NULL; + character = '\0'; + } + Char(char pcharacter) { last = true; character = pcharacter; + next = 0; } char get_character() { diff --git a/src/char_queue.h b/src/char_queue.h index d50586d..eca566e 100644 --- a/src/char_queue.h +++ b/src/char_queue.h @@ -1,39 +1,56 @@ #ifndef _CHAR_QUEUE #define _CHAR_QUEUE +#include #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 */ diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..53dd924 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,5 @@ +all: + g++ -o run_all run_all.cpp + +test: all + ./run_all diff --git a/test/char_queue_test b/test/char_queue_test new file mode 100644 index 0000000..0b85b73 Binary files /dev/null and b/test/char_queue_test differ diff --git a/test/char_queue_test.h b/test/char_queue_test.h index 474a559..1ed7ffe 100644 --- a/test/char_queue_test.h +++ b/test/char_queue_test.h @@ -1,4 +1,5 @@ #include "../src/char_queue.h" +#include #include class CharQueueTest { @@ -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 ); @@ -54,6 +73,40 @@ 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"); + } }; @@ -61,9 +114,17 @@ 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(); } diff --git a/test/run_all b/test/run_all new file mode 100755 index 0000000..2658ead Binary files /dev/null and b/test/run_all differ