/* * Copyright (c) 2009, 2013, 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.
*/
/* @test * @bug 6844313 8011647 * @summary Unit test for java.nio.file.FileTime * @key randomness
*/
// toInstant() int N = 1000; for (TimeUnit unit : EnumSet.allOf(TimeUnit.class)) { for (int i = 0; i < N; i++) { long value = rand.nextLong();
FileTime ft = FileTime.from(value, unit);
Instant instant = ft.toInstant(); if (instant != Instant.MIN && instant != Instant.MAX) {
eqTime(value, unit, instant);
}
}
} for (TimeUnit unit : EnumSet.allOf(TimeUnit.class)) { long value = Long.MIN_VALUE;
FileTime ft = FileTime.from(value, unit);
Instant instant = ft.toInstant(); if (unit.compareTo(TimeUnit.SECONDS) < 0) {
eqTime(value, unit, instant);
} elseif (!instant.equals(Instant.MIN)) { thrownew RuntimeException("should overflow to MIN");
}
value = Long.MAX_VALUE;
ft = FileTime.from(value, unit);
instant = ft.toInstant(); if (unit.compareTo(TimeUnit.SECONDS) < 0) {
eqTime(value, unit, instant);
} elseif (!instant.equals(Instant.MAX)) { thrownew RuntimeException("should overflow to MAX");
}
}
// from(Instant) finallong MAX_SECOND = 31556889864403199L; for (int i = 0; i < N; i++) { long v = rand.nextLong(); long secs = v % MAX_SECOND;
Instant instant = Instant.ofEpochSecond(secs, rand.nextInt(1000_000_000));
FileTime ft = FileTime.from(instant); if (!ft.toInstant().equals(instant) || ft.to(SECONDS) != secs) { thrownew RuntimeException("from(Instant) failed");
} long millis = v;
instant = Instant.ofEpochMilli(millis);
ft = FileTime.from(instant); if (!ft.toInstant().equals(instant) ||
ft.toMillis() != instant.toEpochMilli()) { thrownew RuntimeException("from(Instant) failed");
} long nanos = v;
ft = FileTime.from(nanos, NANOSECONDS);
secs = nanos / 1000_000_000;
nanos = nanos % 1000_000_000;
instant = Instant.ofEpochSecond(secs, nanos); if (!ft.equals(FileTime.from(instant))) { thrownew RuntimeException("from(Instant) failed");
}
}
FileTime time = FileTime.fromMillis(now); if (time.equals(null)) thrownew RuntimeException("should not be equal to null"); try {
time.compareTo(null); thrownew RuntimeException("NullPointerException expected");
} catch (NullPointerException npe) { }
staticvoid overflow(long minmax, long v) { if (v != minmax) thrownew RuntimeException("saturates to Long.MIN/MAX_VALUE expected");
}
staticvoid cmp(long v1, TimeUnit u1, long v2, TimeUnit u2, int expected) { int result = FileTime.from(v1, u1).compareTo(FileTime.from(v2, u2)); if (result != expected) thrownew RuntimeException("unexpected order");
}
staticvoid cmp(Instant ins, long v2, TimeUnit u2, int expected) { int result = FileTime.from(ins).compareTo(FileTime.from(v2, u2)); if (result != expected) thrownew RuntimeException("unexpected order");
}
staticvoid eq(long v1, TimeUnit u1, long v2, TimeUnit u2) {
FileTime t1 = FileTime.from(v1, u1);
FileTime t2 = FileTime.from(v2, u2); if (!t1.equals(t2)) thrownew RuntimeException("not equal"); if (t1.hashCode() != t2.hashCode()) thrownew RuntimeException("hashCodes should be equal");
}
staticvoid eq(Instant ins, long v2, TimeUnit u2) {
FileTime t1 = FileTime.from(ins);
FileTime t2 = FileTime.from(v2, u2); if (!t1.equals(t2)) thrownew RuntimeException("not equal"); if (t1.hashCode() != t2.hashCode()) thrownew RuntimeException("hashCodes should be equal");
}
staticvoid eqTime(long value, TimeUnit unit, Instant instant) { long secs = SECONDS.convert(value, unit); long nanos = NANOSECONDS.convert(value - unit.convert(secs, SECONDS), unit); if (nanos < 0) { // normalize nanoOfSecond to positive
secs -= 1;
nanos += 1000_000_000;
} if (secs != instant.getEpochSecond() || (int)nanos != instant.getNano()) {
System.err.println(" ins=" + instant); thrownew RuntimeException("ft and instant are not the same time point");
}
}
staticvoid neq(long v1, TimeUnit u1, long v2, TimeUnit u2) {
FileTime t1 = FileTime.from(v1, u1);
FileTime t2 = FileTime.from(v2, u2); if (t1.equals(t2)) thrownew RuntimeException("should not be equal");
}
staticvoid neq(Instant ins, long v2, TimeUnit u2) {
FileTime t1 = FileTime.from(ins);
FileTime t2 = FileTime.from(v2, u2); if (t1.equals(t2)) thrownew RuntimeException("should not be equal");
}
staticvoid to(long v, TimeUnit unit) {
FileTime t = FileTime.from(v, unit); for (TimeUnit u: TimeUnit.values()) { long result = t.to(u); long expected = u.convert(v, unit); if (result != expected) { thrownew RuntimeException("unexpected result");
}
}
}
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.