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 
21 import com.google.common.annotations.GwtIncompatible;
22 import java.math.BigInteger;
23 import junit.framework.TestCase;
24 
25 /**
26  * Tests for {@link DiscreteDomain}.
27  *
28  * @author Chris Povirk
29  */
30 @GwtIncompatible // SerializableTester
31 public class DiscreteDomainTest extends TestCase {
testSerialization()32   public void testSerialization() {
33     reserializeAndAssert(DiscreteDomain.integers());
34     reserializeAndAssert(DiscreteDomain.longs());
35     reserializeAndAssert(DiscreteDomain.bigIntegers());
36   }
37 
testIntegersOffset()38   public void testIntegersOffset() {
39     assertEquals(1, DiscreteDomain.integers().offset(0, 1).intValue());
40     assertEquals(
41         Integer.MAX_VALUE,
42         DiscreteDomain.integers().offset(Integer.MIN_VALUE, (1L << 32) - 1).intValue());
43   }
44 
testIntegersOffsetExceptions()45   public void testIntegersOffsetExceptions() {
46     try {
47       DiscreteDomain.integers().offset(0, -1);
48       fail();
49     } catch (IllegalArgumentException expected) {
50     }
51     try {
52       DiscreteDomain.integers().offset(Integer.MAX_VALUE, 1);
53       fail();
54     } catch (IllegalArgumentException expected) {
55     }
56   }
57 
testLongsOffset()58   public void testLongsOffset() {
59     assertEquals(1, DiscreteDomain.longs().offset(0L, 1).longValue());
60     assertEquals(Long.MAX_VALUE, DiscreteDomain.longs().offset(0L, Long.MAX_VALUE).longValue());
61   }
62 
testLongsOffsetExceptions()63   public void testLongsOffsetExceptions() {
64     try {
65       DiscreteDomain.longs().offset(0L, -1);
66       fail();
67     } catch (IllegalArgumentException expected) {
68     }
69     try {
70       DiscreteDomain.longs().offset(Long.MAX_VALUE, 1);
71       fail();
72     } catch (IllegalArgumentException expected) {
73     }
74   }
75 
testBigIntegersOffset()76   public void testBigIntegersOffset() {
77     assertEquals(BigInteger.ONE, DiscreteDomain.bigIntegers().offset(BigInteger.ZERO, 1));
78     assertEquals(
79         BigInteger.valueOf(Long.MAX_VALUE),
80         DiscreteDomain.bigIntegers().offset(BigInteger.ZERO, Long.MAX_VALUE));
81   }
82 
testBigIntegersOffsetExceptions()83   public void testBigIntegersOffsetExceptions() {
84     try {
85       DiscreteDomain.bigIntegers().offset(BigInteger.ZERO, -1);
86       fail();
87     } catch (IllegalArgumentException expected) {
88     }
89   }
90 }
91