< prev index next >
test/jdk/java/foreign/StdLibTest.java
Print this page
*** 34,47 ****
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.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
--- 34,48 ----
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.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,77 ****
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);
}
@Test(dataProvider = "strings")
void test_strlen(String s) {
assertEquals(stdLibHelper.strlen(s), s.length());
--- 68,78 ----
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);
}
@Test(dataProvider = "strings")
void test_strlen(String s) {
assertEquals(stdLibHelper.strlen(s), s.length());
*** 83,105 ****
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());
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()
.isDaylightSavings(Instant.ofEpochMilli(time.seconds() * 1000)));
}
}
@Test(dataProvider = "ints")
--- 84,106 ----
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.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(), ZoneOffset.UTC.getRules()
.isDaylightSavings(Instant.ofEpochMilli(time.seconds() * 1000)));
}
}
@Test(dataProvider = "ints")
*** 180,191 ****
try (Scope s = Scope.newNativeScope()) {
return stdLib.strlen(s.allocateCString(msg));
}
}
! Pointer<Tm> localtime(Pointer<Time> arg) {
! return stdLib.localtime(arg);
}
int[] qsort(int[] array) {
try (Scope s = Scope.newNativeScope()) {
//allocate the array
--- 181,192 ----
try (Scope s = Scope.newNativeScope()) {
return stdLib.strlen(s.allocateCString(msg));
}
}
! Pointer<Tm> gmtime(Pointer<Time> arg) {
! return stdLib.gmtime(arg);
}
int[] qsort(int[] array) {
try (Scope s = Scope.newNativeScope()) {
//allocate the array
*** 223,244 ****
"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)" +
"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);
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);
--- 224,245 ----
"puts=(u64:u8)i32" +
"strcat=(u64:u8u64:i8)u64:u8" +
"strcmp=(u64:u8u64:i8)i32" +
"strlen=(u64:u8)i32" +
"time=(u64:$(Time))$(Time)" +
! "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> 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,324 ****
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();
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)};
}
return instants;
}
@DataProvider
--- 310,325 ----
return stringPairs;
}
@DataProvider
public static Object[][] instants() {
! 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++) {
! Instant instant = start.plusSeconds((long)(Math.random() * (end.getEpochSecond() - start.getEpochSecond())));
! instants[i] = new Object[] { instant };
}
return instants;
}
@DataProvider
< prev index next >