aboutsummaryrefslogtreecommitdiff
path: root/android/guava-testlib/src/com/google/common/collect/testing/Helpers.java
blob: 290e8e300f774e9f56cafc7cc7e0ce48c0daf102 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * Copyright (C) 2009 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect.testing;

import static java.util.Collections.sort;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import junit.framework.Assert;
import junit.framework.AssertionFailedError;
import org.checkerframework.checker.nullness.qual.Nullable;

@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
public class Helpers {
  // Clone of Objects.equal
  static boolean equal(@Nullable Object a, @Nullable Object b) {
    return a == b || (a != null && a.equals(b));
  }

  // Clone of Lists.newArrayList
  public static <E extends @Nullable Object> List<E> copyToList(Iterable<? extends E> elements) {
    List<E> list = new ArrayList<>();
    addAll(list, elements);
    return list;
  }

  public static <E extends @Nullable Object> List<E> copyToList(E[] elements) {
    return copyToList(Arrays.asList(elements));
  }

  // Clone of Sets.newLinkedHashSet
  public static <E extends @Nullable Object> Set<E> copyToSet(Iterable<? extends E> elements) {
    Set<E> set = new LinkedHashSet<>();
    addAll(set, elements);
    return set;
  }

  public static <E extends @Nullable Object> Set<E> copyToSet(E[] elements) {
    return copyToSet(Arrays.asList(elements));
  }

  // Would use Maps.immutableEntry
  public static <K extends @Nullable Object, V extends @Nullable Object> Entry<K, V> mapEntry(
      K key, V value) {
    return Collections.singletonMap(key, value).entrySet().iterator().next();
  }

  private static boolean isEmpty(Iterable<?> iterable) {
    return iterable instanceof Collection
        ? ((Collection<?>) iterable).isEmpty()
        : !iterable.iterator().hasNext();
  }

  public static void assertEmpty(Iterable<?> iterable) {
    if (!isEmpty(iterable)) {
      Assert.fail("Not true that " + iterable + " is empty");
    }
  }

  public static void assertEmpty(Map<?, ?> map) {
    if (!map.isEmpty()) {
      Assert.fail("Not true that " + map + " is empty");
    }
  }

  public static void assertEqualInOrder(Iterable<?> expected, Iterable<?> actual) {
    Iterator<?> expectedIter = expected.iterator();
    Iterator<?> actualIter = actual.iterator();

    while (expectedIter.hasNext() && actualIter.hasNext()) {
      if (!equal(expectedIter.next(), actualIter.next())) {
        Assert.fail(
            "contents were not equal and in the same order: "
                + "expected = "
                + expected
                + ", actual = "
                + actual);
      }
    }

    if (expectedIter.hasNext() || actualIter.hasNext()) {
      // actual either had too few or too many elements
      Assert.fail(
          "contents were not equal and in the same order: "
              + "expected = "
              + expected
              + ", actual = "
              + actual);
    }
  }

  public static void assertContentsInOrder(Iterable<?> actual, Object... expected) {
    assertEqualInOrder(Arrays.asList(expected), actual);
  }

  public static void assertEqualIgnoringOrder(Iterable<?> expected, Iterable<?> actual) {
    List<?> exp = copyToList(expected);
    List<?> act = copyToList(actual);
    String actString = act.toString();

    // Of course we could take pains to give the complete description of the
    // problem on any failure.

    // Yeah it's n^2.
    for (Object object : exp) {
      if (!act.remove(object)) {
        Assert.fail(
            "did not contain expected element "
                + object
                + ", "
                + "expected = "
                + exp
                + ", actual = "
                + actString);
      }
    }
    assertTrue("unexpected elements: " + act, act.isEmpty());
  }

  public static void assertContentsAnyOrder(Iterable<?> actual, Object... expected) {
    assertEqualIgnoringOrder(Arrays.asList(expected), actual);
  }

  public static void assertContains(Iterable<?> actual, Object expected) {
    boolean contained = false;
    if (actual instanceof Collection) {
      contained = ((Collection<?>) actual).contains(expected);
    } else {
      for (Object o : actual) {
        if (equal(o, expected)) {
          contained = true;
          break;
        }
      }
    }

    if (!contained) {
      Assert.fail("Not true that " + actual + " contains " + expected);
    }
  }

