tprintf3.pp 4.9 KB

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