tprintf.pp 5.3 KB

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