Skip to content

Commit

Permalink
queue tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rtlopez committed Dec 8, 2023
1 parent f41ab81 commit b699253
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
1 change: 0 additions & 1 deletion lib/Espfc/src/Target/QueueAtomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ void Queue::send(const Event& e)
Event Queue::receive()
{
Event e;
//while(!_q.pop(e));
_q.pop(e);
return e;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Espfc/src/Target/QueueAtomic.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#if defined(ESPFC_ATOMIC_QUEUE)
#if defined(ESPFC_ATOMIC_QUEUE) || defined(UNIT_TEST)

#include <atomic>

Expand All @@ -12,7 +12,7 @@ class QueueAtomic
public:
enum { Capacity = Size + 1 };

QueueAtomic(): _tail(0), _head(0){}
QueueAtomic(): _tail(0), _head(0) {}
~QueueAtomic() {}

bool push(const Element& item)
Expand Down
65 changes: 65 additions & 0 deletions test/test_math/test_math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "helper_3dmath.h"
#include "Filter.h"
#include "Control/Pid.h"
#include "Target/QueueAtomic.h"

// void setUp(void) {
// // set stuff up here
Expand Down Expand Up @@ -970,6 +971,68 @@ void test_pid_update_sum_limit()
TEST_ASSERT_FLOAT_WITHIN(0.001f, 1.0f, result);
}

void test_queue_atomic()
{
QueueAtomic<int, 3> q;
int e1 = 1, e2 = 2, e3 = 3, e4 = 4;
int r1 = 91, r2 = 92, r3 = 93, r4 = 94;

// empty
TEST_ASSERT_TRUE(q.isEmpty());
TEST_ASSERT_FALSE(q.isFull());

TEST_ASSERT_FALSE(q.pop(r1));
TEST_ASSERT_EQUAL(91, r1);

TEST_ASSERT_TRUE(q.isEmpty());
TEST_ASSERT_FALSE(q.isFull());

// push first element
TEST_ASSERT_TRUE(q.push(e1));

TEST_ASSERT_FALSE(q.isEmpty());
TEST_ASSERT_FALSE(q.isFull());

// pop last element
TEST_ASSERT_TRUE(q.pop(r1));
TEST_ASSERT_EQUAL(1, r1);

TEST_ASSERT_TRUE(q.isEmpty());
TEST_ASSERT_FALSE(q.isFull());

// pop element again
TEST_ASSERT_FALSE(q.pop(r1));
TEST_ASSERT_EQUAL(1, r1);

TEST_ASSERT_TRUE(q.isEmpty());
TEST_ASSERT_FALSE(q.isFull());

// make full
TEST_ASSERT_TRUE(q.push(e1));
TEST_ASSERT_TRUE(q.push(e2));
TEST_ASSERT_TRUE(q.push(e3));
TEST_ASSERT_FALSE(q.push(e4));

TEST_ASSERT_FALSE(q.isEmpty());
TEST_ASSERT_TRUE(q.isFull());

// make empty
TEST_ASSERT_TRUE(q.pop(r1));
TEST_ASSERT_EQUAL(1, r1);

TEST_ASSERT_TRUE(q.pop(r2));
TEST_ASSERT_EQUAL(2, r2);

TEST_ASSERT_TRUE(q.pop(r3));
TEST_ASSERT_EQUAL(3, r3);

TEST_ASSERT_FALSE(q.pop(r4));
TEST_ASSERT_EQUAL(94, r4);

TEST_ASSERT_TRUE(q.isEmpty());
TEST_ASSERT_FALSE(q.isFull());
}

int main(int argc, char **argv)
{
UNITY_BEGIN();
Expand Down Expand Up @@ -1035,6 +1098,8 @@ int main(int argc, char **argv)
RUN_TEST(test_pid_update_sum);
RUN_TEST(test_pid_update_sum_limit);

RUN_TEST(test_queue_atomic);

UNITY_END();

return 0;
Expand Down

0 comments on commit b699253

Please sign in to comment.