tprintf.pp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. { %version=1.1 }
  2. { %NOTE=This test requires a C library }
  3. {$mode objfpc}
  4. uses
  5. strings;
  6. {$ifdef FPC_HAS_TYPE_EXTENDED}
  7. {$define TEST_EXTENDED}
  8. {$endif FPC_HAS_TYPE_EXTENDED}
  9. {$ifdef beos}
  10. {it seems that BeOS doesn't support extended...}
  11. {$undef TEST_EXTENDED}
  12. {$endif beos}
  13. {$ifdef WINDOWS}
  14. const
  15. {$ifdef wince}
  16. CrtLib = 'coredll.dll';
  17. {$else}
  18. { the msvcrt.dll doesn't support extended because MS-C doesn't }
  19. {$undef TEST_EXTENDED}
  20. CrtLib = 'msvcrt.dll';
  21. {$endif}
  22. procedure printf(const formatstr : pchar; const args : array of const);cdecl; external CrtLib name 'printf';
  23. procedure sprintf(p : pchar;const formatstr : pchar; const args : array of const);cdecl; external CrtLib name 'sprintf';
  24. const
  25. int64prefix='I64';
  26. {$else}
  27. {$linklib c}
  28. procedure printf(const formatstr : pchar; const args : array of const);cdecl; external;
  29. procedure sprintf(p : pchar;const formatstr : pchar; const args : array of const);cdecl; external;
  30. const
  31. int64prefix='ll';
  32. {$endif}
  33. const
  34. {$ifdef macos}
  35. lineending = #13;
  36. {$else}
  37. lineending = #10;
  38. {$endif}
  39. type
  40. THandle = longint;
  41. const
  42. l : longint = 45;
  43. ll : int64 = 345;
  44. s : pchar = 'Enclosed text';
  45. s2 : pchar = 'next';
  46. si : single = 32.12;
  47. d : double = 45.45;
  48. e : extended = 74.74;
  49. p : pchar = nil;
  50. has_errors : boolean = false;
  51. begin
  52. getmem(p,500);
  53. Writeln('Testing C printf function called from FPC code');
  54. // printf('Simple test without arg'+lineending,[]);
  55. Writeln('Testing with single pchar argument');
  56. printf('Text containing "%s" text'+lineending,[s]);
  57. sprintf(p,'Text containing "%s" text'+lineending,[s]);
  58. if strpos(p,'g "Enclosed text" ')=nil then
  59. begin
  60. writeln('The output of sprintf for pchar is wrong: ',p);
  61. has_errors:=true;
  62. end;
  63. Writeln('Testing with single longint argument');
  64. printf('Text containing longint: %d'+lineending,[l]);
  65. sprintf(p,'Text containing longint: %d'+lineending,[l]);
  66. if strpos(p,'longint: 45')=nil then
  67. begin
  68. writeln('The output of sprintf for longint is wrong: ',p);
  69. has_errors:=true;
  70. end;
  71. Writeln('Testing with single int64 argument');
  72. printf('Text containing int64: %'+int64prefix+'d'+lineending,[ll]);
  73. sprintf(p,'Text containing int64: %'+int64prefix+'d'+lineending,[ll]);
  74. if strpos(p,'int64: 345')=nil then
  75. begin
  76. writeln('The output of sprintf for int64 is wrong: ',p);
  77. has_errors:=true;
  78. end;
  79. Writeln('Testing with single single argument');
  80. printf('Text containing single: %f'+lineending,[si]);
  81. sprintf(p,'Text containing single: %f'+lineending,[si]);
  82. if strpos(p,'single: 32.1')=nil then
  83. begin
  84. writeln('The output of sprintf for double is wrong: ',p);
  85. has_errors:=true;
  86. end;
  87. Writeln('Testing with single double argument');
  88. printf('Text containing double: %lf'+lineending,[d]);
  89. sprintf(p,'Text containing double: %lf'+lineending,[d]);
  90. if strpos(p,'double: 45.4')=nil then
  91. begin
  92. writeln('The output of sprintf for double is wrong: ',p);
  93. has_errors:=true;
  94. end;
  95. {$ifdef TEST_EXTENDED}
  96. printf('Text containing long double: %Lf'+lineending,[e]);
  97. sprintf(p,'Text containing long double: %Lf'+lineending,[e]);
  98. if strpos(p,'long double: 74.7')=nil then
  99. begin
  100. writeln('The output of sprintf for long double is wrong:',p);
  101. has_errors:=true;
  102. end;
  103. {$endif TEST_EXTENDED}
  104. Writeln('Testing with combined pchar argument');
  105. printf('Text containing "%s" and "%s" text'+lineending,[s,s2]);
  106. sprintf(p,'Text containing "%s" and "%s" text'+lineending,[s,s2]);
  107. if strpos(p,'g "Enclosed text" and "next"')=nil then
  108. begin
  109. writeln('The output of sprintf for two pchars is wrong: ',p);
  110. has_errors:=true;
  111. end;
  112. Writeln('Testing with single longint argument and pchar');
  113. printf('Text containing longint: %d"%s"'+lineending,[l,s2]);
  114. sprintf(p,'Text containing longint: %d"%s"'+lineending,[l,s2]);
  115. if strpos(p,'longint: 45"next"')=nil then
  116. begin
  117. writeln('The output of sprintf for longint is wrong: ',p);
  118. has_errors:=true;
  119. end;
  120. Writeln('Testing with single int64 argument and pchar');
  121. printf('Text containing int64: %'+int64prefix+'d"%s"'+lineending,[ll,s2]);
  122. sprintf(p,'Text containing int64: %'+int64prefix+'d"%s"'+lineending,[ll,s2]);
  123. if strpos(p,'int64: 345"next"')=nil then
  124. begin
  125. writeln('The output of sprintf for int64 is wrong: ',p);
  126. has_errors:=true;
  127. end;
  128. Writeln('Testing with single single argument');
  129. printf('Text containing single: %f"%s"'+lineending,[si,s2]);
  130. sprintf(p,'Text containing single: %f"%s"'+lineending,[si,s2]);
  131. if (strpos(p,'single: 32.1')=nil) or
  132. (strpos(p,'"next"')=nil) then
  133. begin
  134. writeln('The output of sprintf for double is wrong: ',p);
  135. has_errors:=true;
  136. end;
  137. Writeln('Testing with single double argument');
  138. printf('Text containing double: %lf"%s"'+lineending,[d,s2]);
  139. sprintf(p,'Text containing double: %lf"%s"'+lineending,[d,s2]);
  140. if (strpos(p,'double: 45.4')=nil) or
  141. (strpos(p,'"next"')=nil) then
  142. begin
  143. writeln('The output of sprintf for double is wrong: ',p);
  144. has_errors:=true;
  145. end;
  146. {$ifdef TEST_EXTENDED}
  147. printf('Text containing long double: %Lf"%s"'+lineending,[e,s2]);
  148. sprintf(p,'Text containing long double: %Lf"%s"'+lineending,[e,s2]);
  149. if (strpos(p,'long double: 74.7')=nil) or
  150. (strpos(p,'"next"')=nil) then
  151. begin
  152. writeln('The output of sprintf for long double is wrong:',p);
  153. has_errors:=true;
  154. end;
  155. {$endif TEST_EXTENDED}
  156. if has_errors then
  157. halt(1);
  158. end.