1{$mode objfpc}
2
3unit gsettest;
4
5interface
6
7uses fpcunit, testregistry, gset, gutil;
8
9type lesslli=specialize TLess<longint>;
10     setlli=specialize TSet<longint,lesslli>;
11
12type TGSetTest = class(TTestCase)
13  Published
14    procedure SetTest;
15    procedure IteratorTest;
16  public
17    procedure Setup;override;
18  private
19    data:setlli;
20  end;
21
22implementation
23
24procedure TGSetTest.IteratorTest;
25var it:setlli.TIterator;
26begin
27  it:=data.min;
28  if (it <> nil) then
29    AssertEquals('not null min', 0, 1);
30  data.insert(3);
31  data.insert(5);
32  data.insert(7);
33  it:=data.min;
34  AssertEquals('bad value', 3, it.Data);
35  AssertEquals('next not true', true, it.Next);
36  AssertEquals('bad value', 5, it.Data);
37  AssertEquals('next not true', true, it.Next);
38  AssertEquals('bad value', 7, it.Data);
39  AssertEquals('next not false', false, it.Next);
40end;
41
42procedure TGSetTest.SetTest;
43var it:setlli.TIterator;
44begin
45  data.insert(3);
46  data.insert(5);
47  data.insert(7);
48  AssertEquals('Wrong min', 3, data.min().Data);
49  AssertEquals('Wrong max', 7, data.max().Data);
50  data.delete(3);
51  AssertEquals('Wrong size', 2, data.size);
52  AssertEquals('Wrong min', 5, data.min().Data);
53  data.insert(3);
54  data.insert(3);
55  data.insert(3);
56  AssertEquals('Wrong size', 3, data.size);
57  AssertEquals('Wrong min', 3, data.min().Data);
58  if(data.find(4)<>nil) then
59    Fail('Found key which not there');
60  if(data.find(5)=nil) then
61    Fail('Not found key which was there');
62
63  if(data.FindLess(8).Data<>7) then
64    Fail('Wrong less than 8');
65  if(data.FindLess(7).Data<>5) then
66    Fail('Wrong less than 7');
67  if(data.FindLess(3)<>nil) then
68    Fail('Wrong less than 3');
69
70  if(data.FindLessEqual(8).Data<>7) then
71    Fail('Wrong less equal than 8');
72  if(data.FindLessEqual(7).Data<>7) then
73    Fail('Wrong less equal than 7');
74  if(data.FindLessEqual(6).Data<>5) then
75    Fail('Wrong less equal than 6');
76  if(data.FindLessEqual(2)<>nil) then
77    Fail('Wrong less equal than 2');
78
79  if(data.FindGreater(2).Data<>3) then
80    Fail('Wrong greater than 2');
81  if(data.Findgreater(3).Data<>5) then
82    Fail('Wrong greater than 3');
83  if(data.Findgreater(7)<>nil) then
84    Fail('Wrong greater than 7');
85
86  if(data.FindGreaterEqual(2).Data<>3) then
87    Fail('Wrong greater equal than 2');
88  if(data.FindGreaterEqual(3).Data<>3) then
89    Fail('Wrong greater equal than 3');
90  if(data.FindGreaterEqual(4).Data<>5) then
91    Fail('Wrong greater equal than 4');
92  if(data.FindGreaterEqual(8)<>nil) then
93    Fail('Wrong greater equal than 8');
94
95  data.insert(17);
96
97  it:=data.min;
98  AssertEquals('Wrong min', 3, it.Data);
99  AssertEquals('Next not true', true, it.next);
100  AssertEquals('Wrong next', 5, it.Data);
101  AssertEquals('Next not true', true, it.next);
102  AssertEquals('Wrong next', 7, it.Data);
103  AssertEquals('Next not true', true, it.next);
104  AssertEquals('Wrong next', 17, it.Data);
105  AssertEquals('Last next not fail', false, it.next);
106
107  it:=data.max;
108  AssertEquals('Wrong max', 17, it.Data);
109  AssertEquals('Prev not true', true, it.prev);
110  AssertEquals('Wrong prev', 7, it.Data);
111  AssertEquals('Prev not true', true, it.prev);
112  AssertEquals('Wrong prev', 5, it.Data);
113  AssertEquals('Prev not true', true, it.prev);
114  AssertEquals('Wrong prev', 3, it.Data);
115  AssertEquals('First prev not fail', false, it.prev);
116end;
117
118procedure TGSetTest.Setup;
119begin
120  data:=setlli.create;
121end;
122
123initialization
124  RegisterTest(TGSetTest);
125end.
126