tval.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. { Included by several source with different
  2. definitions of the type
  3. IntegerType
  4. to check that the test is working for
  5. all basic integer types }
  6. procedure TestVal(comment,s : string; ExpectedRes : ValTestType; expected : IntegerType);
  7. var
  8. i : IntegerType;
  9. err,err1 : word;
  10. OK : boolean;
  11. begin
  12. OK:=false;
  13. if not silent and (Comment<>'') then
  14. Writeln(Comment);
  15. Val(s,i,err);
  16. if ExpectedRes=ValShouldFail then
  17. begin
  18. if err=0 then
  19. begin
  20. if not silent or not HasErrors then
  21. Writeln('Error: string ',Display(s),
  22. ' is a valid input for val function');
  23. HasErrors:=true;
  24. end
  25. else
  26. begin
  27. OK:=true;
  28. if not silent then
  29. Writeln('Correct: string ',Display(s),
  30. ' is a not valid input for val function');
  31. end;
  32. end
  33. else if ExpectedRes=ValShouldSucceed then
  34. begin
  35. if err=0 then
  36. begin
  37. OK:=true;
  38. if not silent then
  39. Writeln('Correct: string ',Display(s),
  40. ' is a valid input for val function');
  41. end
  42. else
  43. begin
  44. if not silent or not HasErrors then
  45. Writeln('Error: string ',Display(s),
  46. ' is a not valid input for val function',
  47. ' error pos=',err);
  48. HasErrors:=true;
  49. end;
  50. end
  51. else if ExpectedRes=ValShouldSucceedAfterRemovingTrail then
  52. begin
  53. if err=0 then
  54. begin
  55. if not silent or not HasErrors then
  56. Writeln('Error: string ',Display(s),
  57. ' is a valid input for val function');
  58. HasErrors:=true;
  59. end
  60. else
  61. begin
  62. err1:=err;
  63. Val(Copy(s,1,err1-1),i,err);
  64. if err=0 then
  65. begin
  66. OK:=true;
  67. if not silent then
  68. Writeln('Correct: string ',Display(s),
  69. ' is a valid input for val function up to position ',err1);
  70. end
  71. else
  72. begin
  73. if not silent or not HasErrors then
  74. Writeln('Error: string ',Display(Copy(s,1,err1-1)),
  75. ' is a not valid input for val function',
  76. ' error pos=',err);
  77. HasErrors:=true;
  78. end;
  79. end;
  80. end;
  81. if (err=0) and CheckVal and (i<>expected) then
  82. begin
  83. OK:=false;
  84. if not silent or not HasErrors then
  85. Writeln('Error: string ',Display(s),
  86. ' value is ',i,' <> ',expected);
  87. HasErrors:=true;
  88. end;
  89. if OK then
  90. inc(SuccessCount)
  91. else
  92. inc(FailCount);
  93. end;
  94. Procedure TestBase(Const Prefix : string;ValidChars : TCharSet);
  95. var
  96. i,j : longint;
  97. st : string;
  98. begin
  99. CheckVal:=false;
  100. Silent:=true;
  101. for i:=0 to 255 do
  102. begin
  103. st:=prefix+chr(i);
  104. if chr(i) in ValidChars then
  105. TestVal('',st,ValShouldSucceed,0)
  106. else
  107. TestVal('',st,ValShouldFail,0);
  108. end;
  109. for i:=0 to 255 do
  110. for j:=0 to 255 do
  111. begin
  112. st:=prefix+chr(i)+chr(j);
  113. if (chr(i) in ValidChars) and
  114. (chr(j) in ValidChars) then
  115. TestVal('',st,ValShouldSucceed,0)
  116. else
  117. begin
  118. if ((prefix<>'') or
  119. (not (chr(i) in SpecialCharsFirst))) and
  120. not (chr(j) in SpecialCharsSecond) then
  121. TestVal('',st,ValShouldFail,0);
  122. end;
  123. end;
  124. end;
  125. Function TestAll : boolean;
  126. var
  127. S : string;
  128. begin
  129. TestVal('Testing empty string','',ValShouldFail,0);
  130. TestVal('Testing string with #0',#0,ValShouldFail,0);
  131. TestVal('Testing string with base prefix and no value','0x',ValShouldFail,0);
  132. TestVal('Testing string with base prefix and no value','x',ValShouldFail,0);
  133. TestVal('Testing string with base prefix and no value','X',ValShouldFail,0);
  134. TestVal('Testing string with base prefix and no value','$',ValShouldFail,0);
  135. TestVal('Testing string with base prefix and no value','%',ValShouldFail,0);
  136. TestVal('Testing string with base prefix and no value','&',ValShouldFail,0);
  137. TestVal('Testing string with base prefix and #0','0x'#0,ValShouldFail,0);
  138. TestVal('Testing normal ''''0'''' string','0',ValShouldSucceed,0);
  139. TestVal('Testing leading space',' 0',ValShouldSucceed,0);
  140. TestVal('Testing leading 2 spaces',' 0',ValShouldSucceed,0);
  141. TestVal('Testing leading 2 tabs',#9#9'0',ValShouldSucceed,0);
  142. TestVal('Testing leading 3 spaces',' 0',ValShouldSucceed,0);
  143. TestVal('Testing leading 3 tabs',#9#9#9'0',ValShouldSucceed,0);
  144. TestVal('Testing leading space/tab combination',#9' 0',ValShouldSucceed,0);
  145. TestVal('Testing leading space/tab combination',' '#9'0',ValShouldSucceed,0);
  146. TestVal('Testing leading space/tab combination',' '#9' 0',ValShouldSucceed,0);
  147. TestVal('Testing leading space/tab combination',#9' '#9' 0',ValShouldSucceed,0);
  148. TestVal('Testing #0 following normal ''''0''','0'#0,ValShouldSucceed,0);
  149. TestVal('Testing leading space with trailing #0',' 0'#0,ValShouldSucceed,0);
  150. TestVal('Testing leading 2 spaces with trailing #0',' 0'#0,ValShouldSucceed,0);
  151. TestVal('Testing leading 2 tabs with trailing #0',#9#9'0'#0,ValShouldSucceed,0);
  152. TestVal('Testing leading 3 spaces with trailing #0',' 0'#0,ValShouldSucceed,0);
  153. TestVal('Testing leading 3 tabs with trailing #0',#9#9#9'0'#0,ValShouldSucceed,0);
  154. TestVal('Testing leading space/tab combination with trailing #0',#9' 0'#0,ValShouldSucceed,0);
  155. TestVal('Testing leading space/tab combination with trailing #0',' '#9'0'#0,ValShouldSucceed,0);
  156. TestVal('Testing leading space/tab combination with trailing #0',' '#9' 0'#0,ValShouldSucceed,0);
  157. TestVal('Testing leading space/tab combination with trailing #0',#9' '#9' 0'#0,ValShouldSucceed,0);
  158. TestVal('Testing trailing space','0 ',ValShouldSucceedAfterRemovingTrail,0);
  159. TestVal('Testing trailing 2 spaces','0 ',ValShouldSucceedAfterRemovingTrail,0);
  160. TestVal('Testing trailing 2 tabs','0'#9#9,ValShouldSucceedAfterRemovingTrail,0);
  161. TestVal('Testing trailing 3 spaces','0 ',ValShouldSucceedAfterRemovingTrail,0);
  162. TestVal('Testing trailing 3 tabs','0'#9#9#9,ValShouldSucceedAfterRemovingTrail,0);
  163. TestVal('Testing trailing space/tab combination','0'#9' ',ValShouldSucceedAfterRemovingTrail,0);
  164. TestVal('Testing trailing space/tab combination','0 '#9,ValShouldSucceedAfterRemovingTrail,0);
  165. TestVal('Testing trailing space/tab combination','0 '#9' ',ValShouldSucceedAfterRemovingTrail,0);
  166. TestVal('Testing trailing space/tab combination','0'#9' '#9' ',ValShouldSucceedAfterRemovingTrail,0);
  167. TestVal('Testing several zeroes',' 00'#0,ValShouldSucceed,0);
  168. TestVal('Testing normal zero','0',ValShouldSucceed,0);
  169. TestVal('Testing several zeroes','00',ValShouldSucceed,0);
  170. TestVal('Testing normal zero with leading space',' 0',ValShouldSucceed,0);
  171. TestVal('Testing several zeroes with leading space',' 00',ValShouldSucceed,0);
  172. TestVal('Testing string with base prefix and zero','0x0',ValShouldSucceed,0);
  173. TestVal('Testing string with base prefix and zero','x0',ValShouldSucceed,0);
  174. TestVal('Testing string with base prefix and zero','X0',ValShouldSucceed,0);
  175. TestVal('Testing string with base prefix and zero','$0',ValShouldSucceed,0);
  176. TestVal('Testing string with base prefix and zero','%0',ValShouldSucceed,0);
  177. TestVal('Testing string with base prefix and zero','&0',ValShouldSucceed,0);
  178. TestVal('Testing string with base prefix and one','0x1',ValShouldSucceed,1);
  179. TestVal('Testing string with base prefix and one','x1',ValShouldSucceed,1);
  180. TestVal('Testing string with base prefix and one','X1',ValShouldSucceed,1);
  181. TestVal('Testing string with base prefix and one','$1',ValShouldSucceed,1);
  182. TestVal('Testing string with base prefix and one','%1',ValShouldSucceed,1);
  183. TestVal('Testing string with base prefix and one','&1',ValShouldSucceed,1);
  184. TestVal('Testing string with base prefix and two','0x2',ValShouldSucceed,2);
  185. TestVal('Testing string with base prefix and two','x2',ValShouldSucceed,2);
  186. TestVal('Testing string with base prefix and two','X2',ValShouldSucceed,2);
  187. TestVal('Testing string with base prefix and two','$2',ValShouldSucceed,2);
  188. TestVal('Testing string with base prefix and two','%2',ValShouldFail,0);
  189. TestVal('Testing string with base prefix and two','&2',ValShouldSucceed,2);
  190. TestVal('Testing string with base prefix and seven','0x7',ValShouldSucceed,7);
  191. TestVal('Testing string with base prefix and seven','x7',ValShouldSucceed,7);
  192. TestVal('Testing string with base prefix and seven','X7',ValShouldSucceed,7);
  193. TestVal('Testing string with base prefix and seven','$7',ValShouldSucceed,7);
  194. TestVal('Testing string with base prefix and seven','%7',ValShouldFail,0);
  195. TestVal('Testing string with base prefix and seven','&7',ValShouldSucceed,7);
  196. TestVal('Testing string with base prefix and eight','0x8',ValShouldSucceed,8);
  197. TestVal('Testing string with base prefix and eight','x8',ValShouldSucceed,8);
  198. TestVal('Testing string with base prefix and eight','X8',ValShouldSucceed,8);
  199. TestVal('Testing string with base prefix and eight','$8',ValShouldSucceed,8);
  200. TestVal('Testing string with base prefix and eight','%8',ValShouldFail,0);
  201. TestVal('Testing string with base prefix and eight','&8',ValShouldFail,0);
  202. TestVal('Testing string with base prefix and nine','0x9',ValShouldSucceed,9);
  203. TestVal('Testing string with base prefix and nine','x9',ValShouldSucceed,9);
  204. TestVal('Testing string with base prefix and nine','X9',ValShouldSucceed,9);
  205. TestVal('Testing string with base prefix and nine','$9',ValShouldSucceed,9);
  206. TestVal('Testing string with base prefix and nine','%9',ValShouldFail,0);
  207. TestVal('Testing string with base prefix and nine','&9',ValShouldFail,0);
  208. TestVal('Testing string with base prefix and "a"','0xa',ValShouldSucceed,10);
  209. TestVal('Testing string with base prefix and "a"','xa',ValShouldSucceed,10);
  210. TestVal('Testing string with base prefix and "a"','Xa',ValShouldSucceed,10);
  211. TestVal('Testing string with base prefix and "a"','$a',ValShouldSucceed,10);
  212. TestVal('Testing string with base prefix and "a"','%a',ValShouldFail,0);
  213. TestVal('Testing string with base prefix and "a"','&a',ValShouldFail,0);
  214. TestVal('Testing string with base prefix and "A"','0xA',ValShouldSucceed,10);
  215. TestVal('Testing string with base prefix and "A"','xA',ValShouldSucceed,10);
  216. TestVal('Testing string with base prefix and "A"','XA',ValShouldSucceed,10);
  217. TestVal('Testing string with base prefix and "A"','$A',ValShouldSucceed,10);
  218. TestVal('Testing string with base prefix and "A"','%A',ValShouldFail,0);
  219. TestVal('Testing string with base prefix and "A"','&A',ValShouldFail,0);
  220. TestVal('Testing string with base prefix and "f"','0xf',ValShouldSucceed,15);
  221. TestVal('Testing string with base prefix and "f"','xf',ValShouldSucceed,15);
  222. TestVal('Testing string with base prefix and "f"','Xf',ValShouldSucceed,15);
  223. TestVal('Testing string with base prefix and "f"','$f',ValShouldSucceed,15);
  224. TestVal('Testing string with base prefix and "f"','%f',ValShouldFail,0);
  225. TestVal('Testing string with base prefix and "f"','&f',ValShouldFail,0);
  226. TestVal('Testing string with base prefix and "F"','0xF',ValShouldSucceed,15);
  227. TestVal('Testing string with base prefix and "F"','xF',ValShouldSucceed,15);
  228. TestVal('Testing string with base prefix and "F"','XF',ValShouldSucceed,15);
  229. TestVal('Testing string with base prefix and "F"','$F',ValShouldSucceed,15);
  230. TestVal('Testing string with base prefix and "F"','%F',ValShouldFail,0);
  231. TestVal('Testing string with base prefix and "F"','&F',ValShouldFail,0);
  232. // TestVal('Testing -zero','-0',ValShouldSucceed,0);
  233. TestVal('Testing +zero','+0',ValShouldSucceed,0);
  234. TestVal('Testing - zero','- 0',ValShouldFail,0);
  235. TestVal('Testing + zero','+ 0',ValShouldFail,0);
  236. TestVal('Testing --zero','--0',ValShouldFail,0);
  237. TestVal('Testing ++zero','++0',ValShouldFail,0);
  238. TestVal('Testing -+zero','-+0',ValShouldFail,0);
  239. TestBase('%', ValidNumeralsBase2);
  240. TestBase('&', ValidNumeralsBase8);
  241. TestBase('', ValidNumeralsBase10);
  242. TestBase('0x', ValidNumeralsBase16);
  243. if HasErrors then
  244. begin
  245. Writeln(FailCount,' tests failed over ',SuccessCount+FailCount);
  246. end
  247. else
  248. Writeln('All tests succeeded count=',SuccessCount);
  249. TestAll:=HasErrors;
  250. end;