  public static void assertContainsAllOf(Iterable<?> actual, Object... expected) {
    List<Object> expectedList = new ArrayList<>(Arrays.asList(expected));

    for (Object o : actual) {
      expectedList.remove(o);
    }

    if (!expectedList.isEmpty()) {
      Assert.fail("Not true that " + actual + " contains all of " + Arrays.asList(expected));
    }
  }

  @CanIgnoreReturnValue
  public static <E extends @Nullable Object> boolean addAll(
      Collection<E> addTo, Iterable<? extends E> elementsToAdd) {
    boolean modified = false;
    for (E e : elementsToAdd) {
      modified |= addTo.add(e);
    }
    return modified;
  }

  static <T> Iterable<T> reverse(List<T> list) {
    return new Iterable<T>() {
      @Override
      public Iterator<T> iterator() {
        ListIterator<T> listIter = list.listIterator(list.size());
        return new Iterator<T>() {
          @Override
          public boolean hasNext() {
            return listIter.hasPrevious();
          }

          @Override
          public T next() {
            return listIter.previous();
          }

          @Override
          public void remove() {
            listIter.remove();
          }
        };
      }
    };
  }

  static <T> Iterator<T> cycle(Iterable<T> iterable) {
    return new Iterator<T>() {
      Iterator<T> iterator = Collections.<T>emptySet().iterator();

      @Override
      public boolean hasNext() {
        return true;
      }

      @Override
      public T next() {
        if (!iterator.hasNext()) {
          iterator = iterable.iterator();
        }
        return iterator.next();
      }

      @Override
      public void remove() {
        throw new UnsupportedOperationException();
      }
    };
  }

  static <T> T get(Iterator<T> iterator, int position) {
    for (int i = 0; i < position; i++) {
      iterator.next();
    }
    return iterator.next();
  }

  static void fail(Throwable cause, Object message) {
    AssertionFailedError assertionFailedError = new AssertionFailedError(String.valueOf(message));
    assertionFailedError.initCause(cause);
    throw assertionFailedError;
  }

  private static class EntryComparator<K extends @Nullable Object, V extends @Nullable Object>
      implements Comparator<Entry<K, V>> {
    final @Nullable Comparator<? super K> keyComparator;

    public EntryComparator(@Nullable Comparator<? super K> keyComparator) {
      this.keyComparator = keyComparator;
    }

    @Override
    @SuppressWarnings("unchecked") // no less safe than putting it in the map!
    public int compare(Entry<K, V> a, Entry<K, V> b) {
        return (keyComparator == null)
            ? ((Comparable) a.getKey()).compareTo(b.getKey())
            : keyComparator.compare(a.getKey(), b.getKey());
    }
  }

  public static <K, V> Comparator<Entry<K, V>> entryComparator(
      @Nullable Comparator<? super K> keyComparator) {
    return new EntryComparator<K, V>(keyComparator);
  }

  /**
   * Asserts that all pairs of {@code T} values within {@code valuesInExpectedOrder} are ordered
   * consistently between their order within {@code valuesInExpectedOrder} and the order implied by
   * the given {@code comparator}.
   *
   * @see #testComparator(Comparator, List)
   */
  public static <T> void testComparator(
      Comparator<? super T> comparator, T... valuesInExpectedOrder) {
    testComparator(comparator, Arrays.asList(valuesInExpectedOrder));
  }

