ObjectListTest.pm.svn-base
上传用户:market2
上传日期:2018-11-18
资源大小:18786k
文件大小:7k
源码类别:

外挂编程

开发平台:

Windows_Unix

  1. package ObjectListTest;
  2. use strict;
  3. use Test::More;
  4. use Utils::ObjectList;
  5. {
  6. # A random class that's only used for testing ObjectList.
  7. package ObjectListTest::Object;
  8. sub new {
  9. return bless {}, $_[0];
  10. }
  11. }
  12. sub start {
  13. print "### Starting ObjectListTestn";
  14. ObjectListTest->new()->run();
  15. }
  16. ###################################
  17. sub new {
  18. my ($class) = @_;
  19. return bless {}, $class;
  20. }
  21. # Run the test.
  22. sub run {
  23. my ($self) = @_;
  24. $self->testAdd();
  25. $self->testClear();
  26. $self->testRemove();
  27. $self->testClear();
  28. $self->testDuplicate();
  29. }
  30. # Reset to the initial condition.
  31. sub init {
  32. my ($self) = @_;
  33. $self->{list} = new ObjectList;
  34. for (my $i = 1; $i <= 6; $i++) {
  35. $self->{"item$i"} = $self->createTestObject();
  36. }
  37. is($self->{list}->size(), 0);
  38. $self->{list}->checkValidity();
  39. }
  40. sub createTestObject {
  41. return new ObjectListTest::Object;
  42. }
  43. sub testAdd {
  44. my ($self) = @_;
  45. $self->init();
  46. my %indices;
  47. my @items;
  48. my $addCalled = 0;
  49. my $sub = sub {
  50. my $arg = $_[2];
  51. ok($arg->[0] == $items[$#items]);
  52. $addCalled++;
  53. };
  54. my $ID = $self->{list}->onAdd()->add(undef, $sub);
  55. for (my $i = 0; $i < 10; $i++) {
  56. my $item = $self->createTestObject();
  57. push @items, $item;
  58. my $index = $self->{list}->add($item);
  59. is($self->{list}->size(), $i + 1);
  60. $self->{list}->checkValidity();
  61. # Test whether previous indices didn't change.
  62. for (my $j = 0; $j < $i; $j++) {
  63. ok($self->{list}->get($j) == $items[$j]);
  64. }
  65. # Test whether we don't get duplicate indices.
  66. ok(!exists $indices{$index});
  67. $indices{$index} = 1;
  68. }
  69. is_deeply($self->{list}->getItems(), @items);
  70. $self->{list}->onAdd()->remove($ID);
  71. }
  72. sub testRemove {
  73. my ($self) = @_;
  74. $self->init();
  75. my $index1 = $self->{list}->add($self->{item1});
  76. my $index2 = $self->{list}->add($self->{item2});
  77. my $index3 = $self->{list}->add($self->{item3});
  78. my $index4 = $self->{list}->add($self->{item4});
  79. my $index5 = $self->{list}->add($self->{item5});
  80. my ($deletedItem, $deletedIndex);
  81. my $sub = sub {
  82. my $arg = $_[2];
  83. ok($arg->[0] == $deletedItem);
  84. };
  85. $self->{list}->onRemove()->add(undef, $sub);
  86. # Delete item 2
  87. $deletedItem = $self->{item2};
  88. $deletedIndex = $index2;
  89. $self->{list}->remove($deletedItem);
  90. is($self->{list}->size(), 4);
  91. ok($self->{list}->get($index1) == $self->{item1});
  92. ok(!defined $self->{list}->get($index2));
  93. ok($self->{list}->get($index3) == $self->{item3});
  94. ok($self->{list}->get($index4) == $self->{item4});
  95. ok($self->{list}->get($index5) == $self->{item5});
  96. is($self->{list}->find($self->{item1}), $index1);
  97. is($self->{list}->find($self->{item2}), -1);
  98. is($self->{list}->find($self->{item3}), $index3);
  99. is($self->{list}->find($self->{item4}), $index4);
  100. is($self->{list}->find($self->{item5}), $index5);
  101. is($self->{list}->find($self->{item6}), -1);
  102. is_deeply($self->{list}->getItems(), [$self->{item1}, $self->{item3}, $self->{item4}, $self->{item5}]);
  103. $self->{list}->checkValidity();
  104. # Delete item 4
  105. $deletedItem = $self->{item4};
  106. $deletedIndex = $index4;
  107. $self->{list}->remove($deletedItem);
  108. is($self->{list}->size(), 3);
  109. ok($self->{list}->get($index1) == $self->{item1});
  110. ok(!defined $self->{list}->get($index2));
  111. ok($self->{list}->get($index3) == $self->{item3});
  112. ok(!defined $self->{list}->get($index4));
  113. ok($self->{list}->get($index5) == $self->{item5});
  114. is($self->{list}->find($self->{item1}), $index1);
  115. is($self->{list}->find($self->{item2}), -1);
  116. is($self->{list}->find($self->{item3}), $index3);
  117. is($self->{list}->find($self->{item4}), -1);
  118. is($self->{list}->find($self->{item5}), $index5);
  119. is($self->{list}->find($self->{item6}), -1);
  120. is_deeply($self->{list}->getItems(), [$self->{item1}, $self->{item3}, $self->{item5}]);
  121. $self->{list}->checkValidity();
  122. # Put back item 2
  123. my $index = $self->{list}->add($self->{item2});
  124. is($index, $index2);
  125. is($self->{list}->size(), 4);
  126. ok($self->{list}->get($index1) == $self->{item1});
  127. ok($self->{list}->get($index2) == $self->{item2});
  128. ok($self->{list}->get($index3) == $self->{item3});
  129. ok(!defined $self->{list}->get($index4));
  130. ok($self->{list}->get($index5) == $self->{item5});
  131. is($self->{list}->find($self->{item1}), $index1);
  132. is($self->{list}->find($self->{item2}), $index2);
  133. is($self->{list}->find($self->{item3}), $index3);
  134. is($self->{list}->find($self->{item4}), -1);
  135. is($self->{list}->find($self->{item5}), $index5);
  136. is($self->{list}->find($self->{item6}), -1);
  137. is_deeply($self->{list}->getItems(), [$self->{item1}, $self->{item2}, $self->{item3}, $self->{item5}]);
  138. $self->{list}->checkValidity();
  139. # Remove nonexistant item
  140. ok(!$self->{list}->remove($self->{item6}));
  141. is($self->{list}->size(), 4);
  142. ok($self->{list}->get($index1) == $self->{item1});
  143. ok($self->{list}->get($index2) == $self->{item2});
  144. ok($self->{list}->get($index3) == $self->{item3});
  145. ok(!defined $self->{list}->get($index4));
  146. ok($self->{list}->get($index5) == $self->{item5});
  147. is($self->{list}->find($self->{item1}), $index1);
  148. is($self->{list}->find($self->{item2}), $index2);
  149. is($self->{list}->find($self->{item3}), $index3);
  150. is($self->{list}->find($self->{item4}), -1);
  151. is($self->{list}->find($self->{item5}), $index5);
  152. is($self->{list}->find($self->{item6}), -1);
  153. is_deeply($self->{list}->getItems(), [$self->{item1}, $self->{item2}, $self->{item3}, $self->{item5}]);
  154. $self->{list}->checkValidity();
  155. # Put back item 4
  156. $index = $self->{list}->add($self->{item4});
  157. is($index, $index4);
  158. is($self->{list}->size(), 5);
  159. ok($self->{list}->get($index1) == $self->{item1});
  160. ok($self->{list}->get($index2) == $self->{item2});
  161. ok($self->{list}->get($index3) == $self->{item3});
  162. ok($self->{list}->get($index4) == $self->{item4});
  163. ok($self->{list}->get($index5) == $self->{item5});
  164. is($self->{list}->find($self->{item1}), $index1);
  165. is($self->{list}->find($self->{item2}), $index2);
  166. is($self->{list}->find($self->{item3}), $index3);
  167. is($self->{list}->find($self->{item4}), $index4);
  168. is($self->{list}->find($self->{item5}), $index5);
  169. is($self->{list}->find($self->{item6}), -1);
  170. is_deeply($self->{list}->getItems(), [$self->{item1}, $self->{item2}, $self->{item3}, $self->{item4}, $self->{item5}]);
  171. $self->{list}->checkValidity();
  172. }
  173. sub testClear {
  174. my ($self) = @_;
  175. my $clearBeginCalled = 0;
  176. my $clearEndCalled = 0;
  177. my $oldSize;
  178. my $sub1 = sub {
  179. $clearBeginCalled++;
  180. is($self->{list}->size(), $oldSize);
  181. };
  182. my $sub2 = sub {
  183. $clearEndCalled++;
  184. };
  185. my $ID1 = $self->{list}->onClearBegin()->add(undef, $sub1);
  186. my $ID2 = $self->{list}->onClearBegin()->add(undef, $sub2);
  187. $oldSize = $self->{list}->size();
  188. $self->{list}->clear();
  189. $self->{list}->onClearBegin()->remove($ID1);
  190. $self->{list}->onClearBegin()->remove($ID2);
  191. is($clearBeginCalled, 1);
  192. is($clearEndCalled, 1);
  193. is($self->{list}->size(), 0);
  194. $self->{list}->checkValidity();
  195. for (my $i = 0; $i < 15; $i++) {
  196. ok(!defined $self->{list}->get($i));
  197. }
  198. is_deeply($self->{list}->getItems(), []);
  199. }
  200. # Test addition and removal of duplicate items.
  201. sub testDuplicate {
  202. my ($self) = @_;
  203. $self->init();
  204. my $index1 = $self->{list}->add($self->{item1});
  205. my $index2 = $self->{list}->add($self->{item2});
  206. my $index3 = $self->{list}->add($self->{item1});
  207. my $index4 = $self->{list}->add($self->{item3});
  208. $self->{list}->remove($self->{item1});
  209. $self->{list}->remove($self->{item2});
  210. is($self->{list}->size(), 2);
  211. ok(!defined $self->{list}->get($index1));
  212. ok(!defined $self->{list}->get($index2));
  213. ok($self->{list}->get($index3) == $self->{item1});
  214. ok($self->{list}->get($index4) == $self->{item3});
  215. is_deeply($self->{list}->getItems(), [$self->{item1}, $self->{item3}]);
  216. $self->{list}->checkValidity();
  217. }
  218. 1;