PageRenderTime 21ms CodeModel.GetById 1ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/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
  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 */
 16package com.google.test.metric.collection;
 17
 18import static java.util.Arrays.asList;
 19import static java.util.Collections.emptyList;
 20
 21import java.util.Arrays;
 22import java.util.List;
 23import java.util.Set;
 24import java.util.TreeSet;
 25
 26import junit.framework.TestCase;
 27
 28public class KeyedMultiStackTest extends TestCase {
 29
 30  public static class Push extends PopClosure<String, Integer> {
 31
 32    private final List<Integer> items;
 33
 34    public Push(Integer... integers) {
 35      items = Arrays.asList(integers);
 36    }
 37
 38    @Override
 39    public int getSize() {
 40      return 0;
 41    }
 42
 43    @Override
 44    public List<Integer> pop(String key, List<Integer> list) {
 45      return items;
 46    }
 47
 48  }
 49
 50  private class NoopClosure extends PopClosure<String, Integer> {
 51
 52    private final int size;
 53
 54    public NoopClosure(int size) {
 55      this.size = size;
 56    }
 57
 58    @Override
 59    public int getSize() {
 60      return size;
 61    }
 62
 63    @Override
 64    public List<Integer> pop(String key, List<Integer> list) {
 65      return emptyList();
 66    }
 67
 68  }
 69  private class LoggingClosure extends PopClosure<String, Integer> {
 70
 71    private final int size;
 72
 73    public LoggingClosure(int size) {
 74      this.size = size;
 75    }
 76
 77    @Override
 78    public List<Integer> pop(String key, List<Integer> value) {
 79      log.add(value.toString());
 80      return emptyList();
 81    }
 82
 83    @Override
 84    public int getSize() {
 85      return size;
 86    }
 87  }
 88
 89  KeyedMultiStack<String, Integer> stack = new KeyedMultiStack<String, Integer>(
 90      "", new KeyedMultiStack.ValueCompactor<Integer>());
 91
 92  Set<String> log = new TreeSet<String>();
 93
 94  public void testBasicOperationsOnSingleDimension() throws Exception {
 95    stack.apply("", new Push(0));
 96    stack.apply("", new PopClosure<String, Integer>() {
 97      @Override
 98      public List<Integer> pop(String key, List<Integer> value) {
 99        assertEquals("", key);
100        assertEquals(1, value.size());
101        assertEquals(new Integer(0), value.get(0));
102        log.add(value.get(0).toString());
103        return emptyList();
104      }
105
106      @Override
107      public int getSize() {
108        return 1;
109      }
110    });
111    assertEquals("[0]", log.toString());
112    stack.assertEmpty();
113  }
114
115  public void testToString() throws Exception {
116    stack.apply("", new Push(0));
117    assertNotNull(stack.toString());
118  }
119
120  public void testPushPushPopOnSplit() throws Exception {
121    stack.apply("", new Push(0));
122    stack.split("", asList("a", "b"));
123    stack.apply("a", new Push(1));
124    stack.apply("b", new Push(2));
125    stack.apply("a", new LoggingClosure(2));
126    stack.apply("b", new LoggingClosure(2));
127    assertEquals("[[0, 1], [0, 2]]", log.toString());
128  }
129
130  public void testPushSplitPushJoinPOP() throws Exception {
131    stack.apply("", new Push(0));
132    stack.split("", asList("a", "b"));
133    stack.apply("a", new Push(1));
134    stack.apply("b", new Push(2));
135    stack.join(asList("a", "b"), "c");
136    stack.apply("c", new Push(3));
137    stack.apply("c", new LoggingClosure(3));
138    assertEquals("[[0, 1, 3], [0, 2, 3]]", log.toString());
139  }
140
141  public void testSplitAndJoinShouldCollapsMultipleStacksIfTheyAreOfSameContent()
142      throws Exception {
143    stack.apply("", new Push(0));
144    stack.split("", asList("a", "b"));
145    stack.join(asList("a", "b"), "");
146    stack.apply("", new Push(1));
147    stack.apply("", new LoggingClosure(2));
148    assertEquals("[[0, 1]]", log.toString());
149  }
150
151  public void testConcurentPushInPopClosure() throws Exception {
152    stack.apply("", new Push(0));
153    stack.apply("", new Push(1));
154    stack.apply("", new PopClosure<String, Integer>() {
155      @Override
156      public List<Integer> pop(String key, List<Integer> value) {
157        stack.apply(key, new Push(value.get(0) + 10));
158        return emptyList();
159      }
160
161      @Override
162      public int getSize() {
163        return 1;
164      }
165    });
166    stack.apply("", new LoggingClosure(2));
167    assertEquals("[[0, 11]]", log.toString());
168  }
169
170  public void testPopTooMuch() throws Exception {
171    try {
172      stack.apply("", new LoggingClosure(1));
173      fail();
174    } catch (KeyedMultiStack.StackUnderflowException e) {
175    }
176  }
177
178  public void testUnknownKey() throws Exception {
179    try {
180      stack.apply("X", new Push());
181      fail();
182    } catch (KeyedMultiStack.KeyNotFoundException e) {
183    }
184  }
185
186  public void testSplitUnknwonNamespace() throws Exception {
187    try {
188      stack.split("X", asList("A", "B"));
189      fail();
190    } catch (KeyedMultiStack.KeyNotFoundException e) {
191    }
192  }
193
194  public void testJoinUnknownNamespace() throws Exception {
195    try {
196      stack.join(asList("B", "C"), "A");
197      fail();
198    } catch (KeyedMultiStack.KeyNotFoundException e) {
199    }
200  }
201
202  public void testUnevenJoin() throws Exception {
203    stack.split("", asList("a", "b"));
204    stack.apply("a", new Push(0));
205    try {
206      stack.join(asList("a", "b"), "c");
207      fail();
208    } catch (IllegalStateException e) {
209    }
210  }
211
212  public void testJoinThroughSlipt() throws Exception {
213    stack.apply("", new Push(0));
214    stack.split("", asList("a", "b"));
215    stack.apply("a", new Push(1));
216    stack.apply("b", new Push(2));
217    stack.split("a", asList("join"));
218    stack.split("b", asList("join"));
219    stack.apply("join", new LoggingClosure(2));
220    assertEquals("[[0, 1], [0, 2]]", log.toString());
221  }
222
223  public void testParalelPopAndPush() throws Exception {
224    stack.apply("", new Push(0));
225    stack.apply("", new Push(1));
226    stack.split("", asList("a", "b"));
227    stack.apply("a", new Push(2));
228    stack.apply("b", new Push(3));
229    stack.join(asList("a", "b"), "join");
230    stack.apply("join", new PopClosure<String, Integer>() {
231      int id = 3;
232      @Override
233      public List<Integer> pop(String key, List<Integer> list) {
234        return asList(id++, id++);
235      }
236
237      @Override
238      public int getSize() {
239        return 2;
240      }
241    });
242    stack.apply("join", new LoggingClosure(3));
243    assertEquals("[[0, 3, 4], [0, 5, 6]]", log.toString());
244  }
245
246  public void testPathEnsureSize() throws Exception {
247    KeyedMultiStack.Path<String> path = new KeyedMultiStack.Path<String>();
248    path.add("A");
249    path.add("B");
250    path.add("C");
251    path.add("D");
252    path.add("E");
253    path.add("F");
254    path.add("G");
255    assertEquals("A :: B :: C :: D :: E :: F :: G", path.toString());
256  }
257
258  public void testPathHashCode() throws Exception {
259    KeyedMultiStack.Path<String> p1 = new KeyedMultiStack.Path<String>();
260    KeyedMultiStack.Path<String> p2 = new KeyedMultiStack.Path<String>();
261    assertEquals(p1, p2);
262    assertEquals(p1.hashCode(), p2.hashCode());
263  }
264
265  public void testPopTooSlowForVeryLargeSets() throws Exception {
266    long start = System.currentTimeMillis();
267    int counter = 0;
268    String[] subKeys = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"};
269    stack.split("", asList(subKeys));
270    for (String key : subKeys) {
271      stack.apply(key, new Push(counter++));
272    }
273    stack.join(asList(subKeys), "L1");
274
275    stack.split("L1", asList(subKeys));
276    for (String key : subKeys) {
277      stack.apply(key, new Push(counter++));
278    }
279    stack.join(asList(subKeys), "L2");
280
281    stack.split("L2", asList(subKeys));
282    for (String key : subKeys) {
283      stack.apply(key, new Push(counter++));
284    }
285    stack.join(asList(subKeys), "L3");
286
287    stack.apply("L3", new NoopClosure(3));
288    long duration = System.currentTimeMillis() - start;
289    assertTrue("Duration: " + duration, duration < 90);
290  }
291}