From a85d30215d37228ade02b28d5a50194ce35738c3 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Mon, 13 Feb 2017 14:51:15 +0700 Subject: [PATCH] backend/tests: add interop3 (uses callback) --- backend.native/tests/build.gradle | 6 +++++ backend.native/tests/interop/basics/3.kt | 29 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 backend.native/tests/interop/basics/3.kt diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 7f33d7778f2..81d806407ee 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -1359,6 +1359,12 @@ task interop2(type: RunInteropKonanTest) { interop = 'cstdio' } +task interop3(type: RunInteropKonanTest) { + goldValue = "8 9 12 13 14 \n" + source = "interop/basics/3.kt" + interop = 'cstdlib' +} + task interop_echo_server(type: RunInteropKonanTest) { if (isLinux()) { disabled = true diff --git a/backend.native/tests/interop/basics/3.kt b/backend.native/tests/interop/basics/3.kt new file mode 100644 index 00000000000..9840c8ad9bb --- /dev/null +++ b/backend.native/tests/interop/basics/3.kt @@ -0,0 +1,29 @@ +import kotlinx.cinterop.* + +fun main(args: Array) { + memScoped { + val count = 5 + + val values = allocArray(count) + values[0].value = 14 + values[1].value = 12 + values[2].value = 9 + values[3].value = 13 + values[4].value = 8 + + cstdlib.qsort(values[0].ptr, count.toLong(), CInt32Var.size, staticCFunction(::comparator)) + + for (i in 0 .. count - 1) { + print(values[i].value) + print(" ") + } + println() + } +} + +private fun comparator(a: COpaquePointer?, b: COpaquePointer?): Int { + val aValue = a!!.reinterpret().pointed.value + val bValue = b!!.reinterpret().pointed.value + + return (aValue - bValue) +} \ No newline at end of file