tprintf2.pp 5.3 KB

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