Browse Source

tests: for-in loop tests:
+ add small comment at the top of test program

git-svn-id: branches/paul/features@13951 -

paul 16 years ago
parent
commit
b38c78dd55

+ 1 - 0
.gitattributes

@@ -8216,6 +8216,7 @@ tests/test/tforin4.pp svneol=native#text/pascal
 tests/test/tforin5.pp svneol=native#text/pascal
 tests/test/tforin6.pp svneol=native#text/pascal
 tests/test/tforin7.pp svneol=native#text/pascal
+tests/test/tforin8.pp svneol=native#text/pascal
 tests/test/tfpu1.pp svneol=native#text/plain
 tests/test/tfpu2.pp svneol=native#text/plain
 tests/test/tfpu3.pp svneol=native#text/plain

+ 2 - 0
tests/test/tforin1.pp

@@ -1,5 +1,7 @@
 program tforin1;
 
+// test for-in loop for the basic types: Enumeration, Range, Array, String and Set
+
 {$mode objfpc}{$H+}
 {$apptype console}
 uses

+ 2 - 0
tests/test/tforin2.pp

@@ -1,5 +1,7 @@
 program tforin2;
 
+// test operator Enumerator for the string type and built-in into RTL TStrings enumerator
+
 {$mode objfpc}{$H+}
 {$apptype console}
 uses

+ 3 - 2
tests/test/tforin3.pp

@@ -1,8 +1,9 @@
 program tforin3;
 
-{$APPTYPE CONSOLE}
+// check for-in loop if GetEnumerator returns nil
 
-// check the GetEnumerator nil return
+{$mode objfpc}{$H+}
+{$APPTYPE CONSOLE}
 
 type
 

+ 3 - 0
tests/test/tforin4.pp

@@ -1,5 +1,8 @@
 program tforin4;
 
+// test that for-in loop allows array modifications inside the loop body
+
+{$mode objfpc}{$H+}
 {$APPTYPE CONSOLE}
 
 type

+ 3 - 0
tests/test/tforin5.pp

@@ -1,5 +1,8 @@
 program tforin5;
 
+// test operator Enumerator support for classes
+
+{$mode objfpc}{$H+}
 {$APPTYPE CONSOLE}
 
 type

+ 3 - 0
tests/test/tforin6.pp

@@ -1,5 +1,8 @@
 program tforin6;
 
+// test Object enumerator implementation
+
+{$mode objfpc}{$H+}
 {$APPTYPE CONSOLE}
 
 type

+ 3 - 0
tests/test/tforin7.pp

@@ -1,5 +1,8 @@
 program tforin7;
 
+// test 'enumerator MoveNext' and 'enumrator Current' directives
+
+{$mode objfpc}{$H+}
 {$APPTYPE CONSOLE}
 
 type

+ 61 - 0
tests/test/tforin8.pp

@@ -0,0 +1,61 @@
+program tformin8;
+
+// Test IEnumerable and IEnumerator in the for-in loop
+
+{$mode objfpc}{$H+}
+{$APPTYPE CONSOLE}
+
+type
+  { TMyListEnumerator }
+
+  TMyListEnumerator = class(TInterfacedObject, IEnumerator)
+  private
+    FCurrent: Pointer;
+    function GetCurrent: TObject;
+  public
+    function MoveNext: Boolean;
+    procedure Reset;
+    property Current: TObject read GetCurrent;
+  end;
+
+  TMyList = class(TInterfacedObject, IEnumerable)
+  public
+    function GetEnumerator: IEnumerator;
+  end;
+
+{ TMyListEnumerator }
+
+
+function TMyListEnumerator.GetCurrent: TObject;
+begin
+  Result := TObject(FCurrent);
+end;
+
+function TMyListEnumerator.MoveNext: Boolean;
+begin
+  inc(PByte(FCurrent));
+  Result := FCurrent <= Pointer(3);
+end;
+
+procedure TMyListEnumerator.Reset;
+begin
+  FCurrent := nil;
+end;
+
+{ TMyList }
+
+function TMyList.GetEnumerator: IEnumerator;
+begin
+  Result := TMyListEnumerator.Create;
+end;
+
+var
+  List: IEnumerable;
+  i: TObject;
+begin
+  List := TMyList.Create;
+  for i in List do
+    WriteLn(Integer(i));
+  List := nil;
+end.
+