1 /*
2 ** Copyright (C) 2003-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU Lesser General Public License as published by
6 ** the Free Software Foundation; either version 2.1 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 
19 #include "sfconfig.h"
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include <errno.h>
26 
27 #include "common.h"
28 
29 #include "test_main.h"
30 
31 #define	CMP_0_ARGS(line, err, fmt)	\
32 	{	psf->parselog.indx = 0 ;			\
33 		snprintf (buffer, sizeof (buffer), (fmt)) ;	\
34 		psf_log_printf (psf, (fmt)) ;				\
35 		err += compare_strings_or_die (line, fmt, buffer, psf->parselog.buf) ;	\
36 		}
37 
38 #define	CMP_2_ARGS(line, err, fmt, a)	\
39 	{	psf->parselog.indx = 0 ;				\
40 		snprintf (buffer, sizeof (buffer), (fmt), (a), (a)) ;	\
41 		psf_log_printf (psf, (fmt), (a), (a)) ;					\
42 		err += compare_strings_or_die (line, fmt, buffer, psf->parselog.buf) ;	\
43 		}
44 
45 #define	CMP_4_ARGS(line, err, fmt, a)	\
46 	{	psf->parselog.indx = 0 ;				\
47 		snprintf (buffer, sizeof (buffer), (fmt), (a), (a), (a), (a)) ;	\
48 		psf_log_printf (psf, (fmt), (a), (a), (a), (a)) ;				\
49 		err += compare_strings_or_die (line, fmt, buffer, psf->parselog.buf) ;	\
50 		}
51 
52 #define	CMP_5_ARGS(line, err, fmt, a)	\
53 	{	psf->parselog.indx = 0 ;				\
54 		snprintf (buffer, sizeof (buffer), (fmt), (a), (a), (a), (a), (a)) ;	\
55 		psf_log_printf (psf, (fmt), (a), (a), (a), (a), (a)) ;					\
56 		err += compare_strings_or_die (line, fmt, buffer, psf->parselog.buf) ;		\
57 		}
58 
59 #define	CMP_6_ARGS(line, err, fmt, a)	\
60 	{	psf->parselog.indx = 0 ;				\
61 		snprintf (buffer, sizeof (buffer), (fmt), (a), (a), (a), (a), (a), (a)) ;	\
62 		psf_log_printf (psf, (fmt), (a), (a), (a), (a), (a), (a)) ;					\
63 		err += compare_strings_or_die (line, fmt, buffer, psf->parselog.buf) ;			\
64 		}
65 
66 static int
compare_strings_or_die(int linenum,const char * fmt,const char * s1,const char * s2)67 compare_strings_or_die (int linenum, const char *fmt, const char* s1, const char* s2)
68 {	int errors = 0 ;
69 /*-puts (s1) ;puts (s2) ;-*/
70 
71 	if (strcmp (s1, s2) != 0)
72 	{	printf ("\n\nLine %d: string compare mismatch:\n\t", linenum) ;
73 		printf ("\"%s\"\n", fmt) ;
74 		printf ("\t\"%s\"\n\t\"%s\"\n", s1, s2) ;
75 		errors ++ ;
76 		} ;
77 
78 	return errors ;
79 } /* compare_strings_or_die */
80 
81 void
test_log_printf(void)82 test_log_printf (void)
83 {	static char buffer [2048] ;
84 	SF_PRIVATE	sf_private, *psf ;
85 	int			k, errors = 0 ;
86 	int			int_values [] = { 0, 1, 12, 123, 1234, 123456, -1, -12, -123, -1234, -123456 } ;
87 
88 	print_test_name ("Testing psf_log_printf") ;
89 
90 	psf = &sf_private ;
91 	memset (psf, 0, sizeof (sf_private)) ;
92 
93 	CMP_0_ARGS (__LINE__, errors, " ->%%<- ") ;
94 
95 	/* Test printing of ints. */
96 	for (k = 0 ; k < ARRAY_LEN (int_values) ; k++)
97 		CMP_6_ARGS (__LINE__, errors, "int A : %d, % d, %4d, % 4d, %04d, % 04d", int_values [k]) ;
98 
99 	for (k = 0 ; k < ARRAY_LEN (int_values) ; k++)
100 		CMP_5_ARGS (__LINE__, errors, "int B : %+d, %+4d, %+04d, %-d, %-4d", int_values [k]) ;
101 
102 	for (k = 0 ; k < ARRAY_LEN (int_values) ; k++)
103 		CMP_2_ARGS (__LINE__, errors, "int C : %- d, %- 4d", int_values [k]) ;
104 
105 	/* Test printing of unsigned ints. */
106 	for (k = 0 ; k < ARRAY_LEN (int_values) ; k++)
107 		CMP_4_ARGS (__LINE__, errors, "D : %u, %4u, %04u, %0u", int_values [k]) ;
108 
109 	/* Test printing of hex ints. */
110 	for (k = 0 ; k < ARRAY_LEN (int_values) ; k++)
111 		CMP_4_ARGS (__LINE__, errors, "E : %X, %4X, %04X, %0X", int_values [k]) ;
112 
113 	/* Test printing of strings. */
114 	CMP_4_ARGS (__LINE__, errors, "B %s, %3s, %8s, %-8s", "str") ;
115 
116 	if (errors)
117 	{	puts ("\nExiting due to errors.\n") ;
118 		exit (1) ;
119 		} ;
120 
121 	puts ("ok") ;
122 } /* test_log_printf */
123 
124