Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_ffi_c_operators.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2011-2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 class A
18 var value: Int
19
20 init(value: Int) do self.value = value
21
22 fun +(other: A): A import value, A `{
23 int s = A_value( self );
24 int o = A_value( other );
25
26 return new_A( s + o );
27 `}
28
29 fun +: A import value, A `{
30 int s = A_value(self);
31 return new_A(+s);
32 `}
33
34 fun -(other: A): A import value, A `{
35 int s = A_value( self );
36 int o = A_value( other );
37
38 return new_A( s - o );
39 `}
40
41 fun -: A import value, A `{
42 int s = A_value(self);
43 return new_A(-s);
44 `}
45
46 fun *(by: Int): A import value, A `{
47 int s = A_value( self );
48
49 return new_A( s * by );
50 `}
51
52 fun /(by: Int): A import value, A `{
53 int s = A_value( self );
54
55 return new_A( s / by );
56 `}
57
58 redef fun ==(other) import value, nullable Object.as(A) `{
59 if ( nullable_Object_is_a_A( other ) &&
60 A_value( nullable_Object_as_A(other) ) == A_value( self ) )
61 return 1;
62 else
63 return 0;
64 `}
65
66 fun %(other: A): A import value, A `{
67 return new_A( A_value( self ) % A_value( other ) );
68 `}
69
70 fun <=>(other: A): A import value, A `{
71 return new_A( A_value( self )* 1024 );
72 `}
73
74 fun >(other: A): Bool import value `{
75 return A_value( self ) > A_value( other );
76 `}
77
78 fun <(other: A): Bool import value `{
79 return A_value( self ) < A_value( other );
80 `}
81
82 fun >=(other: A): Bool import value `{
83 return A_value( self ) >= A_value( other );
84 `}
85
86 fun <=(other: A): Bool import value `{
87 return A_value( self ) <= A_value( other );
88 `}
89
90 fun >>(other: A): A import value, value=, A `{
91 int new_val = A_value( self ) >> A_value( other );
92 return new_A(new_val);
93 `}
94
95 fun <<(other: A): A import value, A `{
96 int new_val = A_value( self ) << A_value( other );
97 return new_A(new_val);
98 `}
99
100 fun |(other: A): A import value, A `{
101 int new_val = A_value( self ) | A_value( other );
102 return new_A(new_val);
103 `}
104
105 fun ^(other: A): A import value, A `{
106 int new_val = A_value( self ) ^ A_value( other );
107 return new_A(new_val);
108 `}
109
110 fun ~: A import value, A `{
111 int new_val = ~A_value( self );
112 return new_A(new_val);
113 `}
114
115 fun &(other: A): A import value, A `{
116 int new_val = A_value( self ) & A_value( other );
117 return new_A(new_val);
118 `}
119
120 fun [](index: Int): A import A `{
121 return new_A( index );
122 `}
123
124 fun []=(index: Int, value: A): A import A `{
125 return new_A( index + A_value( value ) );
126 `}
127
128 redef fun to_s do return value.to_s
129 end
130
131 print new A(1) + new A(10) # 11
132 print +new A(123)
133 print new A(10) - new A(1) # 9
134 print -new A(123)
135
136 print new A(2) * 11 # 22
137 print new A(33) / 11 # 3
138
139 print new A(44) == null # false
140 print new A(55) == 55 # false
141 print new A(33) == new A(11) # false
142 print new A(22) == new A(77) # false
143 print new A(11) == new A(11) # true
144
145 print new A(147) % new A(12) # 3
146 print new A(4) <=> new A(123) # 4096
147
148 print new A(1) < new A(100) # true
149 print new A(100) < new A(100) # false
150 print new A(100) < new A(1) # false
151
152 print new A(1) > new A(100) # false
153 print new A(100) > new A(100) # false
154 print new A(100) > new A(1) # true
155
156 print new A(1) <= new A(100) # true
157 print new A(100) <= new A(100) # true
158 print new A(100) <= new A(1) # false
159
160 print new A(1) >= new A(100) # false
161 print new A(100) >= new A(100) # true
162 print new A(100) >= new A(1) # true
163
164 print new A(1) << new A(5) # 32
165 print new A(32) >> new A(2) # 8
166
167 print new A(3) | new A(6) # 7
168 print new A(3) ^ new A(6) # 5
169 print new A(3) & new A(6) # 2
170 print ~new A(3) # -4
171
172 var a = new A(456)
173 print a[52] # 52
174
175 a[74] = new A(96)
176 print a # 96