  /**
   * Asserts that all pairs of {@code T} values within {@code valuesInExpectedOrder} are ordered
   * consistently between their order within {@code valuesInExpectedOrder} and the order implied by
   * the given {@code comparator}.
   *
   * <p>In detail, this method asserts
   *
   * <ul>
   *   <li><i>reflexivity</i>: {@code comparator.compare(t, t) = 0} for all {@code t} in {@code
   *       valuesInExpectedOrder}; and
   *   <li><i>consistency</i>: {@code comparator.compare(ti, tj) < 0} and {@code
   *       comparator.compare(tj, ti) > 0} for {@code i < j}, where {@code ti =
   *       valuesInExpectedOrder.get(i)} and {@code tj = valuesInExpectedOrder.get(j)}.
   * </ul>
   */
  public static <T extends @Nullable Object> void testComparator(
      Comparator<? super T> comparator, List<T> valuesInExpectedOrder) {
    // This does an O(n^2) test of all pairs of values in both orders
    for (int i = 0; i < valuesInExpectedOrder.size(); i++) {
      T t = valuesInExpectedOrder.get(i);

      for (int j = 0; j < i; j++) {
        T lesser = valuesInExpectedOrder.get(j);
        assertTrue(
            comparator + ".compare(" + lesser + ", " + t + ")", comparator.compare(lesser, t) < 0);
      }

      assertEquals(comparator + ".compare(" + t + ", " + t + ")", 0, comparator.compare(t, t));

      for (int j = i + 1; j < valuesInExpectedOrder.size(); j++) {
        T greater = valuesInExpectedOrder.get(j);
        assertTrue(
            comparator + ".compare(" + greater + ", " + t + ")",
            comparator.compare(greater, t) > 0);
      }
    }
  }

  @SuppressWarnings({"SelfComparison", "SelfEquals"})
  public static <T extends Comparable<? super T>> void testCompareToAndEquals(
      List<T> valuesInExpectedOrder) {
    // This does an O(n^2) test of all pairs of values in both orders
    for (int i = 0; i < valuesInExpectedOrder.size(); i++) {
      T t = valuesInExpectedOrder.get(i);

      for (int j = 0; j < i; j++) {
        T lesser = valuesInExpectedOrder.get(j);
        assertTrue(lesser + ".compareTo(" + t + ')', lesser.compareTo(t) < 0);
        assertFalse(lesser.equals(t));
      }

      assertEquals(t + ".compareTo(" + t + ')', 0, t.compareTo(t));
      assertTrue(t.equals(t));

      for (int j = i + 1; j < valuesInExpectedOrder.size(); j++) {
        T greater = valuesInExpectedOrder.get(j);
        assertTrue(greater + ".compareTo(" + t + ')', greater.compareTo(t) > 0);
        assertFalse(greater.equals(t));
      }
    }
  }

  /**
   * Returns a collection that simulates concurrent modification by having its size method return
   * incorrect values. This is useful for testing methods that must treat the return value from
   * size() as a hint only.
   *
   * @param delta the difference between the true size of the collection and the values returned by
   *     the size method
   */
  public static <T extends @Nullable Object> Collection<T> misleadingSizeCollection(int delta) {
    // It would be nice to be able to return a real concurrent
    // collection like ConcurrentLinkedQueue, so that e.g. concurrent
    // iteration would work, but that would not be GWT-compatible.
    // We are not "just" inheriting from ArrayList here as this doesn't work for J2kt.
    return new AbstractList<T>() {
      ArrayList<T> data = new ArrayList<>();

      @Override
      public int size() {
        return Math.max(0, data.size() + delta);
      }

      @Override
      public T get(int index) {
        return data.get(index);
      }

      @Override
      public T set(int index, T element) {
        return data.set(index, element);
      }

      @Override
      public boolean add(T element) {
        return data.add(element);
      }

      @Override
      public void add(int index, T element) {
        data.add(index, element);
      }

      @Override
      public T remove(int index) {
        return data.remove(index);
      }

      @Override
      public @Nullable Object[] toArray() {
        return data.toArray();
      }
    };
  }

  /**
   * Returns a "nefarious" map entry with the specified key and value, meaning an entry that is
   * suitable for testing that map entries cannot be modified via a nefarious implementation of
   * equals. This is used for testing unmodifiable collections of map entries; for example, it
   * should not be possible to access the raw (modifiable) map entry via a nefarious equals method.
   */
  public static <K extends @Nullable Object, V extends @Nullable Object>
      Entry<K, V> nefariousMapEntry(K key, V value) {
    return new Entry<K, V>() {
      @Override
      public K getKey() {
        return key;
      }

      @Override
      public V getValue() {
        return value;
      }

      @Override
      public V setValue(V value) {
        throw new UnsupportedOperationException();
      }

      @SuppressWarnings("unchecked")
      @Override
      public boolean equals(@Nullable Object o) {
        if (o instanceof Entry) {
          Entry<K, V> e = (Entry<K, V>) o;
          e.setValue(value); // muhahaha!

          return equal(this.getKey(), e.getKey()) && equal(this.getValue(), e.getValue());
        }
        return false;
      }

      @Override
      public int hashCode() {
        K k = getKey();
        V v = getValue();
        return ((k == null) ? 0 : k.hashCode()) ^ ((v == null) ? 0 : v.hashCode());
      }

      @Override
      public String toString() {
        return getKey() + "=" + getValue();
      }
    };
  }

