Unit test for queue implementation

BUG=chrome-os-partner:19236
TEST=Pass the test.
BRANCH=None

Change-Id: I575e4a9abfd9431e3b74c36da8c3d69285e5c0fb
Signed-off-by: Vic Yang <victoryang@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/58032
Reviewed-by: Yung-Chieh Lo <yjlou@chromium.org>
This commit is contained in:
Vic Yang
2013-06-09 19:02:02 +08:00
committed by ChromeBot
parent a9541220dc
commit cbee574e64
4 changed files with 197 additions and 1 deletions

View File

@@ -42,6 +42,18 @@
} \
} while (0)
#define TEST_ASSERT_ARRAY_EQ(s, d, n) \
do { \
int i; \
for (i = 0; i < n; ++i) \
if ((s)[i] != (d)[i]) { \
ccprintf("%d: ASSERT_ARRAY_EQ failed at " \
"index=%d: %d != %d\n", __LINE__, i, \
(int)(s)[i], (int)(d)[i]); \
return EC_ERROR_UNKNOWN; \
} \
} while (0)
#define TEST_CHECK(n) \
do { \
if (n) \

View File

@@ -28,7 +28,7 @@ test-list-$(BOARD_peppy)=
# Emulator tests
test-list-host=mutex pingpong utils kb_scan kb_mkbp lid_sw power_button hooks
test-list-host+=thermal flash
test-list-host+=thermal flash queue
flash-y=flash.o
hooks-y=hooks.o
@@ -39,6 +39,7 @@ mutex-y=mutex.o
pingpong-y=pingpong.o
power_button-y=power_button.o
powerdemo-y=powerdemo.o
queue-y=queue.o
stress-y=stress.o
thermal-y=thermal.o
thermal-scale=200

166
test/queue.c Normal file
View File

@@ -0,0 +1,166 @@
/* 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"
static char buffer6[6]; /* Max 5 items in queue */
static struct queue test_queue6 = {
.buf_bytes = sizeof(buffer6),
.unit_bytes = sizeof(char),
.buf = buffer6,
};
static char buffer5[5]; /* Max 2 items (2 byte for each) in queue */
static struct queue test_queue5 = {
.buf_bytes = sizeof(buffer5),
.unit_bytes = 2,
.buf = buffer5,
};
#define LOOP_DEQUE(q, d, n) \
do { \
int i; \
for (i = 0; i < n; ++i) \
TEST_ASSERT(queue_remove_unit(&q, d + i)); \
} while (0)
static int test_queue6_empty(void)
{
char dummy = 1;
queue_reset(&test_queue6);
TEST_ASSERT(queue_is_empty(&test_queue6));
queue_add_units(&test_queue6, &dummy, 1);
TEST_ASSERT(!queue_is_empty(&test_queue6));
return EC_SUCCESS;
}
static int test_queue6_reset(void)
{
char dummy = 1;
queue_reset(&test_queue6);
queue_add_units(&test_queue6, &dummy, 1);
queue_reset(&test_queue6);
TEST_ASSERT(queue_is_empty(&test_queue6));
return EC_SUCCESS;
}
static int test_queue6_fifo(void)
{
char buf1[3] = {1, 2, 3};
char buf2[3];
queue_reset(&test_queue6);
queue_add_units(&test_queue6, buf1 + 0, 1);
queue_add_units(&test_queue6, buf1 + 1, 1);
queue_add_units(&test_queue6, buf1 + 2, 1);
LOOP_DEQUE(test_queue6, buf2, 3);
TEST_ASSERT_ARRAY_EQ(buf1, buf2, 3);
return EC_SUCCESS;
}
static int test_queue6_multiple_units_add(void)
{
char buf1[5] = {1, 2, 3, 4, 5};
char buf2[5];
queue_reset(&test_queue6);
TEST_ASSERT(queue_has_space(&test_queue6, 5));
queue_add_units(&test_queue6, buf1, 5);
LOOP_DEQUE(test_queue6, buf2, 5);
TEST_ASSERT_ARRAY_EQ(buf1, buf2, 5);
return EC_SUCCESS;
}
static int test_queue6_removal(void)
{
char buf1[5] = {1, 2, 3, 4, 5};
char buf2[5];
queue_reset(&test_queue6);
queue_add_units(&test_queue6, buf1, 5);
/* 1, 2, 3, 4, 5 */
LOOP_DEQUE(test_queue6, buf2, 3);
TEST_ASSERT_ARRAY_EQ(buf1, buf2, 3);
/* 4, 5 */
queue_add_units(&test_queue6, buf1, 2);
/* 4, 5, 1, 2 */
TEST_ASSERT(queue_has_space(&test_queue6, 1));
TEST_ASSERT(!queue_has_space(&test_queue6, 2));
LOOP_DEQUE(test_queue6, buf2, 1);
TEST_ASSERT(buf2[0] == 4);
/* 5, 1, 2 */
queue_add_units(&test_queue6, buf1 + 2, 2);
/* 5, 1, 2, 3, 4 */
TEST_ASSERT(!queue_has_space(&test_queue6, 1));
LOOP_DEQUE(test_queue6, buf2, 1);
TEST_ASSERT(buf2[0] == 5);
LOOP_DEQUE(test_queue6, buf2, 4);
TEST_ASSERT_ARRAY_EQ(buf1, buf2, 4);
TEST_ASSERT(queue_is_empty(&test_queue6));
/* Empty */
queue_add_units(&test_queue6, buf1, 5);
LOOP_DEQUE(test_queue6, buf2, 5);
TEST_ASSERT_ARRAY_EQ(buf1, buf2, 5);
return EC_SUCCESS;
}
static int test_queue5_odd_even(void)
{
uint16_t buf1[3] = {1, 2, 3};
uint16_t buf2[3];
queue_reset(&test_queue5);
queue_add_units(&test_queue5, buf1, 1);
/* 1 */
TEST_ASSERT(!queue_has_space(&test_queue5, 2));
TEST_ASSERT(queue_has_space(&test_queue5, 1));
queue_add_units(&test_queue5, buf1 + 1, 1);
/* 1, 2 */
TEST_ASSERT(!queue_has_space(&test_queue5, 1));
LOOP_DEQUE(test_queue5, buf2, 2);
TEST_ASSERT_ARRAY_EQ(buf1, buf2, 2);
TEST_ASSERT(queue_is_empty(&test_queue5));
/* Empty */
TEST_ASSERT(!queue_has_space(&test_queue5, 3));
TEST_ASSERT(queue_has_space(&test_queue5, 2));
TEST_ASSERT(queue_has_space(&test_queue5, 1));
queue_add_units(&test_queue5, buf1 + 2, 1);
/* 3 */
LOOP_DEQUE(test_queue5, buf2, 1);
TEST_ASSERT(buf2[0] == 3);
TEST_ASSERT(queue_is_empty(&test_queue5));
return EC_SUCCESS;
}
void run_test(void)
{
test_reset();
RUN_TEST(test_queue6_empty);
RUN_TEST(test_queue6_reset);
RUN_TEST(test_queue6_fifo);
RUN_TEST(test_queue6_multiple_units_add);
RUN_TEST(test_queue6_removal);
RUN_TEST(test_queue5_odd_even);
test_print_result();
}

17
test/queue.tasklist Normal file
View File

@@ -0,0 +1,17 @@
/* 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.
*/
/**
* List of enabled tasks in the priority order
*
* The first one has the lowest priority.
*
* For each task, use the macro TASK_TEST(n, r, d, s) where :
* 'n' in the name of the task
* 'r' in the main routine of the task
* 'd' in an opaque parameter passed to the routine at startup
* 's' is the stack size in bytes; must be a multiple of 8
*/
#define CONFIG_TEST_TASK_LIST /* No test task */