source: rtems/testsuites/libtests/ttest01/init.c @ cbfc3415

5
Last change on this file since cbfc3415 was cbfc3415, checked in by Sebastian Huber <sebastian.huber@…>, on 03/14/19 at 07:20:54

ttest01: New test

This is an example test using the RTEMS Test Framework. It tests also
the framework itself.

Add T_FILE_NAME command line define to get rid of the full file path.
This is important to reduce the read-only data of test files and make
them build system independent.

Update #3199.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2018, 2019 embedded brains GmbH
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <t.h>
29
30#include <sys/time.h>
31#include <string.h>
32
33#include <rtems.h>
34#include <rtems/bspIo.h>
35
36#include "t-self-test.h"
37
38#include <tmacros.h>
39
40const char rtems_test_name[] = "TTEST 1";
41
42#define test_assert(e) (e) ? (void)0 : test_failed(__LINE__, #e)
43
44RTEMS_LINKER_ROSET(t_self_test, const char *);
45
46typedef struct {
47        const char *c;
48        size_t case_begin_count;
49        size_t case_end_count;
50} test_context;
51
52static test_context test_instance;
53
54static void
55test_failed(int line, const char *e)
56{
57        printk("FAILED:%i:%s\n", line, e);
58        rtems_test_exit(1);
59}
60
61static void
62test_putchar(int c, void *arg)
63{
64        test_context *ctx;
65
66        ctx = arg;
67
68        if (c != '\r' && ctx->c != NULL) {
69                test_assert(*ctx->c == c);
70                ++ctx->c;
71        }
72
73        rtems_putc((char)c);
74}
75
76static void
77case_early(const char *name)
78{
79        test_context *ctx;
80        const char **item;
81        ssize_t n;
82
83        ctx = &test_instance;
84        ++ctx->case_begin_count;
85        n = strlen(name);
86
87        RTEMS_LINKER_SET_FOREACH(t_self_test, item) {
88                const char *to;
89
90                to = *item;
91
92                if (strncmp(name, to, n) == 0 && to[n] == ':') {
93                        ctx->c = to + n + 1;
94                        return;
95                }
96        }
97
98        test_assert(0);
99}
100
101static void
102case_late(const char *name)
103{
104        test_context *ctx;
105
106        ctx = &test_instance;
107        ++ctx->case_end_count;
108        test_assert(ctx->c != NULL);
109        test_assert(*ctx->c == '\0');
110        ctx->c = NULL;
111}
112
113static void
114test_action(T_event event, const char *name)
115{
116        (void)name;
117
118        switch (event) {
119        case T_EVENT_CASE_EARLY:
120                case_early(name);
121                break;
122        case T_EVENT_CASE_LATE:
123                case_late(name);
124                break;
125        default:
126                break;
127        };
128}
129
130static Atomic_Uint counter = ATOMIC_INITIALIZER_UINT(0);
131
132static T_time
133now(void)
134{
135        T_time t;
136
137        t = _Atomic_Fetch_add_uint(&counter, 1, ATOMIC_ORDER_RELAXED);
138        return t * SBT_1MS;
139}
140
141static const T_action actions[] = {
142        T_report_hash_sha256,
143        test_action
144};
145
146static const T_config config = {
147        .name = "ttest01",
148        .putchar = test_putchar,
149        .putchar_arg = &test_instance,
150        .verbosity = T_VERBOSE,
151        .now = now,
152        .action_count = T_ARRAY_SIZE(actions),
153        .actions = actions
154};
155
156static void
157Init(rtems_task_argument arg)
158{
159        test_context *ctx;
160        int exit_code;
161        size_t case_count;
162
163        (void)arg;
164        TEST_BEGIN();
165        ctx = &test_instance;
166        test_assert(!T_is_runner());
167        T_register();
168        test_assert(!T_is_runner());
169        exit_code = T_main(&config);
170        test_assert(exit_code == 1);
171        test_assert(!T_is_runner());
172        case_count = RTEMS_LINKER_SET_ITEM_COUNT(t_self_test);
173        test_assert(ctx->case_begin_count == case_count);
174        test_assert(ctx->case_end_count == case_count);
175        TEST_END();
176        rtems_test_exit(0);
177}
178
179#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
180
181#define CONFIGURE_MAXIMUM_TASKS 1
182
183#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
184
185#define CONFIGURE_INIT
186
187#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.