1 unit Antlr.Runtime.Collections.Tests;
2 {
3 
4   Delphi DUnit Test Case
5   ----------------------
6   This unit contains a skeleton test case class generated by the Test Case Wizard.
7   Modify the generated code to correctly setup and call the methods from the unit
8   being tested.
9 
10 }
11 
12 interface
13 
14 uses
15   TestFramework,
16   Antlr.Runtime.Collections,
17   Generics.Collections,
18   Antlr.Runtime.Tools;
19 
20 type
21   // Test methods for class IHashList
22   TestIHashList = class(TTestCase)
23   strict private
24     FIHashList: IHashList<Integer, String>;
25   public
26     procedure SetUp; override;
27     procedure TearDown; override;
28   published
29     procedure TestInsertionOrder;
30     procedure TestRemove;
31   end;
32 
33   // Test methods for class IStackList
34   TestIStackList = class(TTestCase)
35   strict private
36     FIStackList: IStackList<String>;
37   public
38     procedure SetUp; override;
39     procedure TearDown; override;
40   published
41     procedure TestPushPop;
42     procedure TestPeek;
43   end;
44 
45 implementation
46 
47 uses
48   SysUtils;
49 
50 const
51   Values: array [0..9] of Integer = (50, 1, 33, 76, -22, 22, 34, 2, 88, 12);
52 
53 procedure TestIHashList.SetUp;
54 var
55   I: Integer;
56 begin
57   FIHashList := THashList<Integer, String>.Create;
58   for I in Values do
59     FIHashList.Add(I,'Value' + IntToStr(I));
60 end;
61 
62 procedure TestIHashList.TearDown;
63 begin
64   FIHashList := nil;
65 end;
66 
67 procedure TestIHashList.TestInsertionOrder;
68 var
69   I: Integer;
70   P: TPair<Integer, String>;
71 begin
72   I := 0;
73   for P in FIHashList do
74   begin
75     CheckEquals(P.Key, Values[I]);
76     CheckEquals(P.Value, 'Value' + IntToStr(Values[I]));
77     Inc(I);
78   end;
79 end;
80 
81 procedure TestIHashList.TestRemove;
82 var
83   I: Integer;
84   P: TPair<Integer, String>;
85 begin
86   FIHashList.Remove(34);
87   I := 0;
88   for P in FIHashList do
89   begin
90     if (Values[I] = 34) then
91       Inc(I);
92     CheckEquals(P.Key, Values[I]);
93     CheckEquals(P.Value, 'Value' + IntToStr(Values[I]));
94     Inc(I);
95   end;
96 end;
97 
98 procedure TestIStackList.SetUp;
99 begin
100   FIStackList := TStackList<String>.Create;
101 end;
102 
103 procedure TestIStackList.TearDown;
104 begin
105   FIStackList := nil;
106 end;
107 
108 procedure TestIStackList.TestPushPop;
109 var
110   Item: String;
111 begin
112   Item := 'Item 1';
113   FIStackList.Push(Item);
114   Item := 'Item 2';
115   FIStackList.Push(Item);
116   CheckEquals(FIStackList.Pop,'Item 2');
117   CheckEquals(FIStackList.Pop,'Item 1');
118 end;
119 
120 procedure TestIStackList.TestPeek;
121 begin
122   FIStackList.Push('Item 1');
123   FIStackList.Push('Item 2');
124   FIStackList.Push('Item 3');
125   FIStackList.Pop;
126   CheckEquals(FIStackList.Peek, 'Item 2');
127   CheckEquals(FIStackList.Pop, 'Item 2');
128 end;
129 
130 initialization
131   // Register any test cases with the test runner
132   RegisterTest(TestIHashList.Suite);
133   RegisterTest(TestIStackList.Suite);
134 end.
135