xref: /freebsd/contrib/sendmail/libsm/t-float.c (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
1 /*
2  * Copyright (c) 2000-2001 Proofpoint, 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 #include <sm/gen.h>
11 SM_IDSTR(id, "@(#)$Id: t-float.c,v 1.19 2013-11-22 20:51:43 ca Exp $")
12 
13 #include <sm/limits.h>
14 #include <sm/io.h>
15 #include <sm/string.h>
16 #include <sm/test.h>
17 #include <sm/types.h>
18 
19 int
20 main(argc, argv)
21 	int argc;
22 	char **argv;
23 {
24 	double d, d2;
25 	double ld;
26 	char buf[128];
27 	char *r;
28 
29 	/*
30 	**  Sendmail uses printf and scanf with doubles,
31 	**  so make sure that this works.
32 	*/
33 
34 	sm_test_begin(argc, argv, "test floating point stuff");
35 
36 	d = 1.125;
37 	sm_snprintf(buf, sizeof(buf), "%d %.3f %d", 0, d, 1);
38 	r = "0 1.125 1";
39 	if (!SM_TEST(strcmp(buf, r) == 0))
40 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
41 				     "got %s instead\n", buf);
42 
43 	d = 1.125;
44 	sm_snprintf(buf, sizeof(buf), "%.3f", d);
45 	r = "1.125";
46 	if (!SM_TEST(strcmp(buf, r) == 0))
47 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
48 				     "got %s instead\n", buf);
49 	d2 = 0.0;
50 	sm_io_sscanf(buf, "%lf", &d2);
51 #if SM_CONF_BROKEN_STRTOD
52 	if (d != d2)
53 	{
54 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
55 				     "wanted %f, got %f\n", d, d2);
56 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
57 				     "error ignored since SM_CONF_BROKEN_STRTOD is set for this OS\n");
58 	}
59 #else /* SM_CONF_BROKEN_STRTOD */
60 	if (!SM_TEST(d == d2))
61 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
62 				     "wanted %f, got %f\n", d, d2);
63 #endif /* SM_CONF_BROKEN_STRTOD */
64 
65 	ld = 2.5;
66 	sm_snprintf(buf, sizeof(buf), "%.3f %.1f", d, ld);
67 	r = "1.125 2.5";
68 	if (!SM_TEST(strcmp(buf, r) == 0))
69 		(void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
70 				     "got %s instead\n", buf);
71 	return sm_test_end();
72 }
73