xref: /onnv-gate/usr/src/cmd/sendmail/libsm/t-float.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
3*0Sstevel@tonic-gate  *	All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
6*0Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
7*0Sstevel@tonic-gate  * the sendmail distribution.
8*0Sstevel@tonic-gate  */
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
11*0Sstevel@tonic-gate 
12*0Sstevel@tonic-gate #include <sm/gen.h>
13*0Sstevel@tonic-gate SM_IDSTR(id, "@(#)$Id: t-float.c,v 1.16 2001/02/02 23:11:46 ca Exp $")
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate #include <sm/limits.h>
16*0Sstevel@tonic-gate #include <sm/io.h>
17*0Sstevel@tonic-gate #include <sm/string.h>
18*0Sstevel@tonic-gate #include <sm/test.h>
19*0Sstevel@tonic-gate #include <sm/types.h>
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate int
22*0Sstevel@tonic-gate main(argc, argv)
23*0Sstevel@tonic-gate 	int argc;
24*0Sstevel@tonic-gate 	char **argv;
25*0Sstevel@tonic-gate {
26*0Sstevel@tonic-gate 	double d, d2;
27*0Sstevel@tonic-gate 	double ld;
28*0Sstevel@tonic-gate 	char buf[128];
29*0Sstevel@tonic-gate 	char *r;
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate 	/*
32*0Sstevel@tonic-gate 	**  Sendmail uses printf and scanf with doubles,
33*0Sstevel@tonic-gate 	**  so make sure that this works.
34*0Sstevel@tonic-gate 	*/
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate 	sm_test_begin(argc, argv, "test floating point stuff");
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate 	d = 1.125;
39*0Sstevel@tonic-gate 	sm_snprintf(buf, sizeof(buf), "%d %.3f %d", 0, d, 1);
40*0Sstevel@tonic-gate 	r = "0 1.125 1";
41*0Sstevel@tonic-gate 	if (!SM_TEST(strcmp(buf, r) == 0))
42*0Sstevel@tonic-gate 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
43*0Sstevel@tonic-gate 				     "got %s instead\n", buf);
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate 	d = 1.125;
46*0Sstevel@tonic-gate 	sm_snprintf(buf, sizeof(buf), "%.3f", d);
47*0Sstevel@tonic-gate 	r = "1.125";
48*0Sstevel@tonic-gate 	if (!SM_TEST(strcmp(buf, r) == 0))
49*0Sstevel@tonic-gate 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
50*0Sstevel@tonic-gate 				     "got %s instead\n", buf);
51*0Sstevel@tonic-gate 	d2 = 0.0;
52*0Sstevel@tonic-gate 	sm_io_sscanf(buf, "%lf", &d2);
53*0Sstevel@tonic-gate #if SM_CONF_BROKEN_STRTOD
54*0Sstevel@tonic-gate 	if (d != d2)
55*0Sstevel@tonic-gate 	{
56*0Sstevel@tonic-gate 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
57*0Sstevel@tonic-gate 				     "wanted %f, got %f\n", d, d2);
58*0Sstevel@tonic-gate 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
59*0Sstevel@tonic-gate 				     "error ignored since SM_CONF_BROKEN_STRTOD is set for this OS\n");
60*0Sstevel@tonic-gate 	}
61*0Sstevel@tonic-gate #else /* SM_CONF_BROKEN_STRTOD */
62*0Sstevel@tonic-gate 	if (!SM_TEST(d == d2))
63*0Sstevel@tonic-gate 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
64*0Sstevel@tonic-gate 				     "wanted %f, got %f\n", d, d2);
65*0Sstevel@tonic-gate #endif /* SM_CONF_BROKEN_STRTOD */
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	ld = 2.5;
68*0Sstevel@tonic-gate 	sm_snprintf(buf, sizeof(buf), "%.3f %.1f", d, ld);
69*0Sstevel@tonic-gate 	r = "1.125 2.5";
70*0Sstevel@tonic-gate 	if (!SM_TEST(strcmp(buf, r) == 0))
71*0Sstevel@tonic-gate 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
72*0Sstevel@tonic-gate 				     "got %s instead\n", buf);
73*0Sstevel@tonic-gate 	return sm_test_end();
74*0Sstevel@tonic-gate }
75