• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp.temporal;
33 
34 import static org.testng.Assert.assertEquals;
35 
36 import java.io.ByteArrayInputStream;
37 import java.io.ByteArrayOutputStream;
38 import java.io.ObjectInputStream;
39 import java.io.ObjectOutputStream;
40 
41 import org.testng.annotations.DataProvider;
42 import org.testng.annotations.Test;
43 import org.threeten.bp.AbstractTest;
44 
45 /**
46  * Test.
47  */
48 @Test
49 public class TestValueRange extends AbstractTest {
50 
51     //-----------------------------------------------------------------------
52     // Basics
53     //-----------------------------------------------------------------------
54     @Test
test_immutable()55     public void test_immutable() {
56         assertImmutable(ValueRange.class);
57     }
58 
59     //-----------------------------------------------------------------------
60     // Serialization
61     //-----------------------------------------------------------------------
test_serialization()62     public void test_serialization() throws Exception {
63         Object obj = ValueRange.of(1, 2, 3, 4);
64         ByteArrayOutputStream baos = new ByteArrayOutputStream();
65         ObjectOutputStream oos = new ObjectOutputStream(baos);
66         oos.writeObject(obj);
67         oos.close();
68         ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
69         assertEquals(ois.readObject(), obj);
70     }
71 
72     //-----------------------------------------------------------------------
73     // of(long,long)
74     //-----------------------------------------------------------------------
test_of_longlong()75     public void test_of_longlong() {
76         ValueRange test = ValueRange.of(1, 12);
77         assertEquals(test.getMinimum(), 1);
78         assertEquals(test.getLargestMinimum(), 1);
79         assertEquals(test.getSmallestMaximum(), 12);
80         assertEquals(test.getMaximum(), 12);
81         assertEquals(test.isFixed(), true);
82         assertEquals(test.isIntValue(), true);
83     }
84 
test_of_longlong_big()85     public void test_of_longlong_big() {
86         ValueRange test = ValueRange.of(1, 123456789012345L);
87         assertEquals(test.getMinimum(), 1);
88         assertEquals(test.getLargestMinimum(), 1);
89         assertEquals(test.getSmallestMaximum(), 123456789012345L);
90         assertEquals(test.getMaximum(), 123456789012345L);
91         assertEquals(test.isFixed(), true);
92         assertEquals(test.isIntValue(), false);
93     }
94 
95     @Test(expectedExceptions = IllegalArgumentException.class)
test_of_longlong_minGtMax()96     public void test_of_longlong_minGtMax() {
97         ValueRange.of(12, 1);
98     }
99 
100     //-----------------------------------------------------------------------
101     // of(long,long,long)
102     //-----------------------------------------------------------------------
test_of_longlonglong()103     public void test_of_longlonglong() {
104         ValueRange test = ValueRange.of(1, 28, 31);
105         assertEquals(test.getMinimum(), 1);
106         assertEquals(test.getLargestMinimum(), 1);
107         assertEquals(test.getSmallestMaximum(), 28);
108         assertEquals(test.getMaximum(), 31);
109         assertEquals(test.isFixed(), false);
110         assertEquals(test.isIntValue(), true);
111     }
112 
113     @Test(expectedExceptions = IllegalArgumentException.class)
test_of_longlonglong_minGtMax()114     public void test_of_longlonglong_minGtMax() {
115         ValueRange.of(12, 1, 2);
116     }
117 
118     @Test(expectedExceptions = IllegalArgumentException.class)
test_of_longlonglong_smallestmaxminGtMax()119     public void test_of_longlonglong_smallestmaxminGtMax() {
120         ValueRange.of(1, 31, 28);
121     }
122 
123     //-----------------------------------------------------------------------
124     // of(long,long,long,long)
125     //-----------------------------------------------------------------------
126     @DataProvider(name="valid")
data_valid()127     Object[][] data_valid() {
128         return new Object[][] {
129                 {1, 1, 1, 1},
130                 {1, 1, 1, 2},
131                 {1, 1, 2, 2},
132                 {1, 2, 3, 4},
133                 {1, 1, 28, 31},
134                 {1, 3, 31, 31},
135                 {-5, -4, -3, -2},
136                 {-5, -4, 3, 4},
137                 {1, 20, 10, 31},
138         };
139     }
140 
141     @Test(dataProvider="valid")
test_of_longlonglonglong(long sMin, long lMin, long sMax, long lMax)142     public void test_of_longlonglonglong(long sMin, long lMin, long sMax, long lMax) {
143         ValueRange test = ValueRange.of(sMin, lMin, sMax, lMax);
144         assertEquals(test.getMinimum(), sMin);
145         assertEquals(test.getLargestMinimum(), lMin);
146         assertEquals(test.getSmallestMaximum(), sMax);
147         assertEquals(test.getMaximum(), lMax);
148         assertEquals(test.isFixed(), sMin == lMin && sMax == lMax);
149         assertEquals(test.isIntValue(), true);
150     }
151 
152     @DataProvider(name="invalid")
data_invalid()153     Object[][] data_invalid() {
154         return new Object[][] {
155                 {1, 2, 31, 28},
156                 {1, 31, 2, 28},
157                 {31, 2, 1, 28},
158                 {31, 2, 3, 28},
159 
160                 {2, 1, 28, 31},
161                 {2, 1, 31, 28},
162                 {12, 13, 1, 2},
163         };
164     }
165 
166     @Test(dataProvider="invalid", expectedExceptions=IllegalArgumentException.class)
test_of_longlonglonglong_invalid(long sMin, long lMin, long sMax, long lMax)167     public void test_of_longlonglonglong_invalid(long sMin, long lMin, long sMax, long lMax) {
168         ValueRange.of(sMin, lMin, sMax, lMax);
169     }
170 
171     //-----------------------------------------------------------------------
172     // isValidValue(long)
173     //-----------------------------------------------------------------------
test_isValidValue_long()174     public void test_isValidValue_long() {
175         ValueRange test = ValueRange.of(1, 28, 31);
176         assertEquals(test.isValidValue(0), false);
177         assertEquals(test.isValidValue(1), true);
178         assertEquals(test.isValidValue(2), true);
179         assertEquals(test.isValidValue(30), true);
180         assertEquals(test.isValidValue(31), true);
181         assertEquals(test.isValidValue(32), false);
182     }
183 
184     //-----------------------------------------------------------------------
185     // isValidIntValue(long)
186     //-----------------------------------------------------------------------
test_isValidValue_long_int()187     public void test_isValidValue_long_int() {
188         ValueRange test = ValueRange.of(1, 28, 31);
189         assertEquals(test.isValidValue(0), false);
190         assertEquals(test.isValidValue(1), true);
191         assertEquals(test.isValidValue(31), true);
192         assertEquals(test.isValidValue(32), false);
193     }
194 
test_isValidValue_long_long()195     public void test_isValidValue_long_long() {
196         ValueRange test = ValueRange.of(1, 28, Integer.MAX_VALUE + 1L);
197         assertEquals(test.isValidIntValue(0), false);
198         assertEquals(test.isValidIntValue(1), false);
199         assertEquals(test.isValidIntValue(31), false);
200         assertEquals(test.isValidIntValue(32), false);
201     }
202 
203     //-----------------------------------------------------------------------
204     // equals() / hashCode()
205     //-----------------------------------------------------------------------
test_equals1()206     public void test_equals1() {
207         ValueRange a = ValueRange.of(1, 2, 3, 4);
208         ValueRange b = ValueRange.of(1, 2, 3, 4);
209         assertEquals(a.equals(a), true);
210         assertEquals(a.equals(b), true);
211         assertEquals(b.equals(a), true);
212         assertEquals(b.equals(b), true);
213         assertEquals(a.hashCode() == b.hashCode(), true);
214     }
215 
test_equals2()216     public void test_equals2() {
217         ValueRange a = ValueRange.of(1, 2, 3, 4);
218         assertEquals(a.equals(ValueRange.of(0, 2, 3, 4)), false);
219         assertEquals(a.equals(ValueRange.of(1, 3, 3, 4)), false);
220         assertEquals(a.equals(ValueRange.of(1, 2, 4, 4)), false);
221         assertEquals(a.equals(ValueRange.of(1, 2, 3, 5)), false);
222     }
223 
test_equals_otherType()224     public void test_equals_otherType() {
225         ValueRange a = ValueRange.of(1, 12);
226         assertEquals(a.equals("Rubbish"), false);
227     }
228 
test_equals_null()229     public void test_equals_null() {
230         ValueRange a = ValueRange.of(1, 12);
231         assertEquals(a.equals(null), false);
232     }
233 
234     //-----------------------------------------------------------------------
235     // toString()
236     //-----------------------------------------------------------------------
test_toString()237     public void test_toString() {
238         assertEquals(ValueRange.of(1, 1, 4, 4).toString(), "1 - 4");
239         assertEquals(ValueRange.of(1, 1, 3, 4).toString(), "1 - 3/4");
240         assertEquals(ValueRange.of(1, 2, 3, 4).toString(), "1/2 - 3/4");
241         assertEquals(ValueRange.of(1, 2, 4, 4).toString(), "1/2 - 4");
242     }
243 
244 }
245