Line 0
Link Here
|
|
|
1 |
/*- |
2 |
* Copyright (c) 2012 Matthew Story <matthewstory@gmail.com> |
3 |
* All rights reserved. |
4 |
* |
5 |
* Redistribution and use in source and binary forms, with or without |
6 |
* modification, are permitted provided that the following conditions |
7 |
* are met: |
8 |
* 1. Redistributions of source code must retain the above copyright |
9 |
* notice, this list of conditions and the following disclaimer. |
10 |
* 2. Redistributions in binary form must reproduce the above copyright |
11 |
* notice, this list of conditions and the following disclaimer in the |
12 |
* documentation and/or other materials provided with the distribution. |
13 |
* |
14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
15 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
18 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
20 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
21 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
22 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
23 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
24 |
* SUCH DAMAGE. |
25 |
*/ |
26 |
|
27 |
/* |
28 |
* Tests for correct return codes from f(w)printf |
29 |
*/ |
30 |
|
31 |
#include <assert.h> |
32 |
#include <stdio.h> |
33 |
#include <stdlib.h> |
34 |
#include <stdint.h> |
35 |
#include <wchar.h> |
36 |
#include <errno.h> |
37 |
#include <string.h> |
38 |
|
39 |
#define BATTERY_OK "ok %d - printreturn\n" |
40 |
|
41 |
#define run_battery(void) \ |
42 |
_run_battery(__LINE__) |
43 |
|
44 |
void smash_stack(void); |
45 |
void _run_battery(int line); |
46 |
int battery = 1; |
47 |
|
48 |
int |
49 |
main() { |
50 |
/* |
51 |
* All tests occur on stdout, this makes the tests quite noisy. |
52 |
* but keeps them contained -- e.g. no reliance on external |
53 |
* files, or on manipulating file struct internals. |
54 |
*/ |
55 |
|
56 |
printf("1..12\n"); |
57 |
/* |
58 |
* without errors |
59 |
*/ |
60 |
clearerr(stdout); |
61 |
|
62 |
/* line-buffered */ |
63 |
setvbuf(stdout, (char *)NULL, _IOLBF, 0); |
64 |
run_battery(); |
65 |
|
66 |
/* unbuffered */ |
67 |
setvbuf(stdout, (char *)NULL, _IONBF, 0); |
68 |
run_battery(); |
69 |
|
70 |
/* fully-buffered */ |
71 |
setvbuf(stdout, (char *)NULL, _IOFBF, 0); |
72 |
run_battery(); |
73 |
|
74 |
/* |
75 |
* success with errors, would rather not manipulate _flags * directly, but |
76 |
* can't find another direct way to set error, without an actual error. |
77 |
*/ |
78 |
stdout->_flags |= __SERR; |
79 |
|
80 |
/* line-buffered */ |
81 |
setvbuf(stdout, (char *)NULL, _IOLBF, 0); |
82 |
run_battery(); |
83 |
|
84 |
/* unbuffered */ |
85 |
setvbuf(stdout, (char *)NULL, _IONBF, 0); |
86 |
run_battery(); |
87 |
|
88 |
/* fully-buffered */ |
89 |
setvbuf(stdout, (char *)NULL, _IOFBF, 0); |
90 |
run_battery(); |
91 |
|
92 |
return 0; |
93 |
} |
94 |
|
95 |
/* taken from test-printbasic.c */ |
96 |
void |
97 |
smash_stack(void) |
98 |
{ |
99 |
static uint32_t junk = 0xdeadbeef; |
100 |
uint32_t buf[512]; |
101 |
int i; |
102 |
|
103 |
for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++) |
104 |
buf[i] = junk; |
105 |
} |
106 |
|
107 |
void |
108 |
_run_battery(int line) { |
109 |
#define BUF 100 |
110 |
extern int battery; |
111 |
wchar_t ws[BUF], wfmt[BUF]; |
112 |
char s[BUF]; |
113 |
|
114 |
/* regular width tests, no replacement, and replacement */ |
115 |
smash_stack(); |
116 |
sprintf(s, BATTERY_OK, battery); |
117 |
if (0 > fprintf(stdout, s)) { |
118 |
fprintf(stderr, |
119 |
"%d: failure in no replace printf (%d)\n", |
120 |
line, errno); |
121 |
abort(); |
122 |
} |
123 |
++battery; |
124 |
smash_stack(); |
125 |
if (0 > fprintf(stdout, BATTERY_OK, battery)) { |
126 |
fprintf(stderr, "%d: failure in replace printf (%d)\n", line, errno); |
127 |
abort(); |
128 |
} |
129 |
++battery; |
130 |
|
131 |
/* wide tests, no replacement, and replacement */ |
132 |
smash_stack(); |
133 |
mbstowcs(wfmt, BATTERY_OK, BUF - 1); |
134 |
swprintf(ws, sizeof(ws) / sizeof(ws[0]), wfmt, battery); |
135 |
if (0 > fwprintf(stdout, ws)) { |
136 |
fprintf(stderr, |
137 |
"%d: failure in no replace wprintf (%d)\n", |
138 |
line, errno); |
139 |
abort(); |
140 |
} |
141 |
++battery; |
142 |
smash_stack(); |
143 |
if (0 > fwprintf(stdout, wfmt, battery)) { |
144 |
fprintf(stderr, "%d: failure in replace wprintf (%d)\n", line, errno); |
145 |
abort(); |
146 |
} |
147 |
++battery; |
148 |
} |