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