  static <E extends @Nullable Object> List<E> castOrCopyToList(Iterable<E> iterable) {
    if (iterable instanceof List) {
      return (List<E>) iterable;
    }
    List<E> list = new ArrayList<>();
    for (E e : iterable) {
      list.add(e);
    }
    return list;
  }

  private static final Comparator<Comparable> NATURAL_ORDER =
      new Comparator<Comparable>() {
        @SuppressWarnings("unchecked") // assume any Comparable is Comparable<Self>
        @Override
        public int compare(Comparable left, Comparable right) {
          return left.compareTo(right);
        }
      };

  @J2ktIncompatible
  public static <K extends Comparable, V> Iterable<Entry<K, V>> orderEntriesByKey(
      List<Entry<K, V>> insertionOrder) {
    sort(insertionOrder, Helpers.<K, V>entryComparator(NATURAL_ORDER));
    return insertionOrder;
  }

  /**
   * Private replacement for {@link com.google.gwt.user.client.rpc.GwtTransient} to work around
   * build-system quirks.
   */
  private @interface GwtTransient {}

  /**
   * Compares strings in natural order except that null comes immediately before a given value. This
   * works better than Ordering.natural().nullsFirst() because, if null comes before all other
   * values, it lies outside the submap/submultiset ranges we test, and the variety of tests that
   * exercise null handling fail on those subcollections.
   */
  public abstract static class NullsBefore implements Comparator<@Nullable String>, Serializable {
    /*
     * We don't serialize this class in GWT, so we don't care about whether GWT will serialize this
     * field.
     */
    @GwtTransient private final String justAfterNull;

    protected NullsBefore(String justAfterNull) {
      if (justAfterNull == null) {
        throw new NullPointerException();
      }

      this.justAfterNull = justAfterNull;
    }

    @Override
    public int compare(@Nullable String lhs, @Nullable String rhs) {
      if (lhs == rhs) {
        return 0;
      }
      if (lhs == null) {
        // lhs (null) comes just before justAfterNull.
        // If rhs is b, lhs comes first.
        if (rhs.equals(justAfterNull)) {
          return -1;
        }
        return justAfterNull.compareTo(rhs);
      }
      if (rhs == null) {
        // rhs (null) comes just before justAfterNull.
        // If lhs is b, rhs comes first.
        if (lhs.equals(justAfterNull)) {
          return 1;
        }
        return lhs.compareTo(justAfterNull);
      }
      return lhs.compareTo(rhs);
    }

    @Override
    public boolean equals(@Nullable Object obj) {
      if (obj instanceof NullsBefore) {
        NullsBefore other = (NullsBefore) obj;
        return justAfterNull.equals(other.justAfterNull);
      }
      return false;
    }

    @Override
    public int hashCode() {
      return justAfterNull.hashCode();
    }
  }

  public static final class NullsBeforeB extends NullsBefore {
    public static final NullsBeforeB INSTANCE = new NullsBeforeB();

    private NullsBeforeB() {
      super("b");
    }
  }

  public static final class NullsBeforeTwo extends NullsBefore {
    public static final NullsBeforeTwo INSTANCE = new NullsBeforeTwo();

    private NullsBeforeTwo() {
      super("two"); // from TestStringSortedMapGenerator's sample keys
    }
  }

  @J2ktIncompatible
  @GwtIncompatible // reflection
  public static Method getMethod(Class<?> clazz, String name) {
    try {
      return clazz.getMethod(name);
    } catch (Exception e) {
      throw new IllegalArgumentException(e);
    }
  }
}