aboutsummaryrefslogtreecommitdiff
path: root/guava-testlib/src/com/google/common/testing/CollectorTester.java
blob: 2e154c1750e3a8bf9373b7f4c1706855732c446e (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
/*
 * Copyright (C) 2015 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.testing;

import static com.google.common.base.Preconditions.checkNotNull;
import static junit.framework.Assert.assertTrue;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.stream.Collector;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * Tester for {@code Collector} implementations.
 *
 * <p>Example usage:
 *
 * <pre>
 * CollectorTester.of(Collectors.summingInt(Integer::parseInt))
 *     .expectCollects(3, "1", "2")
 *     .expectCollects(10, "1", "4", "3", "2")
 *     .expectCollects(5, "-3", "0", "8");
 * </pre>
 *
 * @author Louis Wasserman
 * @since 21.0
 */
@GwtCompatible
@ElementTypesAreNonnullByDefault
public final class CollectorTester<
    T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object> {
  /**
   * Creates a {@code CollectorTester} for the specified {@code Collector}. The result of the {@code
   * Collector} will be compared to the expected value using {@link Object#equals}.
   */
  public static <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
      CollectorTester<T, A, R> of(Collector<T, A, R> collector) {
    return of(collector, Objects::equals);
  }

  /**
   * Creates a {@code CollectorTester} for the specified {@code Collector}. The result of the {@code
   * Collector} will be compared to the expected value using the specified {@code equivalence}.
   */
  public static <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
      CollectorTester<T, A, R> of(
          Collector<T, A, R> collector, BiPredicate<? super R, ? super R> equivalence) {
    return new CollectorTester<>(collector, equivalence);
  }

  private final Collector<T, A, R> collector;
  private final BiPredicate<? super R, ? super R> equivalence;

  private CollectorTester(
      Collector<T, A, R> collector, BiPredicate<? super R, ? super R> equivalence) {
    this.collector = checkNotNull(collector);
    this.equivalence = checkNotNull(equivalence);
  }

  /**
   * Different orderings for combining the elements of an input array, which must all produce the
   * same result.
   */
  enum CollectStrategy {
    /** Get one accumulator and accumulate the elements into it sequentially. */
    SEQUENTIAL {
      @Override
      final <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
          A result(Collector<T, A, R> collector, Iterable<T> inputs) {
        A accum = collector.supplier().get();
        for (T input : inputs) {
          collector.accumulator().accept(accum, input);
        }
        return accum;
      }
    },
    /** Get one accumulator for each element and merge the accumulators left-to-right. */
    MERGE_LEFT_ASSOCIATIVE {
      @Override
      final <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
          A result(Collector<T, A, R> collector, Iterable<T> inputs) {
        A accum = collector.supplier().get();
        for (T input : inputs) {
          A newAccum = collector.supplier().get();
          collector.accumulator().accept(newAccum, input);
          accum = collector.combiner().apply(accum, newAccum);
        }
        return accum;
      }
    },
    /** Get one accumulator for each element and merge the accumulators right-to-left. */
    MERGE_RIGHT_ASSOCIATIVE {
      @Override
      final <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
          A result(Collector<T, A, R> collector, Iterable<T> inputs) {
        List<A> stack = new ArrayList<>();
        for (T input : inputs) {
          A newAccum = collector.supplier().get();
          collector.accumulator().accept(newAccum, input);
          push(stack, newAccum);
        }
        push(stack, collector.supplier().get());
        while (stack.size() > 1) {
          A right = pop(stack);
          A left = pop(stack);
          push(stack, collector.combiner().apply(left, right));
        }
        return pop(stack);
      }

      <E extends @Nullable Object> void push(List<E> stack, E value) {
        stack.add(value);
      }

      <E extends @Nullable Object> E pop(List<E> stack) {
        return stack.remove(stack.size() - 1);
      }
    };

    abstract <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
        A result(Collector<T, A, R> collector, Iterable<T> inputs);
  }

  /**
   * Verifies that the specified expected result is always produced by collecting the specified
   * inputs, regardless of how the elements are divided.
   */
  @SafeVarargs
  @CanIgnoreReturnValue
  @SuppressWarnings("nullness") // TODO(cpovirk): Remove after we fix whatever the bug is.
  public final CollectorTester<T, A, R> expectCollects(R expectedResult, T... inputs) {
    List<T> list = Arrays.asList(inputs);
    doExpectCollects(expectedResult, list);
    if (collector.characteristics().contains(Collector.Characteristics.UNORDERED)) {
      Collections.reverse(list);
      doExpectCollects(expectedResult, list);
    }
    return this;
  }

  private void doExpectCollects(R expectedResult, List<T> inputs) {
    for (CollectStrategy scheme : CollectStrategy.values()) {
      A finalAccum = scheme.result(collector, inputs);
      if (collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
        assertEquivalent(expectedResult, (R) finalAccum);
      }
      assertEquivalent(expectedResult, collector.finisher().apply(finalAccum));
    }
  }

  private void assertEquivalent(R expected, R actual) {
    assertTrue(
        "Expected " + expected + " got " + actual + " modulo equivalence " + equivalence,
        equivalence.test(expected, actual));
  }
}