tprintf.pp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. type
  20. THandle = longint;
  21. const
  22. l : longint = 45;
  23. ll : int64 = 345;
  24. s : pchar = 'Enclosed text';
  25. s2 : pchar = 'next';
  26. si : single = 32.12;
  27. d : double = 45.45;
  28. e : extended = 74.74;
  29. p : pchar = nil;
  30. has_errors : boolean = false;
  31. begin
  32. getmem(p,500);
  33. Writeln('Testing C printf function called from FPC code');
  34. // printf('Simple test without arg'#10,[]);
  35. Writeln('Testing with single pchar argument');
  36. printf('Text containing "%s" text'#10,[s]);
  37. sprintf(p,'Text containing "%s" text'#10,[s]);
  38. if strpos(p,'g "Enclosed text" ')=nil then
  39. begin
  40. writeln('The output of sprintf for pchar is wrong: ',p);
  41. has_errors:=true;
  42. end;
  43. Writeln('Testing with single longint argument');
  44. printf('Text containing longint: %d'#10,[l]);
  45. sprintf(p,'Text containing longint: %d'#10,[l]);
  46. if strpos(p,'longint: 45')=nil then
  47. begin
  48. writeln('The output of sprintf for longint is wrong: ',p);
  49. has_errors:=true;
  50. end;
  51. Writeln('Testing with single int64 argument');
  52. printf('Text containing int64: %'+int64prefix+'d'#10,[ll]);
  53. sprintf(p,'Text containing int64: %'+int64prefix+'d'#10,[ll]);
  54. if strpos(p,'int64: 345')=nil then
  55. begin
  56. writeln('The output of sprintf for int64 is wrong: ',p);
  57. has_errors:=true;
  58. end;
  59. Writeln('Testing with single single argument');
  60. printf('Text containing single: %f'#10,[si]);
  61. sprintf(p,'Text containing single: %f'#10,[si]);
  62. if strpos(p,'single: 32.1')=nil then
  63. begin
  64. writeln('The output of sprintf for double is wrong: ',p);
  65. has_errors:=true;
  66. end;
  67. Writeln('Testing with single double argument');
  68. printf('Text containing double: %f'#10,[d]);
  69. sprintf(p,'Text containing double: %f'#10,[d]);
  70. if strpos(p,'double: 45.4')=nil then
  71. begin
  72. writeln('The output of sprintf for double is wrong: ',p);
  73. has_errors:=true;
  74. end;
  75. printf('Text containing long double: %f'#10,[e]);
  76. sprintf(p,'Text containing long double: %f'#10,[e]);
  77. if strpos(p,'long double: 74.7')=nil then
  78. begin
  79. writeln('The output of sprintf for long double is wrong:',p);
  80. has_errors:=true;
  81. end;
  82. Writeln('Testing with combined pchar argument');
  83. printf('Text containing "%s" and "%s" text'#10,[s,s2]);
  84. sprintf(p,'Text containing "%s" and "%s" text'#10,[s,s2]);
  85. if strpos(p,'g "Enclosed text" and "next"')=nil then
  86. begin
  87. writeln('The output of sprintf for two pchars is wrong: ',p);
  88. has_errors:=true;
  89. end;
  90. Writeln('Testing with single longint argument and pchar');
  91. printf('Text containing longint: %d"%s"'#10,[l,s2]);
  92. sprintf(p,'Text containing longint: %d"%s"'#10,[l,s2]);
  93. if strpos(p,'longint: 45"next"')=nil then
  94. begin
  95. writeln('The output of sprintf for longint is wrong: ',p);
  96. has_errors:=true;
  97. end;
  98. Writeln('Testing with single int64 argument and pchar');
  99. printf('Text containing int64: %'+int64prefix+'d"%s"'#10,[ll,s2]);
  100. sprintf(p,'Text containing int64: %'+int64prefix+'d"%s"'#10,[ll,s2]);
  101. if strpos(p,'int64: 345"next"')=nil then
  102. begin
  103. writeln('The output of sprintf for int64 is wrong: ',p);
  104. has_errors:=true;
  105. end;
  106. Writeln('Testing with single single argument');
  107. printf('Text containing single: %f"%s"'#10,[si,s2]);
  108. sprintf(p,'Text containing single: %f"%s"'#10,[si,s2]);
  109. if (strpos(p,'single: 32.1')=nil) or
  110. (strpos(p,'"next"')=nil) then
  111. begin
  112. writeln('The output of sprintf for double is wrong: ',p);
  113. has_errors:=true;
  114. end;
  115. Writeln('Testing with single double argument');
  116. printf('Text containing double: %f"%s"'#10,[d,s2]);
  117. sprintf(p,'Text containing double: %f"%s"'#10,[d,s2]);
  118. if (strpos(p,'double: 45.4')=nil) or
  119. (strpos(p,'"next"')=nil) then
  120. begin
  121. writeln('The output of sprintf for double is wrong: ',p);
  122. has_errors:=true;
  123. end;
  124. printf('Text containing long double: %f"%s"'#10,[e,s2]);
  125. sprintf(p,'Text containing long double: %f"%s"'#10,[e,s2]);
  126. if (strpos(p,'long double: 74.7')=nil) or
  127. (strpos(p,'"next"')=nil) then
  128. begin
  129. writeln('The output of sprintf for long double is wrong:',p);
  130. has_errors:=true;
  131. end;
  132. if has_errors then
  133. halt(1);
  134. end.