tprintf.pp 5.6 KB

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