summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorVic Yang <victoryang@chromium.org>2013-06-09 19:02:02 +0800
committerChromeBot <chrome-bot@google.com>2013-06-10 01:48:46 -0700
commitcbee574e6487e2f7dd12c4ed5f2bef8ae7c4b9cb (patch)
treeb3423b55ea71c456820a4cb0487ae33e7ed2587a /test
parenta9541220dc92ba70d9828c6000c89dc8287bc7a1 (diff)
downloadchrome-ec-cbee574e6487e2f7dd12c4ed5f2bef8ae7c4b9cb.tar.gz
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>
Diffstat (limited to 'test')
-rw-r--r--test/build.mk3
-rw-r--r--test/queue.c166
-rw-r--r--test/queue.tasklist17
3 files changed, 185 insertions, 1 deletions
diff --git a/test/build.mk b/test/build.mk
index 90f775de0a..9c9b8030bc 100644
--- a/test/build.mk
+++ b/test/build.mk
@@ -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
diff --git a/test/queue.c b/test/queue.c
new file mode 100644
index 0000000000..9fb886c8a3
--- /dev/null
+++ b/test/queue.c
@@ -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();
+}
diff --git a/test/queue.tasklist b/test/queue.tasklist
new file mode 100644
index 0000000000..26cfc53453
--- /dev/null
+++ b/test/queue.tasklist
@@ -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 */