tprintf2.pp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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); varargs; cdecl; external CrtLib name 'printf';
  23. procedure sprintf(p : pchar;const formatstr : pchar); varargs; cdecl; external CrtLib name 'sprintf';
  24. const
  25. int64prefix='I64';
  26. {$else}
  27. {$linklib c}
  28. procedure printf(const formatstr : pchar); varargs; cdecl; external;
  29. procedure sprintf(p : pchar;const formatstr : pchar); varargs; 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. { for some CPUs, this requires also different calling conventions
  55. than procedures taking a single pchar parameter, see #7504 (FK) }
  56. printf('Simple test without arg'+lineending);
  57. Writeln('Testing with single pchar argument');
  58. printf('Text containing "%s" text'+lineending,s);
  59. sprintf(p,'Text containing "%s" text'+lineending,s);
  60. if strpos(p,'g "Enclosed text" ')=nil then
  61. begin
  62. writeln('The output of sprintf for pchar is wrong: ',p);
  63. has_errors:=true;
  64. end;
  65. Writeln('Testing with single longint argument');
  66. printf('Text containing longint: %d'+lineending,l);
  67. sprintf(p,'Text containing longint: %d'+lineending,l);
  68. if strpos(p,'longint: 45')=nil then
  69. begin
  70. writeln('The output of sprintf for longint is wrong: ',p);
  71. has_errors:=true;
  72. end;
  73. Writeln('Testing with single int64 argument');
  74. printf('Text containing int64: %'+int64prefix+'d'+lineending,ll);
  75. sprintf(p,'Text containing int64: %'+int64prefix+'d'+lineending,ll);
  76. if strpos(p,'int64: 345')=nil then
  77. begin
  78. writeln('The output of sprintf for int64 is wrong: ',p);
  79. has_errors:=true;
  80. end;
  81. Writeln('Testing with single single argument');
  82. printf('Text containing single: %f'+lineending,si);
  83. sprintf(p,'Text containing single: %f'+lineending,si);
  84. if strpos(p,'single: 32.1')=nil then
  85. begin
  86. writeln('The output of sprintf for double is wrong: ',p);
  87. has_errors:=true;
  88. end;
  89. Writeln('Testing with single double argument');
  90. printf('Text containing double: %lf'+lineending,d);
  91. sprintf(p,'Text containing double: %lf'+lineending,d);
  92. if strpos(p,'double: 45.4')=nil then
  93. begin
  94. writeln('The output of sprintf for double is wrong: ',p);
  95. has_errors:=true;
  96. end;
  97. {$ifdef TEST_EXTENDED}
  98. printf('Text containing long double: %Lf'+lineending,e);
  99. sprintf(p,'Text containing long double: %Lf'+lineending,e);
  100. if strpos(p,'long double: 74.7')=nil then
  101. begin
  102. writeln('The output of sprintf for long double is wrong:',p);
  103. has_errors:=true;
  104. end;
  105. {$endif TEST_EXTENDED}
  106. Writeln('Testing with combined pchar argument');
  107. printf('Text containing "%s" and "%s" text'+lineending,s,s2);
  108. sprintf(p,'Text containing "%s" and "%s" text'+lineending,s,s2);
  109. if strpos(p,'g "Enclosed text" and "next"')=nil then
  110. begin
  111. writeln('The output of sprintf for two pchars is wrong: ',p);
  112. has_errors:=true;
  113. end;
  114. Writeln('Testing with single longint argument and pchar');
  115. printf('Text containing longint: %d"%s"'+lineending,l,s2);
  116. sprintf(p,'Text containing longint: %d"%s"'+lineending,l,s2);
  117. if strpos(p,'longint: 45"next"')=nil then
  118. begin
  119. writeln('The output of sprintf for longint is wrong: ',p);
  120. has_errors:=true;
  121. end;
  122. Writeln('Testing with single int64 argument and pchar');
  123. printf('Text containing int64: %'+int64prefix+'d"%s"'+lineending,ll,s2);
  124. sprintf(p,'Text containing int64: %'+int64prefix+'d"%s"'+lineending,ll,s2);
  125. if strpos(p,'int64: 345"next"')=nil then
  126. begin
  127. writeln('The output of sprintf for int64 is wrong: ',p);
  128. has_errors:=true;
  129. end;
  130. Writeln('Testing with single single argument');
  131. printf('Text containing single: %f"%s"'+lineending,si,s2);
  132. sprintf(p,'Text containing single: %f"%s"'+lineending,si,s2);
  133. if (strpos(p,'single: 32.1')=nil) or
  134. (strpos(p,'"next"')=nil) then
  135. begin
  136. writeln('The output of sprintf for double is wrong: ',p);
  137. has_errors:=true;
  138. end;
  139. Writeln('Testing with single double argument');
  140. printf('Text containing double: %lf"%s"'+lineending,d,s2);
  141. sprintf(p,'Text containing double: %lf"%s"'+lineending,d,s2);
  142. if (strpos(p,'double: 45.4')=nil) or
  143. (strpos(p,'"next"')=nil) then
  144. begin
  145. writeln('The output of sprintf for double is wrong: ',p);
  146. has_errors:=true;
  147. end;
  148. {$ifdef TEST_EXTENDED}
  149. printf('Text containing long double: %Lf"%s"'+lineending,e,s2);
  150. sprintf(p,'Text containing long double: %Lf"%s"'+lineending,e,s2);
  151. if (strpos(p,'long double: 74.7')=nil) or
  152. (strpos(p,'"next"')=nil) then
  153. begin
  154. writeln('The output of sprintf for long double is wrong:',p);
  155. has_errors:=true;
  156. end;
  157. {$endif TEST_EXTENDED}
  158. if has_errors then
  159. halt(1);
  160. end.