tests: converts 6 tests from the old nitni to FFI
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 20 Jul 2013 15:19:08 +0000 (11:19 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Fri, 2 Aug 2013 17:52:36 +0000 (13:52 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

12 files changed:
tests/sav/test_ffi_c_accessor.sav [new file with mode: 0644]
tests/sav/test_ffi_c_fibonacci.sav [new file with mode: 0644]
tests/sav/test_ffi_c_new.sav [new file with mode: 0644]
tests/sav/test_ffi_c_operators.sav [new file with mode: 0644]
tests/sav/test_ffi_c_primitives.sav [new file with mode: 0644]
tests/sav/test_ffi_c_super.sav [new file with mode: 0644]
tests/test_ffi_c_accessor.nit [new file with mode: 0644]
tests/test_ffi_c_fibonacci.nit [new file with mode: 0644]
tests/test_ffi_c_new.nit [new file with mode: 0644]
tests/test_ffi_c_operators.nit [new file with mode: 0644]
tests/test_ffi_c_primitives.nit [new file with mode: 0644]
tests/test_ffi_c_super.nit [new file with mode: 0644]

diff --git a/tests/sav/test_ffi_c_accessor.sav b/tests/sav/test_ffi_c_accessor.sav
new file mode 100644 (file)
index 0000000..06a4146
--- /dev/null
@@ -0,0 +1,4 @@
+r rw
+r rw set from native
+rw set from native
+set from native
diff --git a/tests/sav/test_ffi_c_fibonacci.sav b/tests/sav/test_ffi_c_fibonacci.sav
new file mode 100644 (file)
index 0000000..310b228
--- /dev/null
@@ -0,0 +1,8 @@
+0
+1
+5
+144
+0
+1
+5
+144
diff --git a/tests/sav/test_ffi_c_new.sav b/tests/sav/test_ffi_c_new.sav
new file mode 100644 (file)
index 0000000..5979542
--- /dev/null
@@ -0,0 +1,7 @@
+a
+a
+allo from 1234
+allo from 2345
+allo from 3456
+allo from 1234
+allo from 1234
diff --git a/tests/sav/test_ffi_c_operators.sav b/tests/sav/test_ffi_c_operators.sav
new file mode 100644 (file)
index 0000000..789be31
--- /dev/null
@@ -0,0 +1,24 @@
+11
+9
+22
+3
+false
+false
+false
+true
+3
+4096
+true
+false
+false
+false
+false
+true
+true
+true
+false
+false
+true
+true
+52
+456
diff --git a/tests/sav/test_ffi_c_primitives.sav b/tests/sav/test_ffi_c_primitives.sav
new file mode 100644 (file)
index 0000000..c68bddc
--- /dev/null
@@ -0,0 +1,6 @@
+false
+true
+k
+2234
+12345.0
+hello world
diff --git a/tests/sav/test_ffi_c_super.sav b/tests/sav/test_ffi_c_super.sav
new file mode 100644 (file)
index 0000000..71acf9f
--- /dev/null
@@ -0,0 +1,2 @@
+A
+B special A
diff --git a/tests/test_ffi_c_accessor.nit b/tests/test_ffi_c_accessor.nit
new file mode 100644 (file)
index 0000000..d33ae3c
--- /dev/null
@@ -0,0 +1,47 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2011-2013 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.
+
+class A
+       var r : String = "r"
+       var w : String = "w"
+       var rw : String = "rw"
+
+       fun print_all import String::to_cstring, r, rw `{
+               printf( "%s %s\n",
+                       String_to_cstring( A_r( recv ) ),
+                       String_to_cstring( A_rw( recv ) ) );
+       `}
+       fun modify import String::from_cstring, w=, rw= `{
+               A_w__assign( recv, new_String_from_cstring( "w set from native" ) );
+               A_rw__assign( recv, new_String_from_cstring( "rw set from native" ) );
+       `}
+end
+
+class B
+       fun print_and_modify( a : A ) import A::rw, A::rw=, String::to_cstring, String::from_cstring `{
+               printf( "%s\n", String_to_cstring( A_rw( a ) ) );
+               A_rw__assign( a, new_String_from_cstring( "set from native" ) );
+               printf( "%s\n", String_to_cstring( A_rw( a ) ) );
+       `}
+end
+
+var a = new A
+a.print_all
+a.modify
+a.print_all
+
+var b = new B
+b.print_and_modify( a )
diff --git a/tests/test_ffi_c_fibonacci.nit b/tests/test_ffi_c_fibonacci.nit
new file mode 100644 (file)
index 0000000..db8c50b
--- /dev/null
@@ -0,0 +1,50 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2011-2013 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.
+
+class FibonacciEngine
+       fun fibonacci( n : Int ) : Int  import fibonacci `{
+               if ( n == 0 )
+                   return 0;
+               if ( n == 1 )
+                   return 1;
+               else
+                   return FibonacciEngine_fibonacci( recv, n-1 ) + FibonacciEngine_fibonacci( recv,  n-2 );
+       `}
+end
+
+redef class Int
+       fun fibonacci : Int import fibonacci `{
+               if ( recv == 0 )
+                   return 0;
+               else if ( recv == 1 )
+                   return 1;
+               else
+                   return Int_fibonacci( recv-1 ) + Int_fibonacci( recv-2 );
+       `}
+end
+
+var engine = new FibonacciEngine
+print engine.fibonacci( 0 )
+print engine.fibonacci( 1 )
+print engine.fibonacci( 5 )
+print engine.fibonacci( 12 )
+# print engine.fibonacci( 123 )
+
+print 0.fibonacci
+print 1.fibonacci
+print 5.fibonacci
+print 12.fibonacci
+# print 123.fibonacci
diff --git a/tests/test_ffi_c_new.nit b/tests/test_ffi_c_new.nit
new file mode 100644 (file)
index 0000000..d18d931
--- /dev/null
@@ -0,0 +1,58 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2011-2013 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.
+
+`{
+int dv = 1234;
+`}
+
+extern A `{int *`}
+       super Pointer
+
+       new import p `{
+               return &dv;
+       `}
+       new with_args( x : Int, a : A ) `{
+        int* v = (int*)malloc( sizeof(int) );
+        (*v) = (int)x;
+           return v;
+       `}
+
+       fun a do print "a"
+       fun p `{
+           printf( "allo from %i\n", *recv );
+       `}
+       fun d : A import d, A `{
+        return new_A();
+       `}
+end
+
+class B
+       init do end
+end
+
+var a = new A
+var b = new A.with_args( 2345, a )
+a.a
+b.a
+
+a.p # 1234
+b.p # 2345
+new A.with_args( 3456, b ).p # 3456
+
+a.d.p #
+b.d.p #
+
+var c = new B
diff --git a/tests/test_ffi_c_operators.nit b/tests/test_ffi_c_operators.nit
new file mode 100644 (file)
index 0000000..4c89e6a
--- /dev/null
@@ -0,0 +1,160 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2011-2013 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.
+
+class A
+       var value : Int
+
+       init ( value : Int ) do self.value = value
+
+       fun +( other : A ) : A import value, A `{
+               int s = A_value( recv );
+               int o = A_value( other );
+
+               return new_A( s + o );
+       `}
+
+       fun -( other : A ) : A import value, A `{
+               int s = A_value( recv );
+               int o = A_value( other );
+
+               return new_A( s - o );
+       `}
+
+       fun *( by : Int ) : A import value, A `{
+               int s = A_value( recv );
+
+               return new_A( s * by );
+       `}
+
+       fun /( by : Int ) : A import value, A `{
+               int s = A_value( recv );
+
+               return new_A( s / by );
+       `}
+
+       redef fun ==( other ) import value, nullable Object as(A) `{
+               if ( nullable_Object_is_a_A( other ) &&
+                        A_value( nullable_Object_as_A(other) ) == A_value( recv ) )
+                       return 1;
+               else
+                       return 0;
+       `}
+
+       fun %( other : A ) : A import value, A `{
+               return new_A( A_value( recv ) % A_value( other ) );
+       `}
+
+#      fun +=( other : A ) : A import value, value=, A `{
+#              int new_val = A_value( recv ) + A_value( other );
+#              A_value__assign( recv, new_val );
+#              return new_A( new_val );
+#      `}
+
+#      fun -=( other : A ) : A import +=, A, value `{
+#              A inv_other = new_A( -1*A_value( other ) );
+#              return A__plus_equal( recv, int_other );
+#      `}
+
+       fun <=>( other : A ) : A import value, A `{
+               return new_A( A_value( recv )* 1024 );
+       `}
+
+#      fun @( other : A ) : A import value, A `{
+#              return new_A( A_value( recv )* 1000 );
+#      `}
+
+       fun >( other : A ) : Bool import value `{
+               return A_value( recv ) > A_value( other );
+       `}
+
+       fun <( other : A ) : Bool import value `{
+               return A_value( recv ) < A_value( other );
+       `}
+
+       fun >=( other : A ) : Bool import value `{
+               return A_value( recv ) >= A_value( other );
+       `}
+
+       fun <=( other : A ) : Bool import value `{
+               return A_value( recv ) <= A_value( other );
+       `}
+
+       fun >>( other : A ) import value, value=, A `{
+               int new_val = A_value( recv ) >> A_value( other );
+               A_value__assign( recv, new_val );
+       `}
+
+       fun <<( other : A ) import value, A `{
+               int new_val = A_value( recv ) << A_value( other );
+               A_value__assign( recv, new_val );
+       `}
+
+       fun []( index : Int ) : A import A `{
+               return new_A( index );
+       `}
+
+       fun []=( index : Int, value : A ) : A import A `{
+               return new_A( index + A_value( value ) );
+       `}
+
+       redef fun to_s do return value.to_s
+end
+
+print new A( 1 ) + new A( 10 ) # 11
+print new A( 10 ) - new A( 1 ) # 9
+
+print new A( 2 ) * 11 # 22
+print new A( 33 ) / 11 # 3
+
+#print new A( 44 ) == null # false
+print new A( 55 ) == 55 # false
+print new A( 33 ) == new A( 11 ) # false
+print new A( 22 ) == new A( 77 ) # false
+print new A( 11 ) == new A( 11 ) # true
+
+print new A( 147 ) % new A( 12 ) # 3
+print new A( 4 ) <=> new A( 123 ) # 4096
+
+print new A( 1 ) < new A( 100 ) # true
+print new A( 100 ) < new A( 100 ) # false
+print new A( 100 ) < new A( 1 ) # false
+
+print new A( 1 ) > new A( 100 ) # false
+print new A( 100 ) > new A( 100 ) # false
+print new A( 100 ) > new A( 1 ) # true
+
+print new A( 1 ) <= new A( 100 ) # true
+print new A( 100 ) <= new A( 100 ) # true
+print new A( 100 ) <= new A( 1 ) # false
+
+print new A( 1 ) >= new A( 100 ) # false
+print new A( 100 ) >= new A( 100 ) # true
+print new A( 100 ) >= new A( 1 ) # true
+
+#var x = new A( 1 )
+#x << new A( 5 )
+#print x # 16
+
+#var y = new A( 32 )
+#y >> new A( 2 )
+#print y # 8
+
+var a = new A( 456 )
+print a[ 52 ] # 52
+
+a[ 74 ] = new A( 96 )
+print a # 96
+
diff --git a/tests/test_ffi_c_primitives.nit b/tests/test_ffi_c_primitives.nit
new file mode 100644 (file)
index 0000000..ffac1cd
--- /dev/null
@@ -0,0 +1,42 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2011-2013 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.
+
+fun opposite( v : Bool ) : Bool `{
+       return v == 0;
+`}
+fun plus_10( v : Char ) : Char `{
+       return v + 10;
+`}
+fun plus_1000( v : Int ) : Int `{
+       return v + 1000;
+`}
+fun multiply_by_100( v : Float ) : Float `{
+       return v * 100;
+`}
+fun print_ns( s : NativeString ) `{
+       printf( "%s\n", s );
+`}
+
+print opposite( true )
+print opposite( false )
+
+print plus_10( 'a' )
+
+print plus_1000( 1234 )
+
+print multiply_by_100( 123.45 )
+
+print_ns( "hello world".to_cstring )
diff --git a/tests/test_ffi_c_super.nit b/tests/test_ffi_c_super.nit
new file mode 100644 (file)
index 0000000..d9672bc
--- /dev/null
@@ -0,0 +1,42 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2011-2013 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.
+
+class A
+       fun id : String do return "A"
+end
+
+class B
+       super A
+
+       redef fun id : String import super, String::from_cstring, String::to_cstring `{
+               char *new_name;
+               char *prefix = "B special ";
+               char *super_name = String_to_cstring( B_id___super( recv ) );
+
+               new_name = calloc( strlen( prefix )+strlen( super_name )+1, sizeof(char) );
+               strcpy( new_name, prefix );
+               strcpy( new_name+strlen( prefix ), super_name );
+               new_name[ strlen( prefix )+strlen( super_name ) ] = '\0';
+
+               return new_String_from_cstring( new_name );
+       `}
+end
+
+var a = new A
+print a.id
+
+var b = new B
+print b.id