tests: adds tests for the FFI with C
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 5 Feb 2013 14:13:00 +0000 (09:13 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 7 Feb 2013 00:22:18 +0000 (19:22 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

12 files changed:
tests/nitg-e.skip
tests/nitg-s.skip
tests/nitg.skip
tests/niti.skip
tests/sav/test_ffi_c_more.sav [new file with mode: 0644]
tests/sav/test_ffi_c_simple.sav [new file with mode: 0644]
tests/sav/test_ffi_c_types.sav [new file with mode: 0644]
tests/sav/test_ffi_c_types_import.sav [new file with mode: 0644]
tests/test_ffi_c_more.nit [new file with mode: 0644]
tests/test_ffi_c_simple.nit [new file with mode: 0644]
tests/test_ffi_c_types.nit [new file with mode: 0644]
tests/test_ffi_c_types_import.nit [new file with mode: 0644]

index 45e0616..41116ef 100644 (file)
@@ -4,6 +4,7 @@ init_linext
 inline
 test_extern
 test_ni_
+test_ffi_
 nitc
 nitdoc
 nits
index 45e0616..41116ef 100644 (file)
@@ -4,6 +4,7 @@ init_linext
 inline
 test_extern
 test_ni_
+test_ffi_
 nitc
 nitdoc
 nits
index 45e0616..41116ef 100644 (file)
@@ -4,6 +4,7 @@ init_linext
 inline
 test_extern
 test_ni_
+test_ffi_
 nitc
 nitdoc
 nits
index 15d9342..3a2705b 100644 (file)
@@ -3,6 +3,7 @@ init_inherit
 init_linext
 inline
 test_ni_
+test_ffi_
 test_math
 test_mem
 shoot_logic
diff --git a/tests/sav/test_ffi_c_more.sav b/tests/sav/test_ffi_c_more.sav
new file mode 100644 (file)
index 0000000..73e2c80
--- /dev/null
@@ -0,0 +1,4 @@
+10
+allo
+hello
+15
diff --git a/tests/sav/test_ffi_c_simple.sav b/tests/sav/test_ffi_c_simple.sav
new file mode 100644 (file)
index 0000000..b4c9da7
--- /dev/null
@@ -0,0 +1,4 @@
+5.123449
+C implementation!
+in C!
+42
diff --git a/tests/sav/test_ffi_c_types.sav b/tests/sav/test_ffi_c_types.sav
new file mode 100644 (file)
index 0000000..fb9f7e6
--- /dev/null
@@ -0,0 +1,4 @@
+A< 1 2 >
+B< A< 3 4 >
+   5 6 >
+C< 11 22 >
diff --git a/tests/sav/test_ffi_c_types_import.sav b/tests/sav/test_ffi_c_types_import.sav
new file mode 100644 (file)
index 0000000..6dc5f92
--- /dev/null
@@ -0,0 +1,2 @@
+3 5
+A< 3 4 >
diff --git a/tests/test_ffi_c_more.nit b/tests/test_ffi_c_more.nit
new file mode 100644 (file)
index 0000000..51d2a6b
--- /dev/null
@@ -0,0 +1,84 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2011 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+in "C header" `{
+       #include <stdio.h>
+`}
+
+in "C" `{
+       void f( char *str ) {
+               printf( "%s\n", str );
+       }
+`}
+
+`{
+       void g() {
+               printf( "from C\n" );
+       }
+`}
+
+extern A
+       new is extern `{ return malloc(1); `}
+       new new_implicit `{ return malloc(1); `}
+       new new_in_language is extern in "C" `{ return malloc(1); `}
+       new new_in_language_implicit in "C" `{ return malloc(1); `}
+
+       fun m : Int is extern `{ return 10; `}
+
+       fun n : String is extern import String::from_cstring `{
+               return new_String_from_cstring( "allo" );
+       `}
+
+       fun o ( str : String ) is extern import String::to_cstring `{
+               f( String_to_cstring( str ) );
+       `}
+
+       fun p : Int import m `{
+               return A_m( recv ) + 5;
+       `}
+
+       fun in_language : Int is extern in "C" `{
+               return  4;
+       `}
+       fun in_language_implicit : Int in "C" `{
+               return  4;
+       `}
+       fun simple_implicit `{
+               g();
+       `}
+       fun inline_implicit : Int `{ return  7; `}
+end
+
+extern B
+special A
+end
+
+extern C `{int*`}
+end
+
+extern D
+special C
+end
+
+extern E
+special C
+end
+
+var a = new A
+print a.m
+print a.n
+a.o( "hello" )
+print a.p
diff --git a/tests/test_ffi_c_simple.nit b/tests/test_ffi_c_simple.nit
new file mode 100644 (file)
index 0000000..b860ddb
--- /dev/null
@@ -0,0 +1,29 @@
+module test_ffi_c_simple
+
+in "C header" `{
+       #include <stdio.h>
+`}
+
+in "C body" `{
+       int f( void ) {
+               printf( "in C!\n" );
+               return 42;
+       }
+`}
+
+class A
+       fun foo : Int in "C" `{
+               printf( "C implementation!\n" );
+               return f();
+       `}
+end
+
+fun bar : Float is extern `{
+       return 5.12345;
+`}
+
+print bar
+
+var a = new A
+print a.foo
+
diff --git a/tests/test_ffi_c_types.nit b/tests/test_ffi_c_types.nit
new file mode 100644 (file)
index 0000000..9391d7c
--- /dev/null
@@ -0,0 +1,56 @@
+in "C header" `{
+       struct s_a{
+               int x, y;
+               };
+       struct s_b{
+               struct s_a super;
+               int w, h;
+               };
+`}
+
+extern A in "C" `{struct s_a*`}
+       new `{
+               struct s_a* v = malloc(sizeof(struct s_a));
+               v->x = 1;
+               v->y = 2;
+               return v;
+       `}
+       fun p `{
+               printf( "A< %d %d >\n", recv->x, recv->y );
+       `}
+end
+
+extern B in "C" `{struct s_b*`}
+       super A
+       new `{
+               struct s_b* v = malloc(sizeof(struct s_b));
+               v->super.x = 3;
+               v->super.y = 4;
+               v->w = 5;
+               v->h = 6;
+               return v;
+       `}
+       redef fun p import super `{
+               printf( "B< " );
+               B_p___super(recv);
+               printf( "   %d %d >\n", recv->w, recv->h );
+       `}
+end
+
+extern C
+       super A
+       new `{
+               struct s_a* v = malloc(sizeof(struct s_a));
+               v->x = 11;
+               v->y = 22;
+               return v;
+       `}
+       redef fun p `{
+               printf( "C< %d %d >\n", recv->x, recv->y );
+       `}
+end
+
+(new A).p
+(new B).p
+(new C).p
+
diff --git a/tests/test_ffi_c_types_import.nit b/tests/test_ffi_c_types_import.nit
new file mode 100644 (file)
index 0000000..40f91e6
--- /dev/null
@@ -0,0 +1,8 @@
+import test_ffi_c_types
+
+fun foo( b : B ) : A `{
+       printf( "%d %d\n", b->super.x, b->w );
+       return (struct s_a*)b;
+`}
+
+foo( new B ).p