alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (SortedListsTest.java)

This example Java source code file (SortedListsTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

assertionerror, first_after, gwtcompatible, gwtincompatible, immutablelist, inverted_insertion_index, keyabsentbehavior, keypresentbehavior, last_present, next_higher, next_lower, nullpointertester, sortedliststest, testcase, util

The SortedListsTest.java Java example source code

/*
 * 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;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.SortedLists.KeyAbsentBehavior;
import com.google.common.collect.SortedLists.KeyPresentBehavior;
import com.google.common.testing.NullPointerTester;

import junit.framework.TestCase;

import java.util.List;

/**
 * Tests for SortedLists.
 *
 * @author Louis Wasserman
 */
@GwtCompatible(emulated = true)
public class SortedListsTest extends TestCase {
  private static final ImmutableList<Integer> LIST_WITH_DUPS =
      ImmutableList.of(1, 1, 2, 4, 4, 4, 8);

  private static final ImmutableList<Integer> LIST_WITHOUT_DUPS = ImmutableList.of(1, 2, 4, 8);

  void assertModelAgrees(List<Integer> list, Integer key, int answer,
      KeyPresentBehavior presentBehavior, KeyAbsentBehavior absentBehavior) {
    switch (presentBehavior) {
      case FIRST_PRESENT:
        if (list.contains(key)) {
          assertEquals(list.indexOf(key), answer);
          return;
        }
        break;
      case LAST_PRESENT:
        if (list.contains(key)) {
          assertEquals(list.lastIndexOf(key), answer);
          return;
        }
        break;
      case ANY_PRESENT:
        if (list.contains(key)) {
          assertEquals(key, list.get(answer));
          return;
        }
        break;
      case FIRST_AFTER:
        if (list.contains(key)) {
          assertEquals(list.lastIndexOf(key) + 1, answer);
          return;
        }
        break;
      case LAST_BEFORE:
        if (list.contains(key)) {
          assertEquals(list.indexOf(key) - 1, answer);
          return;
        }
        break;
      default:
        throw new AssertionError();
    }
    // key is not present
    int nextHigherIndex = list.size();
    for (int i = list.size() - 1; i >= 0 && list.get(i) > key; i--) {
      nextHigherIndex = i;
    }
    switch (absentBehavior) {
      case NEXT_LOWER:
        assertEquals(nextHigherIndex - 1, answer);
        return;
      case NEXT_HIGHER:
        assertEquals(nextHigherIndex, answer);
        return;
      case INVERTED_INSERTION_INDEX:
        assertEquals(-1 - nextHigherIndex, answer);
        return;
      default:
        throw new AssertionError();
    }
  }

  public void testWithoutDups() {
    for (KeyPresentBehavior presentBehavior : KeyPresentBehavior.values()) {
      for (KeyAbsentBehavior absentBehavior : KeyAbsentBehavior.values()) {
        for (int key = 0; key <= 10; key++) {
          assertModelAgrees(LIST_WITHOUT_DUPS, key,
              SortedLists.binarySearch(LIST_WITHOUT_DUPS, key, presentBehavior, absentBehavior),
              presentBehavior, absentBehavior);
        }
      }
    }
  }

  public void testWithDups() {
    for (KeyPresentBehavior presentBehavior : KeyPresentBehavior.values()) {
      for (KeyAbsentBehavior absentBehavior : KeyAbsentBehavior.values()) {
        for (int key = 0; key <= 10; key++) {
          assertModelAgrees(LIST_WITH_DUPS, key,
              SortedLists.binarySearch(LIST_WITH_DUPS, key, presentBehavior, absentBehavior),
              presentBehavior, absentBehavior);
        }
      }
    }
  }

  @GwtIncompatible // NullPointerTester
  public void testNulls() {
    new NullPointerTester().testAllPublicStaticMethods(SortedLists.class);
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java SortedListsTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.