001/*
002 * Copyright (C) 2015 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.testing;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020import static junit.framework.Assert.assertTrue;
021
022import com.google.common.annotations.Beta;
023import com.google.common.annotations.GwtCompatible;
024import java.util.ArrayList;
025import java.util.Arrays;
026import java.util.Collections;
027import java.util.EnumSet;
028import java.util.List;
029import java.util.Objects;
030import java.util.function.BiPredicate;
031import java.util.stream.Collector;
032import org.checkerframework.checker.nullness.qual.Nullable;
033
034/**
035 * Tester for {@code Collector} implementations.
036 *
037 * <p>Example usage:
038 *
039 * <pre>
040 * CollectorTester.of(Collectors.summingInt(Integer::parseInt))
041 *     .expectCollects(3, "1", "2")
042 *     .expectCollects(10, "1", "4", "3", "2")
043 *     .expectCollects(5, "-3", "0", "8");
044 * </pre>
045 *
046 * @author Louis Wasserman
047 * @since 21.0
048 */
049@Beta
050@GwtCompatible
051public final class CollectorTester<T, A, R> {
052  /**
053   * Creates a {@code CollectorTester} for the specified {@code Collector}. The result of the {@code
054   * Collector} will be compared to the expected value using {@link Object.equals}.
055   */
056  public static <T, A, R> CollectorTester<T, A, R> of(Collector<T, A, R> collector) {
057    return of(collector, Objects::equals);
058  }
059
060  /**
061   * Creates a {@code CollectorTester} for the specified {@code Collector}. The result of the {@code
062   * Collector} will be compared to the expected value using the specified {@code equivalence}.
063   */
064  public static <T, A, R> CollectorTester<T, A, R> of(
065      Collector<T, A, R> collector, BiPredicate<? super R, ? super R> equivalence) {
066    return new CollectorTester<>(collector, equivalence);
067  }
068
069  private final Collector<T, A, R> collector;
070  private final BiPredicate<? super R, ? super R> equivalence;
071
072  private CollectorTester(
073      Collector<T, A, R> collector, BiPredicate<? super R, ? super R> equivalence) {
074    this.collector = checkNotNull(collector);
075    this.equivalence = checkNotNull(equivalence);
076  }
077
078  /**
079   * Different orderings for combining the elements of an input array, which must all produce the
080   * same result.
081   */
082  enum CollectStrategy {
083    /** Get one accumulator and accumulate the elements into it sequentially. */
084    SEQUENTIAL {
085      @Override
086      final <T, A, R> A result(Collector<T, A, R> collector, Iterable<T> inputs) {
087        A accum = collector.supplier().get();
088        for (T input : inputs) {
089          collector.accumulator().accept(accum, input);
090        }
091        return accum;
092      }
093    },
094    /** Get one accumulator for each element and merge the accumulators left-to-right. */
095    MERGE_LEFT_ASSOCIATIVE {
096      @Override
097      final <T, A, R> A result(Collector<T, A, R> collector, Iterable<T> inputs) {
098        A accum = collector.supplier().get();
099        for (T input : inputs) {
100          A newAccum = collector.supplier().get();
101          collector.accumulator().accept(newAccum, input);
102          accum = collector.combiner().apply(accum, newAccum);
103        }
104        return accum;
105      }
106    },
107    /** Get one accumulator for each element and merge the accumulators right-to-left. */
108    MERGE_RIGHT_ASSOCIATIVE {
109      @Override
110      final <T, A, R> A result(Collector<T, A, R> collector, Iterable<T> inputs) {
111        List<A> stack = new ArrayList<>();
112        for (T input : inputs) {
113          A newAccum = collector.supplier().get();
114          collector.accumulator().accept(newAccum, input);
115          push(stack, newAccum);
116        }
117        push(stack, collector.supplier().get());
118        while (stack.size() > 1) {
119          A right = pop(stack);
120          A left = pop(stack);
121          push(stack, collector.combiner().apply(left, right));
122        }
123        return pop(stack);
124      }
125
126      <E> void push(List<E> stack, E value) {
127        stack.add(value);
128      }
129
130      <E> E pop(List<E> stack) {
131        return stack.remove(stack.size() - 1);
132      }
133    };
134
135    abstract <T, A, R> A result(Collector<T, A, R> collector, Iterable<T> inputs);
136  }
137
138  /**
139   * Verifies that the specified expected result is always produced by collecting the specified
140   * inputs, regardless of how the elements are divided.
141   */
142  @SafeVarargs
143  public final CollectorTester<T, A, R> expectCollects(@Nullable R expectedResult, T... inputs) {
144    List<T> list = Arrays.asList(inputs);
145    doExpectCollects(expectedResult, list);
146    if (collector.characteristics().contains(Collector.Characteristics.UNORDERED)) {
147      Collections.reverse(list);
148      doExpectCollects(expectedResult, list);
149    }
150    return this;
151  }
152
153  private void doExpectCollects(@Nullable R expectedResult, List<T> inputs) {
154    for (CollectStrategy scheme : EnumSet.allOf(CollectStrategy.class)) {
155      A finalAccum = scheme.result(collector, inputs);
156      if (collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
157        assertEquivalent(expectedResult, (R) finalAccum);
158      }
159      assertEquivalent(expectedResult, collector.finisher().apply(finalAccum));
160    }
161  }
162
163  private void assertEquivalent(@Nullable R expected, @Nullable R actual) {
164    assertTrue(
165        "Expected " + expected + " got " + actual + " modulo equivalence " + equivalence,
166        equivalence.test(expected, actual));
167  }
168}