< prev index next >
test/jdk/java/foreign/StdLibTest.java
Print this page
@@ -34,14 +34,15 @@
import java.foreign.NativeTypes;
import java.foreign.Scope;
import java.foreign.annotations.NativeCallback;
import java.foreign.annotations.NativeHeader;
import java.foreign.annotations.NativeStruct;
-import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
@@ -67,11 +68,11 @@
assertEquals(Math.signum(stdLibHelper.strcmp(s1, s2)), Math.signum(s1.compareTo(s2)));
}
@Test(dataProvider = "strings")
void test_puts(String s) {
- assertTrue(stdLibHelper.puts(s) > 0);
+ assertTrue(stdLibHelper.puts(s) >= 0);
}
@Test(dataProvider = "strings")
void test_strlen(String s) {
assertEquals(stdLibHelper.strlen(s), s.length());
@@ -83,23 +84,23 @@
StdLibHelper.Time time = s.allocateStruct(StdLibHelper.Time.class);
time.setSeconds(instant.getEpochSecond());
//numbers should be in the same ballpark
assertEquals(time.seconds(), instant.getEpochSecond());
@SuppressWarnings("unchecked")
- StdLibHelper.Tm tm = stdLibHelper.localtime(time.ptr()).get();
- LocalDateTime localTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
+ StdLibHelper.Tm tm = stdLibHelper.gmtime(time.ptr()).get();
+ LocalDateTime localTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
assertEquals(tm.sec(), localTime.getSecond());
assertEquals(tm.min(), localTime.getMinute());
assertEquals(tm.hour(), localTime.getHour());
//day pf year in Java has 1-offset
assertEquals(tm.yday(), localTime.getDayOfYear() - 1);
assertEquals(tm.mday(), localTime.getDayOfMonth());
//days of week starts from Sunday in C, but on Monday in Java, also account for 1-offset
assertEquals((tm.wday() + 6) % 7, localTime.getDayOfWeek().getValue() - 1);
//month in Java has 1-offset
assertEquals(tm.mon(), localTime.getMonth().getValue() - 1);
- assertEquals(tm.isdst(), ZoneId.systemDefault().getRules()
+ assertEquals(tm.isdst(), ZoneOffset.UTC.getRules()
.isDaylightSavings(Instant.ofEpochMilli(time.seconds() * 1000)));
}
}
@Test(dataProvider = "ints")
@@ -180,12 +181,12 @@
try (Scope s = Scope.newNativeScope()) {
return stdLib.strlen(s.allocateCString(msg));
}
}
- Pointer<Tm> localtime(Pointer<Time> arg) {
- return stdLib.localtime(arg);
+ Pointer<Tm> gmtime(Pointer<Time> arg) {
+ return stdLib.gmtime(arg);
}
int[] qsort(int[] array) {
try (Scope s = Scope.newNativeScope()) {
//allocate the array
@@ -223,22 +224,22 @@
"puts=(u64:u8)i32" +
"strcat=(u64:u8u64:i8)u64:u8" +
"strcmp=(u64:u8u64:i8)i32" +
"strlen=(u64:u8)i32" +
"time=(u64:$(Time))$(Time)" +
- "localtime=(u64:$(Time))u64:$(Tm)" +
+ "gmtime=(u64:$(Time))u64:$(Tm)" +
"qsort=(u64:[0i32]i32i32u64:(u64:i32u64:i32)i32)v" +
"rand=()i32" +
"printf=(u64:u8*)i32" +
"fopen=(u64:u8u64:i8)u64:v")
public interface StdLib {
int puts(Pointer<Byte> str);
Pointer<Byte> strcat(Pointer<Byte> s1, Pointer<Byte> s2);
int strcmp(Pointer<Byte> s1, Pointer<Byte> s2);
int strlen(Pointer<Byte> s2);
Time time(Pointer<Time> arg);
- Pointer<Tm> localtime(Pointer<Time> arg);
+ Pointer<Tm> gmtime(Pointer<Time> arg);
void qsort(Pointer<Integer> base, int nitems, int size, Callback<QsortComparator> comparator);
int rand();
int printf(Pointer<Byte> format, Object... args);
Pointer<Void> fopen(Pointer<Byte> filename, Pointer<Byte> mode);
@@ -309,16 +310,16 @@
return stringPairs;
}
@DataProvider
public static Object[][] instants() {
- long start = Timestamp.valueOf("2017-01-01 00:00:00").getTime();
- long end = Timestamp.valueOf("2017-12-31 00:00:00").getTime();
+ Instant start = ZonedDateTime.of(LocalDateTime.parse("2017-01-01T00:00:00"), ZoneOffset.UTC).toInstant();
+ Instant end = ZonedDateTime.of(LocalDateTime.parse("2017-12-31T00:00:00"), ZoneOffset.UTC).toInstant();
Object[][] instants = new Object[100][];
for (int i = 0 ; i < instants.length ; i++) {
- long instant = start + (long)(Math.random() * (end - start));
- instants[i] = new Object[] { Instant.ofEpochSecond(instant)};
+ Instant instant = start.plusSeconds((long)(Math.random() * (end.getEpochSecond() - start.getEpochSecond())));
+ instants[i] = new Object[] { instant };
}
return instants;
}
@DataProvider
< prev index next >