source: rtems/testsuites/libtests/ttest01/init.c @ 77ac1519

5
Last change on this file since 77ac1519 was 77ac1519, checked in by Sebastian Huber <sebastian.huber@…>, on 12/20/19 at 09:55:36

libtest: Use test configuration in T_now()

Use the user provided now handler of the test configuration to get the
time in T_now().

  • Property mode set to 100644
File size: 6.4 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 enum {
47        CENSOR_PASS_THROUGH,
48        CENSOR_DISCARD
49} censor_state;
50
51typedef struct {
52        const char *c;
53        size_t case_begin_count;
54        size_t case_end_count;
55        T_putchar putchar;
56        void *putchar_arg;
57        const char *censor_c;
58        censor_state censor_state;
59} test_context;
60
61static test_context test_instance;
62
63static void
64test_failed(int line, const char *e)
65{
66        printk("FAILED:%i:%s\n", line, e);
67        rtems_test_exit(1);
68}
69
70static void
71test_putchar(int c, void *arg)
72{
73        test_context *ctx;
74
75        ctx = arg;
76
77        if (c != '\r' && ctx->c != NULL) {
78                test_assert(*ctx->c == c);
79                ++ctx->c;
80        }
81
82        rtems_putc((char)c);
83}
84
85static void
86case_early(const char *name)
87{
88        test_context *ctx;
89        const char **item;
90        ssize_t n;
91
92        ctx = &test_instance;
93        ++ctx->case_begin_count;
94        n = strlen(name);
95
96        RTEMS_LINKER_SET_FOREACH(t_self_test, item) {
97                const char *to;
98
99                to = *item;
100
101                if (strncmp(name, to, n) == 0 && to[n] == ':') {
102                        ctx->c = to + n + 1;
103                        return;
104                }
105        }
106
107        test_assert(0);
108}
109
110static void
111case_late(const char *name)
112{
113        test_context *ctx;
114
115        ctx = &test_instance;
116        ++ctx->case_end_count;
117        test_assert(ctx->c != NULL);
118        test_assert(*ctx->c == '\0');
119        ctx->c = NULL;
120}
121
122static const char censored_init[] = "A:ttest01\n"
123"S:Platform:RTEMS\n"
124"S:Compiler:*"
125"S:Version:*"
126"S:BSP:*"
127"S:RTEMS_DEBUG:*"
128"S:RTEMS_MULTIPROCESSING:*"
129"S:RTEMS_POSIX_API:*"
130"S:RTEMS_PROFILING:*"
131"S:RTEMS_SMP:*";
132
133static void
134censor_putchar(int c, void *arg)
135{
136        test_context *ctx;
137
138        ctx = arg;
139
140        if (*ctx->censor_c == '\0') {
141                T_putchar discard_putchar;
142                void *discard_putchar_arg;
143
144                (*ctx->putchar)(c, ctx->putchar_arg);
145                T_set_putchar(ctx->putchar, ctx->putchar_arg, &discard_putchar,
146                   &discard_putchar_arg);
147                return;
148        }
149
150        switch (ctx->censor_state) {
151        case CENSOR_PASS_THROUGH:
152                if (*ctx->censor_c == '*') {
153                        (*ctx->putchar)('*', ctx->putchar_arg);
154                        ctx->censor_state = CENSOR_DISCARD;
155                } else if (c == *ctx->censor_c) {
156                        (*ctx->putchar)(c, ctx->putchar_arg);
157                        ++ctx->censor_c;
158                }
159                break;
160        case CENSOR_DISCARD:
161                if (c == '\n') {
162                        (*ctx->putchar)(c, ctx->putchar_arg);
163                        ctx->censor_state = CENSOR_PASS_THROUGH;
164                        ++ctx->censor_c;
165                }
166                break;
167        }
168}
169
170static void
171run_initialize(void)
172{
173        test_context *ctx;
174
175        ctx = &test_instance;
176        ctx->censor_c = censored_init;
177        T_set_putchar(censor_putchar, ctx, &ctx->putchar, &ctx->putchar_arg);
178}
179
180static const char expected_final[] = "Z:ttest01:C:341:N:1316:F:790:D:0.685999\n"
181"Y:ReportHash:SHA256:cb5ba027ade5b907d9e988776e393835f34a76cc2381d67bb9db44d986a3fecf\n";
182
183static void
184run_finalize(void)
185{
186        test_context *ctx;
187
188        ctx = &test_instance;
189        ctx->c = expected_final;
190}
191
192static void
193test_action(T_event event, const char *name)
194{
195        (void)name;
196
197        switch (event) {
198        case T_EVENT_CASE_EARLY:
199                case_early(name);
200                break;
201        case T_EVENT_CASE_LATE:
202                case_late(name);
203                break;
204        case T_EVENT_RUN_INITIALIZE_EARLY:
205                run_initialize();
206                break;
207        case T_EVENT_RUN_FINALIZE_EARLY:
208                run_finalize();
209                break;
210        default:
211                break;
212        };
213}
214
215static Atomic_Uint counter = ATOMIC_INITIALIZER_UINT(0);
216
217static T_time
218now(void)
219{
220        T_time t;
221
222        t = _Atomic_Fetch_add_uint(&counter, 1, ATOMIC_ORDER_RELAXED);
223        return t * SBT_1MS;
224}
225
226static char buffer[512];
227
228static const T_action actions[] = {
229        T_report_hash_sha256,
230        test_action,
231        T_check_file_descriptors,
232        T_check_rtems_barriers,
233        T_check_rtems_extensions,
234        T_check_rtems_message_queues,
235        T_check_rtems_partitions,
236        T_check_rtems_periods,
237        T_check_rtems_regions,
238        T_check_rtems_semaphores,
239        T_check_rtems_tasks,
240        T_check_rtems_timers,
241        T_check_posix_keys
242};
243
244static const T_config config = {
245        .name = "ttest01",
246        .buf = buffer,
247        .buf_size = sizeof(buffer),
248        .putchar = test_putchar,
249        .putchar_arg = &test_instance,
250        .verbosity = T_VERBOSE,
251        .now = now,
252        .action_count = T_ARRAY_SIZE(actions),
253        .actions = actions
254};
255
256static void
257Init(rtems_task_argument arg)
258{
259        test_context *ctx;
260        int exit_code;
261        size_t case_count;
262
263        (void)arg;
264        TEST_BEGIN();
265        ctx = &test_instance;
266        test_assert(!T_is_runner());
267        T_register();
268        test_assert(!T_is_runner());
269        exit_code = T_main(&config);
270        test_assert(exit_code == 1);
271        test_assert(!T_is_runner());
272        case_count = RTEMS_LINKER_SET_ITEM_COUNT(t_self_test);
273        test_assert(ctx->case_begin_count == case_count);
274        test_assert(ctx->case_end_count == case_count);
275        TEST_END();
276        rtems_test_exit(0);
277}
278
279#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
280
281#define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
282
283#define CONFIGURE_MAXIMUM_BARRIERS 1
284#define CONFIGURE_MAXIMUM_MESSAGE_QUEUES 1
285#define CONFIGURE_MAXIMUM_PARTITIONS 1
286#define CONFIGURE_MAXIMUM_PERIODS 1
287#define CONFIGURE_MAXIMUM_REGIONS 1
288#define CONFIGURE_MAXIMUM_SEMAPHORES 1
289#define CONFIGURE_MAXIMUM_TASKS 2
290#define CONFIGURE_MAXIMUM_TIMERS 1
291#define CONFIGURE_MAXIMUM_USER_EXTENSIONS 1
292
293#define CONFIGURE_MAXIMUM_POSIX_KEYS 1
294
295#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
296
297#define CONFIGURE_INIT
298
299#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.