48 #define mnyfmt( a,b,c,d) mnyfmt(a,b,c,d)
49 #define format_money(a,b,c,d) mnyfmt(a,b,c,d)
51 #define mnyfmt( a,b,c,d) mnyfmt(a,b,c,d)
52 #define format_money(a,b,c,d) mnyfmt(a,b,c,d)
62 int eqstr(
const char *s1,
const char *s2) {
64 const int false = 0;
const int true = !
false;
66 if (s1==0 && s2==0) {
return true; }
67 if (s1==0 || s2==0) {
return false; }
68 return (strcmp(s1,s2) == 0) ?
true :
false;
81 char fill[] = {
'!', (char)(0xf0) , 0x0f, 0x8, 2 , 0 };
82 FMTSTR fmtstr[ (
sizeof(fill)/
sizeof(*fill)) ];
86 if (!format) {
return mnyfmt( (
char*)(0), dec, intpart,CE ); }
88 dim = (
sizeof(fill)/
sizeof(*fill));
90 if (len >=
sizeof(fmtstr[0].str) ) {
return 0; }
91 for ( i=0; i<dim; ++i ) {
92 memset( fmtstr+i , fill[i] ,
sizeof(
FMTSTR) );
93 strcpy( fmtstr[i].str , format );
94 fmtstr[i].sgn =
mnyfmt( fmtstr[i].str, dec, intpart,CE );
95 if ( !fmtstr[i].sgn ) {
97 char *p = (
char*)(&fmtstr[i]), *pEnd= p+
sizeof(fmtstr[0]);
99 if ( *p != fill[i] ) {
return 0; }
104 char *p = fmtstr[i].before;
105 while ( p!= fmtstr[i].str ) {
106 if ( *p != fill[i] ) {
return 0; }
111 char *p = fmtstr[i].str+len+1;
112 if ( 0 != *(fmtstr[i].str+len) ) { printf(
"."); }
113 while ( p!= (
char*)(&fmtstr[i])+
sizeof(
FMTSTR) ) {
114 if ( *p != fill[i] ) {
return 0; }
119 for ( i=1; i<dim; ++i ) {
121 if ( 0!= strcmp(fmtstr[i-1].str,fmtstr[i].str) ) {
return 0; }
123 if ( fmtstr[i].sgn != fmtstr[i-1].sgn+
sizeof(
FMTSTR) ) {
return 0; }
127 return mnyfmt( format, dec, intpart,CE );
142 #define mnyfmt mnyfmt_wrap
145 #define show(x) do { printf("%s->[%s]\n", #x, x ); } while(0)
147 #define sh show(fmtstr);
150 int mnyfmtts(
const int argc ,
const char* argv[] ) {
153 char *sgn, fmtstr[96],mem[96];
155 unsigned CE, ten_pow_CE;
156 CE = 2; ten_pow_CE = 100;
158 strcpy( mem,
"USD$ " );
159 strcpy( fmtstr ,
"99,999,999.99999" );
160 if (( sgn =
mnyfmt( fmtstr ,
'.' ,-10245587,CE ) )) {
162 if ( (*sgn==
'-') && (
','==*(sgn+1)) ) { ++sgn; *sgn=
'-'; }
175 char *sgn, fmtstr[96];
177 strcpy( fmtstr ,
"9,999,999.99" );
178 if (( sgn =
mnyfmt( fmtstr ,
'.' ,-45587,2 ) )) {
182 if ( (*sgn==
'-') && (
','==*(sgn+1)) ) {
192 strcpy( fmtstr ,
"9,999" );
193 if (( sgn =
mnyfmt( fmtstr ,
'.' ,-2455,0 ) )) {
199 assertTrue(
"5 digits are required to format " "-2,455" );
207 strcpy( fmtstr ,
"999,999,999.99" );
208 if (( sgn =
mnyfmt( fmtstr ,
'.' ,-245587,2 ) )) {
212 if ( (*sgn==
'-') && (
','==*(sgn+1)) ) {
224 strcpy( buffer,
"USD$ " );
225 strcpy( fmtstr ,
"99,999,999" );
226 if (( sgn =
mnyfmt( fmtstr ,
'.' ,-102455,87 ) )) {
227 assertFalse(
"ERROR [CE should be 0 not " ",87" "]" );
233 strcpy( fmtstr ,
"99,999,999.9999" );
234 if (( sgn =
mnyfmt( fmtstr ,
'.' ,-1024550077,4 ) )) {
237 if ( (*sgn==
'-') && (
','==*(sgn+1)) ) {
247 strcpy( fmtstr ,
"999,999,999.9999" );
248 if (( sgn =
mnyfmt( fmtstr ,
'.' , -124550077,2 ) )) {
257 typedef struct struct_overwrite {
263 overwrite o = { 0, {
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'\0' }, -1 };
264 assertTrue( 8-1==strlen(o.bytes_8) && o.int_neg == -1 );
266 strcpy( o.bytes_8,
"1234567.." );
268 assertFalse( o.int_neg == -1 &&
"Adjacent memory overwritten " );
271 assertTrue( CHAR_BIT == 8 &&
"8 bits bytes required" );
276 strcpy( fmtstr ,
"999.999.999,99" );
277 if (( sgn =
mnyfmt( fmtstr ,
',', 12345678988,2 ) )) {
281 strcpy( fmtstr ,
"999.999.999,99" );
282 if (( sgn =
mnyfmt( fmtstr ,
',' , -10245587,2 ) )) {
285 if ( (*sgn==
'-') && (
'.'==*(sgn+1)) ) { ++sgn; *sgn=
'-'; }
296 strcpy( fmtstr ,
"999.99999" );
297 if (( sgn =
mnyfmt( fmtstr ,
'.' , 24558700,4 ) )) {
308 strcpy( buffer,
"USD$ " );
309 strcpy( fmtstr ,
"9,999,999.999" );
310 if (( sgn =
mnyfmt( fmtstr ,
'.' ,-102455087,3 ) )) {
312 if (
','==*(sgn+1)) { ++sgn; *sgn=
'-'; }
313 strcat( buffer ,
"(" );
314 strcat( buffer , sgn );
315 strcat( buffer ,
")" );
319 strcat( buffer , sgn );
325 typedef char mnyfmt_size_96_is_enough
326 [ (( (65536.0 * 65536.0 * 65536.0 * 65536.0) < 1.0e20 ) ? 1 :-1 ) ];
327 mnyfmt_size_96_is_enough ch;
334 strcpy( fmtstr ,
"9,999,999.999" );
335 sgn =
mnyfmt( fmtstr ,
'.' ,2455087,3 );
336 strcpy( buffer,
"USD$ " );
339 strcat( buffer ,
"(" );
340 strcat( buffer , sgn );
341 strcat( buffer ,
")" );
344 strcat( buffer , sgn );
353 strcpy( fmtstr ,
"$9,999,999.999" );
354 if (( sgn =
mnyfmt( fmtstr ,
'.' , -455870,3 ) )) {
355 if ( (*sgn==
'-') && (
','==*(sgn+1)) ) { ++sgn; *sgn=
'-'; }
357 for ( --sgn; (sgn!=fmtstr ); --sgn ) {
367 double intdouble, fractdouble;
368 long intpart, fracpart;
369 unsigned CE, ten_pow_CE;
371 CE = 5; ten_pow_CE = 100*1000;
372 fractdouble = modf( 2455.87 , &intdouble );
374 fracpart = (unsigned)(fractdouble*ten_pow_CE);
379 strcpy( fmtstr ,
"[[ 999,999.99999 ]]" );
380 if (( sgn =
mnyfmt( fmtstr ,
'.' , intpart*ten_pow_CE+fracpart,CE ) )) {
384 fracpart = (unsigned)(ceil(fractdouble*100))*1000;
385 strcpy( fmtstr ,
"[[ 999,999.99999 ]]" );
387 if (( sgn =
mnyfmt( fmtstr ,
'.' , intpart*ten_pow_CE+fracpart,5 ) )) {
395 #define SCALE(f,CE) ( (mnyfmt_long) ( (f) * 1.0e##CE ) ) // beware of truncation
398 double val_double = -102455.87;
400 strcpy( fmtstr ,
"99,999,999.99999" );
401 if (( sgn =
mnyfmt( fmtstr ,
'.' ,
SCALE(val_double,2),2 ) )) {
403 if ( (*sgn==
'-') && (
','==*(sgn+1)) ) { ++sgn; *sgn=
'-'; }
413 #define SCALE(f,CE) ( (mnyfmt_long) ( (f) * 1.0e##CE ) )
430 double intdouble, fractdouble;
431 long intpart, fracpart;
434 fractdouble = modf( 2455.87 , &intdouble );
435 intpart = intdouble*10000;
436 fracpart = (unsigned)(fractdouble*100)*100;
442 strcpy( fmtstr ,
"[[ 999,999.9999 ]]" );
443 if (( sgn =
mnyfmt( fmtstr ,
'.' , intpart+fracpart,CE ) )) {
447 fracpart = (unsigned)(ceil(fractdouble*100))*100;
448 strcpy( fmtstr ,
"[[ 999,999.9999 ]]" );
450 if (( sgn =
mnyfmt( fmtstr ,
'.' , intpart+fracpart,CE ) )) {
464 #ifndef MNYFMT_NO_LONG_LONG
466 strcpy( fmtstr ,
"999,999,999,999,999,999,999" );
468 if ( 9223372036854775807LL == LONG_LONG_MAX ) {
469 if (( sgn =
mnyfmt( fmtstr ,
' ' , max,0 ) )) {
474 assertFalse(
"BEWARE: (long long) is not 8 bytes wide" );
479 strcpy( fmtstr ,
"999,999,999,999" );
481 if ( 2147483647L == LONG_MAX ) {
482 if (( sgn =
mnyfmt( fmtstr ,
' ' , max,0 ) )) {
487 assertFalse(
"BEWARE: (long) is not 4 bytes wide" );
495 int N = ( ( CHAR_BIT *
sizeof(int) - 1 ) * 643 / 2136 ) * (10000*10000);
497 strcpy( fmtstr ,
"999,999,999,999.999999" );
498 if (( sgn =
mnyfmt( fmtstr ,
'.', -N,0 ) )) {
499 if ( (*sgn==
'-') && (
','==*(sgn+1)) ) { ++sgn; *sgn=
'-'; }
510 char *src, chBuff[128];
511 wchar_t *dst, *wcBuff;
512 strcpy (chBuff,
"Convert me to (wchar_t)");
513 wcBuff = (
wchar_t*)( malloc(
sizeof(chBuff)*
sizeof(
wchar_t) ) );
514 for ( dst=wcBuff,src=chBuff; (*dst=*src); ++dst,++src ) {}
522 #if defined(__BORLANDC__) && (__BORLANDC__ <= 0x0410)
523 #define wcslen strlen
528 wchar_t DST[
DIM], *dst;
529 strcpy( SRC,
"Convert me to (wchar_t)");
531 for ( dst=DST,src=SRC; (*dst=*src); ++dst,++src ) {}
536 printf(
"sizeof(wchar_t) == %d\n",
sizeof(
wchar_t) );
537 printf(
"%s strlen(SRC)==%d\n", SRC , strlen(SRC) );
538 printf(
"%ls wcslen(SRC)==%d\n", DST , wcslen(DST) );
543 for ( i=0 ; i<strlen(SRC); ++i ) {
549 wchar_t c1 = (wchar_t)(
'A');
552 printf(
"sizeof(wchar_t) == %d\n",
sizeof(
wchar_t) );
558 long long_min = -LONG_MAX-1;
560 long_min = -long_min;
568 strcpy( fmtstr ,
"999,999.9999919,one9." );
569 if (( sgn =
mnyfmt( fmtstr ,
'.', 245587,2 ) )) {
570 if ( (*sgn==
'-') && (
','==*(sgn+1)) ) { ++sgn; *sgn=
'-'; }
576 #ifndef MNYFMT_NO_LONG_LONG
580 strcpy( fmtstr ,
"99,99,99,99,99,99,99,999.99" );
585 if (( sgn =
mnyfmt( fmtstr ,
'.' , 3258472925LL,2 ) )) {
589 strcpy( fmtstr,
"99,99,99,99,99 crores 99 lakhs 99,999 rupees.99 paise" );
590 if (( sgn =
mnyfmt( fmtstr ,
'.' , 3258472925LL,2 ) )) {
591 for ( p=sgn; *p!=
'.'&&*p!=0; ++p ) {}
594 assertTrue(
eqstr( sgn ,
"3 crores 25 lakhs 84,729 rupees 25 paise" ));
606 strcpy( fmtstr ,
"99/99/9999" );
607 if (( sgn =
mnyfmt( fmtstr , 000, 9272002,0 ) )) {
611 strcpy( fmtstr ,
"99:99:99" );
612 if (( sgn =
mnyfmt( fmtstr ,
'?', 21435,0 ))) {
620 strcpy( fmtstr ,
"99-99-9999" );
621 if (( sgn =
mnyfmt( fmtstr ,
'*' , 07272002,0 ) )) {
627 strcpy( fmtstr ,
"99-99-9999" );
628 if (( sgn =
mnyfmt( fmtstr ,
'*' , 27092002,0 ) )) {
632 strcpy( fmtstr ,
"99-99-9999" );
633 if (( sgn =
mnyfmt( fmtstr ,
'*' , 1012002,0 ) )) {
638 strcpy( fmtstr ,
"999,999,999" );
639 if (( sgn =
mnyfmt( fmtstr ,
':' , 27092002,0 ) )) {
643 strcpy( fmtstr ,
"999,999,999" );
644 if (( sgn =
mnyfmt( fmtstr ,
'.' , 123456789,0 ) )) {
649 #ifndef MNYFMT_NO_LONG_LONG
654 strcpy( fmtstr ,
"99,999,999,999,999,999,999" );
656 sgn =
mnyfmt( fmtstr ,
' ' , max,0 );
665 strcpy( fmtstr ,
"9,999,999,999" );
667 sgn =
mnyfmt( fmtstr ,
' ' , max,0 );
677 strcpy( fmtstr ,
"9,999." );
678 if (( sgn =
mnyfmt( fmtstr ,
'.' , 2455,0 ) )) {
682 if (( sgn =
mnyfmt( fmtstr ,
'.' , 1400,0 ) )) {
691 strcpy( fmtstr ,
"9,999." );
692 sgn =
mnyfmt( fmtstr ,
'.' , 1400,0 );
700 strcpy( fmtstr ,
".9,999." );
701 sgn =
mnyfmt( fmtstr ,
'.' , 2455,0 );
704 sgn =
mnyfmt( fmtstr ,
',' , 2455,0 );
709 strcpy( fmtstr ,
"9,999" );
710 sgn =
mnyfmt( fmtstr ,
'.' , 2455,0 );
713 sgn =
mnyfmt( fmtstr ,
',' , 2455,0 );
716 strcpy( fmtstr ,
"9" );
717 sgn =
mnyfmt( fmtstr ,
',' , 0,0 );
719 strcpy( fmtstr ,
"9" );
720 sgn =
mnyfmt( fmtstr ,
',' , 9,0 );
727 strcpy( fmtstr ,
"9.999." );
728 sgn =
mnyfmt( fmtstr ,
'.' ,-0,0 );
732 strcpy( fmtstr ,
"9," );
733 sgn =
mnyfmt( fmtstr ,
',' ,-0,0 );
736 strcpy( fmtstr ,
"," );
737 sgn =
mnyfmt( fmtstr ,
',' ,0,0 );
740 strcpy( fmtstr ,
",9" );
741 sgn =
mnyfmt( fmtstr ,
',' ,0,0 );
744 strcpy( fmtstr ,
"9,9" );
745 sgn =
mnyfmt( fmtstr ,
',' ,0,0 );
748 strcpy( fmtstr ,
"9,9999999" );
749 sgn =
mnyfmt( fmtstr ,
',' ,0,0 );
752 strcpy( fmtstr ,
"" );
753 sgn =
mnyfmt( fmtstr ,
',' ,1241,2 );
756 strcpy( fmtstr ,
"0123456" );
759 sgn =
mnyfmt( fmtstr ,
',' ,1,0 );
766 strcpy( fmtstr ,
"999,99,999" );
767 sgn =
mnyfmt( fmtstr ,
'.' , 99999999,0 );
770 strcpy( fmtstr ,
"999,99,999" );
771 sgn =
mnyfmt( fmtstr ,
'.' , 88888888,0 );
778 strcpy( fmtstr ,
"9,999." );
779 sgn =
mnyfmt( fmtstr ,
'.' , 2455,0 );
782 strcpy( fmtstr ,
"9.999." );
783 sgn =
mnyfmt( fmtstr ,
'?' , 2455,0 );
792 strcpy( fmtstr ,
"Banana 99 x099-099 32 .9.999." );
793 if (( sgn =
mnyfmt( fmtstr ,
'.' ,-245587,2 ) )) {
805 strcpy( fmtstr ,
"Banana 99 x099-099 32 .9.999." );
806 strcpy( fmtstr ,
"" );
809 if (( sgn =
mnyfmt( fmtstr ,
'.' ,-245587,2 ) )) {
814 assertTrue(
"Sorry: I forgot to use a non null string" );
820 if (( sgn =
mnyfmt( (
char*)(0) ,
'.' ,-245587,2 ) )) {
824 assertTrue(
"Sorry: I forgot to use a non null pointer" );
829 typedef char* (*format_money_type)(
char *, char ,
mnyfmt_long , unsigned );
832 strcpy( fmtstr ,
"9,999." );
833 if (( sgn = format_money( fmtstr ,
'.' , 245587,2 ) )) {
842 double fractpart, intpart;
843 fractpart = modf( 3.14159265 , &intpart );
845 assertTrue( fabs( fractpart - 0.14159265 ) < pow(10.0,-8.0) );
847 if ( fractpart == 0.14159265 ) {
848 assertFalse(
"NEVER executed due to floating point rounding error" );
850 assertTrue( !!!
"http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html" );
855 double fractpart, intpart;
856 fractpart = modf( -3.14159265 , &intpart );
859 assertTrue( fabs( -fractpart - 0.14159265 ) < pow(10.0,-8.0) );
866 #define round adh_round
963 return ( (val>0.0) ? floor(val+0.5) : -floor(-val+0.5) );
967 int main(
const int argc ,
const char* argv[] ) {
968 printf(
"TestCase [mnyfmtts.c]\n" );
#define mnyfmt_size
(2^128 < 10^40) && (2*40 < 96) ==> char[96] is big enough for 128 bits
char mnyfmtts_REQUIRES_mnyfmt_format_char_EQUAL_TO_digit_9[(mnyfmt_format_char=='9'?1:-1)]
Ensure that, at least for this test program [mnyfmtts.c], the formatting char is digit '9'...
char * mnyfmt_wrap(char *format, char dec, mnyfmt_long intpart, unsigned CE)
Wrapper around 'mnyfmt()' to check that it does not overwrite adyacent memory.
char * sgn
Returned by mnyfmt()
#define assertTrue(cond)
(cond ? () : cout << "cond" )
#define mnyfmt
[Dirty] Trick to force all test cases to use 'mnyfmt_wrap() instead of mnyfmt()'
int mnyfmtts(const int argc, const char *argv[])
test ==> mnyfmt()
#define assertFalse(cond)
(!(cond) ? () : cout << "!" << (cond)" )
Header file for mnyfmt().
int eqstr(const char *s1, const char *s2)
s1 == s2 ???
int main(const int argc, const char *argv[])
Main test program.
double adh_round(double val)
Round to integer, rounding halfway cases away from zero.
Used by 'mnyfmt_wrap()' to check for memory overwrites.
#define mnyfmt_format_char
Formatting character for mnyfmt().
[u]Micro module for [Unit] program testing.
mnyfmt_long str2mnyCE(const char *amount, char dec, unsigned CE)
Returns an integer value that corresponds to 'amount', scaled 10^CE digits.