tprintf.pp 5.0 KB

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