001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.field;
017:
018: import junit.framework.TestCase;
019: import junit.framework.TestSuite;
020:
021: /**
022: *
023: *
024: * @author Brian S O'Neill
025: */
026: public class TestFieldUtils extends TestCase {
027: public static void main(String[] args) {
028: junit.textui.TestRunner.run(suite());
029: }
030:
031: public static TestSuite suite() {
032: return new TestSuite(TestFieldUtils.class);
033: }
034:
035: public TestFieldUtils(String name) {
036: super (name);
037: }
038:
039: public void testSafeAddInt() {
040: assertEquals(0, FieldUtils.safeAdd(0, 0));
041:
042: assertEquals(5, FieldUtils.safeAdd(2, 3));
043: assertEquals(-1, FieldUtils.safeAdd(2, -3));
044: assertEquals(1, FieldUtils.safeAdd(-2, 3));
045: assertEquals(-5, FieldUtils.safeAdd(-2, -3));
046:
047: assertEquals(Integer.MAX_VALUE - 1, FieldUtils.safeAdd(
048: Integer.MAX_VALUE, -1));
049: assertEquals(Integer.MIN_VALUE + 1, FieldUtils.safeAdd(
050: Integer.MIN_VALUE, 1));
051:
052: assertEquals(-1, FieldUtils.safeAdd(Integer.MIN_VALUE,
053: Integer.MAX_VALUE));
054: assertEquals(-1, FieldUtils.safeAdd(Integer.MAX_VALUE,
055: Integer.MIN_VALUE));
056:
057: try {
058: FieldUtils.safeAdd(Integer.MAX_VALUE, 1);
059: fail();
060: } catch (ArithmeticException e) {
061: }
062:
063: try {
064: FieldUtils.safeAdd(Integer.MAX_VALUE, 100);
065: fail();
066: } catch (ArithmeticException e) {
067: }
068:
069: try {
070: FieldUtils.safeAdd(Integer.MAX_VALUE, Integer.MAX_VALUE);
071: fail();
072: } catch (ArithmeticException e) {
073: }
074:
075: try {
076: FieldUtils.safeAdd(Integer.MIN_VALUE, -1);
077: fail();
078: } catch (ArithmeticException e) {
079: }
080:
081: try {
082: FieldUtils.safeAdd(Integer.MIN_VALUE, -100);
083: fail();
084: } catch (ArithmeticException e) {
085: }
086:
087: try {
088: FieldUtils.safeAdd(Integer.MIN_VALUE, Integer.MIN_VALUE);
089: fail();
090: } catch (ArithmeticException e) {
091: }
092: }
093:
094: public void testSafeAddLong() {
095: assertEquals(0L, FieldUtils.safeAdd(0L, 0L));
096:
097: assertEquals(5L, FieldUtils.safeAdd(2L, 3L));
098: assertEquals(-1L, FieldUtils.safeAdd(2L, -3L));
099: assertEquals(1L, FieldUtils.safeAdd(-2L, 3L));
100: assertEquals(-5L, FieldUtils.safeAdd(-2L, -3L));
101:
102: assertEquals(Long.MAX_VALUE - 1, FieldUtils.safeAdd(
103: Long.MAX_VALUE, -1L));
104: assertEquals(Long.MIN_VALUE + 1, FieldUtils.safeAdd(
105: Long.MIN_VALUE, 1L));
106:
107: assertEquals(-1, FieldUtils.safeAdd(Long.MIN_VALUE,
108: Long.MAX_VALUE));
109: assertEquals(-1, FieldUtils.safeAdd(Long.MAX_VALUE,
110: Long.MIN_VALUE));
111:
112: try {
113: FieldUtils.safeAdd(Long.MAX_VALUE, 1L);
114: fail();
115: } catch (ArithmeticException e) {
116: }
117:
118: try {
119: FieldUtils.safeAdd(Long.MAX_VALUE, 100L);
120: fail();
121: } catch (ArithmeticException e) {
122: }
123:
124: try {
125: FieldUtils.safeAdd(Long.MAX_VALUE, Long.MAX_VALUE);
126: fail();
127: } catch (ArithmeticException e) {
128: }
129:
130: try {
131: FieldUtils.safeAdd(Long.MIN_VALUE, -1L);
132: fail();
133: } catch (ArithmeticException e) {
134: }
135:
136: try {
137: FieldUtils.safeAdd(Long.MIN_VALUE, -100L);
138: fail();
139: } catch (ArithmeticException e) {
140: }
141:
142: try {
143: FieldUtils.safeAdd(Long.MIN_VALUE, Long.MIN_VALUE);
144: fail();
145: } catch (ArithmeticException e) {
146: }
147: }
148:
149: public void testSafeSubtractLong() {
150: assertEquals(0L, FieldUtils.safeSubtract(0L, 0L));
151:
152: assertEquals(-1L, FieldUtils.safeSubtract(2L, 3L));
153: assertEquals(5L, FieldUtils.safeSubtract(2L, -3L));
154: assertEquals(-5L, FieldUtils.safeSubtract(-2L, 3L));
155: assertEquals(1L, FieldUtils.safeSubtract(-2L, -3L));
156:
157: assertEquals(Long.MAX_VALUE - 1, FieldUtils.safeSubtract(
158: Long.MAX_VALUE, 1L));
159: assertEquals(Long.MIN_VALUE + 1, FieldUtils.safeSubtract(
160: Long.MIN_VALUE, -1L));
161:
162: assertEquals(0, FieldUtils.safeSubtract(Long.MIN_VALUE,
163: Long.MIN_VALUE));
164: assertEquals(0, FieldUtils.safeSubtract(Long.MAX_VALUE,
165: Long.MAX_VALUE));
166:
167: try {
168: FieldUtils.safeSubtract(Long.MIN_VALUE, 1L);
169: fail();
170: } catch (ArithmeticException e) {
171: }
172:
173: try {
174: FieldUtils.safeSubtract(Long.MIN_VALUE, 100L);
175: fail();
176: } catch (ArithmeticException e) {
177: }
178:
179: try {
180: FieldUtils.safeSubtract(Long.MIN_VALUE, Long.MAX_VALUE);
181: fail();
182: } catch (ArithmeticException e) {
183: }
184:
185: try {
186: FieldUtils.safeSubtract(Long.MAX_VALUE, -1L);
187: fail();
188: } catch (ArithmeticException e) {
189: }
190:
191: try {
192: FieldUtils.safeSubtract(Long.MAX_VALUE, -100L);
193: fail();
194: } catch (ArithmeticException e) {
195: }
196:
197: try {
198: FieldUtils.safeSubtract(Long.MAX_VALUE, Long.MIN_VALUE);
199: fail();
200: } catch (ArithmeticException e) {
201: }
202: }
203: }
|