source: rtems-libbsd/testsuite/log01/test_main.c @ c991d65

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since c991d65 was c991d65, checked in by Sebastian Huber <sebastian.huber@…>, on 06/23/16 at 12:18:17

Update due to RTEMS printer API changes

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <machine/rtems-bsd-kernel-space.h>
33
34#include <sys/types.h>
35#include <sys/systm.h>
36#include <syslog.h>
37
38#include <assert.h>
39#include <errno.h>
40#include <stdarg.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
45#define TEST_NAME "LIBBSD LOG 1"
46
47typedef struct {
48        char a[99];
49        char b[99];
50        size_t i;
51        int expected_level;
52} test_context;
53
54static test_context test_instance;
55
56static void
57vprintf_putchar(int c, void *arg)
58{
59        test_context *ctx;
60
61        ctx = arg;
62        ctx->a[ctx->i] = (char) c;
63        ++ctx->i;
64}
65
66static int
67vprintf_handler(int level, const char *fmt, va_list ap)
68{
69        test_context *ctx;
70        int n;
71
72        ctx = &test_instance;
73
74        assert(level == ctx->expected_level);
75        n = (kvprintf(fmt, vprintf_putchar, &test_instance, 10, ap));
76        ctx->a[n] = '\0';
77        return (n);
78}
79
80static void
81reset(test_context *ctx, int expected_level)
82{
83
84        ctx->i = 0;
85        ctx->expected_level = expected_level;
86        memset(&ctx->a[0], '?', sizeof(ctx->a));
87        memset(&ctx->b[0], '?', sizeof(ctx->b));
88}
89
90static void
91check(test_context *ctx, const char *b)
92{
93
94        memcpy(&ctx->b[0], b, strlen(b) + 1);
95        assert(memcmp(&ctx->a[0], &ctx->b[0], sizeof(ctx->a)) == 0);
96}
97
98static void
99test_main(void)
100{
101        test_context *ctx;
102        int n;
103
104        ctx = &test_instance;
105
106        rtems_bsd_set_vprintf_handler(vprintf_handler);
107
108        reset(ctx, 0);
109        n = sprintf(&ctx->a[0], "%04i", 123);
110        assert(n == 4);
111        check(ctx, "0123");
112
113        reset(ctx, 0);
114        n = snprintf(&ctx->a[0], 3, "%04i", 123);
115        assert(n == 4);
116        check(ctx, "01");
117
118        reset(ctx, 0);
119        n = snprintf(&ctx->a[0], sizeof(ctx->a), "%i%f%i", 123, 0.0, 456);
120        assert(n == 7);
121        check(ctx, "123%f%i");
122
123        reset(ctx, LOG_INFO);
124        log(LOG_INFO, "log %i", 456);
125        check(ctx, "log 456");
126
127        reset(ctx, LOG_WARNING);
128        syslog(LOG_WARNING, "syslog %i", 789);
129        check(ctx, "syslog 789");
130
131        reset(ctx, LOG_PRINTF);
132        printf("printf %i", 0);
133        check(ctx, "printf 0");
134
135        reset(ctx, LOG_PRINTF);
136        printf("reg=%b", 3, "\10\2BITTWO\1BITONE");
137        check(ctx, "reg=3<BITTWO,BITONE>");
138
139        reset(ctx, LOG_PRINTF);
140        printf("out:    %4D", "AAAA", ":");
141        check(ctx, "out:        41:41:41:41");
142
143        errno = ENOMSG;
144        reset(ctx, LOG_PRINTF);
145        printf("%m");
146        check(ctx, "No message of desired type");
147
148        exit(0);
149}
150
151#include <rtems/bsd/test/default-init.h>
Note: See TracBrowser for help on using the repository browser.