Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
int_s16.cc
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #include <mln/value/int_s16.hh>
27 
28 #define test_operator(T, OP, V1, V2) \
29  \
30 { \
31  T i = V1; \
32  T j = V2; \
33  \
34  i = i OP j; \
35  mln_assertion(i == (V1 OP V2)); \
36  mln_assertion(j == V2); \
37  \
38  i OP##= i; \
39  mln_assertion(i == (((V1 OP V2) OP (V1 OP V2)))); \
40 }
41 
42 #define test_interop(T1, T2, OP, V1, V2) \
43  \
44 { \
45  T1 i = V1; \
46  T2 j = V2; \
47  \
48  i = i OP j; \
49  mln_assertion(i == (V1 OP V2)); \
50  mln_assertion(j == V2); \
51  \
52  i OP##= i; \
53  mln_assertion(i == (((V1 OP V2) OP (V1 OP V2)))); \
54 }
55 
56 
57 // test_operator
58 
59 int main()
60 {
61  using namespace mln;
62  using value::int_s16;
63 
64  int_s16 i = 3, j;
65 
66  // Assignment.
67  {
68  i = 51;
69  mln_assertion(i == 51);
70 
71  i = 51u;
72  mln_assertion(i == 51);
73 
74  signed char c = 51;
75  i = c;
76  mln_assertion(i == 51);
77 
78  j = i;
79  mln_assertion(j == 51);
80 
81  i = 3;
82  mln_assertion(3.0f == i);
83  mln_assertion(i != 2.99f);
84 
85  // FIXME: Is this an incorrect behavior or what?
86  // Error at run-time as expected :-)
87  // i = 256;
88  // i = -1;
89  // i = 255, ++i;
90  }
91 
92 
93  // Comparaison
94  {
95  int_s16 i = 42;
96  int_s16 j = 51;
97 
98  mln_assertion(i < j);
99  mln_assertion(j > i);
100  mln_assertion(i < 12345);
101  mln_assertion(12345 > i);
102 
103  mln_assertion(i != j);
104  mln_assertion(i == 42);
105  mln_assertion(42 == i);
106  mln_assertion(i != 69);
107  mln_assertion(69 != i);
108 
109  }
110 
111  // Addition.
112  {
113  test_operator(int_s16, +, -5, 1);
114  test_interop(int_s16, int, +, 5, -1);
115  test_interop(int_s16, char, +, -4, 2);
116  test_interop(int_s16, unsigned char, +, 4, 2);
117 
118  int_s16 i = 234;
119 
120  i++;
121  mln_assertion(i == 235);
122 
123  ++i;
124  mln_assertion(i == 236);
125 
126  i = +i;
127  mln_assertion(i == 236);
128 
129  }
130 
131  // Soustraction
132  {
133  test_operator(int_s16, -, 100, 5);
134  test_interop(int_s16, int, -, 100, 5);
135  test_interop(int_s16, char, -, 100, 5);
136  test_interop(int_s16, unsigned char, -, 100, 5);
137 
138  int_s16 c = 255;
139  c -= c;
140 
141  mln_assertion(c == 0);
142 
143  int_s16 i = 236;
144 
145  i--;
146  mln_assertion(i == 235);
147 
148  --i;
149  mln_assertion(i == 234);
150 
151  mln_assertion(-i == -234);
152  }
153 
154  // Multiplication
155  {
156  test_operator(int_s16, *, 5, 1);
157  test_interop(int_s16, int, *, 5, 1);
158  test_interop(int_s16, char, *, 4, 2);
159  test_interop(int_s16, unsigned char, *, 4, 2);
160 
161  int_s16 c = 255;
162 
163  c *= 0;
164  mln_assertion(c == 0);
165 
166  i *= 2;
167  int k; k *= i;
168 
169  unsigned char d = 0;
170  i *= d;
171  mln_assertion(i == 0);
172 
173  // FIXME: Is this an incorrect behavior or what?
174  // Error at run-time as expected :-)
175  // i = 128;
176  // i *= 2;
177 
178  }
179 
180  // Division
181  {
182  test_operator(int_s16, /, 5, 1);
183  test_interop(int_s16, int, /, 5, 1);
184  test_interop(int_s16, char, /, 4, 2);
185  test_interop(int_s16, unsigned char, /, 4, 2);
186 
187  int_s16 c = 200;
188 
189  c /= 1;
190  mln_assertion(c == 200);
191  c /= 2;
192  mln_assertion(c == 100);
193 
194  int_s16 d = 2;
195  c /= 2;
196  mln_assertion(c == 50);
197 
198  }
199 
200 
201  // Modulo
202  {
203  test_operator(int_s16, %, 5, 10);
204  test_interop(int_s16, int, %, 5, 10);
205  test_interop(int_s16, char, %, 4, 20);
206  test_interop(int_s16, unsigned char, %, 4, 20);
207  }
208 }