Files
OpenCellular/test/queue.c
Anton Staaf a6da62d284 Queue: Add functionality needed by new USART stream driver
Previously there was no way to remove multiple units at a time
from the queue, and the queue was wasting an entry to disambiguate
full from empty.  There was also no way to get the free entry
count from the queue, only the ability to query if it was above
a required amount.  The queue was also storing its constant
compile time configuration as well as its dynamic state in the
same structure.  This wasted RAM on configuration information
that doesn't change.

This refactor fixes these issues, making the queue suitable for
use in the new USART stream driver.

Signed-off-by: Anton Staaf <robotboy@chromium.org>

BRANCH=None
BUG=None
TEST=make buildall -j

Change-Id: I284cee52d8189928dbc4c499f87ab34e14019e5a
Reviewed-on: https://chromium-review.googlesource.com/210533
Reviewed-by: Vic Yang <victoryang@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Commit-Queue: Anton Staaf <robotboy@chromium.org>
Tested-by: Anton Staaf <robotboy@chromium.org>
2014-09-18 02:59:24 +00:00

170 lines
4.5 KiB
C

/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Test queue.
*/
#include "common.h"
#include "console.h"
#include "queue.h"
#include "test_util.h"
#include "timer.h"
#include "util.h"
QUEUE_CONFIG(test_queue8, 8, char)
QUEUE_CONFIG(test_queue2, 2, int16_t)
static int test_queue8_empty(void)
{
char dummy = 1;
queue_init(&test_queue8);
TEST_ASSERT(queue_is_empty(&test_queue8));
TEST_ASSERT(!queue_remove_units(&test_queue8, &dummy, 1));
TEST_ASSERT(queue_add_units(&test_queue8, &dummy, 1) == 1);
TEST_ASSERT(!queue_is_empty(&test_queue8));
return EC_SUCCESS;
}
static int test_queue8_init(void)
{
char dummy = 1;
queue_init(&test_queue8);
TEST_ASSERT(queue_add_units(&test_queue8, &dummy, 1) == 1);
queue_init(&test_queue8);
TEST_ASSERT(queue_is_empty(&test_queue8));
return EC_SUCCESS;
}
static int test_queue8_fifo(void)
{
char buf1[3] = {1, 2, 3};
char buf2[3];
queue_init(&test_queue8);
TEST_ASSERT(queue_add_units(&test_queue8, buf1 + 0, 1) == 1);
TEST_ASSERT(queue_add_units(&test_queue8, buf1 + 1, 1) == 1);
TEST_ASSERT(queue_add_units(&test_queue8, buf1 + 2, 1) == 1);
TEST_ASSERT(queue_remove_units(&test_queue8, buf2, 3) == 3);
TEST_ASSERT_ARRAY_EQ(buf1, buf2, 3);
return EC_SUCCESS;
}
static int test_queue8_multiple_units_add(void)
{
char buf1[5] = {1, 2, 3, 4, 5};
char buf2[5];
queue_init(&test_queue8);
TEST_ASSERT(queue_space(&test_queue8) >= 5);
TEST_ASSERT(queue_add_units(&test_queue8, buf1, 5) == 5);
TEST_ASSERT(queue_remove_units(&test_queue8, buf2, 5) == 5);
TEST_ASSERT_ARRAY_EQ(buf1, buf2, 5);
return EC_SUCCESS;
}
static int test_queue8_removal(void)
{
char buf1[5] = {1, 2, 3, 4, 5};
char buf2[5];
queue_init(&test_queue8);
TEST_ASSERT(queue_add_units(&test_queue8, buf1, 5) == 5);
/* 1, 2, 3, 4, 5 */
TEST_ASSERT(queue_remove_units(&test_queue8, buf2, 3) == 3);
TEST_ASSERT_ARRAY_EQ(buf1, buf2, 3);
/* 4, 5 */
TEST_ASSERT(queue_add_units(&test_queue8, buf1, 2) == 2);
/* 4, 5, 1, 2 */
TEST_ASSERT(queue_space(&test_queue8) == 4);
TEST_ASSERT(queue_remove_units(&test_queue8, buf2, 1) == 1);
TEST_ASSERT(buf2[0] == 4);
/* 5, 1, 2 */
TEST_ASSERT(queue_add_units(&test_queue8, buf1 + 2, 2) == 2);
/* 5, 1, 2, 3, 4 */
TEST_ASSERT(queue_space(&test_queue8) == 3);
TEST_ASSERT(queue_add_units(&test_queue8, buf1 + 2, 3) == 3);
/* 5, 1, 2, 3, 4, 3, 4, 5 */
TEST_ASSERT(queue_space(&test_queue8) == 0);
TEST_ASSERT(queue_remove_units(&test_queue8, buf2, 1) == 1);
TEST_ASSERT(buf2[0] == 5);
TEST_ASSERT(queue_remove_units(&test_queue8, buf2, 4) == 4);
TEST_ASSERT_ARRAY_EQ(buf1, buf2, 4);
TEST_ASSERT(queue_remove_units(&test_queue8, buf2, 3) == 3);
TEST_ASSERT_ARRAY_EQ(buf1 + 2, buf2, 3);
TEST_ASSERT(queue_is_empty(&test_queue8));
/* Empty */
TEST_ASSERT(queue_add_units(&test_queue8, buf1, 5) == 5);
TEST_ASSERT(queue_remove_units(&test_queue8, buf2, 5) == 5);
TEST_ASSERT_ARRAY_EQ(buf1, buf2, 5);
return EC_SUCCESS;
}
static int test_queue8_peek(void)
{
char buf1[5] = {1, 2, 3, 4, 5};
char buf2[5];
queue_init(&test_queue8);
TEST_ASSERT(queue_add_units(&test_queue8, buf1, 5) == 5);
/* 1, 2, 3, 4, 5 */
TEST_ASSERT(queue_count(&test_queue8) == 5);
TEST_ASSERT(queue_space(&test_queue8) == 3);
TEST_ASSERT(queue_peek_units(&test_queue8, buf2, 2, 3) == 3);
TEST_ASSERT_ARRAY_EQ(buf1 + 2, buf2, 3);
TEST_ASSERT(queue_count(&test_queue8) == 5);
TEST_ASSERT(queue_space(&test_queue8) == 3);
return EC_SUCCESS;
}
static int test_queue2_odd_even(void)
{
uint16_t buf1[3] = {1, 2, 3};
uint16_t buf2[3];
queue_init(&test_queue2);
TEST_ASSERT(queue_add_units(&test_queue2, buf1, 1) == 1);
/* 1 */
TEST_ASSERT(queue_space(&test_queue2) == 1);
TEST_ASSERT(queue_add_units(&test_queue2, buf1 + 1, 1) == 1);
/* 1, 2 */
TEST_ASSERT(queue_space(&test_queue2) == 0);
TEST_ASSERT(queue_remove_units(&test_queue2, buf2, 2) == 2);
TEST_ASSERT_ARRAY_EQ(buf1, buf2, 2);
TEST_ASSERT(queue_is_empty(&test_queue2));
/* Empty */
TEST_ASSERT(queue_space(&test_queue2) == 2);
TEST_ASSERT(queue_add_units(&test_queue2, buf1 + 2, 1) == 1);
/* 3 */
TEST_ASSERT(queue_remove_units(&test_queue2, buf2, 1) == 1);
TEST_ASSERT(buf2[0] == 3);
TEST_ASSERT(queue_is_empty(&test_queue2));
return EC_SUCCESS;
}
void run_test(void)
{
test_reset();
RUN_TEST(test_queue8_empty);
RUN_TEST(test_queue8_init);
RUN_TEST(test_queue8_fifo);
RUN_TEST(test_queue8_multiple_units_add);
RUN_TEST(test_queue8_removal);
RUN_TEST(test_queue8_peek);
RUN_TEST(test_queue2_odd_even);
test_print_result();
}