tval.inc 13 KB

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