< prev index next >

test/java/time/tck/java/time/TCKDuration.java

Print this page




  48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59  */
  60 package tck.java.time;
  61 
  62 import static java.time.temporal.ChronoUnit.DAYS;
  63 import static java.time.temporal.ChronoUnit.HALF_DAYS;
  64 import static java.time.temporal.ChronoUnit.HOURS;
  65 import static java.time.temporal.ChronoUnit.MICROS;
  66 import static java.time.temporal.ChronoUnit.MILLIS;
  67 import static java.time.temporal.ChronoUnit.MINUTES;

  68 import static java.time.temporal.ChronoUnit.NANOS;
  69 import static java.time.temporal.ChronoUnit.SECONDS;
  70 import static java.time.temporal.ChronoUnit.WEEKS;

  71 import static org.testng.Assert.assertEquals;
  72 import static org.testng.Assert.assertTrue;
  73 import static org.testng.Assert.fail;
  74 
  75 import java.io.ByteArrayOutputStream;
  76 import java.io.DataOutputStream;
  77 import java.time.DateTimeException;
  78 import java.time.Duration;
  79 import java.time.Instant;
  80 import java.time.LocalDate;
  81 import java.time.LocalDateTime;
  82 import java.time.LocalTime;
  83 import java.time.Period;
  84 import java.time.ZoneOffset;
  85 import java.time.ZonedDateTime;
  86 import java.time.format.DateTimeParseException;
  87 import java.time.temporal.ChronoUnit;
  88 import java.time.temporal.Temporal;
  89 import java.time.temporal.TemporalAmount;
  90 import java.time.temporal.TemporalUnit;


2270 
2271     @Test
2272     public void multipliedBy_min() {
2273         Duration test = Duration.ofSeconds(1);
2274         assertEquals(test.multipliedBy(Long.MIN_VALUE), Duration.ofSeconds(Long.MIN_VALUE));
2275     }
2276 
2277     @Test(expectedExceptions=ArithmeticException.class)
2278     public void multipliedBy_tooBig() {
2279         Duration test = Duration.ofSeconds(1, 1);
2280         test.multipliedBy(Long.MAX_VALUE);
2281     }
2282 
2283     @Test(expectedExceptions=ArithmeticException.class)
2284     public void multipliedBy_tooBig_negative() {
2285         Duration test = Duration.ofSeconds(1, 1);
2286         test.multipliedBy(Long.MIN_VALUE);
2287     }
2288 
2289     //-----------------------------------------------------------------------































































































































