From 50a80e6ee35fb0a45c17bf63afbbe450c9783174 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Thu, 31 Jan 2019 23:01:42 +0000 Subject: [PATCH] Add a testcase for testing %native --- Examples/test-suite/common.mk | 1 + .../java/native_directive_runme.java | 22 ++++++++++ Examples/test-suite/native_directive.i | 43 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 Examples/test-suite/java/native_directive_runme.java create mode 100644 Examples/test-suite/native_directive.i diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index 96c9c4b14..d3b617f21 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -314,6 +314,7 @@ CPP_TEST_CASES += \ namespace_virtual_method \ nspace \ nspace_extend \ + native_directive \ naturalvar \ naturalvar_more \ naturalvar_onoff \ diff --git a/Examples/test-suite/java/native_directive_runme.java b/Examples/test-suite/java/native_directive_runme.java new file mode 100644 index 000000000..fc83ba2d0 --- /dev/null +++ b/Examples/test-suite/java/native_directive_runme.java @@ -0,0 +1,22 @@ +import native_directive.*; + +public class native_directive_runme { + + static { + try { + System.loadLibrary("native_directive"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) { + String s = "abc.DEF-123"; + if (native_directive.CountAlphas(s) != 6) + throw new RuntimeException("CountAlphas failed"); + if (native_directive.CountAlphaCharacters(s) != 6) + throw new RuntimeException("CountAlphaCharacters failed"); + } +} + diff --git a/Examples/test-suite/native_directive.i b/Examples/test-suite/native_directive.i new file mode 100644 index 000000000..d08c9a954 --- /dev/null +++ b/Examples/test-suite/native_directive.i @@ -0,0 +1,43 @@ +%module native_directive + +%{ +#include +int alpha_count(const char *instring) { + int count = 0; + const char *s = instring; + while (s && *s) { + if (isalpha((int)*s)) + count++; + s++; + }; + return count; +} +%} + +%inline %{ +int CountAlphas(const char *instring) { + return alpha_count(instring); +} +%} + +// Languages that support %native should code up language specific implementations below + +#if defined(SWIGJAVA) +%native(CountAlphaCharacters) int alpha_count(const char *inputString); +%{ +extern "C" JNIEXPORT jint JNICALL Java_native_1directive_native_1directiveJNI_CountAlphaCharacters(JNIEnv *jenv, jclass jcls, jstring instring) { + jint jresult = 0 ; + (void)jcls; + + if (instring) { + const char *s = (char *)jenv->GetStringUTFChars(instring, 0); + if (s) { + jresult = (jint)alpha_count(s); + jenv->ReleaseStringUTFChars(instring, s); + } + } + return jresult; +} +%} +#endif +