/* * Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * This file is available under and governed by the GNU General Public * License version 2 only, as published by the Free Software Foundation. * However, the following notice accompanied the original version of this * file: * * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * * Neither the name of JSR-310 nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ package tck.java.time;
privatevoid check(LocalTime test, int h, int m, int s, int n) {
assertEquals(test.getHour(), h);
assertEquals(test.getMinute(), m);
assertEquals(test.getSecond(), s);
assertEquals(test.getNano(), n);
assertEquals(test, test);
assertEquals(test.hashCode(), test.hashCode());
assertEquals(LocalTime.of(h, m, s, n), test);
}
//-----------------------------------------------------------------------
@Test(dataProvider="sampleTimes") publicvoid test_get(int h, int m, int s, int ns) {
LocalTime a = LocalTime.of(h, m, s, ns);
assertEquals(a.getHour(), h);
assertEquals(a.getMinute(), m);
assertEquals(a.getSecond(), s);
assertEquals(a.getNano(), ns);
}
// Returns a {@code LocalTime} with the specified nano-of-second. // The hour, minute and second will be unchanged.
@Test publicvoid test_with_longTemporalField_nanoOfSecond() { for (long i : testPoints(1_000_000_000L)) {
LocalTime test = TEST_12_30_40_987654321.with(NANO_OF_SECOND, i);
assertEquals(test.get(NANO_OF_SECOND), i);
assertEquals(test.get(HOUR_OF_DAY), TEST_12_30_40_987654321.get(HOUR_OF_DAY));
assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR));
assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE));
}
}
// Returns a {@code LocalTime} with the specified nano-of-day. // This completely replaces the time and is equivalent to {@link #ofNanoOfDay(long)}.
@Test publicvoid test_with_longTemporalField_nanoOfDay() { for (long i : testPoints(86_400_000_000_000L)) {
LocalTime test = TEST_12_30_40_987654321.with(NANO_OF_DAY, i);
assertEquals(test, LocalTime.ofNanoOfDay(i));
}
}
// Returns a {@code LocalTime} with the nano-of-second replaced by the specified // micro-of-second multiplied by 1,000. // The hour, minute and second will be unchanged.
@Test publicvoid test_with_longTemporalField_microOfSecond() { for (long i : testPoints(1_000_000L)) {
LocalTime test = TEST_12_30_40_987654321.with(MICRO_OF_SECOND, i);
assertEquals(test.get(NANO_OF_SECOND), i * 1_000);
assertEquals(test.get(HOUR_OF_DAY), TEST_12_30_40_987654321.get(HOUR_OF_DAY));
assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR));
assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE));
}
}
// Returns a {@code LocalTime} with the specified micro-of-day. // This completely replaces the time and is equivalent to using {@link #ofNanoOfDay(long)} // with the micro-of-day multiplied by 1,000.
@Test publicvoid test_with_longTemporalField_microOfDay() { for (long i : testPoints(86_400_000_000L)) {
LocalTime test = TEST_12_30_40_987654321.with(MICRO_OF_DAY, i);
assertEquals(test, LocalTime.ofNanoOfDay(i * 1000));
}
}
// Returns a {@code LocalTime} with the nano-of-second replaced by the specified // milli-of-second multiplied by 1,000,000. // The hour, minute and second will be unchanged.
@Test publicvoid test_with_longTemporalField_milliOfSecond() { for (long i : testPoints(1_000L)) {
LocalTime test = TEST_12_30_40_987654321.with(MILLI_OF_SECOND, i);
assertEquals(test.get(NANO_OF_SECOND), i * 1_000_000);
assertEquals(test.get(HOUR_OF_DAY), TEST_12_30_40_987654321.get(HOUR_OF_DAY));
assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR));
assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE));
}
}
// Returns a {@code LocalTime} with the specified milli-of-day. // This completely replaces the time and is equivalent to using {@link #ofNanoOfDay(long)} // with the milli-of-day multiplied by 1,000,000.
@Test publicvoid test_with_longTemporalField_milliOfDay() { for (long i : testPoints(86_400_000L)) {
LocalTime test = TEST_12_30_40_987654321.with(MILLI_OF_DAY, i);
assertEquals(test, LocalTime.ofNanoOfDay(i * 1_000_000));
}
}
// Returns a {@code LocalTime} with the specified second-of-minute. // The hour, minute and nano-of-second will be unchanged.
@Test publicvoid test_with_longTemporalField_secondOfMinute() { for (long i : testPoints(60L)) {
LocalTime test = TEST_12_30_40_987654321.with(SECOND_OF_MINUTE, i);
assertEquals(test.get(SECOND_OF_MINUTE), i);
assertEquals(test.get(HOUR_OF_DAY), TEST_12_30_40_987654321.get(HOUR_OF_DAY));
assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR));
assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND));
}
}
// Returns a {@code LocalTime} with the specified second-of-day. // The nano-of-second will be unchanged.
@Test publicvoid test_with_longTemporalField_secondOfDay() { for (long i : testPoints(24 * 60 * 60)) {
LocalTime test = TEST_12_30_40_987654321.with(SECOND_OF_DAY, i);
assertEquals(test.get(SECOND_OF_DAY), i);
assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND));
}
}
// Returns a {@code LocalTime} with the specified minute-of-hour. // The hour, second-of-minute and nano-of-second will be unchanged.
@Test publicvoid test_with_longTemporalField_minuteOfHour() { for (long i : testPoints(60)) {
LocalTime test = TEST_12_30_40_987654321.with(MINUTE_OF_HOUR, i);
assertEquals(test.get(MINUTE_OF_HOUR), i);
assertEquals(test.get(HOUR_OF_DAY), TEST_12_30_40_987654321.get(HOUR_OF_DAY));
assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE));
assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND));
}
}
// Returns a {@code LocalTime} with the specified minute-of-day. // The second-of-minute and nano-of-second will be unchanged.
@Test publicvoid test_with_longTemporalField_minuteOfDay() { for (long i : testPoints(24 * 60)) {
LocalTime test = TEST_12_30_40_987654321.with(MINUTE_OF_DAY, i);
assertEquals(test.get(MINUTE_OF_DAY), i);
assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE));
assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND));
}
}
// Returns a {@code LocalTime} with the specified hour-of-am-pm. // The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
@Test publicvoid test_with_longTemporalField_hourOfAmPm() { for (int i = 0; i < 12; i++) {
LocalTime test = TEST_12_30_40_987654321.with(HOUR_OF_AMPM, i);
assertEquals(test.get(HOUR_OF_AMPM), i);
assertEquals(test.get(AMPM_OF_DAY), TEST_12_30_40_987654321.get(AMPM_OF_DAY));
assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR));
assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE));
assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND));
}
}
// Returns a {@code LocalTime} with the specified clock-hour-of-am-pm. // The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
@Test publicvoid test_with_longTemporalField_clockHourOfAmPm() { for (int i = 1; i <= 12; i++) {
LocalTime test = TEST_12_30_40_987654321.with(CLOCK_HOUR_OF_AMPM, i);
assertEquals(test.get(CLOCK_HOUR_OF_AMPM), i);
assertEquals(test.get(AMPM_OF_DAY), TEST_12_30_40_987654321.get(AMPM_OF_DAY));
assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR));
assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE));
assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND));
}
}
// Returns a {@code LocalTime} with the specified hour-of-day. // The minute-of-hour, second-of-minute and nano-of-second will be unchanged.
@Test publicvoid test_with_longTemporalField_hourOfDay() { for (int i = 0; i < 24; i++) {
LocalTime test = TEST_12_30_40_987654321.with(HOUR_OF_DAY, i);
assertEquals(test.get(HOUR_OF_DAY), i);
assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR));
assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE));
assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND));
}
}
// Returns a {@code LocalTime} with the specified clock-hour-of-day. // The minute-of-hour, second-of-minute and nano-of-second will be unchanged.
@Test publicvoid test_with_longTemporalField_clockHourOfDay() { for (int i = 1; i <= 24; i++) {
LocalTime test = TEST_12_30_40_987654321.with(CLOCK_HOUR_OF_DAY, i);
assertEquals(test.get(CLOCK_HOUR_OF_DAY), i);
assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR));
assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE));
assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND));
}
}
// Returns a {@code LocalTime} with the specified AM/PM. // The hour-of-am-pm, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
@Test publicvoid test_with_longTemporalField_amPmOfDay() { for (int i = 0; i <= 1; i++) {
LocalTime test = TEST_12_30_40_987654321.with(AMPM_OF_DAY, i);
assertEquals(test.get(AMPM_OF_DAY), i);
assertEquals(test.get(HOUR_OF_AMPM), TEST_12_30_40_987654321.get(HOUR_OF_AMPM));
assertEquals(test.get(MINUTE_OF_HOUR), TEST_12_30_40_987654321.get(MINUTE_OF_HOUR));
assertEquals(test.get(SECOND_OF_MINUTE), TEST_12_30_40_987654321.get(SECOND_OF_MINUTE));
assertEquals(test.get(NANO_OF_SECOND), TEST_12_30_40_987654321.get(NANO_OF_SECOND));
}
}
// The supported fields behave as follows... // In all cases, if the new value is outside the valid range of values for the field // then a {@code DateTimeException} will be thrown.
@DataProvider(name = "withTemporalField_outOfRange")
Object[][] data_withTemporalField_outOfRange() { returnnew Object[][] {
{NANO_OF_SECOND, time(0, 0, 0, 0), NANO_OF_SECOND.range().getMinimum() - 1},
{NANO_OF_SECOND, time(0, 0, 0, 0), NANO_OF_SECOND.range().getMaximum() + 1},
@Test(dataProvider = "withTemporalField_outOfRange") publicvoid test_with_longTemporalField_invalid(TemporalField field, LocalTime base, long newValue) { try {
base.with(field, newValue);
fail("Field should not be allowed " + field);
} catch (DateTimeException ex) { // expected
}
}
// All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
@Test(expectedExceptions=UnsupportedTemporalTypeException.class) publicvoid test_with_longTemporalField_otherChronoField() {
TEST_12_30_40_987654321.with(ChronoField.DAY_OF_MONTH, 1);
}
// If the field is not a {@code ChronoField}, then the result of this method // is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} // passing {@code this} as the argument.
@Test publicvoid test_with_longTemporalField_notChronoField() { final LocalTime result = LocalTime.of(12, 30); final LocalTime base = LocalTime.of(15, 45);
TemporalField field = new TemporalField() { public ValueRange rangeRefinedBy(TemporalAccessor temporal) { thrownew UnsupportedOperationException();
} public ValueRange range() { returnnull;
} publicboolean isTimeBased() { thrownew UnsupportedOperationException();
} publicboolean isSupportedBy(TemporalAccessor temporal) { thrownew UnsupportedOperationException();
} publicboolean isDateBased() { thrownew UnsupportedOperationException();
} public TemporalUnit getRangeUnit() { thrownew UnsupportedOperationException();
} publiclong getFrom(TemporalAccessor temporal) { thrownew UnsupportedOperationException();
} public TemporalUnit getBaseUnit() { thrownew UnsupportedOperationException();
} public <R extends Temporal> R adjustInto(R temporal, long newValue) {
assertEquals(temporal, base);
assertEquals(newValue, 12L);
@SuppressWarnings("unchecked")
R r = (R) result; return r;
}
};
LocalTime test = base.with(field, 12L);
assertSame(test, result);
}
//----------------------------------------------------------------------- // plusHours() //-----------------------------------------------------------------------
@Test publicvoid test_plusHours_one() {
LocalTime t = LocalTime.MIDNIGHT; for (int i = 0; i < 50; i++) {
t = t.plusHours(1);
assertEquals(t.getHour(), (i + 1) % 24);
}
}
@Test publicvoid test_plusHours_fromZero() {
LocalTime base = LocalTime.MIDNIGHT; for (int i = -50; i < 50; i++) {
LocalTime t = base.plusHours(i);
assertEquals(t.getHour(), (i + 72) % 24);
}
}
@Test publicvoid test_plusHours_fromOne() {
LocalTime base = LocalTime.of(1, 0); for (int i = -50; i < 50; i++) {
LocalTime t = base.plusHours(i);
assertEquals(t.getHour(), (1 + i + 72) % 24);
}
}
@Test publicvoid test_plusHours_big() {
LocalTime t = LocalTime.of(2, 30).plusHours(Long.MAX_VALUE); int hours = (int) (Long.MAX_VALUE % 24L);
assertEquals(t, LocalTime.of(2, 30).plusHours(hours));
}
//----------------------------------------------------------------------- // plusMinutes() //-----------------------------------------------------------------------
@Test publicvoid test_plusMinutes_one() {
LocalTime t = LocalTime.MIDNIGHT; int hour = 0; int min = 0; for (int i = 0; i < 70; i++) {
t = t.plusMinutes(1);
min++; if (min == 60) {
hour++;
min = 0;
}
assertEquals(t.getHour(), hour);
assertEquals(t.getMinute(), min);
}
}
@Test publicvoid test_plusMinutes_fromZero() {
LocalTime base = LocalTime.MIDNIGHT; int hour; int min; for (int i = -70; i < 70; i++) {
LocalTime t = base.plusMinutes(i); if (i < -60) {
hour = 22;
min = i + 120;
} elseif (i < 0) {
hour = 23;
min = i + 60;
} elseif (i >= 60) {
hour = 1;
min = i - 60;
} else {
hour = 0;
min = i;
}
assertEquals(t.getHour(), hour);
--> --------------------
--> maximum size reached
--> --------------------
Messung V0.5
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.