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