2290     // dividedBy()
2291     //-----------------------------------------------------------------------
2292     @DataProvider(name="DividedBy")
2293     Object[][] provider_dividedBy() {
2294        return new Object[][] {
2295           {-4, 666666667, -3,  1, 111111111},
2296           {-4, 666666667, -2,  1, 666666666},
2297           {-4, 666666667, -1,  3, 333333333},
2298           {-4, 666666667,  1, -4, 666666667},
2299           {-4, 666666667,  2, -2, 333333334},
2300           {-4, 666666667,  3, -2, 888888889},
2301 
2302           {-3, 0, -3,  1, 0},
2303           {-3, 0, -2,  1, 500000000},
2304           {-3, 0, -1,  3, 0},
2305           {-3, 0,  1, -3, 0},
2306           {-3, 0,  2, -2, 500000000},
2307           {-3, 0,  3, -1, 0},
2308 
2309           {-2, 0, -3,  0, 666666666},




  48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59  */
  60 package tck.java.time;
  61 
  62 import static java.time.temporal.ChronoUnit.DAYS;
  63 import static java.time.temporal.ChronoUnit.HALF_DAYS;
  64 import static java.time.temporal.ChronoUnit.HOURS;
  65 import static java.time.temporal.ChronoUnit.MICROS;
  66 import static java.time.temporal.ChronoUnit.MILLIS;
  67 import static java.time.temporal.ChronoUnit.MINUTES;
  68 import static java.time.temporal.ChronoUnit.MONTHS;
  69 import static java.time.temporal.ChronoUnit.NANOS;
  70 import static java.time.temporal.ChronoUnit.SECONDS;
  71 import static java.time.temporal.ChronoUnit.WEEKS;
  72 import static java.time.temporal.ChronoUnit.YEARS;
  73 import static org.testng.Assert.assertEquals;
  74 import static org.testng.Assert.assertTrue;
  75 import static org.testng.Assert.fail;
  76 
  77 import java.io.ByteArrayOutputStream;
  78 import java.io.DataOutputStream;
  79 import java.time.DateTimeException;
  80 import java.time.Duration;
  81 import java.time.Instant;
  82 import java.time.LocalDate;
  83 import java.time.LocalDateTime;
  84 import java.time.LocalTime;
  85 import java.time.Period;
  86 import java.time.ZoneOffset;
  87 import java.time.ZonedDateTime;
  88 import java.time.format.DateTimeParseException;
  89 import java.time.temporal.ChronoUnit;
  90 import java.time.temporal.Temporal;
  91 import java.time.temporal.TemporalAmount;
  92 import java.time.temporal.TemporalUnit;


2272 
2273     @Test
2274     public void multipliedBy_min() {
2275         Duration test = Duration.ofSeconds(1);
2276         assertEquals(test.multipliedBy(Long.MIN_VALUE), Duration.ofSeconds(Long.MIN_VALUE));
2277     }
2278 
2279     @Test(expectedExceptions=ArithmeticException.class)
2280     public void multipliedBy_tooBig() {
2281         Duration test = Duration.ofSeconds(1, 1);
2282         test.multipliedBy(Long.MAX_VALUE);
2283     }
2284 
2285     @Test(expectedExceptions=ArithmeticException.class)
2286     public void multipliedBy_tooBig_negative() {
2287         Duration test = Duration.ofSeconds(1, 1);
2288         test.multipliedBy(Long.MIN_VALUE);
2289     }
2290 
2291     //-----------------------------------------------------------------------
2292     //  truncated(TemporalUnit)
2293     //-----------------------------------------------------------------------
2294     TemporalUnit NINETY_MINS = new TemporalUnit() {
2295         @Override
2296         public Duration getDuration() {
2297             return Duration.ofMinutes(90);
2298         }
2299         @Override
2300         public boolean isDurationEstimated() {
2301             return false;
2302         }
2303         @Override
2304         public boolean isDateBased() {
2305             return false;
2306         }
2307         @Override
2308         public boolean isTimeBased() {
2309             return true;
2310         }
2311         @Override
2312         public boolean isSupportedBy(Temporal temporal) {
2313             return false;
2314         }
2315         @Override
2316         public <R extends Temporal> R addTo(R temporal, long amount) {
2317             throw new UnsupportedOperationException();
2318         }
2319         @Override
2320         public long between(Temporal temporal1, Temporal temporal2) {
2321             throw new UnsupportedOperationException();
2322         }
2323         @Override
2324         public String toString() {
2325             return "NinetyMins";
2326         }
2327     };
2328 
2329     TemporalUnit NINETY_FIVE_MINS = new TemporalUnit() {
2330         @Override
2331         public Duration getDuration() {
2332             return Duration.ofMinutes(95);
2333         }
2334         @Override
2335         public boolean isDurationEstimated() {
2336             return false;
2337         }
2338         @Override
2339         public boolean isDateBased() {
2340             return false;
2341         }
2342         @Override
2343         public boolean isTimeBased() {
2344             return false;
2345         }
2346         @Override
2347         public boolean isSupportedBy(Temporal temporal) {
2348             return false;
2349         }
2350         @Override
2351         public <R extends Temporal> R addTo(R temporal, long amount) {
2352             throw new UnsupportedOperationException();
2353         }
2354         @Override
2355         public long between(Temporal temporal1, Temporal temporal2) {
2356             throw new UnsupportedOperationException();
2357         }
2358         @Override
2359         public String toString() {
2360             return "NinetyFiveMins";
2361         }
2362     };
2363 
2364     @DataProvider(name="truncatedToValid")
2365     Object[][] data_truncatedToValid() {
2366         return new Object[][] {
2367                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), NANOS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789)},
2368                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), MICROS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_000)},
2369                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), MILLIS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 1230_00_000)},
2370                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), SECONDS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 0)},
2371                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), MINUTES, Duration.ofSeconds(86400 + 3600 + 60, 0)},
2372                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), HOURS, Duration.ofSeconds(86400 + 3600, 0)},
2373                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), DAYS, Duration.ofSeconds(86400, 0)},
2374 
2375                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), NINETY_MINS, Duration.ofSeconds(86400 + 0, 0)},
2376                 {Duration.ofSeconds(86400 + 7200 + 60 + 1, 123_456_789), NINETY_MINS, Duration.ofSeconds(86400 + 5400, 0)},
2377                 {Duration.ofSeconds(86400 + 10800 + 60 + 1, 123_456_789), NINETY_MINS, Duration.ofSeconds(86400 + 10800, 0)},
2378 
2379                 {Duration.ofSeconds(-86400 - 3600 - 60 - 1, 123_456_789), MINUTES, Duration.ofSeconds(-86400 - 3600 - 60, 0 )},
2380                 {Duration.ofSeconds(-86400 - 3600 - 60 - 1, 123_456_789), MICROS, Duration.ofSeconds(-86400 - 3600 - 60 - 1, 123_457_000)},
2381 
2382                 {Duration.ofSeconds(86400 + 3600 + 60 + 1, 0), SECONDS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 0)},
2383                 {Duration.ofSeconds(-86400 - 3600 - 120, 0), MINUTES, Duration.ofSeconds(-86400 - 3600 - 120, 0)},
2384 
2385                 {Duration.ofSeconds(-1, 0), SECONDS, Duration.ofSeconds(-1, 0)},
2386                 {Duration.ofSeconds(-1, 123_456_789), SECONDS, Duration.ofSeconds(0, 0)},
2387                 {Duration.ofSeconds(-1, 123_456_789), NANOS, Duration.ofSeconds(0, -876_543_211)},
2388                 {Duration.ofSeconds(0, 123_456_789), SECONDS, Duration.ofSeconds(0, 0)},
2389                 {Duration.ofSeconds(0, 123_456_789), NANOS, Duration.ofSeconds(0, 123_456_789)},
2390         };
2391     }
2392 
2393     @Test(dataProvider="truncatedToValid")
2394     public void test_truncatedTo_valid(Duration input, TemporalUnit unit, Duration expected) {
2395         assertEquals(input.truncatedTo(unit), expected);
2396     }
2397 
2398     @DataProvider(name="truncatedToInvalid")
2399     Object[][] data_truncatedToInvalid() {
2400         return new Object[][] {
2401                 {Duration.ofSeconds(1, 123_456_789), NINETY_FIVE_MINS},
2402                 {Duration.ofSeconds(1, 123_456_789), WEEKS},
2403                 {Duration.ofSeconds(1, 123_456_789), MONTHS},
2404                 {Duration.ofSeconds(1, 123_456_789), YEARS},
2405         };
2406     }
2407 
2408     @Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
2409     public void test_truncatedTo_invalid(Duration input, TemporalUnit unit) {
2410         input.truncatedTo(unit);
2411     }
2412 
2413     @Test(expectedExceptions=NullPointerException.class)
2414     public void test_truncatedTo_null() {
2415         Duration.ofSeconds(1234).truncatedTo(null);
2416     }
2417 
2418     //-----------------------------------------------------------------------
2419     // dividedBy()
2420     //-----------------------------------------------------------------------
2421     @DataProvider(name="DividedBy")
2422     Object[][] provider_dividedBy() {
2423        return new Object[][] {
2424           {-4, 666666667, -3,  1, 111111111},
2425           {-4, 666666667, -2,  1, 666666666},
2426           {-4, 666666667, -1,  3, 333333333},
2427           {-4, 666666667,  1, -4, 666666667},
2428           {-4, 666666667,  2, -2, 333333334},
2429           {-4, 666666667,  3, -2, 888888889},
2430 
2431           {-3, 0, -3,  1, 0},
2432           {-3, 0, -2,  1, 500000000},
2433           {-3, 0, -1,  3, 0},
2434           {-3, 0,  1, -3, 0},
2435           {-3, 0,  2, -2, 500000000},
2436           {-3, 0,  3, -1, 0},
2437 
2438           {-2, 0, -3,  0, 666666666},


< prev index next >