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

com.google.common.collect.testing.NavigableMapTestSuiteBuilder 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.

There is a newer version: 33.3.1-jre
Show 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.Helpers.castOrCopyToList;
import static java.util.Collections.reverse;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.testing.DerivedCollectionGenerators.Bound;
import com.google.common.collect.testing.DerivedCollectionGenerators.ForwardingTestMapGenerator;
import com.google.common.collect.testing.DerivedCollectionGenerators.SortedMapSubmapTestMapGenerator;
import com.google.common.collect.testing.features.Feature;
import com.google.common.collect.testing.testers.NavigableMapNavigationTester;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.SortedMap;
import junit.framework.TestSuite;

/**
 * Creates, based on your criteria, a JUnit test suite that exhaustively tests a NavigableMap
 * implementation.
 */
@GwtIncompatible
public class NavigableMapTestSuiteBuilder extends SortedMapTestSuiteBuilder {
  public static  NavigableMapTestSuiteBuilder using(
      TestSortedMapGenerator generator) {
    NavigableMapTestSuiteBuilder result = new NavigableMapTestSuiteBuilder<>();
    result.usingGenerator(generator);
    return result;
  }

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

  @Override
  protected List createDerivedSuites(
      FeatureSpecificTestSuiteBuilder<
              ?, ? extends OneSizeTestContainerGenerator, Entry>>
          parentBuilder) {
    List derivedSuites = super.createDerivedSuites(parentBuilder);

    if (!parentBuilder.getFeatures().contains(NoRecurse.DESCENDING)) {
      derivedSuites.add(createDescendingSuite(parentBuilder));
    }

    if (!parentBuilder.getFeatures().contains(NoRecurse.SUBMAP)) {
      // Other combinations are inherited from SortedMapTestSuiteBuilder.
      derivedSuites.add(createSubmapSuite(parentBuilder, Bound.NO_BOUND, Bound.INCLUSIVE));
      derivedSuites.add(createSubmapSuite(parentBuilder, Bound.EXCLUSIVE, Bound.NO_BOUND));
      derivedSuites.add(createSubmapSuite(parentBuilder, Bound.EXCLUSIVE, Bound.EXCLUSIVE));
      derivedSuites.add(createSubmapSuite(parentBuilder, Bound.EXCLUSIVE, Bound.INCLUSIVE));
      derivedSuites.add(createSubmapSuite(parentBuilder, Bound.INCLUSIVE, Bound.INCLUSIVE));
    }

    return derivedSuites;
  }

  @Override
  protected NavigableSetTestSuiteBuilder createDerivedKeySetSuite(
      TestSetGenerator keySetGenerator) {
    return NavigableSetTestSuiteBuilder.using((TestSortedSetGenerator) keySetGenerator);
  }

  public static final class NavigableMapSubmapTestMapGenerator
      extends SortedMapSubmapTestMapGenerator {
    public NavigableMapSubmapTestMapGenerator(
        TestSortedMapGenerator delegate, Bound to, Bound from) {
      super(delegate, to, from);
    }

    @Override
    NavigableMap createSubMap(SortedMap sortedMap, K firstExclusive, K lastExclusive) {
      NavigableMap map = (NavigableMap) sortedMap;
      if (from == Bound.NO_BOUND && to == Bound.INCLUSIVE) {
        return map.headMap(lastInclusive, true);
      } else if (from == Bound.EXCLUSIVE && to == Bound.NO_BOUND) {
        return map.tailMap(firstExclusive, false);
      } else if (from == Bound.EXCLUSIVE && to == Bound.EXCLUSIVE) {
        return map.subMap(firstExclusive, false, lastExclusive, false);
      } else if (from == Bound.EXCLUSIVE && to == Bound.INCLUSIVE) {
        return map.subMap(firstExclusive, false, lastInclusive, true);
      } else if (from == Bound.INCLUSIVE && to == Bound.INCLUSIVE) {
        return map.subMap(firstInclusive, true, lastInclusive, true);
      } else {
        return (NavigableMap) super.createSubMap(map, firstExclusive, lastExclusive);
      }
    }
  }

  @Override
  public NavigableMapTestSuiteBuilder newBuilderUsing(
      TestSortedMapGenerator delegate, Bound to, Bound from) {
    return subSuiteUsing(new NavigableMapSubmapTestMapGenerator(delegate, to, from));
  }

  /** Create a suite whose maps are descending views of other maps. */
  private TestSuite createDescendingSuite(
      FeatureSpecificTestSuiteBuilder<
              ?, ? extends OneSizeTestContainerGenerator, Entry>>
          parentBuilder) {
    TestSortedMapGenerator delegate =
        (TestSortedMapGenerator) parentBuilder.getSubjectGenerator().getInnerGenerator();

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

    return subSuiteUsing(new DescendingTestMapGenerator(delegate))
        .named(parentBuilder.getName() + " descending")
        .withFeatures(features)
        .suppressing(parentBuilder.getSuppressedTests())
        .createTestSuite();
  }

  NavigableMapTestSuiteBuilder subSuiteUsing(TestSortedMapGenerator generator) {
    return using(generator);
  }

  static class DescendingTestMapGenerator extends ForwardingTestMapGenerator
      implements TestSortedMapGenerator {
    DescendingTestMapGenerator(TestSortedMapGenerator delegate) {
      super(delegate);
    }

    @Override
    public NavigableMap create(Object... entries) {
      NavigableMap map = (NavigableMap) delegate.create(entries);
      return map.descendingMap();
    }

    @Override
    public Iterable> order(List> insertionOrder) {
      insertionOrder = castOrCopyToList(delegate.order(insertionOrder));
      reverse(insertionOrder);
      return insertionOrder;
    }

    TestSortedMapGenerator delegate() {
      return (TestSortedMapGenerator) delegate;
    }

    @Override
    public Entry belowSamplesLesser() {
      return delegate().aboveSamplesGreater();
    }

    @Override
    public Entry belowSamplesGreater() {
      return delegate().aboveSamplesLesser();
    }

    @Override
    public Entry aboveSamplesLesser() {
      return delegate().belowSamplesGreater();
    }

    @Override
    public Entry aboveSamplesGreater() {
      return delegate().belowSamplesLesser();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy