aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/test/com/google/common/collect/SortedListsTest.java
blob: 884f5f9ad3c314ad2b6729c6a1503141912104f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * 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.base.Function;
import com.google.common.base.Functions;
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() throws Exception {
    NullPointerTester tester = new NullPointerTester();
    tester.setDefault(Function.class, Functions.identity());
    tester.setDefault(List.class, LIST_WITH_DUPS);
    tester.setDefault(Comparable.class, 2);
    tester.setDefault(KeyPresentBehavior.class, KeyPresentBehavior.ANY_PRESENT);
    tester.setDefault(KeyAbsentBehavior.class, KeyAbsentBehavior.NEXT_HIGHER);
    tester.testAllPublicStaticMethods(SortedLists.class);
  }
}