Browse Source

+ more comprehensive accepted val pattern test

git-svn-id: trunk@7130 -
pierre 18 years ago
parent
commit
2cf09d68f7
2 changed files with 292 additions and 0 deletions
  1. 1 0
      .gitattributes
  2. 291 0
      tests/test/units/system/tval.pp

+ 1 - 0
.gitattributes

@@ -7017,6 +7017,7 @@ tests/test/units/system/tslice2.pp svneol=native#text/plain
 tests/test/units/system/tstring.pp svneol=native#text/plain
 tests/test/units/system/ttrig.pas svneol=native#text/plain
 tests/test/units/system/ttrunc.pp svneol=native#text/plain
+tests/test/units/system/tval.pp -text
 tests/test/units/sysutils/execansi.pp svneol=native#text/plain
 tests/test/units/sysutils/execedbya.pp svneol=native#text/plain
 tests/test/units/sysutils/extractquote.pp svneol=native#text/plain

+ 291 - 0
tests/test/units/system/tval.pp

@@ -0,0 +1,291 @@
+
+const
+  HasErrors : boolean = false;
+  Silent : boolean = false;
+  CheckVal : boolean = true;
+
+type
+  TCharSet = set of char;
+const
+  ValidNumeralsBase2 : TCHarSet = ['0'..'1'];
+  ValidNumeralsBase8 : TCHarSet = ['0'..'7'];
+  ValidNumeralsBase10 : TCHarSet = ['0'..'9'];
+  ValidNumeralsBase16 : TCHarSet = ['0'..'9','a'..'f','A'..'F'];
+  SpecialCharsFirst : TCharSet = [' ',#9,'x','X','$','&','%','+','-'];
+  SpecialCharsSecond : TCharSet = [#0];
+
+type
+
+  ValTestType =
+  (ValShouldFail,
+   ValShouldSucceed,
+   ValShouldSucceedAfterRemovingTrail);
+
+
+function Display(const s : string) : string;
+var
+  res,ordval : string;
+  i : longint;
+  quoted : boolean;
+begin
+  res:='"';
+  quoted:=false;
+  for i:=1 to length(s) do
+    if ord(s[i])<32 then
+      begin
+        if quoted then
+          res:=res+'''';
+        str(ord(s[i]),ordval);
+        res:=res+'#'+ordval;
+        quoted:=false;
+      end
+    else
+      begin
+        if not quoted then
+          res:=res+'''';
+        quoted:=true;
+        res:=res+s[i];
+      end;
+  if quoted then
+    res:=res+'''';
+  res:=res+'"';
+  Display:=res;
+end;
+
+procedure TestVal(comment,s : string; ExpectedRes : ValTestType; expected : longint);
+var
+  i : longint;
+  err,err1 : word;
+begin
+  if not silent and (Comment<>'') then
+    Writeln(Comment);
+  Val(s,i,err);
+  if ExpectedRes=ValShouldFail then
+    begin
+      if err=0 then
+        begin
+          Writeln('Error: string ',Display(s),
+            ' is a valid input for val function');
+          HasErrors:=true;
+        end
+      else
+        begin
+          if not silent then
+            Writeln('Correct: string ',Display(s),
+              ' is a not valid input for val function');
+        end;
+    end
+  else if ExpectedRes=ValShouldSucceed then
+    begin
+      if err=0 then
+        begin
+          if not silent then
+            Writeln('Correct: string ',Display(s),
+              ' is a valid input for val function');
+        end
+      else
+        begin
+          Writeln('Error: string ',Display(s),
+            ' is a not valid input for val function',
+            ' error pos=',err);
+          HasErrors:=true;
+        end;
+    end
+  else if ExpectedRes=ValShouldSucceedAfterRemovingTrail then
+    begin
+      if err=0 then
+        begin
+          Writeln('Error: string ',Display(s),
+            ' is a valid input for val function');
+          HasErrors:=true;
+        end
+      else
+        begin
+          err1:=err;
+          Val(Copy(s,1,err1-1),i,err);
+          if err=0 then
+            begin
+              if not silent then
+                Writeln('Correct: string ',Display(s),
+                  ' is a valid input for val function up to position ',err1);
+            end
+          else
+            begin
+              Writeln('Error: string ',Display(Copy(s,1,err1-1)),
+                ' is a not valid input for val function',
+                ' error pos=',err);
+              HasErrors:=true;
+            end;
+        end;
+    end;
+  if (err=0) and CheckVal and (i<>expected) then
+    begin
+      Writeln('Error: string ',Display(s),
+        ' value is ',i,' <> ',expected);
+          HasErrors:=true;
+    end;
+end;
+
+Procedure TestBase(Const Prefix : string;ValidChars : TCharSet);
+var
+  i,j : longint;
+  st : string;
+begin
+  CheckVal:=false;
+  Silent:=true;
+  for i:=0 to 255 do
+    begin
+      st:=prefix+chr(i);
+      if chr(i) in ValidChars then
+        TestVal('',st,ValShouldSucceed,0)
+      else
+        TestVal('',st,ValShouldFail,0);
+    end;
+  for i:=0 to 255 do
+    for j:=0 to 255 do
+      begin
+        st:=prefix+chr(i)+chr(j);
+        if (chr(i) in ValidChars) and
+           (chr(j) in ValidChars) then
+          TestVal('',st,ValShouldSucceed,0)
+        else
+          begin
+            if ((prefix<>'') or
+               (not (chr(i) in SpecialCharsFirst))) and
+                not (chr(j) in SpecialCharsSecond) then
+              TestVal('',st,ValShouldFail,0);
+          end;
+      end;
+end;
+
+var
+  S : string;
+begin
+  if (paramcount>0) and
+     (paramstr(1)='silent') then
+       silent:=true;
+  TestVal('Testing empty string','',ValShouldFail,0);
+  TestVal('Testing string with #0',#0,ValShouldFail,0);
+  TestVal('Testing string with base prefix and no value','0x',ValShouldFail,0);
+  TestVal('Testing string with base prefix and no value','x',ValShouldFail,0);
+  TestVal('Testing string with base prefix and no value','X',ValShouldFail,0);
+  TestVal('Testing string with base prefix and no value','$',ValShouldFail,0);
+  TestVal('Testing string with base prefix and no value','%',ValShouldFail,0);
+  TestVal('Testing string with base prefix and no value','&',ValShouldFail,0);
+  TestVal('Testing string with base prefix and #0','0x'#0,ValShouldFail,0);
+  TestVal('Testing normal ''''0'''' string','0',ValShouldSucceed,0);
+  TestVal('Testing leading space',' 0',ValShouldSucceed,0);
+  TestVal('Testing leading 2 spaces','  0',ValShouldSucceed,0);
+  TestVal('Testing leading 2 tabs',#9#9'0',ValShouldSucceed,0);
+  TestVal('Testing leading 3 spaces','   0',ValShouldSucceed,0);
+  TestVal('Testing leading 3 tabs',#9#9#9'0',ValShouldSucceed,0);
+  TestVal('Testing leading space/tab combination',#9' 0',ValShouldSucceed,0);
+  TestVal('Testing leading space/tab combination',' '#9'0',ValShouldSucceed,0);
+  TestVal('Testing leading space/tab combination',' '#9' 0',ValShouldSucceed,0);
+  TestVal('Testing leading space/tab combination',#9' '#9' 0',ValShouldSucceed,0);
+  TestVal('Testing #0 following normal ''''0''','0'#0,ValShouldSucceed,0);
+  TestVal('Testing leading space with trailing #0',' 0'#0,ValShouldSucceed,0);
+  TestVal('Testing leading 2 spaces with trailing #0','  0'#0,ValShouldSucceed,0);
+  TestVal('Testing leading 2 tabs with trailing #0',#9#9'0'#0,ValShouldSucceed,0);
+  TestVal('Testing leading 3 spaces with trailing #0','   0'#0,ValShouldSucceed,0);
+  TestVal('Testing leading 3 tabs with trailing #0',#9#9#9'0'#0,ValShouldSucceed,0);
+  TestVal('Testing leading space/tab combination with trailing #0',#9' 0'#0,ValShouldSucceed,0);
+  TestVal('Testing leading space/tab combination with trailing #0',' '#9'0'#0,ValShouldSucceed,0);
+  TestVal('Testing leading space/tab combination with trailing #0',' '#9' 0'#0,ValShouldSucceed,0);
+  TestVal('Testing leading space/tab combination with trailing #0',#9' '#9' 0'#0,ValShouldSucceed,0);
+  TestVal('Testing trailing space','0 ',ValShouldSucceedAfterRemovingTrail,0);
+  TestVal('Testing trailing 2 spaces','0  ',ValShouldSucceedAfterRemovingTrail,0);
+  TestVal('Testing trailing 2 tabs','0'#9#9,ValShouldSucceedAfterRemovingTrail,0);
+  TestVal('Testing trailing 3 spaces','0   ',ValShouldSucceedAfterRemovingTrail,0);
+  TestVal('Testing trailing 3 tabs','0'#9#9#9,ValShouldSucceedAfterRemovingTrail,0);
+  TestVal('Testing trailing space/tab combination','0'#9' ',ValShouldSucceedAfterRemovingTrail,0);
+  TestVal('Testing trailing space/tab combination','0 '#9,ValShouldSucceedAfterRemovingTrail,0);
+  TestVal('Testing trailing space/tab combination','0 '#9' ',ValShouldSucceedAfterRemovingTrail,0);
+  TestVal('Testing trailing space/tab combination','0'#9' '#9' ',ValShouldSucceedAfterRemovingTrail,0);
+  TestVal('Testing several zeroes',' 00'#0,ValShouldSucceed,0);
+  TestVal('Testing normal zero','0',ValShouldSucceed,0);
+  TestVal('Testing several zeroes','00',ValShouldSucceed,0);
+  TestVal('Testing normal zero with leading space',' 0',ValShouldSucceed,0);
+  TestVal('Testing several zeroes with leading space',' 00',ValShouldSucceed,0);
+  TestVal('Testing string with base prefix and zero','0x0',ValShouldSucceed,0);
+  TestVal('Testing string with base prefix and zero','x0',ValShouldSucceed,0);
+  TestVal('Testing string with base prefix and zero','X0',ValShouldSucceed,0);
+  TestVal('Testing string with base prefix and zero','$0',ValShouldSucceed,0);
+  TestVal('Testing string with base prefix and zero','%0',ValShouldSucceed,0);
+  TestVal('Testing string with base prefix and zero','&0',ValShouldSucceed,0);
+  TestVal('Testing string with base prefix and one','0x1',ValShouldSucceed,1);
+  TestVal('Testing string with base prefix and one','x1',ValShouldSucceed,1);
+  TestVal('Testing string with base prefix and one','X1',ValShouldSucceed,1);
+  TestVal('Testing string with base prefix and one','$1',ValShouldSucceed,1);
+  TestVal('Testing string with base prefix and one','%1',ValShouldSucceed,1);
+  TestVal('Testing string with base prefix and one','&1',ValShouldSucceed,1);
+  TestVal('Testing string with base prefix and two','0x2',ValShouldSucceed,2);
+  TestVal('Testing string with base prefix and two','x2',ValShouldSucceed,2);
+  TestVal('Testing string with base prefix and two','X2',ValShouldSucceed,2);
+  TestVal('Testing string with base prefix and two','$2',ValShouldSucceed,2);
+  TestVal('Testing string with base prefix and two','%2',ValShouldFail,0);
+  TestVal('Testing string with base prefix and two','&2',ValShouldSucceed,2);
+  TestVal('Testing string with base prefix and seven','0x7',ValShouldSucceed,7);
+  TestVal('Testing string with base prefix and seven','x7',ValShouldSucceed,7);
+  TestVal('Testing string with base prefix and seven','X7',ValShouldSucceed,7);
+  TestVal('Testing string with base prefix and seven','$7',ValShouldSucceed,7);
+  TestVal('Testing string with base prefix and seven','%7',ValShouldFail,0);
+  TestVal('Testing string with base prefix and seven','&7',ValShouldSucceed,7);
+  TestVal('Testing string with base prefix and eight','0x8',ValShouldSucceed,8);
+  TestVal('Testing string with base prefix and eight','x8',ValShouldSucceed,8);
+  TestVal('Testing string with base prefix and eight','X8',ValShouldSucceed,8);
+  TestVal('Testing string with base prefix and eight','$8',ValShouldSucceed,8);
+  TestVal('Testing string with base prefix and eight','%8',ValShouldFail,0);
+  TestVal('Testing string with base prefix and eight','&8',ValShouldFail,0);
+  TestVal('Testing string with base prefix and nine','0x9',ValShouldSucceed,9);
+  TestVal('Testing string with base prefix and nine','x9',ValShouldSucceed,9);
+  TestVal('Testing string with base prefix and nine','X9',ValShouldSucceed,9);
+  TestVal('Testing string with base prefix and nine','$9',ValShouldSucceed,9);
+  TestVal('Testing string with base prefix and nine','%9',ValShouldFail,0);
+  TestVal('Testing string with base prefix and nine','&9',ValShouldFail,0);
+  TestVal('Testing string with base prefix and "a"','0xa',ValShouldSucceed,10);
+  TestVal('Testing string with base prefix and "a"','xa',ValShouldSucceed,10);
+  TestVal('Testing string with base prefix and "a"','Xa',ValShouldSucceed,10);
+  TestVal('Testing string with base prefix and "a"','$a',ValShouldSucceed,10);
+  TestVal('Testing string with base prefix and "a"','%a',ValShouldFail,0);
+  TestVal('Testing string with base prefix and "a"','&a',ValShouldFail,0);
+  TestVal('Testing string with base prefix and "A"','0xA',ValShouldSucceed,10);
+  TestVal('Testing string with base prefix and "A"','xA',ValShouldSucceed,10);
+  TestVal('Testing string with base prefix and "A"','XA',ValShouldSucceed,10);
+  TestVal('Testing string with base prefix and "A"','$A',ValShouldSucceed,10);
+  TestVal('Testing string with base prefix and "A"','%A',ValShouldFail,0);
+  TestVal('Testing string with base prefix and "A"','&A',ValShouldFail,0);
+  TestVal('Testing string with base prefix and "f"','0xf',ValShouldSucceed,15);
+  TestVal('Testing string with base prefix and "f"','xf',ValShouldSucceed,15);
+  TestVal('Testing string with base prefix and "f"','Xf',ValShouldSucceed,15);
+  TestVal('Testing string with base prefix and "f"','$f',ValShouldSucceed,15);
+  TestVal('Testing string with base prefix and "f"','%f',ValShouldFail,0);
+  TestVal('Testing string with base prefix and "f"','&f',ValShouldFail,0);
+  TestVal('Testing string with base prefix and "F"','0xF',ValShouldSucceed,15);
+  TestVal('Testing string with base prefix and "F"','xF',ValShouldSucceed,15);
+  TestVal('Testing string with base prefix and "F"','XF',ValShouldSucceed,15);
+  TestVal('Testing string with base prefix and "F"','$F',ValShouldSucceed,15);
+  TestVal('Testing string with base prefix and "F"','%F',ValShouldFail,0);
+  TestVal('Testing string with base prefix and "F"','&F',ValShouldFail,0);
+
+  TestVal('Testing -zero','-0',ValShouldSucceed,0);
+  TestVal('Testing +zero','+0',ValShouldSucceed,0);
+  TestVal('Testing - zero','- 0',ValShouldFail,0);
+  TestVal('Testing + zero','+ 0',ValShouldFail,0);
+  TestVal('Testing --zero','--0',ValShouldFail,0);
+  TestVal('Testing ++zero','++0',ValShouldFail,0);
+  TestVal('Testing -+zero','-+0',ValShouldFail,0);
+
+  TestBase('%', ValidNumeralsBase2);
+  TestBase('&', ValidNumeralsBase8);
+  TestBase('', ValidNumeralsBase10);
+  TestBase('0x', ValidNumeralsBase16);
+
+  if HasErrors then
+    begin
+      Writeln('At least one test failed');
+      Halt(1);
+    end
+  else
+    Writeln('All tests succeeded');
+end.