1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/* Test of sprintf(), float version, '%f(F)' specification.
$Id: sprintf_flt-f01.c,v 1.1 2007/02/18 13:45:38 dmix Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "progmem.h"
PROGMEM static const struct sprf_s {
char fmt[8];
float val;
char pattern[21];
} t[] = {
/* Nonregular cases. */
{ "%f", 0, "0.000000" },
{ "%7.3f", 9.9999, " 10.000" },
/* Flags separatly. */
{ "%f", 1.234567, "1.234567" },
{ "%F", 1.234567, "1.234567" },
{ "%f", -1.234567, "-1.234567" },
{ "% f", 1.234567, " 1.234567" },
{ "%+f", 1.234567, "+1.234567" },
{ "%.3f", 1.234567, "1.235" },
{ "%12f", 1.234567, " 1.234567" },
{ "%-12f", 1.234567, "1.234567 " },
{ "%012f", 1.234567, "00001.234567" },
/* Flags together. */
{ "% F", 1.234567, " 1.234567" },
{ "% +f", 1.234567, "+1.234567" },
{ "% f", -1.234567, "-1.234567" },
{ "%+012f", 1.234567, "+0001.234567" },
{ "%-012f", 1.234567, "1.234567 " },
/* Precision. */
{ "%.7f", 1.2345678, "1.2345678" },
{ "%.6f", 1.2345678, "1.234568" },
{ "%.5f", 1.2345678, "1.23457" },
{ "%.4f", 1.2345678, "1.2346" },
{ "%.3f", 1.2345678, "1.235" },
{ "%.2f", 1.2345678, "1.23" },
{ "%.1f", 1.2345678, "1.2" },
{ "%.0f", 1.2345678, "1" },
{ "%.f", 1.2345678, "1" },
/* Width, without dec. point */
{ "%.0f", 10, "10" },
{ "%0.0f", 10, "10" },
{ "%1.0f", 10, "10" },
{ "%2.0f", 10, "10" },
{ "%3.0f", 10, " 10" },
{ "%12.0f", 10, " 10" },
{ "%12.0f", -10, " -10" },
/* Width, only fractional part. */
{ "%.3f", 0.123, "0.123" },
{ "%0.3f", 0.123, "0.123" },
{ "%1.3f", 0.123, "0.123" },
{ "%5.3f", 0.123, "0.123" },
{ "%6.3f", 0.123, " 0.123" },
{ "%12.3f", 0.123, " 0.123" },
{ "%12.3f", -0.123, " -0.123" },
/* Width, integer and fractional are present. */
{ "%.3f", 1.234, "1.234" },
{ "%0.3f", 1.234, "1.234" },
{ "%1.3f", 1.234, "1.234" },
{ "%5.3f", 1.234, "1.234" },
{ "%6.3f", 1.234, " 1.234" },
{ "%12.3f", 1.234, " 1.234" },
{ "%12.3f", -1.234, " -1.234" },
/* Double rounding elimination (ftoa_engine give at least 1 digit). */
{ "%.3f", 0.46, "0.460" },
{ "%.2f", 0.46, "0.46" },
{ "%.1f", 0.46, "0.5" },
{ "%.0f", 0.46, "0" }, /* just that very case */
{ "%1.0f", 0.46, "0" },
{ "%2.0f", 0.46, " 0" },
/* Rounding cases. */
{ "%.3f", 0.51, "0.510" },
{ "%.2f", 0.51, "0.51" },
{ "%.1f", 0.51, "0.5" },
{ "%.0f", 0.51, "1" },
{ "%1.0f", 0.51, "1" },
{ "%2.0f", 0.51, " 1" },
{ "%.3f", 0.58, "0.580" },
{ "%.2f", 0.58, "0.58" },
{ "%.1f", 0.58, "0.6" },
{ "%.0f", 0.58, "1" },
{ "%1.0f", 0.58, "1" },
{ "%2.0f", 0.58, " 1" },
{ "%.4f", 0.4999, "0.4999"},
{ "%.3f", 0.4999, "0.500" },
{ "%.2f", 0.4999, "0.50" },
{ "%.1f", 0.4999, "0.5" },
{ "%.0f", 0.4999, "0" },
{ "%.2f", 0.049, "0.05" },
{ "%.1f", 0.049, "0.0" },
{ "%.0f", 0.049, "0" },
{ "%.10f", 0.00000000049, "0.0000000005" },
{ "%.9f", 0.00000000049, "0.000000000" },
{ "%.8f", 0.00000000049, "0.00000000" },
{ "%.10f", 0.00000000051, "0.0000000005" },
{ "%.9f", 0.00000000051, "0.000000001" },
{ "%.8f", 0.00000000051, "0.00000000" },
};
#ifndef __AVR__
# define strlen_P strlen
#endif
void run_sprf (const struct sprf_s *pt, int testno)
{
static char s[300];
int n;
int code;
#ifdef __AVR__
n = sprintf_P (s, pt->fmt, pgm_read_dword (& pt->val));
#else
n = sprintf (s, pt->fmt, pt->val);
#endif
if (n != (int)strlen_P (pt->pattern))
code = testno + 1000;
else if (strcmp_P (s, pt->pattern))
code = testno;
else
return;
#if !defined(__AVR__)
printf ("\ntestno %3d: expect: %3d, \"%s\","
"\n output: %3d, \"%s\"\n",
testno, strlen(pt->pattern), pt->pattern, n, s);
exit (code < 256 ? testno : 255);
#elif defined(DEBUG)
exit ((int)s);
#endif
exit (code);
}
int main ()
{
int i;
for (i = 0; (unsigned)i != sizeof(t)/sizeof(t[0]); i++)
run_sprf (t+i, i+1);
return 0;
}
|