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; 33 34 import static org.testng.Assert.assertEquals; 35 import static org.testng.Assert.assertTrue; 36 37 import java.lang.reflect.Constructor; 38 import java.lang.reflect.Modifier; 39 import java.util.Collections; 40 41 import org.testng.annotations.DataProvider; 42 import org.testng.annotations.Test; 43 import org.threeten.bp.jdk8.Jdk8Methods; 44 45 /** 46 * Test. 47 */ 48 @Test 49 public class TestDateTimes_implementation { 50 51 @SuppressWarnings("rawtypes") test_constructor()52 public void test_constructor() throws Exception { 53 for (Constructor constructor : Jdk8Methods.class.getDeclaredConstructors()) { 54 assertTrue(Modifier.isPrivate(constructor.getModifiers())); 55 constructor.setAccessible(true); 56 constructor.newInstance(Collections.nCopies(constructor.getParameterTypes().length, null).toArray()); 57 } 58 } 59 60 //----------------------------------------------------------------------- 61 // safeAdd() 62 //----------------------------------------------------------------------- 63 @DataProvider(name="safeAddIntProvider") safeAddIntProvider()64 Object[][] safeAddIntProvider() { 65 return new Object[][] { 66 {Integer.MIN_VALUE, 1, Integer.MIN_VALUE + 1}, 67 {-1, 1, 0}, 68 {0, 0, 0}, 69 {1, -1, 0}, 70 {Integer.MAX_VALUE, -1, Integer.MAX_VALUE - 1}, 71 }; 72 } 73 74 @Test(dataProvider="safeAddIntProvider") test_safeAddInt(int a, int b, int expected)75 public void test_safeAddInt(int a, int b, int expected) { 76 assertEquals(Jdk8Methods.safeAdd(a, b), expected); 77 } 78 79 @DataProvider(name="safeAddIntProviderOverflow") safeAddIntProviderOverflow()80 Object[][] safeAddIntProviderOverflow() { 81 return new Object[][] { 82 {Integer.MIN_VALUE, - 1}, 83 {Integer.MIN_VALUE + 1, -2}, 84 {Integer.MAX_VALUE - 1, 2}, 85 {Integer.MAX_VALUE, 1}, 86 }; 87 } 88 89 @Test(dataProvider="safeAddIntProviderOverflow", expectedExceptions=ArithmeticException.class) test_safeAddInt_overflow(int a, int b)90 public void test_safeAddInt_overflow(int a, int b) { 91 Jdk8Methods.safeAdd(a, b); 92 } 93 94 @DataProvider(name="safeAddLongProvider") safeAddLongProvider()95 Object[][] safeAddLongProvider() { 96 return new Object[][] { 97 {Long.MIN_VALUE, 1, Long.MIN_VALUE + 1}, 98 {-1, 1, 0}, 99 {0, 0, 0}, 100 {1, -1, 0}, 101 {Long.MAX_VALUE, -1, Long.MAX_VALUE - 1}, 102 }; 103 } 104 105 @Test(dataProvider="safeAddLongProvider") test_safeAddLong(long a, long b, long expected)106 public void test_safeAddLong(long a, long b, long expected) { 107 assertEquals(Jdk8Methods.safeAdd(a, b), expected); 108 } 109 110 @DataProvider(name="safeAddLongProviderOverflow") safeAddLongProviderOverflow()111 Object[][] safeAddLongProviderOverflow() { 112 return new Object[][] { 113 {Long.MIN_VALUE, - 1}, 114 {Long.MIN_VALUE + 1, -2}, 115 {Long.MAX_VALUE - 1, 2}, 116 {Long.MAX_VALUE, 1}, 117 }; 118 } 119 120 @Test(dataProvider="safeAddLongProviderOverflow", expectedExceptions=ArithmeticException.class) test_safeAddLong_overflow(long a, long b)121 public void test_safeAddLong_overflow(long a, long b) { 122 Jdk8Methods.safeAdd(a, b); 123 } 124 125 //----------------------------------------------------------------------- 126 // safeSubtract() 127 //----------------------------------------------------------------------- 128 @DataProvider(name="safeSubtractIntProvider") safeSubtractIntProvider()129 Object[][] safeSubtractIntProvider() { 130 return new Object[][] { 131 {Integer.MIN_VALUE, -1, Integer.MIN_VALUE + 1}, 132 {-1, -1, 0}, 133 {0, 0, 0}, 134 {1, 1, 0}, 135 {Integer.MAX_VALUE, 1, Integer.MAX_VALUE - 1}, 136 }; 137 } 138 139 @Test(dataProvider="safeSubtractIntProvider") test_safeSubtractInt(int a, int b, int expected)140 public void test_safeSubtractInt(int a, int b, int expected) { 141 assertEquals(Jdk8Methods.safeSubtract(a, b), expected); 142 } 143 144 @DataProvider(name="safeSubtractIntProviderOverflow") safeSubtractIntProviderOverflow()145 Object[][] safeSubtractIntProviderOverflow() { 146 return new Object[][] { 147 {Integer.MIN_VALUE, 1}, 148 {Integer.MIN_VALUE + 1, 2}, 149 {Integer.MAX_VALUE - 1, -2}, 150 {Integer.MAX_VALUE, -1}, 151 }; 152 } 153 154 @Test(dataProvider="safeSubtractIntProviderOverflow", expectedExceptions=ArithmeticException.class) test_safeSubtractInt_overflow(int a, int b)155 public void test_safeSubtractInt_overflow(int a, int b) { 156 Jdk8Methods.safeSubtract(a, b); 157 } 158 159 @DataProvider(name="safeSubtractLongProvider") safeSubtractLongProvider()160 Object[][] safeSubtractLongProvider() { 161 return new Object[][] { 162 {Long.MIN_VALUE, -1, Long.MIN_VALUE + 1}, 163 {-1, -1, 0}, 164 {0, 0, 0}, 165 {1, 1, 0}, 166 {Long.MAX_VALUE, 1, Long.MAX_VALUE - 1}, 167 }; 168 } 169 170 @Test(dataProvider="safeSubtractLongProvider") test_safeSubtractLong(long a, long b, long expected)171 public void test_safeSubtractLong(long a, long b, long expected) { 172 assertEquals(Jdk8Methods.safeSubtract(a, b), expected); 173 } 174 175 @DataProvider(name="safeSubtractLongProviderOverflow") safeSubtractLongProviderOverflow()176 Object[][] safeSubtractLongProviderOverflow() { 177 return new Object[][] { 178 {Long.MIN_VALUE, 1}, 179 {Long.MIN_VALUE + 1, 2}, 180 {Long.MAX_VALUE - 1, -2}, 181 {Long.MAX_VALUE, -1}, 182 }; 183 } 184 185 @Test(dataProvider="safeSubtractLongProviderOverflow", expectedExceptions=ArithmeticException.class) test_safeSubtractLong_overflow(long a, long b)186 public void test_safeSubtractLong_overflow(long a, long b) { 187 Jdk8Methods.safeSubtract(a, b); 188 } 189 190 //----------------------------------------------------------------------- 191 // safeMultiply() 192 //----------------------------------------------------------------------- 193 @DataProvider(name="safeMultiplyIntProvider") safeMultiplyIntProvider()194 Object[][] safeMultiplyIntProvider() { 195 return new Object[][] { 196 {Integer.MIN_VALUE, 1, Integer.MIN_VALUE}, 197 {Integer.MIN_VALUE / 2, 2, Integer.MIN_VALUE}, 198 {-1, -1, 1}, 199 {-1, 1, -1}, 200 {0, -1, 0}, 201 {0, 0, 0}, 202 {0, 1, 0}, 203 {1, -1, -1}, 204 {1, 1, 1}, 205 {Integer.MAX_VALUE / 2, 2, Integer.MAX_VALUE - 1}, 206 {Integer.MAX_VALUE, -1, Integer.MIN_VALUE + 1}, 207 }; 208 } 209 210 @Test(dataProvider="safeMultiplyIntProvider") test_safeMultiplyInt(int a, int b, int expected)211 public void test_safeMultiplyInt(int a, int b, int expected) { 212 assertEquals(Jdk8Methods.safeMultiply(a, b), expected); 213 } 214 215 @DataProvider(name="safeMultiplyIntProviderOverflow") safeMultiplyIntProviderOverflow()216 Object[][] safeMultiplyIntProviderOverflow() { 217 return new Object[][] { 218 {Integer.MIN_VALUE, 2}, 219 {Integer.MIN_VALUE / 2 - 1, 2}, 220 {Integer.MAX_VALUE, 2}, 221 {Integer.MAX_VALUE / 2 + 1, 2}, 222 {Integer.MIN_VALUE, -1}, 223 {-1, Integer.MIN_VALUE}, 224 }; 225 } 226 227 @Test(dataProvider="safeMultiplyIntProviderOverflow", expectedExceptions=ArithmeticException.class) test_safeMultiplyInt_overflow(int a, int b)228 public void test_safeMultiplyInt_overflow(int a, int b) { 229 Jdk8Methods.safeMultiply(a, b); 230 } 231 232 //----------------------------------------------------------------------- 233 @DataProvider(name="safeMultiplyLongProvider") safeMultiplyLongProvider()234 Object[][] safeMultiplyLongProvider() { 235 return new Object[][] { 236 {Long.MIN_VALUE, 1, Long.MIN_VALUE}, 237 {Long.MIN_VALUE / 2, 2, Long.MIN_VALUE}, 238 {-1, -1, 1}, 239 {-1, 1, -1}, 240 {0, -1, 0}, 241 {0, 0, 0}, 242 {0, 1, 0}, 243 {1, -1, -1}, 244 {1, 1, 1}, 245 {Long.MAX_VALUE / 2, 2, Long.MAX_VALUE - 1}, 246 {Long.MAX_VALUE, -1, Long.MIN_VALUE + 1}, 247 {-1, Integer.MIN_VALUE, -((long) Integer.MIN_VALUE)}, 248 }; 249 } 250 251 @Test(dataProvider="safeMultiplyLongProvider") test_safeMultiplyLong(long a, int b, long expected)252 public void test_safeMultiplyLong(long a, int b, long expected) { 253 assertEquals(Jdk8Methods.safeMultiply(a, b), expected); 254 } 255 256 @DataProvider(name="safeMultiplyLongProviderOverflow") safeMultiplyLongProviderOverflow()257 Object[][] safeMultiplyLongProviderOverflow() { 258 return new Object[][] { 259 {Long.MIN_VALUE, 2}, 260 {Long.MIN_VALUE / 2 - 1, 2}, 261 {Long.MAX_VALUE, 2}, 262 {Long.MAX_VALUE / 2 + 1, 2}, 263 {Long.MIN_VALUE, -1}, 264 }; 265 } 266 267 @Test(dataProvider="safeMultiplyLongProviderOverflow", expectedExceptions=ArithmeticException.class) test_safeMultiplyLong_overflow(long a, int b)268 public void test_safeMultiplyLong_overflow(long a, int b) { 269 Jdk8Methods.safeMultiply(a, b); 270 } 271 272 //----------------------------------------------------------------------- 273 @DataProvider(name="safeMultiplyLongLongProvider") safeMultiplyLongLongProvider()274 Object[][] safeMultiplyLongLongProvider() { 275 return new Object[][] { 276 {Long.MIN_VALUE, 1, Long.MIN_VALUE}, 277 {Long.MIN_VALUE / 2, 2, Long.MIN_VALUE}, 278 {-1, -1, 1}, 279 {-1, 1, -1}, 280 {0, -1, 0}, 281 {0, 0, 0}, 282 {0, 1, 0}, 283 {1, -1, -1}, 284 {1, 1, 1}, 285 {Long.MAX_VALUE / 2, 2, Long.MAX_VALUE - 1}, 286 {Long.MAX_VALUE, -1, Long.MIN_VALUE + 1}, 287 }; 288 } 289 290 @Test(dataProvider="safeMultiplyLongLongProvider") test_safeMultiplyLongLong(long a, long b, long expected)291 public void test_safeMultiplyLongLong(long a, long b, long expected) { 292 assertEquals(Jdk8Methods.safeMultiply(a, b), expected); 293 } 294 295 @DataProvider(name="safeMultiplyLongLongProviderOverflow") safeMultiplyLongLongProviderOverflow()296 Object[][] safeMultiplyLongLongProviderOverflow() { 297 return new Object[][] { 298 {Long.MIN_VALUE, 2}, 299 {Long.MIN_VALUE / 2 - 1, 2}, 300 {Long.MAX_VALUE, 2}, 301 {Long.MAX_VALUE / 2 + 1, 2}, 302 {Long.MIN_VALUE, -1}, 303 {-1, Long.MIN_VALUE}, 304 }; 305 } 306 307 @Test(dataProvider="safeMultiplyLongLongProviderOverflow", expectedExceptions=ArithmeticException.class) test_safeMultiplyLongLong_overflow(long a, long b)308 public void test_safeMultiplyLongLong_overflow(long a, long b) { 309 Jdk8Methods.safeMultiply(a, b); 310 } 311 312 //----------------------------------------------------------------------- 313 // safeToInt() 314 //----------------------------------------------------------------------- 315 @DataProvider(name="safeToIntProvider") safeToIntProvider()316 Object[][] safeToIntProvider() { 317 return new Object[][] { 318 {Integer.MIN_VALUE}, 319 {Integer.MIN_VALUE + 1}, 320 {-1}, 321 {0}, 322 {1}, 323 {Integer.MAX_VALUE - 1}, 324 {Integer.MAX_VALUE}, 325 }; 326 } 327 328 @Test(dataProvider="safeToIntProvider") test_safeToInt(long l)329 public void test_safeToInt(long l) { 330 assertEquals(Jdk8Methods.safeToInt(l), l); 331 } 332 333 @DataProvider(name="safeToIntProviderOverflow") safeToIntProviderOverflow()334 Object[][] safeToIntProviderOverflow() { 335 return new Object[][] { 336 {Long.MIN_VALUE}, 337 {Integer.MIN_VALUE - 1L}, 338 {Integer.MAX_VALUE + 1L}, 339 {Long.MAX_VALUE}, 340 }; 341 } 342 343 @Test(dataProvider="safeToIntProviderOverflow", expectedExceptions=ArithmeticException.class) test_safeToInt_overflow(long l)344 public void test_safeToInt_overflow(long l) { 345 Jdk8Methods.safeToInt(l); 346 } 347 348 //----------------------------------------------------------------------- 349 // safeCompare() 350 //----------------------------------------------------------------------- test_safeCompare_int()351 public void test_safeCompare_int() { 352 doTest_safeCompare_int( 353 Integer.MIN_VALUE, 354 Integer.MIN_VALUE + 1, 355 Integer.MIN_VALUE + 2, 356 -2, 357 -1, 358 0, 359 1, 360 2, 361 Integer.MAX_VALUE - 2, 362 Integer.MAX_VALUE - 1, 363 Integer.MAX_VALUE 364 ); 365 } 366 doTest_safeCompare_int(int... values)367 private void doTest_safeCompare_int(int... values) { 368 for (int i = 0; i < values.length; i++) { 369 int a = values[i]; 370 for (int j = 0; j < values.length; j++) { 371 int b = values[j]; 372 assertEquals(Jdk8Methods.compareInts(a, b), a < b ? -1 : (a > b ? 1 : 0), a + " <=> " + b); 373 } 374 } 375 } 376 test_safeCompare_long()377 public void test_safeCompare_long() { 378 doTest_safeCompare_long( 379 Long.MIN_VALUE, 380 Long.MIN_VALUE + 1, 381 Long.MIN_VALUE + 2, 382 Integer.MIN_VALUE, 383 Integer.MIN_VALUE + 1, 384 Integer.MIN_VALUE + 2, 385 -2, 386 -1, 387 0, 388 1, 389 2, 390 Integer.MAX_VALUE - 2, 391 Integer.MAX_VALUE - 1, 392 Integer.MAX_VALUE, 393 Long.MAX_VALUE - 2, 394 Long.MAX_VALUE - 1, 395 Long.MAX_VALUE 396 ); 397 } 398 doTest_safeCompare_long(long... values)399 private void doTest_safeCompare_long(long... values) { 400 for (int i = 0; i < values.length; i++) { 401 long a = values[i]; 402 for (int j = 0; j < values.length; j++) { 403 long b = values[j]; 404 assertEquals(Jdk8Methods.compareLongs(a, b), a < b ? -1 : (a > b ? 1 : 0), a + " <=> " + b); 405 } 406 } 407 } 408 409 //------------------------------------------------------------------------- 410 @DataProvider(name="FloorDiv") data_floorDiv()411 Object[][] data_floorDiv() { 412 return new Object[][] { 413 {5L, 4, 1L}, 414 {4L, 4, 1L}, 415 {3L, 4, 0L}, 416 {2L, 4, 0L}, 417 {1L, 4, 0L}, 418 {0L, 4, 0L}, 419 {-1L, 4, -1L}, 420 {-2L, 4, -1L}, 421 {-3L, 4, -1L}, 422 {-4L, 4, -1L}, 423 {-5L, 4, -2L}, 424 }; 425 } 426 427 @Test(dataProvider="FloorDiv") test_floorDiv_long(long a, int b, long expected)428 public void test_floorDiv_long(long a, int b, long expected) { 429 assertEquals(Jdk8Methods.floorDiv(a, b), expected); 430 } 431 432 @Test(dataProvider="FloorDiv") test_floorDiv_int(long a, int b, long expected)433 public void test_floorDiv_int(long a, int b, long expected) { 434 if (a <= Integer.MAX_VALUE && a >= Integer.MIN_VALUE) { 435 assertEquals(Jdk8Methods.floorDiv((int) a, b), (int) expected); 436 } 437 } 438 439 //------------------------------------------------------------------------- 440 @DataProvider(name="FloorMod") data_floorMod()441 Object[][] data_floorMod() { 442 return new Object[][] { 443 {5L, 4, 1}, 444 {4L, 4, 0}, 445 {3L, 4, 3}, 446 {2L, 4, 2}, 447 {1L, 4, 1}, 448 {0L, 4, 0}, 449 {-1L, 4, 3}, 450 {-2L, 4, 2}, 451 {-3L, 4, 1}, 452 {-4L, 4, 0}, 453 {-5L, 4, 3}, 454 }; 455 } 456 457 @Test(dataProvider="FloorMod") test_floorMod_long(long a, long b, int expected)458 public void test_floorMod_long(long a, long b, int expected) { 459 assertEquals(Jdk8Methods.floorMod(a, b), expected); 460 } 461 462 @Test(dataProvider="FloorMod") test_floorMod_long(long a, int b, int expected)463 public void test_floorMod_long(long a, int b, int expected) { 464 assertEquals(Jdk8Methods.floorMod(a, b), expected); 465 } 466 467 @Test(dataProvider="FloorMod") test_floorMod_int(long a, int b, int expected)468 public void test_floorMod_int(long a, int b, int expected) { 469 if (a <= Integer.MAX_VALUE && a >= Integer.MIN_VALUE) { 470 assertEquals(Jdk8Methods.floorMod((int) a, b), expected); 471 } 472 } 473 474 } 475