PageRenderTime 152ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/testability-explorer/src/test/java/com/google/test/metric/collection/KeyedMultiStackTest.java

http://testability-explorer.googlecode.com/
Java | 291 lines | 232 code | 44 blank | 15 comment | 3 complexity | 68f28eeb429be16c678110c98fc59151 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2007 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric.collection;
  17. import static java.util.Arrays.asList;
  18. import static java.util.Collections.emptyList;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. import java.util.Set;
  22. import java.util.TreeSet;
  23. import junit.framework.TestCase;
  24. public class KeyedMultiStackTest extends TestCase {
  25. public static class Push extends PopClosure<String, Integer> {
  26. private final List<Integer> items;
  27. public Push(Integer... integers) {
  28. items = Arrays.asList(integers);
  29. }
  30. @Override
  31. public int getSize() {
  32. return 0;
  33. }
  34. @Override
  35. public List<Integer> pop(String key, List<Integer> list) {
  36. return items;
  37. }
  38. }
  39. private class NoopClosure extends PopClosure<String, Integer> {
  40. private final int size;
  41. public NoopClosure(int size) {
  42. this.size = size;
  43. }
  44. @Override
  45. public int getSize() {
  46. return size;
  47. }
  48. @Override
  49. public List<Integer> pop(String key, List<Integer> list) {
  50. return emptyList();
  51. }
  52. }
  53. private class LoggingClosure extends PopClosure<String, Integer> {
  54. private final int size;
  55. public LoggingClosure(int size) {
  56. this.size = size;
  57. }
  58. @Override
  59. public List<Integer> pop(String key, List<Integer> value) {
  60. log.add(value.toString());
  61. return emptyList();
  62. }
  63. @Override
  64. public int getSize() {
  65. return size;
  66. }
  67. }
  68. KeyedMultiStack<String, Integer> stack = new KeyedMultiStack<String, Integer>(
  69. "", new KeyedMultiStack.ValueCompactor<Integer>());
  70. Set<String> log = new TreeSet<String>();
  71. public void testBasicOperationsOnSingleDimension() throws Exception {
  72. stack.apply("", new Push(0));
  73. stack.apply("", new PopClosure<String, Integer>() {
  74. @Override
  75. public List<Integer> pop(String key, List<Integer> value) {
  76. assertEquals("", key);
  77. assertEquals(1, value.size());
  78. assertEquals(new Integer(0), value.get(0));
  79. log.add(value.get(0).toString());
  80. return emptyList();
  81. }
  82. @Override
  83. public int getSize() {
  84. return 1;
  85. }
  86. });
  87. assertEquals("[0]", log.toString());
  88. stack.assertEmpty();
  89. }
  90. public void testToString() throws Exception {
  91. stack.apply("", new Push(0));
  92. assertNotNull(stack.toString());
  93. }
  94. public void testPushPushPopOnSplit() throws Exception {
  95. stack.apply("", new Push(0));
  96. stack.split("", asList("a", "b"));
  97. stack.apply("a", new Push(1));
  98. stack.apply("b", new Push(2));
  99. stack.apply("a", new LoggingClosure(2));
  100. stack.apply("b", new LoggingClosure(2));
  101. assertEquals("[[0, 1], [0, 2]]", log.toString());
  102. }
  103. public void testPushSplitPushJoinPOP() throws Exception {
  104. stack.apply("", new Push(0));
  105. stack.split("", asList("a", "b"));
  106. stack.apply("a", new Push(1));
  107. stack.apply("b", new Push(2));
  108. stack.join(asList("a", "b"), "c");
  109. stack.apply("c", new Push(3));
  110. stack.apply("c", new LoggingClosure(3));
  111. assertEquals("[[0, 1, 3], [0, 2, 3]]", log.toString());
  112. }
  113. public void testSplitAndJoinShouldCollapsMultipleStacksIfTheyAreOfSameContent()
  114. throws Exception {
  115. stack.apply("", new Push(0));
  116. stack.split("", asList("a", "b"));
  117. stack.join(asList("a", "b"), "");
  118. stack.apply("", new Push(1));
  119. stack.apply("", new LoggingClosure(2));
  120. assertEquals("[[0, 1]]", log.toString());
  121. }
  122. public void testConcurentPushInPopClosure() throws Exception {
  123. stack.apply("", new Push(0));
  124. stack.apply("", new Push(1));
  125. stack.apply("", new PopClosure<String, Integer>() {
  126. @Override
  127. public List<Integer> pop(String key, List<Integer> value) {
  128. stack.apply(key, new Push(value.get(0) + 10));
  129. return emptyList();
  130. }
  131. @Override
  132. public int getSize() {
  133. return 1;
  134. }
  135. });
  136. stack.apply("", new LoggingClosure(2));
  137. assertEquals("[[0, 11]]", log.toString());
  138. }
  139. public void testPopTooMuch() throws Exception {
  140. try {
  141. stack.apply("", new LoggingClosure(1));
  142. fail();
  143. } catch (KeyedMultiStack.StackUnderflowException e) {
  144. }
  145. }
  146. public void testUnknownKey() throws Exception {
  147. try {
  148. stack.apply("X", new Push());
  149. fail();
  150. } catch (KeyedMultiStack.KeyNotFoundException e) {
  151. }
  152. }
  153. public void testSplitUnknwonNamespace() throws Exception {
  154. try {
  155. stack.split("X", asList("A", "B"));
  156. fail();
  157. } catch (KeyedMultiStack.KeyNotFoundException e) {
  158. }
  159. }
  160. public void testJoinUnknownNamespace() throws Exception {
  161. try {
  162. stack.join(asList("B", "C"), "A");
  163. fail();
  164. } catch (KeyedMultiStack.KeyNotFoundException e) {
  165. }
  166. }
  167. public void testUnevenJoin() throws Exception {
  168. stack.split("", asList("a", "b"));
  169. stack.apply("a", new Push(0));
  170. try {
  171. stack.join(asList("a", "b"), "c");
  172. fail();
  173. } catch (IllegalStateException e) {
  174. }
  175. }
  176. public void testJoinThroughSlipt() throws Exception {
  177. stack.apply("", new Push(0));
  178. stack.split("", asList("a", "b"));
  179. stack.apply("a", new Push(1));
  180. stack.apply("b", new Push(2));
  181. stack.split("a", asList("join"));
  182. stack.split("b", asList("join"));
  183. stack.apply("join", new LoggingClosure(2));
  184. assertEquals("[[0, 1], [0, 2]]", log.toString());
  185. }
  186. public void testParalelPopAndPush() throws Exception {
  187. stack.apply("", new Push(0));
  188. stack.apply("", new Push(1));
  189. stack.split("", asList("a", "b"));
  190. stack.apply("a", new Push(2));
  191. stack.apply("b", new Push(3));
  192. stack.join(asList("a", "b"), "join");
  193. stack.apply("join", new PopClosure<String, Integer>() {
  194. int id = 3;
  195. @Override
  196. public List<Integer> pop(String key, List<Integer> list) {
  197. return asList(id++, id++);
  198. }
  199. @Override
  200. public int getSize() {
  201. return 2;
  202. }
  203. });
  204. stack.apply("join", new LoggingClosure(3));
  205. assertEquals("[[0, 3, 4], [0, 5, 6]]", log.toString());
  206. }
  207. public void testPathEnsureSize() throws Exception {
  208. KeyedMultiStack.Path<String> path = new KeyedMultiStack.Path<String>();
  209. path.add("A");
  210. path.add("B");
  211. path.add("C");
  212. path.add("D");
  213. path.add("E");
  214. path.add("F");
  215. path.add("G");
  216. assertEquals("A :: B :: C :: D :: E :: F :: G", path.toString());
  217. }
  218. public void testPathHashCode() throws Exception {
  219. KeyedMultiStack.Path<String> p1 = new KeyedMultiStack.Path<String>();
  220. KeyedMultiStack.Path<String> p2 = new KeyedMultiStack.Path<String>();
  221. assertEquals(p1, p2);
  222. assertEquals(p1.hashCode(), p2.hashCode());
  223. }
  224. public void testPopTooSlowForVeryLargeSets() throws Exception {
  225. long start = System.currentTimeMillis();
  226. int counter = 0;
  227. String[] subKeys = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"};
  228. stack.split("", asList(subKeys));
  229. for (String key : subKeys) {
  230. stack.apply(key, new Push(counter++));
  231. }
  232. stack.join(asList(subKeys), "L1");
  233. stack.split("L1", asList(subKeys));
  234. for (String key : subKeys) {
  235. stack.apply(key, new Push(counter++));
  236. }
  237. stack.join(asList(subKeys), "L2");
  238. stack.split("L2", asList(subKeys));
  239. for (String key : subKeys) {
  240. stack.apply(key, new Push(counter++));
  241. }
  242. stack.join(asList(subKeys), "L3");
  243. stack.apply("L3", new NoopClosure(3));
  244. long duration = System.currentTimeMillis() - start;
  245. assertTrue("Duration: " + duration, duration < 90);
  246. }
  247. }