• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import static com.google.common.testing.SerializableTester.reserializeAndAssert;
20 import static org.junit.Assert.assertThrows;
21 
22 import com.google.common.annotations.GwtIncompatible;
23 import java.math.BigInteger;
24 import junit.framework.TestCase;
25 
26 /**
27  * Tests for {@link DiscreteDomain}.
28  *
29  * @author Chris Povirk
30  */
31 @GwtIncompatible // SerializableTester
32 public class DiscreteDomainTest extends TestCase {
testSerialization()33   public void testSerialization() {
34     reserializeAndAssert(DiscreteDomain.integers());
35     reserializeAndAssert(DiscreteDomain.longs());
36     reserializeAndAssert(DiscreteDomain.bigIntegers());
37   }
38 
testIntegersOffset()39   public void testIntegersOffset() {
40     assertEquals(1, DiscreteDomain.integers().offset(0, 1).intValue());
41     assertEquals(
42         Integer.MAX_VALUE,
43         DiscreteDomain.integers().offset(Integer.MIN_VALUE, (1L << 32) - 1).intValue());
44   }
45 
testIntegersOffsetExceptions()46   public void testIntegersOffsetExceptions() {
47     assertThrows(IllegalArgumentException.class, () -> DiscreteDomain.integers().offset(0, -1));
48     assertThrows(
49         IllegalArgumentException.class,
50         () -> DiscreteDomain.integers().offset(Integer.MAX_VALUE, 1));
51   }
52 
testLongsOffset()53   public void testLongsOffset() {
54     assertEquals(1, DiscreteDomain.longs().offset(0L, 1).longValue());
55     assertEquals(Long.MAX_VALUE, DiscreteDomain.longs().offset(0L, Long.MAX_VALUE).longValue());
56   }
57 
testLongsOffsetExceptions()58   public void testLongsOffsetExceptions() {
59     assertThrows(IllegalArgumentException.class, () -> DiscreteDomain.longs().offset(0L, -1));
60     assertThrows(
61         IllegalArgumentException.class, () -> DiscreteDomain.longs().offset(Long.MAX_VALUE, 1));
62   }
63 
testBigIntegersOffset()64   public void testBigIntegersOffset() {
65     assertEquals(BigInteger.ONE, DiscreteDomain.bigIntegers().offset(BigInteger.ZERO, 1));
66     assertEquals(
67         BigInteger.valueOf(Long.MAX_VALUE),
68         DiscreteDomain.bigIntegers().offset(BigInteger.ZERO, Long.MAX_VALUE));
69   }
70 
testBigIntegersOffsetExceptions()71   public void testBigIntegersOffsetExceptions() {
72     assertThrows(
73         IllegalArgumentException.class,
74         () -> DiscreteDomain.bigIntegers().offset(BigInteger.ZERO, -1));
75   }
76 
testCustomOffsetExceptions()77   public void testCustomOffsetExceptions() {
78     assertThrows(IllegalArgumentException.class, () -> new MyIntegerDomain().offset(0, -1));
79     assertThrows(
80         IllegalArgumentException.class, () -> new MyIntegerDomain().offset(Integer.MAX_VALUE, 1));
81   }
82 
83   private static final class MyIntegerDomain extends DiscreteDomain<Integer> {
84     static final DiscreteDomain<Integer> DELEGATE = DiscreteDomain.integers();
85 
86     @Override
next(Integer value)87     public Integer next(Integer value) {
88       return DELEGATE.next(value);
89     }
90 
91     @Override
previous(Integer value)92     public Integer previous(Integer value) {
93       return DELEGATE.previous(value);
94     }
95 
96     // Do *not* override offset() to delegate: We want to test the default implementation.
97 
98     @Override
distance(Integer start, Integer end)99     public long distance(Integer start, Integer end) {
100       return DELEGATE.distance(start, end);
101     }
102 
103     @Override
minValue()104     public Integer minValue() {
105       return DELEGATE.minValue();
106     }
107 
108     @Override
maxValue()109     public Integer maxValue() {
110       return DELEGATE.maxValue();
111     }
112   }
113 }
114