diff options
| author | Scott Shawcroft <[email protected]> | 2019-01-04 18:27:00 -0800 |
|---|---|---|
| committer | Scott Shawcroft <[email protected]> | 2019-01-04 18:27:00 -0800 |
| commit | 048fc9694c100715cdb4003522c8dacc227b5ace (patch) | |
| tree | 95cb7db17d7f14df90039a35d22e65ee8bc3d211 /tests | |
| parent | a3173b8de48b37e802d4de45821d2efbf54b1652 (diff) | |
Split out txbuf and add unit test.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/common/Makefile | 91 | ||||
| -rw-r--r-- | tests/common/build/results/test_txbuf.txt | 8 | ||||
| -rw-r--r-- | tests/common/test/test_txbuf.c | 145 | ||||
| m--------- | tests/vendor/unity | 0 |
4 files changed, 244 insertions, 0 deletions
diff --git a/tests/common/Makefile b/tests/common/Makefile new file mode 100644 index 000000000..2810fbae0 --- /dev/null +++ b/tests/common/Makefile @@ -0,0 +1,91 @@ +ifeq ($(OS),Windows_NT) + ifeq ($(shell uname -s),) # not in a bash-like shell + CLEANUP = del /F /Q + MKDIR = mkdir + else # in a bash-like shell, like msys + CLEANUP = rm -f + MKDIR = mkdir -p + endif + TARGET_EXTENSION=.exe +else + CLEANUP = rm -f + MKDIR = mkdir -p + TARGET_EXTENSION=out +endif + +.PHONY: clean +.PHONY: test + +PATHU = ../vendor/unity/src/ +PATHS = ../../src/ +PATHT = test/ +PATHB = build/ +PATHD = build/depends/ +PATHO = build/objs/ +PATHR = build/results/ + +BUILD_PATHS = $(PATHB) $(PATHD) $(PATHO) $(PATHR) + +SRCT = $(wildcard $(PATHT)*.c) + +$(info $(SRCT)) + +COMPILE=gcc -c +LINK=gcc +DEPEND=gcc -MM -MG -MF +CFLAGS=-I. -I$(PATHU) -I$(PATHS) -DTEST + +RESULTS = $(patsubst $(PATHT)test%.c,$(PATHR)test%.txt,$(SRCT) ) + +PASSED = `grep -s PASS $(PATHR)*.txt` +FAIL = `grep -s FAIL $(PATHR)*.txt` +IGNORE = `grep -s IGNORE $(PATHR)*.txt` + +test: $(BUILD_PATHS) $(RESULTS) + @echo "-----------------------\nIGNORES:\n-----------------------" + @echo "$(IGNORE)" + @echo "-----------------------\nFAILURES:\n-----------------------" + @echo "$(FAIL)" + @echo "-----------------------\nPASSED:\n-----------------------" + @echo "$(PASSED)" + @echo "\nDONE" + +$(PATHR)%.txt: $(PATHB)%.$(TARGET_EXTENSION) + -./$< > $@ 2>&1 + +$(PATHB)test_%.$(TARGET_EXTENSION): $(PATHO)test_%.o $(PATHO)tusb_%.o $(PATHU)unity.o #$(PATHD)test%.d + $(LINK) -o $@ $^ + +$(PATHO)%.o:: $(PATHT)%.c + $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHS)common/%.c + $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h + $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHD)%.d:: $(PATHT)%.c + $(DEPEND) $@ $< + +$(PATHB): + $(MKDIR) $(PATHB) + +$(PATHD): + $(MKDIR) $(PATHD) + +$(PATHO): + $(MKDIR) $(PATHO) + +$(PATHR): + $(MKDIR) $(PATHR) + +clean: + $(CLEANUP) $(PATHO)*.o + $(CLEANUP) $(PATHB)*.$(TARGET_EXTENSION) + $(CLEANUP) $(PATHR)*.txt + +.PRECIOUS: $(PATHB)test%.$(TARGET_EXTENSION) +.PRECIOUS: $(PATHD)%.d +.PRECIOUS: $(PATHO)%.o +.PRECIOUS: $(PATHR)%.txt diff --git a/tests/common/build/results/test_txbuf.txt b/tests/common/build/results/test_txbuf.txt new file mode 100644 index 000000000..a763a4717 --- /dev/null +++ b/tests/common/build/results/test_txbuf.txt @@ -0,0 +1,8 @@ +test/test_txbuf.c:140:test_normal:PASS +test/test_txbuf.c:141:test_nearly_full:PASS +test/test_txbuf.c:142:test_wrap_around:PASS +test/test_txbuf.c:143:test_wrap_around_too_much:PASS + +----------------------- +4 Tests 0 Failures 0 Ignored +OK diff --git a/tests/common/test/test_txbuf.c b/tests/common/test/test_txbuf.c new file mode 100644 index 000000000..8111c4715 --- /dev/null +++ b/tests/common/test/test_txbuf.c @@ -0,0 +1,145 @@ +/**************************************************************************/ +/*! + @file test_fifo.c + @author Scott Shawcroft + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2019, Scott Shawcroft + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +#include "unity.h" +#include "common/tusb_txbuf.h" + +uint32_t xfer_count = 0; +uint32_t freshly_transmitted = 0; + +bool mock_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes) { + TEST_ASSERT_EQUAL_INT_MESSAGE(0, ((size_t) buffer) % 4, "Transferred buffer not word aligned."); + xfer_count++; + freshly_transmitted += total_bytes; + return true; +} + +tu_txbuf_t txbuf; +__attribute__((aligned(4))) uint8_t buffer[16]; + +uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; + +void setUp(void) { + tu_txbuf_config(&txbuf, buffer, 16, mock_xfer); + tu_txbuf_set_ep_addr(&txbuf, 1); +} + +void tearDown(void) { + xfer_count = 0; + freshly_transmitted = 0; +} + +void xfer_callback(void) { + uint32_t previously_transmitted = freshly_transmitted; + freshly_transmitted = 0; + + tu_tx_buf_transmit_done_cb(&txbuf, previously_transmitted); +} + +// Test that we transfer immediately at the start and that the pointer is rounded to a word. +void test_normal(void) { + uint16_t written = tu_txbuf_write_n(&txbuf, data, 4); + TEST_ASSERT_EQUAL_INT(4, written); + TEST_ASSERT_EQUAL_INT(1, xfer_count); + xfer_callback(); + TEST_ASSERT_EQUAL_INT(1, xfer_count); +} + +// Test that we only accept the data we have room for. +void test_nearly_full(void) { + uint16_t written = tu_txbuf_write_n(&txbuf, data, 11); + TEST_ASSERT_EQUAL_INT(11, written); + TEST_ASSERT_EQUAL_INT(1, xfer_count); + + written = tu_txbuf_write_n(&txbuf, data, 11); + // We only have space for 4 more bytes because 11 + padding are being written. + TEST_ASSERT_EQUAL_INT(4, written); + + // Callback triggers a second write of remaining data. + xfer_callback(); + TEST_ASSERT_EQUAL_INT(2, xfer_count); + TEST_ASSERT_EQUAL_INT(4, freshly_transmitted); +} + +// Test that we only accept the data even when we have to wrap around. +void test_wrap_around(void) { + uint16_t written = tu_txbuf_write_n(&txbuf, data, 11); + TEST_ASSERT_EQUAL_INT(11, written); + TEST_ASSERT_EQUAL_INT(1, xfer_count); + xfer_callback(); + TEST_ASSERT_EQUAL_INT(1, xfer_count); + + written = tu_txbuf_write_n(&txbuf, data, 11); + // We can queue all 11 bytes but they are split. + TEST_ASSERT_EQUAL_INT(11, written); + // Four immediately and seven more after the callback. + TEST_ASSERT_EQUAL_INT(2, xfer_count); + TEST_ASSERT_EQUAL_INT(4, freshly_transmitted); + xfer_callback(); + TEST_ASSERT_EQUAL_INT(7, freshly_transmitted); + TEST_ASSERT_EQUAL_INT(3, xfer_count); +} + +// Test that we only accept the data even when we have to wrap around. +void test_wrap_around_too_much(void) { + uint16_t written = tu_txbuf_write_n(&txbuf, data, 11); + TEST_ASSERT_EQUAL_INT(11, written); + TEST_ASSERT_EQUAL_INT(1, xfer_count); + xfer_callback(); + TEST_ASSERT_EQUAL_INT(1, xfer_count); + + written = tu_txbuf_write_n(&txbuf, data, 17); + // We can queue 16 of 17 bytes but they are split. + TEST_ASSERT_EQUAL_INT(16, written); + // Four immediately and 12 more after the callback. + TEST_ASSERT_EQUAL_INT(2, xfer_count); + TEST_ASSERT_EQUAL_INT(4, freshly_transmitted); + xfer_callback(); + TEST_ASSERT_EQUAL_INT(12, freshly_transmitted); + TEST_ASSERT_EQUAL_INT(3, xfer_count); +} + +int main(void) +{ +UNITY_BEGIN(); +RUN_TEST(test_normal); +RUN_TEST(test_nearly_full); +RUN_TEST(test_wrap_around); +RUN_TEST(test_wrap_around_too_much); +return UNITY_END(); +} diff --git a/tests/vendor/unity b/tests/vendor/unity new file mode 160000 +Subproject a2849843654524194b72567b198c25d1c1dfead |
