/* ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, 2011,2012 Giovanni Di Sirio. This file is part of ChibiOS/RT. ChibiOS/RT is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. ChibiOS/RT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ /** * @file test.h * @brief Tests support header. * * @addtogroup test * @{ */ #ifndef _TEST_H_ #define _TEST_H_ /** * @brief Delay inserted between test cases. */ #if !defined(DELAY_BETWEEN_TESTS) || defined(__DOXYGEN__) #define DELAY_BETWEEN_TESTS 200 #endif /** * @brief If @p TRUE then benchmarks are not included. */ #if !defined(TEST_NO_BENCHMARKS) || defined(__DOXYGEN__) #define TEST_NO_BENCHMARKS FALSE #endif #define MAX_THREADS 5 #define MAX_TOKENS 16 #if defined(CH_ARCHITECTURE_AVR) || defined(CH_ARCHITECTURE_MSP430) #define THREADS_STACK_SIZE 48 #elif defined(CH_ARCHITECTURE_STM8) #define THREADS_STACK_SIZE 64 #elif defined(CH_ARCHITECTURE_SIMIA32) #define THREADS_STACK_SIZE 512 #else #define THREADS_STACK_SIZE 128 #endif #define WA_SIZE THD_WA_SIZE(THREADS_STACK_SIZE) /** * @brief Structure representing a test case. */ struct testcase { const char *name; /**< @brief Test case name. */ void (*setup)(void); /**< @brief Test case preparation function. */ void (*teardown)(void); /**< @brief Test case clean up function. */ void (*execute)(void); /**< @brief Test case execution function. */ }; #ifndef __DOXYGEN__ union test_buffers { struct { WORKING_AREA(T0, THREADS_STACK_SIZE); WORKING_AREA(T1, THREADS_STACK_SIZE); WORKING_AREA(T2, THREADS_STACK_SIZE); WORKING_AREA(T3, THREADS_STACK_SIZE); WORKING_AREA(T4, THREADS_STACK_SIZE); } wa; uint8_t buffer[WA_SIZE * 5]; }; #endif #ifdef __cplusplus extern "C" { #endif msg_t TestThread(void *p); void test_printn(uint32_t n); void test_print(const char *msgp); void test_println(const char *msgp); void test_emit_token(char token); bool_t _test_fail(unsigned point); bool_t _test_assert(unsigned point, bool_t condition); bool_t _test_assert_sequence(unsigned point, char *expected); bool_t _test_assert_time_window(unsigned point, systime_t start, systime_t end); void test_terminate_threads(void); void test_wait_threads(void); systime_t test_wait_tick(void); void test_start_timer(unsigned ms); #if CH_DBG_THREADS_PROFILING void test_cpu_pulse(unsigned duration); #endif #if defined(WIN32) void ChkIntSources(void); #endif #ifdef __cplusplus } #endif /** * @brief Test failure enforcement. */ #define test_fail(point) { \ _test_fail(point); \ return; \ } /** * @brief Test assertion. * * @param[in] point numeric assertion identifier * @param[in] condition a boolean expression that must be verified to be true * @param[in] msg failure message */ #define test_assert(point, condition, msg) { \ if (_test_assert(point, condition)) \ return; \ } /** * @brief Test assertion with lock. * * @param[in] point numeric assertion identifier * @param[in] condition a boolean expression that must be verified to be true * @param[in] msg failure message */ #define test_assert_lock(point, condition, msg) { \ chSysLock(); \ if (_test_assert(point, condition)) { \ chSysUnlock(); \ return; \ } \ chSysUnlock(); \ } /** * @brief Test sequence assertion. * * @param[in] point numeric assertion identifier * @param[in] expected string to be matched with the tokens buffer */ #define test_assert_sequence(point, expected) { \ if (_test_assert_sequence(point, expected)) \ return; \ } /** * @brief Test time window assertion. * * @param[in] point numeric assertion identifier * @param[in] start initial time in the window (included) * @param[in] end final time in the window (not included) */ #define test_assert_time_window(point, start, end) { \ if (_test_assert_time_window(point, start, end)) \ return; \ } #if !defined(__DOXYGEN__) extern Thread *threads[MAX_THREADS]; extern union test_buffers test; extern void * ROMCONST wa[]; extern bool_t test_timer_done; #endif #endif /* _TEST_H_ */ /** @} */ 7 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/*
    ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <ch.h>

#include "test.h"

/**
 * @page test_mbox Mailboxes test
 *
 * <h2>Description</h2>
 * This module implements the test sequence for the @ref mailboxes subsystem.
 *
 * <h2>Objective</h2>
 * Objective of the test module is to cover 100% of the @ref mailboxes
 * subsystem code.<br>
 * Note that the @ref mailboxes subsystem depends on the @ref semaphores
 * subsystem that has to met its testing objectives as well.
 *
 * <h2>Preconditions</h2>
 * The module requires the following kernel options:
 * - @p CH_USE_MAILBOXES
 * .
 * In case some of the required options are not enabled then some or all tests
 * may be skipped.
 *
 * <h2>Test Cases</h2>
 * - @subpage test_mbox_001
 * .
 * @file testmbox.c
 * @brief Mailboxes test source file
 * @file testmbox.h
 * @brief Mailboxes header file
 */

