123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- { %version=1.1 }
- { %NOTE=This test requires a C library }
- {$mode objfpc}
- uses
- strings;
- {$ifdef win32}
- { $linklib msvcrt}
- procedure printf(const formatstr : pchar; const args : array of const);cdecl; external 'msvcrt.dll' name 'printf';
- procedure sprintf(p : pchar;const formatstr : pchar; const args : array of const);cdecl; external 'msvcrt.dll' name 'sprintf';
- const
- int64prefix='I64';
- {$else}
- {$linklib c}
- procedure printf(const formatstr : pchar; const args : array of const);cdecl; external;
- procedure sprintf(p : pchar;const formatstr : pchar; const args : array of const);cdecl; external;
- const
- int64prefix='ll';
- {$endif}
- const
- {$ifdef macos}
- lineending = #13;
- {$else}
- lineending = #10;
- {$endif}
- type
- THandle = longint;
- const
- l : longint = 45;
- ll : int64 = 345;
- s : pchar = 'Enclosed text';
- s2 : pchar = 'next';
- si : single = 32.12;
- d : double = 45.45;
- e : extended = 74.74;
- p : pchar = nil;
- has_errors : boolean = false;
- begin
- getmem(p,500);
- Writeln('Testing C printf function called from FPC code');
- // printf('Simple test without arg'+lineending,[]);
- Writeln('Testing with single pchar argument');
- printf('Text containing "%s" text'+lineending,[s]);
- sprintf(p,'Text containing "%s" text'+lineending,[s]);
- if strpos(p,'g "Enclosed text" ')=nil then
- begin
- writeln('The output of sprintf for pchar is wrong: ',p);
- has_errors:=true;
- end;
- Writeln('Testing with single longint argument');
- printf('Text containing longint: %d'+lineending,[l]);
- sprintf(p,'Text containing longint: %d'+lineending,[l]);
- if strpos(p,'longint: 45')=nil then
- begin
- writeln('The output of sprintf for longint is wrong: ',p);
- has_errors:=true;
- end;
- Writeln('Testing with single int64 argument');
- printf('Text containing int64: %'+int64prefix+'d'+lineending,[ll]);
- sprintf(p,'Text containing int64: %'+int64prefix+'d'+lineending,[ll]);
- if strpos(p,'int64: 345')=nil then
- begin
- writeln('The output of sprintf for int64 is wrong: ',p);
- has_errors:=true;
- end;
- Writeln('Testing with single single argument');
- printf('Text containing single: %f'+lineending,[si]);
- sprintf(p,'Text containing single: %f'+lineending,[si]);
- if strpos(p,'single: 32.1')=nil then
- begin
- writeln('The output of sprintf for double is wrong: ',p);
- has_errors:=true;
- end;
- Writeln('Testing with single double argument');
- printf('Text containing double: %f'+lineending,[d]);
- sprintf(p,'Text containing double: %f'+lineending,[d]);
- if strpos(p,'double: 45.4')=nil then
- begin
- writeln('The output of sprintf for double is wrong: ',p);
- has_errors:=true;
- end;
- printf('Text containing long double: %f'+lineending,[e]);
- sprintf(p,'Text containing long double: %f'+lineending,[e]);
- if strpos(p,'long double: 74.7')=nil then
- begin
- writeln('The output of sprintf for long double is wrong:',p);
- has_errors:=true;
- end;
- Writeln('Testing with combined pchar argument');
- printf('Text containing "%s" and "%s" text'+lineending,[s,s2]);
- sprintf(p,'Text containing "%s" and "%s" text'+lineending,[s,s2]);
- if strpos(p,'g "Enclosed text" and "next"')=nil then
- begin
- writeln('The output of sprintf for two pchars is wrong: ',p);
- has_errors:=true;
- end;
- Writeln('Testing with single longint argument and pchar');
- printf('Text containing longint: %d"%s"'+lineending,[l,s2]);
- sprintf(p,'Text containing longint: %d"%s"'+lineending,[l,s2]);
- if strpos(p,'longint: 45"next"')=nil then
- begin
- writeln('The output of sprintf for longint is wrong: ',p);
- has_errors:=true;
- end;
- Writeln('Testing with single int64 argument and pchar');
- printf('Text containing int64: %'+int64prefix+'d"%s"'+lineending,[ll,s2]);
- sprintf(p,'Text containing int64: %'+int64prefix+'d"%s"'+lineending,[ll,s2]);
- if strpos(p,'int64: 345"next"')=nil then
- begin
- writeln('The output of sprintf for int64 is wrong: ',p);
- has_errors:=true;
- end;
- Writeln('Testing with single single argument');
- printf('Text containing single: %f"%s"'+lineending,[si,s2]);
- sprintf(p,'Text containing single: %f"%s"'+lineending,[si,s2]);
- if (strpos(p,'single: 32.1')=nil) or
- (strpos(p,'"next"')=nil) then
- begin
- writeln('The output of sprintf for double is wrong: ',p);
- has_errors:=true;
- end;
- Writeln('Testing with single double argument');
- printf('Text containing double: %f"%s"'+lineending,[d,s2]);
- sprintf(p,'Text containing double: %f"%s"'+lineending,[d,s2]);
- if (strpos(p,'double: 45.4')=nil) or
- (strpos(p,'"next"')=nil) then
- begin
- writeln('The output of sprintf for double is wrong: ',p);
- has_errors:=true;
- end;
- printf('Text containing long double: %f"%s"'+lineending,[e,s2]);
- sprintf(p,'Text containing long double: %f"%s"'+lineending,[e,s2]);
- if (strpos(p,'long double: 74.7')=nil) or
- (strpos(p,'"next"')=nil) then
- begin
- writeln('The output of sprintf for long double is wrong:',p);
- has_errors:=true;
- end;
- if has_errors then
- halt(1);
- end.
|