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(Integer.MAX_VALUE, -1)); 048 assertEquals(Integer.MIN_VALUE + 1, FieldUtils.safeAdd(Integer.MIN_VALUE, 1)); 049 050 assertEquals(-1, FieldUtils.safeAdd(Integer.MIN_VALUE, Integer.MAX_VALUE)); 051 assertEquals(-1, FieldUtils.safeAdd(Integer.MAX_VALUE, Integer.MIN_VALUE)); 052 053 try { 054 FieldUtils.safeAdd(Integer.MAX_VALUE, 1); 055 fail(); 056 } catch (ArithmeticException e) { 057 } 058 059 try { 060 FieldUtils.safeAdd(Integer.MAX_VALUE, 100); 061 fail(); 062 } catch (ArithmeticException e) { 063 } 064 065 try { 066 FieldUtils.safeAdd(Integer.MAX_VALUE, Integer.MAX_VALUE); 067 fail(); 068 } catch (ArithmeticException e) { 069 } 070 071 try { 072 FieldUtils.safeAdd(Integer.MIN_VALUE, -1); 073 fail(); 074 } catch (ArithmeticException e) { 075 } 076 077 try { 078 FieldUtils.safeAdd(Integer.MIN_VALUE, -100); 079 fail(); 080 } catch (ArithmeticException e) { 081 } 082 083 try { 084 FieldUtils.safeAdd(Integer.MIN_VALUE, Integer.MIN_VALUE); 085 fail(); 086 } catch (ArithmeticException e) { 087 } 088 } 089 090 public void testSafeAddLong() { 091 assertEquals(0L, FieldUtils.safeAdd(0L, 0L)); 092 093 assertEquals(5L, FieldUtils.safeAdd(2L, 3L)); 094 assertEquals(-1L, FieldUtils.safeAdd(2L, -3L)); 095 assertEquals(1L, FieldUtils.safeAdd(-2L, 3L)); 096 assertEquals(-5L, FieldUtils.safeAdd(-2L, -3L)); 097 098 assertEquals(Long.MAX_VALUE - 1, FieldUtils.safeAdd(Long.MAX_VALUE, -1L)); 099 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