All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.common.collect.testing.google.DerivedGoogleCollectionGenerators Maven / Gradle / Ivy

Go to download

Guava testlib is a set of java classes used for more convenient unit testing - particularly to assist the tests for Guava itself.

The newest version!
/*
 * Copyright (C) 2012 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.google;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.BiMap;
import com.google.common.collect.testing.DerivedGenerator;
import com.google.common.collect.testing.Helpers;
import com.google.common.collect.testing.OneSizeTestContainerGenerator;
import com.google.common.collect.testing.SampleElements;
import com.google.common.collect.testing.TestMapGenerator;
import com.google.common.collect.testing.TestSetGenerator;
import com.google.common.collect.testing.TestSubjectGenerator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Derived suite generators for Guava collection interfaces, split out of the suite builders so that
 * they are available to GWT.
 *
 * @author Louis Wasserman
 */
@GwtCompatible
public final class DerivedGoogleCollectionGenerators {
  public static class MapGenerator implements TestMapGenerator, DerivedGenerator {

    private final OneSizeTestContainerGenerator, Entry> generator;

    public MapGenerator(
        OneSizeTestContainerGenerator, Entry> oneSizeTestContainerGenerator) {
      this.generator = oneSizeTestContainerGenerator;
    }

    @Override
    public SampleElements> samples() {
      return generator.samples();
    }

    @Override
    public Map create(Object... elements) {
      return generator.create(elements);
    }

    @Override
    public Map.Entry[] createArray(int length) {
      return generator.createArray(length);
    }

    @Override
    public Iterable> order(List> insertionOrder) {
      return generator.order(insertionOrder);
    }

    @SuppressWarnings("unchecked")
    @Override
    public K[] createKeyArray(int length) {
      return (K[]) new Object[length];
    }

    @SuppressWarnings("unchecked")
    @Override
    public V[] createValueArray(int length) {
      return (V[]) new Object[length];
    }

    public TestSubjectGenerator getInnerGenerator() {
      return generator;
    }
  }

  public static class InverseBiMapGenerator
      implements TestBiMapGenerator, DerivedGenerator {

    private final OneSizeTestContainerGenerator, Entry> generator;

    public InverseBiMapGenerator(
        OneSizeTestContainerGenerator, Entry> oneSizeTestContainerGenerator) {
      this.generator = oneSizeTestContainerGenerator;
    }

    @Override
    public SampleElements> samples() {
      SampleElements> samples = generator.samples();
      return new SampleElements>(reverse(samples.e0), reverse(samples.e1),
          reverse(samples.e2), reverse(samples.e3), reverse(samples.e4));
    }

    private Map.Entry reverse(Map.Entry entry) {
      return Helpers.mapEntry(entry.getValue(), entry.getKey());
    }

    @SuppressWarnings("unchecked")
    @Override
    public BiMap create(Object... elements) {
      Entry[] entries = new Entry[elements.length];
      for (int i = 0; i < elements.length; i++) {
        entries[i] = reverse((Entry) elements[i]);
      }
      return generator.create((Object[]) entries).inverse();
    }

    @SuppressWarnings("unchecked")
    @Override
    public Map.Entry[] createArray(int length) {
      return new Entry[length];
    }

    @Override
    public Iterable> order(List> insertionOrder) {
      return insertionOrder;
    }

    @SuppressWarnings("unchecked")
    @Override
    public V[] createKeyArray(int length) {
      return (V[]) new Object[length];
    }

    @SuppressWarnings("unchecked")
    @Override
    public K[] createValueArray(int length) {
      return (K[]) new Object[length];
    }

    public TestSubjectGenerator getInnerGenerator() {
      return generator;
    }
  }

  public static class BiMapValueSetGenerator
      implements TestSetGenerator, DerivedGenerator {
    private final OneSizeTestContainerGenerator, Map.Entry>
        mapGenerator;
    private final SampleElements samples;

    public BiMapValueSetGenerator(
        OneSizeTestContainerGenerator, Entry> mapGenerator) {
      this.mapGenerator = mapGenerator;
      final SampleElements> mapSamples =
          this.mapGenerator.samples();
      this.samples = new SampleElements(
          mapSamples.e0.getValue(),
          mapSamples.e1.getValue(),
          mapSamples.e2.getValue(),
          mapSamples.e3.getValue(),
          mapSamples.e4.getValue());
    }

    @Override
    public SampleElements samples() {
      return samples;
    }

    @Override
    public Set create(Object... elements) {
      @SuppressWarnings("unchecked")
      V[] valuesArray = (V[]) elements;

      // Start with a suitably shaped collection of entries
      Collection> originalEntries =
          mapGenerator.getSampleElements(elements.length);

      // Create a copy of that, with the desired value for each value
      Collection> entries =
          new ArrayList>(elements.length);
      int i = 0;
      for (Map.Entry entry : originalEntries) {
        entries.add(Helpers.mapEntry(entry.getKey(), valuesArray[i++]));
      }

      return mapGenerator.create(entries.toArray()).values();
    }

    @Override
    public V[] createArray(int length) {
      final V[] vs = ((TestBiMapGenerator) mapGenerator.getInnerGenerator())
          .createValueArray(length);
      return vs;
    }

    @Override
    public Iterable order(List insertionOrder) {
      return insertionOrder;
    }

    public TestSubjectGenerator getInnerGenerator() {
      return mapGenerator;
    }
  }

  private DerivedGoogleCollectionGenerators() {}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy