tprintf3.pp 5.0 KB

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