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

com.google.common.collect.testing.NavigableSetTestSuiteBuilder 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) 2010 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 com.google.common.collect.testing.features.CollectionFeature.DESCENDING_VIEW;
import static com.google.common.collect.testing.features.CollectionFeature.SUBSET_VIEW;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.testing.DerivedCollectionGenerators.Bound;
import com.google.common.collect.testing.DerivedCollectionGenerators.SortedSetSubsetTestSetGenerator;
import com.google.common.collect.testing.features.Feature;
import com.google.common.collect.testing.testers.NavigableSetNavigationTester;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.NavigableSet;
import java.util.Set;
import java.util.SortedSet;
import junit.framework.TestSuite;

/**
 * Creates, based on your criteria, a JUnit test suite that exhaustively tests a NavigableSet
 * implementation.
 */
@GwtIncompatible
public final class NavigableSetTestSuiteBuilder extends SortedSetTestSuiteBuilder {
  public static  NavigableSetTestSuiteBuilder using(TestSortedSetGenerator generator) {
    NavigableSetTestSuiteBuilder builder = new NavigableSetTestSuiteBuilder<>();
    builder.usingGenerator(generator);
    return builder;
  }

  @Override
  protected List createDerivedSuites(
      FeatureSpecificTestSuiteBuilder, E>>
          parentBuilder) {
    List derivedSuites = new ArrayList<>(super.createDerivedSuites(parentBuilder));

    if (!parentBuilder.getFeatures().contains(SUBSET_VIEW)) {
      // Other combinations are inherited from SortedSetTestSuiteBuilder.
      derivedSuites.add(createSubsetSuite(parentBuilder, Bound.NO_BOUND, Bound.INCLUSIVE));
      derivedSuites.add(createSubsetSuite(parentBuilder, Bound.EXCLUSIVE, Bound.NO_BOUND));
      derivedSuites.add(createSubsetSuite(parentBuilder, Bound.EXCLUSIVE, Bound.EXCLUSIVE));
      derivedSuites.add(createSubsetSuite(parentBuilder, Bound.EXCLUSIVE, Bound.INCLUSIVE));
      derivedSuites.add(createSubsetSuite(parentBuilder, Bound.INCLUSIVE, Bound.INCLUSIVE));
    }
    if (!parentBuilder.getFeatures().contains(DESCENDING_VIEW)) {
      derivedSuites.add(createDescendingSuite(parentBuilder));
    }
    return derivedSuites;
  }

  public static final class NavigableSetSubsetTestSetGenerator
      extends SortedSetSubsetTestSetGenerator {
    public NavigableSetSubsetTestSetGenerator(
        TestSortedSetGenerator delegate, Bound to, Bound from) {
      super(delegate, to, from);
    }

    @Override
    NavigableSet createSubSet(SortedSet sortedSet, E firstExclusive, E lastExclusive) {
      NavigableSet set = (NavigableSet) sortedSet;
      if (from == Bound.NO_BOUND && to == Bound.INCLUSIVE) {
        return set.headSet(lastInclusive, true);
      } else if (from == Bound.EXCLUSIVE && to == Bound.NO_BOUND) {
        return set.tailSet(firstExclusive, false);
      } else if (from == Bound.EXCLUSIVE && to == Bound.EXCLUSIVE) {
        return set.subSet(firstExclusive, false, lastExclusive, false);
      } else if (from == Bound.EXCLUSIVE && to == Bound.INCLUSIVE) {
        return set.subSet(firstExclusive, false, lastInclusive, true);
      } else if (from == Bound.INCLUSIVE && to == Bound.INCLUSIVE) {
        return set.subSet(firstInclusive, true, lastInclusive, true);
      } else {
        return (NavigableSet) super.createSubSet(set, firstExclusive, lastExclusive);
      }
    }
  }

  @Override
  public NavigableSetTestSuiteBuilder newBuilderUsing(
      TestSortedSetGenerator delegate, Bound to, Bound from) {
    return using(new NavigableSetSubsetTestSetGenerator(delegate, to, from));
  }

  /** Create a suite whose maps are descending views of other maps. */
  private TestSuite createDescendingSuite(
      FeatureSpecificTestSuiteBuilder, E>>
          parentBuilder) {
    TestSetGenerator delegate =
        (TestSetGenerator) parentBuilder.getSubjectGenerator().getInnerGenerator();

    List> features = new ArrayList<>();
    features.add(DESCENDING_VIEW);
    features.addAll(parentBuilder.getFeatures());

    return NavigableSetTestSuiteBuilder.using(
            new TestSetGenerator() {

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

              @Override
              public E[] createArray(int length) {
                return delegate.createArray(length);
              }

              @Override
              public Iterable order(List insertionOrder) {
                List list = new ArrayList<>();
                for (E e : delegate.order(insertionOrder)) {
                  list.add(e);
                }
                Collections.reverse(list);
                return list;
              }

              @Override
              public Set create(Object... elements) {
                NavigableSet navigableSet = (NavigableSet) delegate.create(elements);
                return navigableSet.descendingSet();
              }
            })
        .named(parentBuilder.getName() + " descending")
        .withFeatures(features)
        .suppressing(parentBuilder.getSuppressedTests())
        .createTestSuite();
  }

  @SuppressWarnings("rawtypes") // class literals
  @Override
  protected List> getTesters() {
    List> testers = Helpers.copyToList(super.getTesters());
    testers.add(NavigableSetNavigationTester.class);
    return testers;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy