File: nutlogtest.c

package info (click to toggle)
nut 2.8.4%2Breally-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,720 kB
  • sloc: ansic: 132,030; sh: 17,256; cpp: 12,566; makefile: 5,646; python: 1,114; perl: 856; xml: 47
file content (196 lines) | stat: -rw-r--r-- 8,008 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* nutlogtest - some trivial usage for upslog*() and upsdebug*() related
 * routines to sanity-check their code (compiler does not warn, test runs
 * do not crash).
 *
 * Copyright (C)
 *	2020-2024	Jim Klimov <jimklimov@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "config.h"
#include "common.h"

int main(void) {
	const char *s1 = "!NULL";
	const char *s2 = NULL;
	int ret = 0;

#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_OVERFLOW)
#  pragma GCC diagnostic push
#  pragma GCC diagnostic ignored "-Wformat-overflow"
#endif

	upsdebugx(0, "D: checking with libc handling of NULL (can segfault for some libc implementations):");
	upsdebugx(0, "D:   '%s' vs '%s'", s1, s2);

/* This explicitly does not work with -Wformat, due to verbatim NULL without a var:
 * nutlogtest.c:20:5: error: reading through null pointer (argument 4) [-Werror=format=]
 * and also due to (void*) vs. (char*) in naive case:
 *   upsdebugx(0, "D: '%s' vs. '%s'", NUT_STRARG(NULL), NULL);
 * but with casting the explicit NULL remains:
 *   upsdebugx(0, "D: '%s' vs. '%s'", NUT_STRARG((char *)NULL), (char *)NULL);
 */

	upsdebugx(0, "D: checking with NUT_STRARG macro: '%s' vs '%s'", NUT_STRARG(s2), s2);

#ifdef NUT_STRARG
#undef NUT_STRARG
#endif

#define NUT_STRARG(x) (x?x:"<N/A>")

/* This explicitly does not work with -Wformat, due to a NULL in the '%s'
 * format string expansion (e.g. due to NUT PR #675 conversion to macros):
 *   ../include/common.h:155:41: warning: '%s' directive argument is null [-Wformat-overflow=]
 *   <...snip...>
 *   nutlogtest.c:45:63: note: format string is defined here
 *      45 |     upsdebugx(0, "D: checking with NUT_STRARG macro: '%s' vs. '%s'", NUT_STRARG(s2), s2);
 *         |                                                               ^~
 */
	upsdebugx(0, "D: checking that macro wrap trick works: '%s' vs '%s'", NUT_STRARG(s2), s2);

#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_OVERFLOW)
#  pragma GCC diagnostic pop
#endif

	if (1) {	/* scoping */
		char *dynfmt = "Test '%s'", *p;
		char buf[LARGEBUF];

		nut_debug_level = 1;
		if (snprintf_dynamic(buf, sizeof(buf), dynfmt, "%s", "Single string via dynamic format") > 0) {
			upsdebugx(0, "D: >>> %s", buf);
			if (!strcmp(buf, "Test 'Single string via dynamic format'")) {
				upsdebugx(0, "D: snprintf_dynamic() prepared a dynamically formatted string with expected content");
			} else {
				upsdebugx(0, "E: snprintf_dynamic() failed to prepare a dynamically formatted string: got unexpected content");
				ret++;
			}
		} else {
			upsdebugx(0, "E: snprintf_dynamic() failed to prepare a dynamically formatted string: returned empty or error");
			ret++;
		}

#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT)
#  pragma GCC diagnostic push
#  pragma GCC diagnostic ignored "-Wformat"
#endif
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_EXTRA_ARGS)
#  pragma GCC diagnostic push
#  pragma GCC diagnostic ignored "-Wformat-extra-args"
#endif
		if (snprintf_dynamic(buf, sizeof(buf), dynfmt, "%d", "Single string via dynamic format", 1) < 0) {
			upsdebugx(0, "D: snprintf_dynamic() correctly reports mis-matched formats");
		} else {
			upsdebugx(0, "E: snprintf_dynamic() wrongly reports well-matched formats");
			ret++;
		}
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_EXTRA_ARGS)
#  pragma GCC diagnostic pop
#endif
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT)
#  pragma GCC diagnostic pop
#endif

		if (snprintf_dynamic(buf, sizeof(buf), dynfmt, "%s%d", "Single string via dynamic format, plus ignored garbage", 1) < 0) {
			upsdebugx(0, "E: snprintf_dynamic() wrongly reports well-matched formats");
			ret++;
		} else {
			upsdebugx(0, "D: snprintf_dynamic() correctly reports mis-matched formats");
		}

		/* Note extra non-type chars in "expected" format are stripped */
		p = mkstr_dynamic(dynfmt, " %.4s %%", "Single string inlined by mkstr_dynamic()");
		upsdebugx(0, ">>> %s", NUT_STRARG(p));
		if (!p || *p == '\0' || strcmp(p, "Test 'Single string inlined by mkstr_dynamic()'")) {
			upsdebugx(0, "E: mkstr_dynamic() failed to prepare a dynamically formatted string: got unexpected content");
			ret++;
		} else {
			upsdebugx(0, "D: mkstr_dynamic() prepared a dynamically formatted string with expected content");
		}
	}

	if (1) {	/* scoping */
		int	res;
		char	**p,
			*fmtFloat[] = { "%f", " %A", " %0.1E%% ", NULL },
			*fmtNotFloat[] = { "%s", NULL },
			/* TODO: Add conversion? More methods? */
			*fmtConvertFloatOKAY[] = { "<empty>", "%f%", "%m", "$f", NULL },
			*fmtConvertFloatTODO[] = { "%lf", "%d", NULL },
			*fmtConvertIntOKAY[] = { "<empty>", NULL },
			*fmtConvertIntTODO[] = { "%ld", "%lld", "%hd", NULL }
			;

		for (p = &(fmtFloat[0]); *p; p++) {
			if ((res = validate_formatting_string(*p, "Voltage: %G is not %%d", 1)) < 0) {
				upsdebugx(0, "E: validate_formatting_string() expecting %%f equivalent failed (%i) for: '%s'", res, *p);
				ret++;
			} else {
				upsdebugx(0, "D: validate_formatting_string() expecting %%f equivalent passed (%i) for: '%s'", res, *p);
			}
		}

		for (p = &(fmtNotFloat[0]); *p; p++) {
			if ((res = validate_formatting_string(*p, "%f", 1)) < 0) {
				upsdebugx(0, "D: validate_formatting_string() expecting %%f failed (%i) (as it should have) for: '%s'", res, *p);
			} else {
				upsdebugx(0, "E: validate_formatting_string() expecting %%f passed (%i) (but should not have) for: '%s'", res, *p);
				ret++;
			}
		}

		/* Auto-conversion or other non-exact equivalence */
		for (p = &(fmtConvertFloatOKAY[0]); *p; p++) {
			if ((res = validate_formatting_string(*p, "%f", 1)) > 0) {
				upsdebugx(0, "D: validate_formatting_string() expecting %%f passed (%i) (non-exactly) for: '%s'", res, *p);
			} else {
				upsdebugx(0, "E: validate_formatting_string() expecting %%f failed (%i) for: '%s'", res, *p);
				ret++;
			}
		}

		for (p = &(fmtConvertIntOKAY[0]); *p; p++) {
			if ((res = validate_formatting_string(*p, "%d", 1)) > 0) {
				upsdebugx(0, "D: validate_formatting_string() expecting %%f passed (%i) (non-exactly) for: '%s'", res, *p);
			} else {
				upsdebugx(0, "E: validate_formatting_string() expecting %%f failed (%i) for: '%s'", res, *p);
				ret++;
			}
		}

		/* TODO: Make such cases safely fit */
		for (p = &(fmtConvertFloatTODO[0]); *p; p++) {
			if ((res = validate_formatting_string(*p, "%f", 1)) < 0) {
				upsdebugx(0, "D: validate_formatting_string() expecting %%f failed (%i) (as it should have) for: '%s'", res, *p);
			} else {
				upsdebugx(0, "E: validate_formatting_string() expecting %%f passed (%i) (but should not have) for: '%s'", res, *p);
				ret++;
			}
		}

		for (p = &(fmtConvertIntTODO[0]); *p; p++) {
			if ((res = validate_formatting_string(*p, "%d", 1)) < 0) {
				upsdebugx(0, "D: validate_formatting_string() expecting %%d failed (%i) (as it should have) for: '%s'", res, *p);
			} else {
				upsdebugx(0, "E: validate_formatting_string() expecting %%d passed (%i) (but should not have) for: '%s'", res, *p);
				ret++;
			}
		}
	}

	return ret;
}