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