#if CH_USE_MAILBOXES

#define ALLOWED_DELAY MS2ST(5)
#define MB_SIZE 5

/*
 * Note, the static initializers are not really required because the
 * variables are explicitly initialized in each test case. It is done in order
 * to test the macros.
 */
static MAILBOX_DECL(mb1, test.waT0, MB_SIZE);

/**
 * @page test_mbox_001 Queuing and timeouts
 *
 * <h2>Description</h2>
 * Messages are posted/fetched from a mailbox in carefully designed sequences
 * in order to stimulate all the possible code paths inside the mailbox.<br>
 * The test expects to find a consistent mailbox status after each operation.
 */

static char *mbox1_gettest(void) {

  return "Mailboxes, queuing and timeouts";
}

static void mbox1_setup(void) {

  chMBInit(&mb1, (msg_t *)test.waT0, MB_SIZE);
}

static void mbox1_execute(void) {
  msg_t msg1, msg2;
  unsigned i;

  /*
   * Testing initial space.
   */
  test_assert(1, chMBGetEmpty(&mb1) == MB_SIZE, "wrong size");

  /*
   * Testing enqueuing and backward circularity.
   */
  for (i = 0; i < MB_SIZE - 1; i++) {
    msg1 = chMBPost(&mb1, 'B' + i, TIME_INFINITE);
    test_assert(2, msg1 == RDY_OK, "wrong wake-up message");
  }
  msg1 = chMBPostAhead(&mb1, 'A', TIME_INFINITE);
  test_assert(3, msg1 == RDY_OK, "wrong wake-up message");

  /*
   * Testing post timeout.
   */
  msg1 = chMBPost(&mb1, 'X', 1);
  test_assert(4, msg1 == RDY_TIMEOUT, "wrong wake-up message");

  /*
   * Testing final conditions.
   */
  test_assert(5, chMBGetEmpty(&mb1) == 0, "still empty");
  test_assert(6, chMBGetFull(&mb1) == MB_SIZE, "not full");
  test_assert(7, mb1.mb_rdptr == mb1.mb_wrptr, "pointers not aligned");

  /*
   * Testing dequeuing.
   */
  for (i = 0; i < MB_SIZE; i++) {
  msg1 = chMBFetch(&mb1, &msg2, TIME_INFINITE);
    test_assert(8, msg1 == RDY_OK, "wrong wake-up message");
    test_emit_token(msg2);
  }
  test_assert_sequence(9, "ABCDE");

  /*
   * Testing buffer circularity.
   */
  msg1 = chMBPost(&mb1, 'B' + i, TIME_INFINITE);
  test_assert(10, msg1 == RDY_OK, "wrong wake-up message");
  msg1 = chMBFetch(&mb1, &msg2, TIME_INFINITE);
  test_assert(11, msg1 == RDY_OK, "wrong wake-up message");
  test_assert(12, mb1.mb_buffer == mb1.mb_wrptr, "write pointer not aligned to base");
  test_assert(13, mb1.mb_buffer == mb1.mb_rdptr, "read pointer not aligned to base");

  /*
   * Testing fetch timeout.
   */
  msg1 = chMBFetch(&mb1, &msg2, 1);
  test_assert(14, msg1 == RDY_TIMEOUT, "wrong wake-up message");

  /*
   * Testing final conditions.
   */
  test_assert(15, chMBGetEmpty(&mb1) == MB_SIZE, "not empty");
  test_assert(16, chMBGetFull(&mb1) == 0, "still full");
  test_assert(17, mb1.mb_rdptr == mb1.mb_wrptr, "pointers not aligned");

  /*
   * Testing reset.
   */
  chMBReset(&mb1);

  /*
   * Re-testing final conditions.
   */
  test_assert(18, chMBGetEmpty(&mb1) == MB_SIZE, "not empty");
  test_assert(19, chMBGetFull(&mb1) == 0, "still full");
  test_assert(20, mb1.mb_buffer == mb1.mb_wrptr, "write pointer not aligned to base");
  test_assert(21, mb1.mb_buffer == mb1.mb_rdptr, "read pointer not aligned to base");
}

const struct testcase testmbox1 = {
  mbox1_gettest,
  mbox1_setup,
  NULL,
  mbox1_execute
};

#endif /* CH_USE_MAILBOXES */

/*
 * Test sequence for mailboxes pattern.
 */
const struct testcase * const patternmbox[] = {
#if CH_USE_MAILBOXES
  &testmbox1,
#endif
  NULL
};