reifydb_value/value/number/safe/convert/
i16.rs1use super::*;
5
6impl_safe_convert_demote!(i16 => i8);
7impl_safe_convert_promote!(i16 => i32, i64, i128);
8
9impl_safe_convert!(i16 => u8, u16, u32, u64, u128);
10
11impl_safe_convert_signed_to_float!(24; i16 => f32);
12impl_safe_convert_signed_to_float!(53; i16 => f64);
13
14impl_safe_convert_to_decimal_from_int!(i16);
15
16#[cfg(test)]
17pub mod tests {
18 use super::SafeConvert;
19
20 mod u8 {
21 use super::*;
22
23 #[test]
24 fn test_checked_convert_happy() {
25 let x: i16 = 42;
26 let y: Option<u8> = x.checked_convert();
27 assert_eq!(y, Some(42u8));
28 }
29
30 #[test]
31 fn test_checked_convert_unhappy() {
32 let x: i16 = -1;
33 let y: Option<u8> = x.checked_convert();
34 assert_eq!(y, None);
35 }
36
37 #[test]
38 fn test_saturating_convert() {
39 let x: i16 = -1;
40 let y: u8 = x.saturating_convert();
41 assert_eq!(y, 0u8);
42 }
43
44 #[test]
45 fn test_wrapping_convert() {
46 let x: i16 = -1;
47 let y: u8 = x.wrapping_convert();
48 assert_eq!(y, 255u8);
49 }
50 }
51
52 mod u16 {
53 use super::*;
54
55 #[test]
56 fn test_checked_convert_happy() {
57 let x: i16 = 42;
58 let y: Option<u16> = x.checked_convert();
59 assert_eq!(y, Some(42u16));
60 }
61
62 #[test]
63 fn test_checked_convert_unhappy() {
64 let x: i16 = -1;
65 let y: Option<u16> = x.checked_convert();
66 assert_eq!(y, None);
67 }
68
69 #[test]
70 fn test_saturating_convert() {
71 let x: i16 = -1;
72 let y: u16 = x.saturating_convert();
73 assert_eq!(y, 0u16);
74 }
75
76 #[test]
77 fn test_wrapping_convert() {
78 let x: i16 = -1;
79 let y: u16 = x.wrapping_convert();
80 assert_eq!(y, 65535u16);
81 }
82 }
83
84 mod u32 {
85 use super::*;
86
87 #[test]
88 fn test_checked_convert_happy() {
89 let x: i16 = 42;
90 let y: Option<u32> = x.checked_convert();
91 assert_eq!(y, Some(42u32));
92 }
93
94 #[test]
95 fn test_checked_convert_unhappy() {
96 let x: i16 = -1;
97 let y: Option<u32> = x.checked_convert();
98 assert_eq!(y, None);
99 }
100
101 #[test]
102 fn test_saturating_convert() {
103 let x: i16 = -1;
104 let y: u32 = x.saturating_convert();
105 assert_eq!(y, 0u32);
106 }
107
108 #[test]
109 fn test_wrapping_convert() {
110 let x: i16 = -1;
111 let y: u32 = x.wrapping_convert();
112 assert_eq!(y, 4294967295u32);
113 }
114 }
115
116 mod u64 {
117 use super::*;
118
119 #[test]
120 fn test_checked_convert_happy() {
121 let x: i16 = 42;
122 let y: Option<u64> = x.checked_convert();
123 assert_eq!(y, Some(42u64));
124 }
125
126 #[test]
127 fn test_checked_convert_unhappy() {
128 let x: i16 = -1;
129 let y: Option<u64> = x.checked_convert();
130 assert_eq!(y, None);
131 }
132
133 #[test]
134 fn test_saturating_convert() {
135 let x: i16 = -1;
136 let y: u64 = x.saturating_convert();
137 assert_eq!(y, 0u64);
138 }
139
140 #[test]
141 fn test_wrapping_convert() {
142 let x: i16 = -1;
143 let y: u64 = x.wrapping_convert();
144 assert_eq!(y, 18446744073709551615u64);
145 }
146 }
147
148 mod u128 {
149 use super::*;
150
151 #[test]
152 fn test_checked_convert_happy() {
153 let x: i16 = 42;
154 let y: Option<u128> = x.checked_convert();
155 assert_eq!(y, Some(42u128));
156 }
157
158 #[test]
159 fn test_checked_convert_unhappy() {
160 let x: i16 = -1;
161 let y: Option<u128> = x.checked_convert();
162 assert_eq!(y, None);
163 }
164
165 #[test]
166 fn test_saturating_convert() {
167 let x: i16 = -1;
168 let y: u128 = x.saturating_convert();
169 assert_eq!(y, 0u128);
170 }
171
172 #[test]
173 fn test_wrapping_convert() {
174 let x: i16 = -1;
175 let y: u128 = x.wrapping_convert();
176 assert_eq!(y, 340282366920938463463374607431768211455u128);
177 }
178 }
179
180 mod f32 {
181 use super::*;
182
183 #[test]
184 fn test_checked_convert() {
185 let x: i16 = 42;
186 let y: Option<f32> = x.checked_convert();
187 assert_eq!(y, Some(42.0f32));
188 }
189
190 #[test]
191 fn test_saturating_convert() {
192 let x: i16 = 100;
193 let y: f32 = x.saturating_convert();
194 assert_eq!(y, 100.0f32);
195 }
196
197 #[test]
198 fn test_wrapping_convert() {
199 let x: i16 = -1;
200 let y: f32 = x.wrapping_convert();
201 assert_eq!(y, -1.0f32);
202 }
203 }
204
205 mod f64 {
206 use super::*;
207
208 #[test]
209 fn test_checked_convert() {
210 let x: i16 = 42;
211 let y: Option<f64> = x.checked_convert();
212 assert_eq!(y, Some(42.0f64));
213 }
214
215 #[test]
216 fn test_saturating_convert() {
217 let x: i16 = 100;
218 let y: f64 = x.saturating_convert();
219 assert_eq!(y, 100.0f64);
220 }
221
222 #[test]
223 fn test_wrapping_convert() {
224 let x: i16 = -1;
225 let y: f64 = x.wrapping_convert();
226 assert_eq!(y, -1.0f64);
227 }
228 }
229
230 mod i8 {
231 use super::*;
232
233 #[test]
234 fn test_checked_convert_happy() {
235 let x: i16 = 127;
236 let y: Option<i8> = x.checked_convert();
237 assert_eq!(y, Some(127i8));
238 }
239
240 #[test]
241 fn test_checked_convert_unhappy() {
242 let x: i16 = 128;
243 let y: Option<i8> = x.checked_convert();
244 assert_eq!(y, None);
245 }
246
247 #[test]
248 fn test_saturating_convert_min() {
249 let x: i16 = -129;
250 let y: i8 = x.saturating_convert();
251 assert_eq!(y, i8::MIN);
252 }
253
254 #[test]
255 fn test_saturating_convert_max() {
256 let x: i16 = 128;
257 let y: i8 = x.saturating_convert();
258 assert_eq!(y, i8::MAX);
259 }
260
261 #[test]
262 fn test_wrapping_convert() {
263 let x: i16 = 128;
264 let y: i8 = x.wrapping_convert();
265 assert_eq!(y, -128);
266 }
267 }
268
269 mod i32 {
270 use super::*;
271
272 #[test]
273 fn test_checked_convert() {
274 let x: i16 = -32768;
275 let y: Option<i32> = x.checked_convert();
276 assert_eq!(y, Some(-32768i32));
277 }
278
279 #[test]
280 fn test_saturating_convert() {
281 let x: i16 = 32767;
282 let y: i32 = x.saturating_convert();
283 assert_eq!(y, 32767i32);
284 }
285
286 #[test]
287 fn test_wrapping_convert() {
288 let x: i16 = -1;
289 let y: i32 = x.wrapping_convert();
290 assert_eq!(y, -1i32);
291 }
292 }
293
294 mod i64 {
295 use super::*;
296
297 #[test]
298 fn test_checked_convert() {
299 let x: i16 = -32768;
300 let y: Option<i64> = x.checked_convert();
301 assert_eq!(y, Some(-32768i64));
302 }
303
304 #[test]
305 fn test_saturating_convert() {
306 let x: i16 = 32767;
307 let y: i64 = x.saturating_convert();
308 assert_eq!(y, 32767i64);
309 }
310
311 #[test]
312 fn test_wrapping_convert() {
313 let x: i16 = -1;
314 let y: i64 = x.wrapping_convert();
315 assert_eq!(y, -1i64);
316 }
317 }
318
319 mod i128 {
320 use super::*;
321
322 #[test]
323 fn test_checked_convert() {
324 let x: i16 = -32768;
325 let y: Option<i128> = x.checked_convert();
326 assert_eq!(y, Some(-32768i128));
327 }
328
329 #[test]
330 fn test_saturating_convert() {
331 let x: i16 = 32767;
332 let y: i128 = x.saturating_convert();
333 assert_eq!(y, 32767i128);
334 }
335
336 #[test]
337 fn test_wrapping_convert() {
338 let x: i16 = -1;
339 let y: i128 = x.wrapping_convert();
340 assert_eq!(y, -1i128);
341 }
342 }
343
344 mod decimal {
345 use super::*;
346 use crate::value::decimal::Decimal;
347
348 #[test]
349 fn test_checked_convert() {
350 let x: i16 = 42;
351 let y: Option<Decimal> = x.checked_convert();
352 assert!(y.is_some());
353 let decimal = y.unwrap();
354 assert_eq!(decimal.to_string(), "42");
355 }
356
357 #[test]
358 fn test_saturating_convert() {
359 let x: i16 = -32768;
360 let y: Decimal = x.saturating_convert();
361 assert_eq!(y.to_string(), "-32768");
362 }
363
364 #[test]
365 fn test_wrapping_convert() {
366 let x: i16 = 32767;
367 let y: Decimal = x.wrapping_convert();
368 assert_eq!(y.to_string(), "32767");
369 }
370 }
371}