1 /* 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.util; 27 28 import org.junit.Test; 29 import static org.junit.Assert.*; 30 31 /** 32 */ 33 public class DurationTest { 34 35 /************************************************************************************ 36 * 37 * Tests for the static millis() method 38 * 39 ***********************************************************************************/ 40 41 @Test public void millis_withZeroResultsIn_ZERO() { 42 assertSame(Duration.ZERO, Duration.millis(0)); 43 } 44 45 @Test public void millis_withOneResultsIn_ONE() { 46 assertSame(Duration.ONE, Duration.millis(1)); 47 } 48 49 @Test public void millis_withPositiveInfinityResultsIn_INDEFINITE() { 50 assertSame(Duration.INDEFINITE, Duration.millis(Double.POSITIVE_INFINITY)); 51 } 52 53 @Test public void millis_withNaNResultsIn_UNKNOWN() { 54 assertSame(Duration.UNKNOWN, Duration.millis(Double.NaN)); 55 } 56 57 @Test public void millis_withPositiveResultsInNewDuration() { 58 final Duration result = Duration.millis(9); 59 assertEquals(result.toMillis(), 9, 0); 60 assertFalse(result.isIndefinite()); 61 assertFalse(result.isUnknown()); 62 } 63 64 /************************************************************************************ 65 * 66 * Tests for the static seconds() method 67 * 68 ***********************************************************************************/ 69 70 @Test public void seconds_withZeroResultsIn_ZERO() { 71 assertSame(Duration.ZERO, Duration.seconds(0)); 72 } 73 74 @Test public void seconds_withPositiveInfinityResultsIn_INDEFINITE() { 75 assertSame(Duration.INDEFINITE, Duration.seconds(Double.POSITIVE_INFINITY)); 76 } 77 78 @Test public void seconds_withNaNResultsIn_UNKNOWN() { 79 assertSame(Duration.UNKNOWN, Duration.seconds(Double.NaN)); 80 } 81 82 @Test public void seconds_withPositiveResultsInNewDuration() { 83 final Duration result = Duration.seconds(9); 84 assertEquals(result.toSeconds(), 9, 0); 85 assertFalse(result.isIndefinite()); 86 assertFalse(result.isUnknown()); 87 } 88 89 /************************************************************************************ 90 * 91 * Tests for the static minutes() method 92 * 93 ***********************************************************************************/ 94 95 @Test public void minutes_withZeroResultsIn_ZERO() { 96 assertSame(Duration.ZERO, Duration.minutes(0)); 97 } 98 99 @Test public void minutes_withPositiveInfinityResultsIn_INDEFINITE() { 100 assertSame(Duration.INDEFINITE, Duration.minutes(Double.POSITIVE_INFINITY)); 101 } 102 103 @Test public void minutes_withNaNResultsIn_UNKNOWN() { 104 assertSame(Duration.UNKNOWN, Duration.minutes(Double.NaN)); 105 } 106 107 @Test public void minutes_withPositiveResultsInNewDuration() { 108 final Duration result = Duration.minutes(9); 109 assertEquals(result.toMinutes(), 9, 0); 110 assertFalse(result.isIndefinite()); 111 assertFalse(result.isUnknown()); 112 } 113 114 /************************************************************************************ 115 * 116 * Tests for the static hours() method 117 * 118 ***********************************************************************************/ 119 120 @Test public void hours_withZeroResultsIn_ZERO() { 121 assertSame(Duration.ZERO, Duration.hours(0)); 122 } 123 124 @Test public void hours_withPositiveInfinityResultsIn_INDEFINITE() { 125 assertSame(Duration.INDEFINITE, Duration.hours(Double.POSITIVE_INFINITY)); 126 } 127 128 @Test public void hours_withNaNResultsIn_UNKNOWN() { 129 assertSame(Duration.UNKNOWN, Duration.hours(Double.NaN)); 130 } 131 132 @Test public void hours_withPositiveResultsInNewDuration() { 133 final Duration result = Duration.hours(9); 134 assertEquals(result.toHours(), 9, 0); 135 assertFalse(result.isIndefinite()); 136 assertFalse(result.isUnknown()); 137 } 138 139 /************************************************************************************ 140 * 141 * Tests for the one-arg constructor 142 * 143 ***********************************************************************************/ 144 145 @Test public void constructor_withZeroEquals_ZERO() { 146 assertEquals(Duration.ZERO, new Duration(0)); 147 } 148 149 @Test public void constructor_withOneEquals_ONE() { 150 assertEquals(Duration.ONE, new Duration(1)); 151 } 152 153 @Test public void constructor_withPositiveInfinityEquals_INDEFINITE() { 154 assertTrue(Duration.INDEFINITE.equals(new Duration(Double.POSITIVE_INFINITY))); 155 } 156 157 @Test public void constructor_withNaNDoesNotEqual_UNKNOWN() { 158 assertFalse(Duration.UNKNOWN.equals(new Duration(Double.NaN))); 159 } 160 161 @Test public void constructor_withPositiveResultsInNewDuration() { 162 final Duration result = new Duration(9); 163 assertEquals(result.toMillis(), 9, 0); 164 assertFalse(result.isIndefinite()); 165 assertFalse(result.isUnknown()); 166 } 167 168 /************************************************************************************ 169 * 170 * Tests for the toMillis method 171 * 172 ***********************************************************************************/ 173 174 @Test public void toMillis_when_ZERO_resultsInZero() { 175 assertEquals(0, Duration.ZERO.toMillis(), 0); 176 } 177 178 @Test public void toMillis_when_ONE_resultsInOne() { 179 assertEquals(1, Duration.ONE.toMillis(), 0); 180 } 181 182 @Test public void toMillis_when_INDEFINITE_resultsInPositiveInfinity() { 183 assertTrue(Double.isInfinite(Duration.INDEFINITE.toMillis())); 184 } 185 186 @Test public void toMillis_whenNegativeInfinityResultsInNegativeInfinity() { 187 assertTrue(Double.isInfinite(new Duration(Double.NEGATIVE_INFINITY).toMillis())); 188 } 189 190 @Test public void toMillis_when_UNKNOWN_ResultsInNaN() { 191 assertTrue(Double.isNaN(Duration.UNKNOWN.toMillis())); 192 } 193 194 @Test public void toMillis_whenPositiveNumberResultsInTheSameNumber() { 195 assertEquals(87, new Duration(87).toMillis(), 0); 196 } 197 198 @Test public void toMillis_whenNegativeNumberResultsInTheSameNumber() { 199 assertEquals(-7, new Duration(-7).toMillis(), 0); 200 } 201 202 /************************************************************************************ 203 * 204 * Tests for the toSeconds method 205 * 206 ***********************************************************************************/ 207 208 @Test public void toSeconds_when_ZERO_resultsInZero() { 209 assertEquals(0, Duration.ZERO.toSeconds(), 0); 210 } 211 212 @Test public void toSeconds_when_ONE_resultsInPointZeroZeroOne() { 213 assertEquals(.001, Duration.ONE.toSeconds(), 0); 214 } 215 216 @Test public void toSeconds_when_INDEFINITE_resultsInPositiveInfinity() { 217 assertTrue(Double.isInfinite(Duration.INDEFINITE.toSeconds())); 218 } 219 220 @Test public void toSeconds_whenNegativeInfinityResultsInNegativeInfinity() { 221 assertTrue(Double.isInfinite(new Duration(Double.NEGATIVE_INFINITY).toSeconds())); 222 } 223 224 @Test public void toSeconds_when_UNKNOWN_ResultsInNaN() { 225 assertTrue(Double.isNaN(Duration.UNKNOWN.toSeconds())); 226 } 227 228 @Test public void toSeconds_whenPositiveNumberResultsInTheSameNumber() { 229 assertEquals(.087, new Duration(87).toSeconds(), 0); 230 } 231 232 @Test public void toSeconds_whenNegativeNumberResultsInTheSameNumber() { 233 assertEquals(-.007, new Duration(-7).toSeconds(), 0); 234 } 235 236 // toMinutes 237 // 0, +infinity, -infinity, Nan, positive value, negative value 238 // toHours 239 // 0, +infinity, -infinity, Nan, positive value, negative value 240 241 242 /************************************************************************************ 243 * 244 * Tests for adding two Durations. For the most part I just test millis, 245 * because I know the implementation stores just millis, but for completeness 246 * and to avoid regressions should we change the storage format, I'll also 247 * throw in a few tests which work on seconds, minutes, and a mixture of 248 * seconds and minutes. 249 * 250 ***********************************************************************************/ 251 252 @Test public void add_doesNotModifyTheInputArguments() { 253 Duration a = new Duration(10); 254 Duration b = new Duration(20); 255 final Duration result = a.add(b); 256 assertEquals(new Duration(30), result); 257 assertEquals(10, a.toMillis(), 0); 258 assertEquals(20, b.toMillis(), 0); 259 assertFalse(result.isIndefinite()); 260 assertFalse(result.isUnknown()); 261 } 262 263 @Test public void add_ZERO_and_ZERO_ResultsIn_ZERO() { 264 assertSame(Duration.ZERO, Duration.ZERO.add(Duration.ZERO)); 265 } 266 267 @Test public void add_ZERO_and_ONE_ResultsIn_ONE() { 268 assertSame(Duration.ONE, Duration.ZERO.add(Duration.ONE)); 269 assertSame(Duration.ONE, Duration.ONE.add(Duration.ZERO)); 270 } 271 272 @Test public void add_ONE_and_ONE_ResultsInTwo() { 273 final Duration result = Duration.ONE.add(Duration.ONE); 274 assertEquals(new Duration(2), result); 275 assertFalse(result.isIndefinite()); 276 assertFalse(result.isUnknown()); 277 } 278 279 @Test public void add_ONE_Second_and_ONE_Milli_ResultsInOneThousandOneMillis() { 280 final Duration result = Duration.seconds(1).add(Duration.ONE); 281 assertEquals(new Duration(1001), result); 282 assertFalse(result.isIndefinite()); 283 assertFalse(result.isUnknown()); 284 } 285 286 @Test public void add_ZERO_and_INDEFINITE_ResultsInIndefinite() { 287 //assertTrue(0.0 + Double.POSITIVE_INFINITY == Double.POSITIVE_INFINITY); // sanity check 288 assertEquals(new Double(Double.POSITIVE_INFINITY), new Double(0.0 + Double.POSITIVE_INFINITY)); // sanity check 289 290 assertTrue(Duration.ZERO.add(Duration.INDEFINITE).isIndefinite()); 291 assertFalse(Duration.ZERO.add(Duration.INDEFINITE).isUnknown()); 292 } 293 294 @Test public void add_ONE_and_INDEFINITE_ResultsInIndefinite() { 295 //assertTrue(1.0 + Double.POSITIVE_INFINITY == Double.POSITIVE_INFINITY); // sanity check 296 assertEquals(new Double(Double.POSITIVE_INFINITY), new Double(1.0 + Double.POSITIVE_INFINITY)); // sanity check 297 298 assertTrue(Duration.ONE.add(Duration.INDEFINITE).isIndefinite()); 299 assertFalse(Duration.ONE.add(Duration.INDEFINITE).isUnknown()); 300 } 301 302 @Test public void add_INDEFINITE_and_INDEFINITE_ResultsInIndefinite() { 303 //assertTrue(Double.POSITIVE_INFINITY + Double.POSITIVE_INFINITY == Double.POSITIVE_INFINITY); // sanity check 304 assertEquals(new Double(Double.POSITIVE_INFINITY), new Double(Double.POSITIVE_INFINITY + Double.POSITIVE_INFINITY)); // sanity check 305 306 assertTrue(Duration.INDEFINITE.add(Duration.INDEFINITE).isIndefinite()); 307 assertFalse(Duration.INDEFINITE.add(Duration.INDEFINITE).isUnknown()); 308 } 309 310 @Test public void add_UNKNOWN_and_INDEFINITE_ResultsInUnknown() { 311 assertTrue(Double.isNaN(Double.NaN + Double.POSITIVE_INFINITY)); // sanity check 312 assertEquals(new Double(Double.NaN), new Double(Double.NaN + Double.POSITIVE_INFINITY)); // sanity check 313 314 assertFalse(Duration.UNKNOWN.add(Duration.INDEFINITE).isIndefinite()); 315 assertTrue(Duration.UNKNOWN.add(Duration.INDEFINITE).isUnknown()); 316 } 317 318 @Test public void add_ZERO_and_UNKNOWN_ResultsInUnknown() { 319 assertTrue(Double.isNaN(0.0 + Double.NaN)); // sanity check 320 assertEquals(new Double(Double.NaN), new Double(0.0 + Double.NaN)); // sanity check 321 322 assertFalse(Duration.ZERO.add(Duration.UNKNOWN).isIndefinite()); 323 assertTrue(Duration.ZERO.add(Duration.UNKNOWN).isUnknown()); 324 } 325 326 @Test public void add_ONE_and_UNKNOWN_ResultsInUnknown() { 327 assertTrue(Double.isNaN(1.0 + Double.NaN)); // sanity check 328 assertEquals(new Double(Double.NaN), new Double(1.0 + Double.NaN)); // sanity check 329 330 assertFalse(Duration.ONE.add(Duration.UNKNOWN).isIndefinite()); 331 assertTrue(Duration.ONE.add(Duration.UNKNOWN).isUnknown()); 332 } 333 334 @Test public void testAddUsingMixedUnits() { 335 Duration a = Duration.seconds(30); 336 Duration b = Duration.minutes(10); 337 Duration result = a.add(b); 338 Duration expected = Duration.millis((1000 * 30) + (10 * 60 * 1000)); 339 assertEquals(expected, result); 340 assertFalse(result.isIndefinite()); 341 assertFalse(result.isUnknown()); 342 } 343 344 /************************************************************************************ 345 * 346 * Tests for subtracting two Durations 347 * 348 ***********************************************************************************/ 349 350 @Test public void subtract_doesNotModifyTheInputArguments() { 351 Duration a = new Duration(30); 352 Duration b = new Duration(20); 353 Duration result = a.subtract(b); 354 assertEquals(new Duration(10), result); 355 assertEquals(30, a.toMillis(), 0); 356 assertEquals(20, b.toMillis(), 0); 357 assertFalse(result.isIndefinite()); 358 assertFalse(result.isUnknown()); 359 } 360 361 @Test public void subtract_ZERO_and_ZERO_ResultsIn_ZERO() { 362 assertSame(Duration.ZERO, Duration.ZERO.subtract(Duration.ZERO)); 363 } 364 365 @Test public void subtract_ZERO_and_ONE_ResultsIn_NegativeOne() { 366 assertEquals(new Duration(-1), Duration.ZERO.subtract(Duration.ONE)); 367 } 368 369 @Test public void subtract_ONE_and_ZERO_ResultsIn_ONE() { 370 assertSame(Duration.ONE, Duration.ONE.subtract(Duration.ZERO)); 371 } 372 373 @Test public void subtract_ONE_and_ONE_ResultsInZERO() { 374 assertEquals(Duration.ZERO, Duration.ONE.subtract(Duration.ONE)); 375 } 376 377 @Test public void subtract_ONE_Second_and_ONE_Milli_ResultsInNineHundredNinetyNineMillis() { 378 assertEquals(new Duration(999), Duration.seconds(1).subtract(Duration.ONE)); 379 } 380 381 @Test public void subtract_ZERO_and_INDEFINITE_ResultsInNegativeInfinity() { 382 //assertTrue(0.0 - Double.POSITIVE_INFINITY == Double.NEGATIVE_INFINITY); // sanity check 383 assertEquals(new Double(Double.NEGATIVE_INFINITY), new Double(0.0 - Double.POSITIVE_INFINITY)); // sanity check 384 385 final Duration result = Duration.ZERO.subtract(Duration.INDEFINITE); 386 assertFalse(result.isIndefinite()); 387 assertFalse(result.isUnknown()); 388 assertEquals(new Duration(Double.NEGATIVE_INFINITY), result); 389 } 390 391 @Test public void subtract_ONE_and_INDEFINITE_ResultsInNegativeInfinity() { 392 //assertTrue(1.0 - Double.POSITIVE_INFINITY == Double.NEGATIVE_INFINITY); // sanity check 393 assertEquals(new Double(Double.NEGATIVE_INFINITY), new Double(1.0 - Double.POSITIVE_INFINITY)); // sanity check 394 395 final Duration result = Duration.ONE.subtract(Duration.INDEFINITE); 396 assertFalse(result.isIndefinite()); 397 assertFalse(result.isUnknown()); 398 assertEquals(new Duration(Double.NEGATIVE_INFINITY), result); 399 } 400 401 @Test public void subtract_INDEFINITE_and_INDEFINITE_ResultsInUnknown() { 402 assertTrue(Double.isNaN(Double.POSITIVE_INFINITY - Double.POSITIVE_INFINITY)); // sanity check 403 assertEquals(new Double(Double.NaN), new Double(Double.POSITIVE_INFINITY - Double.POSITIVE_INFINITY)); // sanity check 404 405 assertFalse(Duration.INDEFINITE.subtract(Duration.INDEFINITE).isIndefinite()); 406 assertTrue(Duration.INDEFINITE.subtract(Duration.INDEFINITE).isUnknown()); 407 } 408 409 @Test public void subtract_UNKNOWN_and_INDEFINITE_ResultsInUnknown() { 410 assertTrue(Double.isNaN(Double.NaN - Double.POSITIVE_INFINITY)); // sanity check 411 assertEquals(new Double(Double.NaN), new Double(Double.NaN - Double.POSITIVE_INFINITY)); // sanity check 412 413 assertFalse(Duration.UNKNOWN.subtract(Duration.INDEFINITE).isIndefinite()); 414 assertTrue(Duration.UNKNOWN.subtract(Duration.INDEFINITE).isUnknown()); 415 } 416 417 @Test public void subtract_ZERO_and_UNKNOWN_ResultsInUnknown() { 418 assertTrue(Double.isNaN(0 - Double.NaN)); // sanity check 419 assertEquals(new Double(Double.NaN), new Double(0.0 - Double.NaN)); // sanity check 420 421 assertFalse(Duration.ZERO.subtract(Duration.UNKNOWN).isIndefinite()); 422 assertTrue(Duration.ZERO.subtract(Duration.UNKNOWN).isUnknown()); 423 } 424 425 @Test public void subtract_ONE_and_UNKNOWN_ResultsInUnknown() { 426 assertTrue(Double.isNaN(1.0 - Double.NaN)); // sanity check 427 assertEquals(new Double(Double.NaN), new Double(1.0 - Double.NaN)); // sanity check 428 429 assertFalse(Duration.ONE.subtract(Duration.UNKNOWN).isIndefinite()); 430 assertTrue(Duration.ONE.subtract(Duration.UNKNOWN).isUnknown()); 431 } 432 433 @Test public void testSubtractUsingMixedUnits() { 434 Duration a = Duration.minutes(10); 435 Duration b = Duration.seconds(30); 436 Duration expected = Duration.millis((1000 * 30) + (9 * 60 * 1000)); 437 assertEquals(expected, a.subtract(b)); 438 } 439 440 /************************************************************************************ 441 * 442 * Tests for multiplying two Durations 443 * 444 ***********************************************************************************/ 445 446 @Test public void multiply_doesNotModifyTheInputArguments() { 447 Duration a = new Duration(3); 448 Duration result = a.multiply(2); 449 assertEquals(new Duration(6), result); 450 assertEquals(3, a.toMillis(), 0); 451 assertFalse(result.isIndefinite()); 452 assertFalse(result.isUnknown()); 453 } 454 455 @Test public void multiply_ZERO_and_ZERO_ResultsIn_ZERO() { 456 assertSame(Duration.ZERO, Duration.ZERO.multiply(0)); 457 } 458 459 @Test public void multiply_ZERO_and_ONE_ResultsIn_ZERO() { 460 assertSame(Duration.ZERO, Duration.ZERO.multiply(1)); 461 assertSame(Duration.ZERO, Duration.ONE.multiply(0)); 462 } 463 464 @Test public void multiply_ONE_and_ONE_ResultsIn_ONE() { 465 assertEquals(Duration.ONE, Duration.ONE.multiply(1)); 466 } 467 468 @Test public void multiply_ONE_Second_and_ONE_Milli_ResultsInOneSecond() { 469 assertEquals(new Duration(1000), Duration.seconds(1).multiply(1)); 470 } 471 472 @Test public void multiply_ZERO_and_INDEFINITE_ResultsInUnknown() { 473 assertTrue(Double.isNaN(0.0 * Double.POSITIVE_INFINITY)); // sanity check 474 assertEquals(new Double(Double.NaN), new Double(0.0 * Double.POSITIVE_INFINITY)); // sanity check 475 476 assertFalse(Duration.ZERO.multiply(Double.POSITIVE_INFINITY).isIndefinite()); 477 assertTrue(Duration.ZERO.multiply(Double.POSITIVE_INFINITY).isUnknown()); 478 } 479 480 @Test public void multiply_ONE_and_INDEFINITE_ResultsInIndefinite() { 481 //assertTrue(1.0 * Double.POSITIVE_INFINITY == Double.POSITIVE_INFINITY); // sanity check 482 assertEquals(new Double(Double.POSITIVE_INFINITY), new Double(1.0 * Double.POSITIVE_INFINITY)); // sanity check 483 484 assertTrue(Duration.ONE.multiply(Double.POSITIVE_INFINITY).isIndefinite()); 485 assertFalse(Duration.ONE.multiply(Double.POSITIVE_INFINITY).isUnknown()); 486 } 487 488 @Test public void multiply_INDEFINITE_and_INDEFINITE_ResultsInIndefinite() { 489 //assertTrue(Double.POSITIVE_INFINITY * Double.POSITIVE_INFINITY == Double.POSITIVE_INFINITY); // sanity check 490 assertEquals(new Double(Double.POSITIVE_INFINITY), new Double(Double.POSITIVE_INFINITY * Double.POSITIVE_INFINITY)); // sanity check 491 492 assertTrue(Duration.INDEFINITE.multiply(Double.POSITIVE_INFINITY).isIndefinite()); 493 assertFalse(Duration.INDEFINITE.multiply(Double.POSITIVE_INFINITY).isUnknown()); 494 } 495 496 @Test public void multiply_UNKNOWN_and_INDEFINITE_ResultsInUnknown() { 497 assertTrue(Double.isNaN(Double.NaN * Double.POSITIVE_INFINITY)); // sanity check 498 assertEquals(new Double(Double.NaN), new Double(Double.NaN * Double.POSITIVE_INFINITY)); // sanity check 499 500 assertFalse(Duration.UNKNOWN.multiply(Double.POSITIVE_INFINITY).isIndefinite()); 501 assertTrue(Duration.UNKNOWN.multiply(Double.POSITIVE_INFINITY).isUnknown()); 502 } 503 504 @Test public void multiply_ZERO_and_UNKNOWN_ResultsInUnknown() { 505 assertTrue(Double.isNaN(0 * Double.NaN)); // sanity check 506 assertEquals(new Double(Double.NaN), new Double(0.0 * Double.NaN)); // sanity check 507 508 assertFalse(Duration.ZERO.multiply(Double.NaN).isIndefinite()); 509 assertTrue(Duration.ZERO.multiply(Double.NaN).isUnknown()); 510 } 511 512 @Test public void multiply_ONE_and_UNKNOWN_ResultsInUnknown() { 513 assertTrue(Double.isNaN(1.0 * Double.NaN)); // sanity check 514 assertEquals(new Double(Double.NaN), new Double(1.0 * Double.NaN)); // sanity check 515 516 assertFalse(Duration.ONE.multiply(Double.NaN).isIndefinite()); 517 assertTrue(Duration.ONE.multiply(Double.NaN).isUnknown()); 518 } 519 520 @SuppressWarnings("deprecation") 521 @Test public void testMultiplyUsingMixedUnits() { 522 Duration a = Duration.minutes(10); 523 Duration b = Duration.seconds(30); 524 Duration expected = Duration.millis((1000.0 * 30) * (10 * 60 * 1000.0)); 525 assertEquals(expected, a.multiply(b)); 526 } 527 528 /************************************************************************************ 529 * 530 * Tests for dividing two Durations 531 * 532 ***********************************************************************************/ 533 534 @Test public void divide_doesNotModifyTheInputArguments() { 535 Duration a = new Duration(10); 536 Duration result = a.divide(2); 537 assertEquals(new Duration(5), result); 538 assertEquals(10, a.toMillis(), 0); 539 assertFalse(result.isIndefinite()); 540 assertFalse(result.isUnknown()); 541 } 542 543 @Test public void divide_Ten_by_ZERO_ResultsIn_INDEFINITE() { 544 assertTrue(new Duration(10).divide(0).isIndefinite()); 545 assertFalse(new Duration(10).divide(0).isUnknown()); 546 } 547 548 @Test public void divide_ZERO_by_Ten_ResultsIn_ZERO() { 549 assertSame(Duration.ZERO, Duration.ZERO.divide(10)); 550 } 551 552 @Test public void divide_ONE_by_ONE_ResultsIn_ONE() { 553 assertEquals(Duration.ONE, Duration.ONE.divide(1)); 554 } 555 556 @Test public void divide_ONE_Second_by_ONE_Milli_ResultsInOneSecond() { 557 assertEquals(new Duration(1000), Duration.seconds(1).divide(1)); 558 } 559 560 @Test public void divide_ZERO_by_INDEFINITE_ResultsIn_ZERO() { 561 //assertTrue(0.0 / Double.POSITIVE_INFINITY == 0.0); // sanity check 562 assertEquals(new Double(0.0), new Double(0.0 / Double.POSITIVE_INFINITY)); // sanity check 563 564 assertSame(Duration.ZERO, Duration.ZERO.divide(Double.POSITIVE_INFINITY)); 565 } 566 567 @Test public void divide_ONE_by_INDEFINITE_ResultsIn_ZERO() { 568 //assertTrue(1.0 / Double.POSITIVE_INFINITY == 0.0); // sanity check 569 assertEquals(new Double(0.0), new Double(1.0 / Double.POSITIVE_INFINITY)); // sanity check 570 571 assertSame(Duration.ZERO, Duration.ONE.divide(Double.POSITIVE_INFINITY)); 572 } 573 574 @Test public void divide_INDEFINITE_by_INDEFINITE_ResultsInUnknown() { 575 assertTrue(Double.isNaN(Double.POSITIVE_INFINITY / Double.POSITIVE_INFINITY)); // sanity check 576 assertEquals(new Double(Double.NaN), new Double(Double.POSITIVE_INFINITY / Double.POSITIVE_INFINITY)); // sanity check 577 578 assertFalse(Duration.INDEFINITE.divide(Double.POSITIVE_INFINITY).isIndefinite()); 579 assertTrue(Duration.INDEFINITE.divide(Double.POSITIVE_INFINITY).isUnknown()); 580 } 581 582 @Test public void divide_UNKNOWN_by_INDEFINITE_ResultsInUnknown() { 583 assertTrue(Double.isNaN(Double.NaN / Double.POSITIVE_INFINITY)); // sanity check 584 assertEquals(new Double(Double.NaN), new Double(Double.NaN / Double.POSITIVE_INFINITY)); // sanity check 585 586 assertFalse(Duration.UNKNOWN.divide(Double.POSITIVE_INFINITY).isIndefinite()); 587 assertTrue(Duration.UNKNOWN.divide(Double.POSITIVE_INFINITY).isUnknown()); 588 } 589 590 @Test public void divide_ZERO_by_UNKNOWN_ResultsInUnknown() { 591 assertTrue(Double.isNaN(0.0 / Double.NaN)); // sanity check 592 assertEquals(new Double(Double.NaN), new Double(0.0 / Double.NaN)); // sanity check 593 594 assertFalse(Duration.ZERO.divide(Double.NaN).isIndefinite()); 595 assertTrue(Duration.ZERO.divide(Double.NaN).isUnknown()); 596 } 597 598 @Test public void divide_ONE_by_UNKNOWN_ResultsInUnknown() { 599 assertTrue(Double.isNaN(1.0 / Double.NaN)); // sanity check 600 assertEquals(new Double(Double.NaN), new Double(1.0 / Double.NaN)); // sanity check 601 602 assertFalse(Duration.ONE.divide(Double.NaN).isIndefinite()); 603 assertTrue(Duration.ONE.divide(Double.NaN).isUnknown()); 604 } 605 606 @SuppressWarnings("deprecation") 607 @Test public void testDivideUsingMixedUnits() { 608 Duration a = Duration.minutes(10); 609 Duration b = Duration.seconds(30); 610 Duration expected = Duration.millis((10 * 60 * 1000.0) / (1000.0 * 30)); 611 assertEquals(expected, a.divide(b)); 612 } 613 614 /************************************************************************************ 615 * 616 * Tests for negating a Duration 617 * 618 ***********************************************************************************/ 619 620 @Test public void negate_ZERO_ResultsIn_NegativeZeroOr_ZERO() { 621 assertEquals(new Duration(-0), Duration.ZERO.negate()); 622 assertSame(Duration.ZERO, Duration.ZERO.negate()); 623 } 624 625 @Test public void negate_ONE_ResultsIn_NegativeOne() { 626 assertEquals(new Duration(-1), Duration.ONE.negate()); 627 } 628 629 @Test public void negate_INDEFINITE_ResultsIn_NegativeInfinity() { 630 final Duration result = Duration.INDEFINITE.negate(); 631 assertEquals(new Duration(Double.NEGATIVE_INFINITY), result); 632 assertNotSame(Duration.INDEFINITE, result); 633 } 634 635 @Test public void negate_UNKNOWN_ResultsIn_UNKNOWN() { 636 final Duration result = Duration.UNKNOWN.negate(); 637 assertSame(Duration.UNKNOWN, result); 638 assertFalse(result.isIndefinite()); 639 assertTrue(result.isUnknown()); 640 } 641 642 @Test public void negate_NegativeResultsInPositive() { 643 assertEquals(new Duration(50), new Duration(-50).negate()); 644 } 645 646 @Test public void negate_PositiveResultsInNegative() { 647 assertEquals(new Duration(-50), new Duration(50).negate()); 648 } 649 650 /************************************************************************************ 651 * 652 * Tests for lessThan comparison with Duration 653 * 654 ***********************************************************************************/ 655 656 @Test public void negativeInfinityIsLessThan_ZERO() { 657 assertTrue(Double.NEGATIVE_INFINITY < 0); 658 assertTrue(new Duration(Double.NEGATIVE_INFINITY).lessThan(Duration.ZERO)); 659 } 660 661 @Test public void negativeInfinityIsNotLessThanNegativeInfinity() { 662 assertFalse(Double.NEGATIVE_INFINITY < Double.NEGATIVE_INFINITY); 663 Duration a = new Duration(Double.NEGATIVE_INFINITY); 664 assertFalse(a.lessThan(a)); 665 } 666 667 @Test public void negativeNumberIsLessThanZero() { 668 assertTrue(-10 < 0.0); 669 assertTrue(new Duration(-10).lessThan(Duration.ZERO)); 670 } 671 672 @Test public void ZERO_isLessThan_ONE() { 673 assertTrue(0 < 1); 674 assertTrue(Duration.ZERO.lessThan(Duration.ONE)); 675 } 676 677 @Test public void ONE_isLessThan_INDEFINITE() { 678 assertTrue(0 < Double.POSITIVE_INFINITY); 679 assertTrue(Duration.ONE.lessThan(Duration.INDEFINITE)); 680 } 681 682 @Test public void INDEFINITE_isNotLessThan_INDEFINITE() { 683 assertFalse(Double.POSITIVE_INFINITY < Double.POSITIVE_INFINITY); 684 assertFalse(Duration.INDEFINITE.lessThan(Duration.INDEFINITE)); 685 } 686 687 @Test public void UNKNOWN_isNotLessThan_NegativeInfinity() { 688 assertFalse(Double.NaN < Double.NEGATIVE_INFINITY); 689 assertFalse(Duration.UNKNOWN.lessThan(new Duration(Double.NEGATIVE_INFINITY))); 690 } 691 692 @Test public void UNKNOWN_isNotLessThan_ZERO() { 693 assertFalse(Double.NaN < 0.0); 694 assertFalse(Duration.UNKNOWN.lessThan(Duration.ZERO)); 695 } 696 697 @Test public void UNKNOWN_isNotLessThan_ONE() { 698 assertFalse(Double.NaN < 1.0); 699 assertFalse(Duration.UNKNOWN.lessThan(Duration.ONE)); 700 } 701 702 @Test public void UNKNOWN_isNotLessThan_INDEFINITE() { 703 assertFalse(Double.NaN < Double.POSITIVE_INFINITY); 704 assertFalse(Duration.UNKNOWN.lessThan(Duration.INDEFINITE)); 705 } 706 707 @Test public void UNKNOWN_isNotLessThan_UNKNOWN() { 708 assertFalse(Double.NaN < Double.NaN); 709 assertFalse(Duration.UNKNOWN.lessThan(Duration.UNKNOWN)); 710 } 711 712 /************************************************************************************ 713 * 714 * Tests for lessThanOrEqualTo comparison with Duration 715 * 716 ***********************************************************************************/ 717 718 @Test public void negativeInfinityIsLessThanOrEqualTo_ZERO() { 719 assertTrue(Double.NEGATIVE_INFINITY <= 0); 720 assertTrue(new Duration(Double.NEGATIVE_INFINITY).lessThanOrEqualTo(Duration.ZERO)); 721 } 722 723 @Test public void negativeInfinityIsLessThanOrEqualToNegativeInfinity() { 724 assertTrue(Double.NEGATIVE_INFINITY <= Double.NEGATIVE_INFINITY); 725 Duration a = new Duration(Double.NEGATIVE_INFINITY); 726 assertTrue(a.lessThanOrEqualTo(a)); 727 } 728 729 @Test public void negativeNumberIsLessOrEqualToThanZero() { 730 assertTrue(-10 <= 0); 731 assertTrue(new Duration(-10).lessThanOrEqualTo(Duration.ZERO)); 732 } 733 734 @Test public void ZERO_isLessThanOrEqualTo_ONE() { 735 assertTrue(0 <= 1); 736 assertTrue(Duration.ZERO.lessThanOrEqualTo(Duration.ONE)); 737 } 738 739 @Test public void ONE_isLessThanOrEqualTo_INDEFINITE() { 740 assertTrue(1 <= Double.POSITIVE_INFINITY); 741 assertTrue(Duration.ONE.lessThanOrEqualTo(Duration.INDEFINITE)); 742 } 743 744 @Test public void INDEFINITE_isLessThanOrEqualTo_INDEFINITE() { 745 assertTrue(Double.POSITIVE_INFINITY <= Double.POSITIVE_INFINITY); 746 assertTrue(Duration.INDEFINITE.lessThanOrEqualTo(Duration.INDEFINITE)); 747 } 748 749 @Test public void UNKNOWN_isNotLessThanOrEqualTo_NegativeInfinity() { 750 assertFalse(Double.NaN <= Double.NEGATIVE_INFINITY); 751 assertFalse(Duration.UNKNOWN.lessThanOrEqualTo(new Duration(Double.NEGATIVE_INFINITY))); 752 } 753 754 @Test public void UNKNOWN_isNotLessThanOrEqualTo_ZERO() { 755 assertFalse(Double.NaN <= 0.0); 756 assertFalse(Duration.UNKNOWN.lessThanOrEqualTo(Duration.ZERO)); 757 } 758 759 @Test public void UNKNOWN_isNotLessThanOrEqualTo_ONE() { 760 assertFalse(Double.NaN <= 1.0); 761 assertFalse(Duration.UNKNOWN.lessThanOrEqualTo(Duration.ONE)); 762 } 763 764 @Test public void UNKNOWN_isNotLessThanOrEqualTo_INDEFINITE() { 765 assertFalse(Double.NaN <= Double.POSITIVE_INFINITY); 766 assertFalse(Duration.UNKNOWN.lessThanOrEqualTo(Duration.INDEFINITE)); 767 } 768 769 @Test public void UNKNOWN_isNotLessThanOrEqualTo_UNKNOWN() { 770 assertFalse(Double.NaN <= Double.NaN); 771 assertFalse(Duration.UNKNOWN.lessThanOrEqualTo(Duration.UNKNOWN)); 772 } 773 774 /************************************************************************************ 775 * 776 * Tests for greaterThan comparison with Duration 777 * 778 ***********************************************************************************/ 779 780 @Test public void ZERO_isGreaterThanNegativeInfinity() { 781 assertTrue(Duration.ZERO.greaterThan(new Duration(Double.NEGATIVE_INFINITY))); 782 } 783 784 @Test public void negativeInfinityIsNotGreaterThanNegativeInfinity() { 785 Duration a = new Duration(Double.NEGATIVE_INFINITY); 786 assertFalse(a.greaterThan(a)); 787 } 788 789 @Test public void ZERO_isGreaterThanNegativeNumber() { 790 assertTrue(Duration.ZERO.greaterThan(new Duration(-10))); 791 } 792 793 @Test public void ONE_isGreaterThan_ZERO() { 794 assertTrue(Duration.ONE.greaterThan(Duration.ZERO)); 795 } 796 797 @Test public void INDEFINITE_isGreaterThan_ONE() { 798 assertTrue(Duration.INDEFINITE.greaterThan(Duration.ONE)); 799 } 800 801 @Test public void INDEFINITE_isNotGreaterThan_INDEFINITE() { 802 assertFalse(Duration.INDEFINITE.greaterThan(Duration.INDEFINITE)); 803 } 804 805 @Test public void UNKNOWN_isNotGreaterThan_NegativeInfinity() { 806 assertFalse(Duration.UNKNOWN.greaterThan(new Duration(Double.NEGATIVE_INFINITY))); 807 } 808 809 @Test public void UNKNOWN_isNotGreaterThan_ZERO() { 810 assertFalse(Duration.UNKNOWN.greaterThan(Duration.ZERO)); 811 } 812 813 @Test public void UNKNOWN_isNotGreaterThan_ONE() { 814 assertFalse(Duration.UNKNOWN.greaterThan(Duration.ONE)); 815 } 816 817 @Test public void UNKNOWN_isNotGreaterThan_INDEFINITE() { 818 assertFalse(Duration.UNKNOWN.greaterThan(Duration.INDEFINITE)); 819 } 820 821 @Test public void UNKNOWN_isNotGreaterThan_UNKNOWN() { 822 assertFalse(Duration.UNKNOWN.greaterThan(Duration.UNKNOWN)); 823 } 824 825 /************************************************************************************ 826 * 827 * Tests for greaterThanOrEqualTo comparison with Duration 828 * 829 ***********************************************************************************/ 830 831 @Test public void ZERO_isGreaterThanOrEqualToNegativeInfinity() { 832 assertTrue(Duration.ZERO.greaterThanOrEqualTo(new Duration(Double.NEGATIVE_INFINITY))); 833 } 834 835 @Test public void negativeInfinityIsGreaterThanOrEqualToNegativeInfinity() { 836 Duration a = new Duration(Double.NEGATIVE_INFINITY); 837 assertTrue(a.greaterThanOrEqualTo(a)); 838 } 839 840 @Test public void ZERO_isGreaterThanOrEqualToNegativeNumber() { 841 assertTrue(Duration.ZERO.greaterThanOrEqualTo(new Duration(-10))); 842 } 843 844 @Test public void ONE_isGreaterThanOrEqualTo_ZERO() { 845 assertTrue(Duration.ONE.greaterThanOrEqualTo(Duration.ZERO)); 846 } 847 848 @Test public void INDEFINITE_isGreaterThanOrEqualTo_ONE() { 849 assertTrue(Duration.INDEFINITE.greaterThanOrEqualTo(Duration.ONE)); 850 } 851 852 @Test public void INDEFINITE_isGreaterThanOrEqualTo_INDEFINITE() { 853 assertTrue(Duration.INDEFINITE.greaterThanOrEqualTo(Duration.INDEFINITE)); 854 } 855 856 @Test public void UNKNOWN_isNotGreaterThanOrEqualTo_NegativeInfinity() { 857 assertFalse(Duration.UNKNOWN.greaterThanOrEqualTo(new Duration(Double.NEGATIVE_INFINITY))); 858 } 859 860 @Test public void UNKNOWN_isNotGreaterThanOrEqualTo_ZERO() { 861 assertFalse(Duration.UNKNOWN.greaterThanOrEqualTo(Duration.ZERO)); 862 } 863 864 @Test public void UNKNOWN_isNotGreaterThanOrEqualTo_ONE() { 865 assertFalse(Duration.UNKNOWN.greaterThanOrEqualTo(Duration.ONE)); 866 } 867 868 @Test public void UNKNOWN_isNotGreaterThanOrEqualTo_INDEFINITE() { 869 assertFalse(Duration.UNKNOWN.greaterThanOrEqualTo(Duration.INDEFINITE)); 870 } 871 872 @Test public void UNKNOWN_isNotGreaterThanOrEqualTo_UNKNOWN() { 873 assertFalse(Duration.UNKNOWN.greaterThanOrEqualTo(Duration.UNKNOWN)); 874 } 875 876 /************************************************************************************ 877 * 878 * Tests for equality comparison with Duration 879 * 880 ***********************************************************************************/ 881 882 @Test public void NegativeInfinityEqualsNegativeInfinity() { 883 assertEquals(new Duration(Double.NEGATIVE_INFINITY), new Duration(Double.NEGATIVE_INFINITY)); 884 } 885 886 @Test public void ZERO_EqualsZero() { 887 assertEquals(Duration.ZERO, new Duration(0)); 888 } 889 890 @Test public void ONE_EqualsOne() { 891 assertEquals(Duration.ONE, new Duration(1)); 892 } 893 894 @Test public void INDEFINITE_Equals_PositiveInfinity() { 895 assertEquals(Duration.INDEFINITE, new Duration(Double.POSITIVE_INFINITY)); 896 } 897 898 @Test public void UNKNOWN_DoesNotEqualNaN() { 899 assertFalse(Duration.UNKNOWN.equals(new Duration(Double.NaN))); 900 } 901 902 @Test public void UNKNOWN_Equals_UNKNOWN() { 903 assertEquals(Duration.UNKNOWN, Duration.UNKNOWN); 904 } 905 }