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
|
/* Header+source file to test the dtostrf() function.
$Id: dtostrf.h,v 1.1 2007/02/06 12:36:58 dmix Exp $
*/
#ifndef PATTERN_SIZE
# define PATTERN_SIZE 15
#endif
struct dtostrf_s {
union {
long lo;
float fl;
};
signed char width;
unsigned char prec;
char pattern[PATTERN_SIZE];
};
#ifndef __AVR__
# include <stdio.h>
# include <string.h>
/* This function is intended for run of test cases on the host computer.
*/
char * dtostrf (double val, signed char width, unsigned char prec, char *s)
{
char fmt[16]; /* "%-110.100F" */
sprintf (fmt, "%%%d.%dF", width, prec);
sprintf (s, fmt, val);
return s;
}
#endif /* !__AVR__ */
#define PZLEN 5 /* protected zone length */
void run_dtostrf (const struct dtostrf_s *pt, int testno)
{
union {
long lo;
float fl;
} val;
signed char width;
unsigned char prec;
static char s[2*PZLEN + sizeof(pt->pattern)];
char c, *ps;
void *pv;
memset (s, testno, sizeof(s));
#ifdef __AVR__
val.lo = pgm_read_dword (& pt->lo);
width = pgm_read_byte (& pt->width);
prec = pgm_read_byte (& pt->prec);
#else
val.lo = pt->lo;
width = pt->width;
prec = pt->prec;
#endif
ps = dtostrf (val.fl, width, prec, s + PZLEN);
if (ps != s + PZLEN)
exit (testno + 0x1000);
for (ps = s; ps != s + PZLEN; ps++) {
if ((unsigned char)*ps != (testno & 0377))
exit (testno + 0x2000);
}
pv = & pt->pattern;
#ifdef __AVR__
do {
c = pgm_read_byte(pv++);
if (*ps++ != c) {
exit (testno + 0x3000);
}
} while (c);
#else
do {
c = *(char *)(pv++);
if (*ps++ != c) {
printf ("*** testno= %d: must= %s was= %s\n",
testno, pt->pattern, s + PZLEN);
exit (testno + 0x3000);
}
} while (c);
#endif
for (; ps != s + sizeof(s); ps++) {
if ((unsigned char)*ps != (testno & 0377))
exit (testno + 0x4000);
}
}
|