123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- /*
- ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- #include "hal.h"
- #include "oslib_test_root.h"
- /**
- * @file oslib_test_sequence_002.c
- * @brief Test Sequence 002 code.
- *
- * @page oslib_test_sequence_002 [2] Pipes
- *
- * File: @ref oslib_test_sequence_002.c
- *
- * <h2>Description</h2>
- * This sequence tests the ChibiOS library functionalities related to
- * pipes.
- *
- * <h2>Conditions</h2>
- * This sequence is only executed if the following preprocessor condition
- * evaluates to true:
- * - CH_CFG_USE_PIPES
- * .
- *
- * <h2>Test Cases</h2>
- * - @subpage oslib_test_002_001
- * - @subpage oslib_test_002_002
- * .
- */
- #if (CH_CFG_USE_PIPES) || defined(__DOXYGEN__)
- /****************************************************************************
- * Shared code.
- ****************************************************************************/
- #include <string.h>
- #define PIPE_SIZE 16
- static uint8_t buffer[PIPE_SIZE];
- static PIPE_DECL(pipe1, buffer, PIPE_SIZE);
- static const uint8_t pipe_pattern[] = "0123456789ABCDEF";
- /****************************************************************************
- * Test cases.
- ****************************************************************************/
- /**
- * @page oslib_test_002_001 [2.1] Pipes normal API, non-blocking tests
- *
- * <h2>Description</h2>
- * The pipe functionality is tested by loading and emptying it, all
- * conditions are tested.
- *
- * <h2>Test Steps</h2>
- * - [2.1.1] Resetting pipe.
- * - [2.1.2] Writing data, must fail.
- * - [2.1.3] Reading data, must fail.
- * - [2.1.4] Reactivating pipe.
- * - [2.1.5] Filling whole pipe.
- * - [2.1.6] Emptying pipe.
- * - [2.1.7] Small write.
- * - [2.1.8] Filling remaining space.
- * - [2.1.9] Small Read.
- * - [2.1.10] Reading remaining data.
- * - [2.1.11] Small Write.
- * - [2.1.12] Small Read.
- * - [2.1.13] Write wrapping buffer boundary.
- * - [2.1.14] Read wrapping buffer boundary.
- * .
- */
- static void oslib_test_002_001_setup(void) {
- chPipeObjectInit(&pipe1, buffer, PIPE_SIZE);
- }
- static void oslib_test_002_001_execute(void) {
- /* [2.1.1] Resetting pipe.*/
- test_set_step(1);
- {
- chPipeReset(&pipe1);
- test_assert((pipe1.rdptr == pipe1.buffer) &&
- (pipe1.wrptr == pipe1.buffer) &&
- (pipe1.cnt == 0),
- "invalid pipe state");
- }
- /* [2.1.2] Writing data, must fail.*/
- test_set_step(2);
- {
- size_t n;
- n = chPipeWriteTimeout(&pipe1, pipe_pattern, PIPE_SIZE, TIME_IMMEDIATE);
- test_assert(n == 0, "not reset");
- test_assert((pipe1.rdptr == pipe1.buffer) &&
- (pipe1.wrptr == pipe1.buffer) &&
- (pipe1.cnt == 0),
- "invalid pipe state");
- }
- /* [2.1.3] Reading data, must fail.*/
- test_set_step(3);
- {
- size_t n;
- uint8_t buf[PIPE_SIZE];
- n = chPipeReadTimeout(&pipe1, buf, PIPE_SIZE, TIME_IMMEDIATE);
- test_assert(n == 0, "not reset");
- test_assert((pipe1.rdptr == pipe1.buffer) &&
- (pipe1.wrptr == pipe1.buffer) &&
- (pipe1.cnt == 0),
- "invalid pipe state");
- }
- /* [2.1.4] Reactivating pipe.*/
- test_set_step(4);
- {
- chPipeResume(&pipe1);
- test_assert((pipe1.rdptr == pipe1.buffer) &&
- (pipe1.wrptr == pipe1.buffer) &&
- (pipe1.cnt == 0),
- "invalid pipe state");
- }
- /* [2.1.5] Filling whole pipe.*/
- test_set_step(5);
- {
- size_t n;
- n = chPipeWriteTimeout(&pipe1, pipe_pattern, PIPE_SIZE, TIME_IMMEDIATE);
- test_assert(n == PIPE_SIZE, "wrong size");
- test_assert((pipe1.rdptr == pipe1.buffer) &&
- (pipe1.wrptr == pipe1.buffer) &&
- (pipe1.cnt == PIPE_SIZE),
- "invalid pipe state");
- }
- /* [2.1.6] Emptying pipe.*/
- test_set_step(6);
- {
- size_t n;
- uint8_t buf[PIPE_SIZE];
- n = chPipeReadTimeout(&pipe1, buf, PIPE_SIZE, TIME_IMMEDIATE);
- test_assert(n == PIPE_SIZE, "wrong size");
- test_assert((pipe1.rdptr == pipe1.buffer) &&
- (pipe1.wrptr == pipe1.buffer) &&
- (pipe1.cnt == 0),
- "invalid pipe state");
- test_assert(memcmp(pipe_pattern, buf, PIPE_SIZE) == 0, "content mismatch");
- }
- /* [2.1.7] Small write.*/
- test_set_step(7);
- {
- size_t n;
- n = chPipeWriteTimeout(&pipe1, pipe_pattern, 4, TIME_IMMEDIATE);
- test_assert(n == 4, "wrong size");
- test_assert((pipe1.rdptr != pipe1.wrptr) &&
- (pipe1.rdptr == pipe1.buffer) &&
- (pipe1.cnt == 4),
- "invalid pipe state");
- }
- /* [2.1.8] Filling remaining space.*/
- test_set_step(8);
- {
- size_t n;
- n = chPipeWriteTimeout(&pipe1, pipe_pattern, PIPE_SIZE - 4, TIME_IMMEDIATE);
- test_assert(n == PIPE_SIZE - 4, "wrong size");
- test_assert((pipe1.rdptr == pipe1.buffer) &&
- (pipe1.wrptr == pipe1.buffer) &&
- (pipe1.cnt == PIPE_SIZE),
- "invalid pipe state");
- }
- /* [2.1.9] Small Read.*/
- test_set_step(9);
- {
- size_t n;
- uint8_t buf[PIPE_SIZE];
- n = chPipeReadTimeout(&pipe1, buf, 4, TIME_IMMEDIATE);
- test_assert(n == 4, "wrong size");
- test_assert((pipe1.rdptr != pipe1.buffer) &&
- (pipe1.wrptr == pipe1.buffer) &&
- (pipe1.cnt == PIPE_SIZE - 4),
- "invalid pipe state");
- test_assert(memcmp(pipe_pattern, buf, 4) == 0, "content mismatch");
- }
- /* [2.1.10] Reading remaining data.*/
- test_set_step(10);
- {
- size_t n;
- uint8_t buf[PIPE_SIZE];
- n = chPipeReadTimeout(&pipe1, buf, PIPE_SIZE - 4, TIME_IMMEDIATE);
- test_assert(n == PIPE_SIZE - 4, "wrong size");
- test_assert((pipe1.rdptr == pipe1.buffer) &&
- (pipe1.wrptr == pipe1.buffer) &&
- (pipe1.cnt == 0),
- "invalid pipe state");
- test_assert(memcmp(pipe_pattern, buf, PIPE_SIZE - 4) == 0, "content mismatch");
- }
- /* [2.1.11] Small Write.*/
- test_set_step(11);
- {
- size_t n;
- n = chPipeWriteTimeout(&pipe1, pipe_pattern, 5, TIME_IMMEDIATE);
- test_assert(n == 5, "wrong size");
- test_assert((pipe1.rdptr != pipe1.wrptr) &&
- (pipe1.rdptr == pipe1.buffer) &&
- (pipe1.cnt == 5),
- "invalid pipe state");
- }
- /* [2.1.12] Small Read.*/
- test_set_step(12);
- {
- size_t n;
- uint8_t buf[PIPE_SIZE];
- n = chPipeReadTimeout(&pipe1, buf, 5, TIME_IMMEDIATE);
- test_assert(n == 5, "wrong size");
- test_assert((pipe1.rdptr == pipe1.wrptr) &&
- (pipe1.wrptr != pipe1.buffer) &&
- (pipe1.cnt == 0),
- "invalid pipe state");
- test_assert(memcmp(pipe_pattern, buf, 5) == 0, "content mismatch");
- }
- /* [2.1.13] Write wrapping buffer boundary.*/
- test_set_step(13);
- {
- size_t n;
- n = chPipeWriteTimeout(&pipe1, pipe_pattern, PIPE_SIZE, TIME_IMMEDIATE);
- test_assert(n == PIPE_SIZE, "wrong size");
- test_assert((pipe1.rdptr == pipe1.wrptr) &&
- (pipe1.wrptr != pipe1.buffer) &&
- (pipe1.cnt == PIPE_SIZE),
- "invalid pipe state");
- }
- /* [2.1.14] Read wrapping buffer boundary.*/
- test_set_step(14);
- {
- size_t n;
- uint8_t buf[PIPE_SIZE];
- n = chPipeReadTimeout(&pipe1, buf, PIPE_SIZE, TIME_IMMEDIATE);
- test_assert(n == PIPE_SIZE, "wrong size");
- test_assert((pipe1.rdptr == pipe1.wrptr) &&
- (pipe1.wrptr != pipe1.buffer) &&
- (pipe1.cnt == 0),
- "invalid pipe state");
- test_assert(memcmp(pipe_pattern, buf, PIPE_SIZE) == 0, "content mismatch");
- }
- }
- static const testcase_t oslib_test_002_001 = {
- "Pipes normal API, non-blocking tests",
- oslib_test_002_001_setup,
- NULL,
- oslib_test_002_001_execute
- };
- /**
- * @page oslib_test_002_002 [2.2] Pipe timeouts
- *
- * <h2>Description</h2>
- * The pipe API is tested for timeouts.
- *
- * <h2>Test Steps</h2>
- * - [2.2.1] Reading while pipe is empty.
- * - [2.2.2] Writing a string larger than pipe buffer.
- * .
- */
- static void oslib_test_002_002_setup(void) {
- chPipeObjectInit(&pipe1, buffer, PIPE_SIZE / 2);
- }
- static void oslib_test_002_002_execute(void) {
- /* [2.2.1] Reading while pipe is empty.*/
- test_set_step(1);
- {
- size_t n;
- uint8_t buf[PIPE_SIZE];
- n = chPipeReadTimeout(&pipe1, buf, PIPE_SIZE, TIME_IMMEDIATE);
- test_assert(n == 0, "wrong size");
- test_assert((pipe1.rdptr == pipe1.buffer) &&
- (pipe1.wrptr == pipe1.buffer) &&
- (pipe1.cnt == 0),
- "invalid pipe state");
- }
- /* [2.2.2] Writing a string larger than pipe buffer.*/
- test_set_step(2);
- {
- size_t n;
- n = chPipeWriteTimeout(&pipe1, pipe_pattern, PIPE_SIZE, TIME_IMMEDIATE);
- test_assert(n == PIPE_SIZE / 2, "wrong size");
- test_assert((pipe1.rdptr == pipe1.wrptr) &&
- (pipe1.wrptr == pipe1.buffer) &&
- (pipe1.cnt == PIPE_SIZE / 2),
- "invalid pipe state");
- }
- }
- static const testcase_t oslib_test_002_002 = {
- "Pipe timeouts",
- oslib_test_002_002_setup,
- NULL,
- oslib_test_002_002_execute
- };
- /****************************************************************************
- * Exported data.
- ****************************************************************************/
- /**
- * @brief Array of test cases.
- */
- const testcase_t * const oslib_test_sequence_002_array[] = {
- &oslib_test_002_001,
- &oslib_test_002_002,
- NULL
- };
- /**
- * @brief Pipes.
- */
- const testsequence_t oslib_test_sequence_002 = {
- "Pipes",
- oslib_test_sequence_002_array
- };
- #endif /* CH_CFG_USE_PIPES */
|