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