timeunittest.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 483 行 · 第 1/2 页
JAVA
483 行
TimeUnit.DAYS.toHours(t));
assertEquals(t,
TimeUnit.HOURS.toHours(t));
assertEquals(t,
TimeUnit.MINUTES.toHours(t*60));
assertEquals(t,
TimeUnit.SECONDS.toHours(t*60*60));
assertEquals(t,
TimeUnit.MILLISECONDS.toHours(t*1000L*60*60));
assertEquals(t,
TimeUnit.MICROSECONDS.toHours(t*1000000L*60*60));
assertEquals(t,
TimeUnit.NANOSECONDS.toHours(t*1000000000L*60*60));
}
}
/**
* toDays correctly converts sample values in different units to
* days
*/
public void testToDays() {
for (long t = 0; t < 88888; ++t) {
assertEquals(t,
TimeUnit.DAYS.toDays(t));
assertEquals(t,
TimeUnit.HOURS.toDays(t*24));
assertEquals(t,
TimeUnit.MINUTES.toDays(t*60*24));
assertEquals(t,
TimeUnit.SECONDS.toDays(t*60*60*24));
assertEquals(t,
TimeUnit.MILLISECONDS.toDays(t*1000L*60*60*24));
assertEquals(t,
TimeUnit.MICROSECONDS.toDays(t*1000000L*60*60*24));
assertEquals(t,
TimeUnit.NANOSECONDS.toDays(t*1000000000L*60*60*24));
}
}
/**
* convert saturates positive too-large values to Long.MAX_VALUE
* and negative to LONG.MIN_VALUE
*/
public void testConvertSaturate() {
assertEquals(Long.MAX_VALUE,
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
TimeUnit.SECONDS));
assertEquals(Long.MIN_VALUE,
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
TimeUnit.SECONDS));
assertEquals(Long.MAX_VALUE,
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
TimeUnit.MINUTES));
assertEquals(Long.MIN_VALUE,
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
TimeUnit.MINUTES));
assertEquals(Long.MAX_VALUE,
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
TimeUnit.HOURS));
assertEquals(Long.MIN_VALUE,
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
TimeUnit.HOURS));
assertEquals(Long.MAX_VALUE,
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
TimeUnit.DAYS));
assertEquals(Long.MIN_VALUE,
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
TimeUnit.DAYS));
}
/**
* toNanos saturates positive too-large values to Long.MAX_VALUE
* and negative to LONG.MIN_VALUE
*/
public void testToNanosSaturate() {
assertEquals(Long.MAX_VALUE,
TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
assertEquals(Long.MIN_VALUE,
TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
}
/**
* toString returns string containing common name of unit
*/
public void testToString() {
String s = TimeUnit.SECONDS.toString();
assertTrue(s.indexOf("ECOND") >= 0);
}
/**
* Timed wait without holding lock throws
* IllegalMonitorStateException
*/
public void testTimedWait_IllegalMonitorException() {
//created a new thread with anonymous runnable
Thread t = new Thread(new Runnable() {
public void run() {
Object o = new Object();
TimeUnit tu = TimeUnit.MILLISECONDS;
try {
tu.timedWait(o,LONG_DELAY_MS);
threadShouldThrow();
}
catch (InterruptedException ie) {
threadUnexpectedException();
}
catch(IllegalMonitorStateException success) {
}
}
});
t.start();
try {
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
} catch(Exception e) {
unexpectedException();
}
}
/**
* timedWait throws InterruptedException when interrupted
*/
public void testTimedWait() {
Thread t = new Thread(new Runnable() {
public void run() {
Object o = new Object();
TimeUnit tu = TimeUnit.MILLISECONDS;
try {
synchronized(o) {
tu.timedWait(o,MEDIUM_DELAY_MS);
}
threadShouldThrow();
}
catch(InterruptedException success) {}
catch(IllegalMonitorStateException failure) {
threadUnexpectedException();
}
}
});
t.start();
try {
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
} catch(Exception e) {
unexpectedException();
}
}
/**
* timedJoin throws InterruptedException when interrupted
*/
public void testTimedJoin() {
Thread t = new Thread(new Runnable() {
public void run() {
TimeUnit tu = TimeUnit.MILLISECONDS;
try {
Thread s = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(MEDIUM_DELAY_MS);
} catch(InterruptedException success){}
}
});
s.start();
tu.timedJoin(s,MEDIUM_DELAY_MS);
threadShouldThrow();
}
catch(Exception e) {}
}
});
t.start();
try {
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
} catch(Exception e) {
unexpectedException();
}
}
/**
* timedSleep throws InterruptedException when interrupted
*/
public void testTimedSleep() {
//created a new thread with anonymous runnable
Thread t = new Thread(new Runnable() {
public void run() {
TimeUnit tu = TimeUnit.MILLISECONDS;
try {
tu.sleep(MEDIUM_DELAY_MS);
threadShouldThrow();
}
catch(InterruptedException success) {}
}
});
t.start();
try {
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
} catch(Exception e) {
unexpectedException();
}
}
/**
* a deserialized serialized unit is equal
*/
public void testSerialization() {
TimeUnit q = TimeUnit.MILLISECONDS;
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
out.writeObject(q);
out.close();
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
TimeUnit r = (TimeUnit)in.readObject();
assertEquals(q.toString(), r.toString());
} catch(Exception e){
e.printStackTrace();
unexpectedException();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?