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

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 356f59c was 356f59c, checked in by Sebastian Huber <sebastian.huber@…>, on 05/06/16 at 10:16:19

kvprintf: Add support for %m

  • 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 <rtems/bsd/sys/types.h>
35#include <sys/systm.h>
36#include <syslog.h>
37
38#include <assert.h>
39#include <errno.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43
44#define TEST_NAME "LIBBSD LOG 1"
45
46typedef struct {
47        char a[99];
48        char b[99];
49        size_t i;
50        int expected_level;
51} test_context;
52
53static test_context test_instance;
54
55static void
56vprintf_putchar(int c, void *arg)
57{
58        test_context *ctx;
59
60        ctx = arg;
61        ctx->a[ctx->i] = (char) c;
62        ++ctx->i;
63}
64
65static int
66vprintf_handler(int level, const char *fmt, va_list ap)
67{
68        test_context *ctx;
69        int n;
70
71        ctx = &test_instance;
72
73        assert(level == ctx->expected_level);
74        n = (kvprintf(fmt, vprintf_putchar, &test_instance, 10, ap));
75        ctx->a[n] = '\0';
76        return (n);
77}
78
79static void
80reset(test_context *ctx, int expected_level)
81{
82
83        ctx->i = 0;
84        ctx->expected_level = expected_level;
85        memset(&ctx->a[0], '?', sizeof(ctx->a));
86        memset(&ctx->b[0], '?', sizeof(ctx->b));
87}
88
89static void
90check(test_context *ctx, const char *b)
91{
92
93        memcpy(&ctx->b[0], b, strlen(b) + 1);
94        assert(memcmp(&ctx->a[0], &ctx->b[0], sizeof(ctx->a)) == 0);
95}
96
97static void
98test_main(void)
99{
100        test_context *ctx;
101        int n;
102
103        ctx = &test_instance;
104
105        rtems_bsd_set_vprintf_handler(vprintf_handler);
106
107        reset(ctx, 0);
108        n = sprintf(&ctx->a[0], "%04i", 123);
109        assert(n == 4);
110        check(ctx, "0123");
111
112        reset(ctx, 0);
113        n = snprintf(&ctx->a[0], 3, "%04i", 123);
114        assert(n == 4);
115        check(ctx, "01");
116
117        reset(ctx, 0);
118        n = snprintf(&ctx->a[0], sizeof(ctx->a), "%i%f%i", 123, 0.0, 456);
119        assert(n == 7);
120        check(ctx, "123%f%i");
121
122        reset(ctx, LOG_INFO);
123        log(LOG_INFO, "log %i", 456);
124        check(ctx, "log 456");
125
126        reset(ctx, LOG_WARNING);
127        syslog(LOG_WARNING, "syslog %i", 789);
128        check(ctx, "syslog 789");
129
130        reset(ctx, LOG_PRINTF);
131        printf("printf %i", 0);
132        check(ctx, "printf 0");
133
134        reset(ctx, LOG_PRINTF);
135        printf("reg=%b", 3, "\10\2BITTWO\1BITONE");
136        check(ctx, "reg=3<BITTWO,BITONE>");
137
138        reset(ctx, LOG_PRINTF);
139        printf("out:    %4D", "AAAA", ":");
140        check(ctx, "out:        41:41:41:41");
141
142        errno = ENOMSG;
143        reset(ctx, LOG_PRINTF);
144        printf("%m");
145        check(ctx, "No message of desired type");
146
147        exit(0);
148}
149
150#include <rtems/bsd/test/default-init.h>
Note: See TracBrowser for help on using the repository browser.