View Javadoc

1   /*
2    *  Copyright 2001-2005 Stephen Colebourne
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  package org.joda.time.field;
17  
18  import junit.framework.TestCase;
19  import junit.framework.TestSuite;
20  
21  /**
22   * 
23   *
24   * @author Brian S O'Neill
25   */
26  public class TestFieldUtils extends TestCase {
27      public static void main(String[] args) {
28          junit.textui.TestRunner.run(suite());
29      }
30  
31      public static TestSuite suite() {
32          return new TestSuite(TestFieldUtils.class);
33      }
34  
35      public TestFieldUtils(String name) {
36          super(name);
37      }
38  
39      public void testSafeAddInt() {
40          assertEquals(0, FieldUtils.safeAdd(0, 0));
41  
42          assertEquals(5, FieldUtils.safeAdd(2, 3));
43          assertEquals(-1, FieldUtils.safeAdd(2, -3));
44          assertEquals(1, FieldUtils.safeAdd(-2, 3));
45          assertEquals(-5, FieldUtils.safeAdd(-2, -3));
46  
47          assertEquals(Integer.MAX_VALUE - 1, FieldUtils.safeAdd(Integer.MAX_VALUE, -1));
48          assertEquals(Integer.MIN_VALUE + 1, FieldUtils.safeAdd(Integer.MIN_VALUE, 1));
49  
50          assertEquals(-1, FieldUtils.safeAdd(Integer.MIN_VALUE, Integer.MAX_VALUE));
51          assertEquals(-1, FieldUtils.safeAdd(Integer.MAX_VALUE, Integer.MIN_VALUE));
52  
53          try {
54              FieldUtils.safeAdd(Integer.MAX_VALUE, 1);
55              fail();
56          } catch (ArithmeticException e) {
57          }
58  
59          try {
60              FieldUtils.safeAdd(Integer.MAX_VALUE, 100);
61              fail();
62          } catch (ArithmeticException e) {
63          }
64  
65          try {
66              FieldUtils.safeAdd(Integer.MAX_VALUE, Integer.MAX_VALUE);
67              fail();
68          } catch (ArithmeticException e) {
69          }
70  
71          try {
72              FieldUtils.safeAdd(Integer.MIN_VALUE, -1);
73              fail();
74          } catch (ArithmeticException e) {
75          }
76  
77          try {
78              FieldUtils.safeAdd(Integer.MIN_VALUE, -100);
79              fail();
80          } catch (ArithmeticException e) {
81          }
82  
83          try {
84              FieldUtils.safeAdd(Integer.MIN_VALUE, Integer.MIN_VALUE);
85              fail();
86          } catch (ArithmeticException e) {
87          }
88      }
89  
90      public void testSafeAddLong() {
91          assertEquals(0L, FieldUtils.safeAdd(0L, 0L));
92  
93          assertEquals(5L, FieldUtils.safeAdd(2L, 3L));
94          assertEquals(-1L, FieldUtils.safeAdd(2L, -3L));
95          assertEquals(1L, FieldUtils.safeAdd(-2L, 3L));
96          assertEquals(-5L, FieldUtils.safeAdd(-2L, -3L));
97  
98          assertEquals(Long.MAX_VALUE - 1, FieldUtils.safeAdd(Long.MAX_VALUE, -1L));
99          assertEquals(Long.MIN_VALUE + 1, FieldUtils.safeAdd(Long.MIN_VALUE, 1L));
100 
101         assertEquals(-1, FieldUtils.safeAdd(Long.MIN_VALUE, Long.MAX_VALUE));
102         assertEquals(-1, FieldUtils.safeAdd(Long.MAX_VALUE, Long.MIN_VALUE));
103 
104         try {
105             FieldUtils.safeAdd(Long.MAX_VALUE, 1L);
106             fail();
107         } catch (ArithmeticException e) {
108         }
109 
110         try {
111             FieldUtils.safeAdd(Long.MAX_VALUE, 100L);
112             fail();
113         } catch (ArithmeticException e) {
114         }
115 
116         try {
117             FieldUtils.safeAdd(Long.MAX_VALUE, Long.MAX_VALUE);
118             fail();
119         } catch (ArithmeticException e) {
120         }
121 
122         try {
123             FieldUtils.safeAdd(Long.MIN_VALUE, -1L);
124             fail();
125         } catch (ArithmeticException e) {
126         }
127 
128         try {
129             FieldUtils.safeAdd(Long.MIN_VALUE, -100L);
130             fail();
131         } catch (ArithmeticException e) {
132         }
133 
134         try {
135             FieldUtils.safeAdd(Long.MIN_VALUE, Long.MIN_VALUE);
136             fail();
137         } catch (ArithmeticException e) {
138         }
139     }
140 
141     public void testSafeSubtractLong() {
142         assertEquals(0L, FieldUtils.safeSubtract(0L, 0L));
143 
144         assertEquals(-1L, FieldUtils.safeSubtract(2L, 3L));
145         assertEquals(5L, FieldUtils.safeSubtract(2L, -3L));
146         assertEquals(-5L, FieldUtils.safeSubtract(-2L, 3L));
147         assertEquals(1L, FieldUtils.safeSubtract(-2L, -3L));
148 
149         assertEquals(Long.MAX_VALUE - 1, FieldUtils.safeSubtract(Long.MAX_VALUE, 1L));
150         assertEquals(Long.MIN_VALUE + 1, FieldUtils.safeSubtract(Long.MIN_VALUE, -1L));
151 
152         assertEquals(0, FieldUtils.safeSubtract(Long.MIN_VALUE, Long.MIN_VALUE));
153         assertEquals(0, FieldUtils.safeSubtract(Long.MAX_VALUE, Long.MAX_VALUE));
154 
155         try {
156             FieldUtils.safeSubtract(Long.MIN_VALUE, 1L);
157             fail();
158         } catch (ArithmeticException e) {
159         }
160 
161         try {
162             FieldUtils.safeSubtract(Long.MIN_VALUE, 100L);
163             fail();
164         } catch (ArithmeticException e) {
165         }
166 
167         try {
168             FieldUtils.safeSubtract(Long.MIN_VALUE, Long.MAX_VALUE);
169             fail();
170         } catch (ArithmeticException e) {
171         }
172 
173         try {
174             FieldUtils.safeSubtract(Long.MAX_VALUE, -1L);
175             fail();
176         } catch (ArithmeticException e) {
177         }
178 
179         try {
180             FieldUtils.safeSubtract(Long.MAX_VALUE, -100L);
181             fail();
182         } catch (ArithmeticException e) {
183         }
184 
185         try {
186             FieldUtils.safeSubtract(Long.MAX_VALUE, Long.MIN_VALUE);
187             fail();
188         } catch (ArithmeticException e) {
189         }
190     }
191 
192     //-----------------------------------------------------------------------
193     public void testSafeMultiplyLongLong() {
194         assertEquals(0L, FieldUtils.safeMultiply(0L, 0L));
195         
196         assertEquals(1L, FieldUtils.safeMultiply(1L, 1L));
197         assertEquals(3L, FieldUtils.safeMultiply(1L, 3L));
198         assertEquals(3L, FieldUtils.safeMultiply(3L, 1L));
199         
200         assertEquals(6L, FieldUtils.safeMultiply(2L, 3L));
201         assertEquals(-6L, FieldUtils.safeMultiply(2L, -3L));
202         assertEquals(-6L, FieldUtils.safeMultiply(-2L, 3L));
203         assertEquals(6L, FieldUtils.safeMultiply(-2L, -3L));
204         
205         assertEquals(Long.MAX_VALUE, FieldUtils.safeMultiply(Long.MAX_VALUE, 1L));
206         assertEquals(Long.MIN_VALUE, FieldUtils.safeMultiply(Long.MIN_VALUE, 1L));
207         assertEquals(-Long.MAX_VALUE, FieldUtils.safeMultiply(Long.MAX_VALUE, -1L));
208         
209         try {
210             FieldUtils.safeMultiply(Long.MIN_VALUE, -1L);
211             fail();
212         } catch (ArithmeticException e) {
213         }
214         
215         try {
216             FieldUtils.safeMultiply(-1L, Long.MIN_VALUE);
217             fail();
218         } catch (ArithmeticException e) {
219         }
220       
221         try {
222             FieldUtils.safeMultiply(Long.MIN_VALUE, 100L);
223             fail();
224         } catch (ArithmeticException e) {
225         }
226         
227         try {
228             FieldUtils.safeMultiply(Long.MIN_VALUE, Long.MAX_VALUE);
229             fail();
230         } catch (ArithmeticException e) {
231         }
232         
233         try {
234             FieldUtils.safeMultiply(Long.MAX_VALUE, Long.MIN_VALUE);
235             fail();
236         } catch (ArithmeticException e) {
237         }
238     }
239 
240     //-----------------------------------------------------------------------
241     public void testSafeMultiplyLongInt() {
242         assertEquals(0L, FieldUtils.safeMultiply(0L, 0));
243         
244         assertEquals(1L, FieldUtils.safeMultiply(1L, 1));
245         assertEquals(3L, FieldUtils.safeMultiply(1L, 3));
246         assertEquals(3L, FieldUtils.safeMultiply(3L, 1));
247         
248         assertEquals(6L, FieldUtils.safeMultiply(2L, 3));
249         assertEquals(-6L, FieldUtils.safeMultiply(2L, -3));
250         assertEquals(-6L, FieldUtils.safeMultiply(-2L, 3));
251         assertEquals(6L, FieldUtils.safeMultiply(-2L, -3));
252         
253         assertEquals(-1L * Integer.MIN_VALUE, FieldUtils.safeMultiply(-1L, Integer.MIN_VALUE));
254         
255         assertEquals(Long.MAX_VALUE, FieldUtils.safeMultiply(Long.MAX_VALUE, 1));
256         assertEquals(Long.MIN_VALUE, FieldUtils.safeMultiply(Long.MIN_VALUE, 1));
257         assertEquals(-Long.MAX_VALUE, FieldUtils.safeMultiply(Long.MAX_VALUE, -1));
258         
259         try {
260             FieldUtils.safeMultiply(Long.MIN_VALUE, -1);
261             fail();
262         } catch (ArithmeticException e) {
263         }
264         
265         try {
266             FieldUtils.safeMultiply(Long.MIN_VALUE, 100);
267             fail();
268         } catch (ArithmeticException e) {
269         }
270         
271         try {
272             FieldUtils.safeMultiply(Long.MIN_VALUE, Integer.MAX_VALUE);
273             fail();
274         } catch (ArithmeticException e) {
275         }
276         
277         try {
278             FieldUtils.safeMultiply(Long.MAX_VALUE, Integer.MIN_VALUE);
279             fail();
280         } catch (ArithmeticException e) {
281         }
282     }
283 }
284