-
-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clean up ffc impl, added initial tests
- Loading branch information
Showing
5 changed files
with
180 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
fxgl-core/src/test/kotlin/com/almasb/fxgl/core/reflect/ForeignFunctionCallerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* FXGL - JavaFX Game Library. The MIT License (MIT). | ||
* Copyright (c) AlmasB ([email protected]). | ||
* See LICENSE for details. | ||
*/ | ||
|
||
package com.almasb.fxgl.core.reflect | ||
|
||
import com.almasb.fxgl.core.util.ResourceExtractor | ||
import org.hamcrest.CoreMatchers | ||
import org.hamcrest.CoreMatchers.* | ||
import org.hamcrest.MatcherAssert | ||
import org.hamcrest.MatcherAssert.* | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable | ||
import org.junit.jupiter.api.condition.EnabledOnOs | ||
import org.junit.jupiter.api.condition.OS | ||
import java.lang.foreign.FunctionDescriptor | ||
import java.lang.foreign.ValueLayout | ||
import java.nio.file.Files | ||
import java.util.concurrent.CountDownLatch | ||
|
||
/** | ||
* @author Almas Baim (https://github.com/AlmasB) | ||
*/ | ||
class ForeignFunctionCallerTest { | ||
|
||
@EnabledOnOs(OS.WINDOWS) | ||
@Test | ||
@EnabledIfEnvironmentVariable(named = "CI", matches = "true") | ||
fun `Downcall a native function`() { | ||
val file = ResourceExtractor.extractNativeLibAsPath("native-lib-test.dll") | ||
|
||
val countDown = CountDownLatch(1) | ||
|
||
val ffc = ForeignFunctionCaller(listOf(file)) | ||
ffc.setOnLoaded { | ||
countDown.countDown() | ||
} | ||
|
||
ffc.load() | ||
|
||
ffc.execute { | ||
val result = it.call( | ||
"testDownCall", | ||
FunctionDescriptor.of( | ||
ValueLayout.JAVA_INT, | ||
ValueLayout.JAVA_INT | ||
), | ||
5 | ||
) as Int | ||
|
||
assertThat(result, `is`(25)) | ||
} | ||
|
||
ffc.unload() | ||
|
||
countDown.await() | ||
|
||
// block some time until the native lib is fully unloaded | ||
Thread.sleep(2500) | ||
|
||
Files.deleteIfExists(file) | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
fxgl-core/src/test/kotlin/com/almasb/fxgl/core/util/ResourceExtractorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* FXGL - JavaFX Game Library. The MIT License (MIT). | ||
* Copyright (c) AlmasB ([email protected]). | ||
* See LICENSE for details. | ||
*/ | ||
|
||
package com.almasb.fxgl.core.util | ||
|
||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.`is` | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
|
||
/** | ||
* @author Almas Baim (https://github.com/AlmasB) | ||
*/ | ||
class ResourceExtractorTest { | ||
|
||
@Test | ||
@EnabledIfEnvironmentVariable(named = "CI", matches = "true") | ||
fun `File is correctly extracted from resources`() { | ||
val testFile = Paths.get(System.getProperty("user.home")) | ||
.resolve(".openjfx") | ||
.resolve("cache") | ||
.resolve("fxgl-21") | ||
.resolve("test_file.txt") | ||
|
||
Files.deleteIfExists(testFile) | ||
|
||
assertTrue(Files.notExists(testFile)) | ||
|
||
val file = Paths.get( | ||
ResourceExtractor.extract(javaClass.getResource("/com/almasb/fxgl/localization/LocalEnglish.properties"), "test_file.txt").toURI() | ||
) | ||
|
||
val s = Files.readString(file) | ||
|
||
Files.deleteIfExists(file) | ||
|
||
assertThat(s, `is`("data.key = Data2")) | ||
} | ||
} |
Binary file added
BIN
+59.5 KB
fxgl-core/src/test/resources/nativeLibs/windows64/native-lib-test.dll
Binary file not shown.