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_int!(i16);
15
16impl SafeConvert<Uint> for i16 {
17 fn checked_convert(self) -> Option<Uint> {
18 if self >= 0 {
19 Some(Uint(BigInt::from(self)))
20 } else {
21 None
22 }
23 }
24
25 fn saturating_convert(self) -> Uint {
26 if self >= 0 {
27 Uint(BigInt::from(self))
28 } else {
29 Uint::zero()
30 }
31 }
32
33 fn wrapping_convert(self) -> Uint {
34 Uint(BigInt::from(self as u16))
35 }
36}
37
38impl_safe_convert_to_decimal_from_int!(i16);
39
40#[cfg(test)]
41pub mod tests {
42 use super::SafeConvert;
43
44 mod u8 {
45 use super::*;
46
47 #[test]
48 fn test_checked_convert_happy() {
49 let x: i16 = 42;
50 let y: Option<u8> = x.checked_convert();
51 assert_eq!(y, Some(42u8));
52 }
53
54 #[test]
55 fn test_checked_convert_unhappy() {
56 let x: i16 = -1;
57 let y: Option<u8> = x.checked_convert();
58 assert_eq!(y, None);
59 }
60
61 #[test]
62 fn test_saturating_convert() {
63 let x: i16 = -1;
64 let y: u8 = x.saturating_convert();
65 assert_eq!(y, 0u8);
66 }
67
68 #[test]
69 fn test_wrapping_convert() {
70 let x: i16 = -1;
71 let y: u8 = x.wrapping_convert();
72 assert_eq!(y, 255u8);
73 }
74 }
75
76 mod u16 {
77 use super::*;
78
79 #[test]
80 fn test_checked_convert_happy() {
81 let x: i16 = 42;
82 let y: Option<u16> = x.checked_convert();
83 assert_eq!(y, Some(42u16));
84 }
85
86 #[test]
87 fn test_checked_convert_unhappy() {
88 let x: i16 = -1;
89 let y: Option<u16> = x.checked_convert();
90 assert_eq!(y, None);
91 }
92
93 #[test]
94 fn test_saturating_convert() {
95 let x: i16 = -1;
96 let y: u16 = x.saturating_convert();
97 assert_eq!(y, 0u16);
98 }
99
100 #[test]
101 fn test_wrapping_convert() {
102 let x: i16 = -1;
103 let y: u16 = x.wrapping_convert();
104 assert_eq!(y, 65535u16);
105 }
106 }
107
108 mod u32 {
109 use super::*;
110
111 #[test]
112 fn test_checked_convert_happy() {
113 let x: i16 = 42;
114 let y: Option<u32> = x.checked_convert();
115 assert_eq!(y, Some(42u32));
116 }
117
118 #[test]
119 fn test_checked_convert_unhappy() {
120 let x: i16 = -1;
121 let y: Option<u32> = x.checked_convert();
122 assert_eq!(y, None);
123 }
124
125 #[test]
126 fn test_saturating_convert() {
127 let x: i16 = -1;
128 let y: u32 = x.saturating_convert();
129 assert_eq!(y, 0u32);
130 }
131
132 #[test]
133 fn test_wrapping_convert() {
134 let x: i16 = -1;
135 let y: u32 = x.wrapping_convert();
136 assert_eq!(y, 4294967295u32);
137 }
138 }
139
140 mod u64 {
141 use super::*;
142
143 #[test]
144 fn test_checked_convert_happy() {
145 let x: i16 = 42;
146 let y: Option<u64> = x.checked_convert();
147 assert_eq!(y, Some(42u64));
148 }
149
150 #[test]
151 fn test_checked_convert_unhappy() {
152 let x: i16 = -1;
153 let y: Option<u64> = x.checked_convert();
154 assert_eq!(y, None);
155 }
156
157 #[test]
158 fn test_saturating_convert() {
159 let x: i16 = -1;
160 let y: u64 = x.saturating_convert();
161 assert_eq!(y, 0u64);
162 }
163
164 #[test]
165 fn test_wrapping_convert() {
166 let x: i16 = -1;
167 let y: u64 = x.wrapping_convert();
168 assert_eq!(y, 18446744073709551615u64);
169 }
170 }
171
172 mod u128 {
173 use super::*;
174
175 #[test]
176 fn test_checked_convert_happy() {
177 let x: i16 = 42;
178 let y: Option<u128> = x.checked_convert();
179 assert_eq!(y, Some(42u128));
180 }
181
182 #[test]
183 fn test_checked_convert_unhappy() {
184 let x: i16 = -1;
185 let y: Option<u128> = x.checked_convert();
186 assert_eq!(y, None);
187 }
188
189 #[test]
190 fn test_saturating_convert() {
191 let x: i16 = -1;
192 let y: u128 = x.saturating_convert();
193 assert_eq!(y, 0u128);
194 }
195
196 #[test]
197 fn test_wrapping_convert() {
198 let x: i16 = -1;
199 let y: u128 = x.wrapping_convert();
200 assert_eq!(y, 340282366920938463463374607431768211455u128);
201 }
202 }
203
204 mod f32 {
205 use super::*;
206
207 #[test]
208 fn test_checked_convert() {
209 let x: i16 = 42;
210 let y: Option<f32> = x.checked_convert();
211 assert_eq!(y, Some(42.0f32));
212 }
213
214 #[test]
215 fn test_saturating_convert() {
216 let x: i16 = 100;
217 let y: f32 = x.saturating_convert();
218 assert_eq!(y, 100.0f32);
219 }
220
221 #[test]
222 fn test_wrapping_convert() {
223 let x: i16 = -1;
224 let y: f32 = x.wrapping_convert();
225 assert_eq!(y, -1.0f32);
226 }
227 }
228
229 mod f64 {
230 use super::*;
231
232 #[test]
233 fn test_checked_convert() {
234 let x: i16 = 42;
235 let y: Option<f64> = x.checked_convert();
236 assert_eq!(y, Some(42.0f64));
237 }
238
239 #[test]
240 fn test_saturating_convert() {
241 let x: i16 = 100;
242 let y: f64 = x.saturating_convert();
243 assert_eq!(y, 100.0f64);
244 }
245
246 #[test]
247 fn test_wrapping_convert() {
248 let x: i16 = -1;
249 let y: f64 = x.wrapping_convert();
250 assert_eq!(y, -1.0f64);
251 }
252 }
253
254 mod i8 {
255 use super::*;
256
257 #[test]
258 fn test_checked_convert_happy() {
259 let x: i16 = 127;
260 let y: Option<i8> = x.checked_convert();
261 assert_eq!(y, Some(127i8));
262 }
263
264 #[test]
265 fn test_checked_convert_unhappy() {
266 let x: i16 = 128;
267 let y: Option<i8> = x.checked_convert();
268 assert_eq!(y, None);
269 }
270
271 #[test]
272 fn test_saturating_convert_min() {
273 let x: i16 = -129;
274 let y: i8 = x.saturating_convert();
275 assert_eq!(y, i8::MIN);
276 }
277
278 #[test]
279 fn test_saturating_convert_max() {
280 let x: i16 = 128;
281 let y: i8 = x.saturating_convert();
282 assert_eq!(y, i8::MAX);
283 }
284
285 #[test]
286 fn test_wrapping_convert() {
287 let x: i16 = 128;
288 let y: i8 = x.wrapping_convert();
289 assert_eq!(y, -128);
290 }
291 }
292
293 mod i32 {
294 use super::*;
295
296 #[test]
297 fn test_checked_convert() {
298 let x: i16 = -32768;
299 let y: Option<i32> = x.checked_convert();
300 assert_eq!(y, Some(-32768i32));
301 }
302
303 #[test]
304 fn test_saturating_convert() {
305 let x: i16 = 32767;
306 let y: i32 = x.saturating_convert();
307 assert_eq!(y, 32767i32);
308 }
309
310 #[test]
311 fn test_wrapping_convert() {
312 let x: i16 = -1;
313 let y: i32 = x.wrapping_convert();
314 assert_eq!(y, -1i32);
315 }
316 }
317
318 mod i64 {
319 use super::*;
320
321 #[test]
322 fn test_checked_convert() {
323 let x: i16 = -32768;
324 let y: Option<i64> = x.checked_convert();
325 assert_eq!(y, Some(-32768i64));
326 }
327
328 #[test]
329 fn test_saturating_convert() {
330 let x: i16 = 32767;
331 let y: i64 = x.saturating_convert();
332 assert_eq!(y, 32767i64);
333 }
334
335 #[test]
336 fn test_wrapping_convert() {
337 let x: i16 = -1;
338 let y: i64 = x.wrapping_convert();
339 assert_eq!(y, -1i64);
340 }
341 }
342
343 mod i128 {
344 use super::*;
345
346 #[test]
347 fn test_checked_convert() {
348 let x: i16 = -32768;
349 let y: Option<i128> = x.checked_convert();
350 assert_eq!(y, Some(-32768i128));
351 }
352
353 #[test]
354 fn test_saturating_convert() {
355 let x: i16 = 32767;
356 let y: i128 = x.saturating_convert();
357 assert_eq!(y, 32767i128);
358 }
359
360 #[test]
361 fn test_wrapping_convert() {
362 let x: i16 = -1;
363 let y: i128 = x.wrapping_convert();
364 assert_eq!(y, -1i128);
365 }
366 }
367
368 mod decimal {
369 use super::*;
370 use crate::value::decimal::Decimal;
371
372 #[test]
373 fn test_checked_convert() {
374 let x: i16 = 42;
375 let y: Option<Decimal> = x.checked_convert();
376 assert!(y.is_some());
377 let decimal = y.unwrap();
378 assert_eq!(decimal.to_string(), "42");
379 }
380
381 #[test]
382 fn test_saturating_convert() {
383 let x: i16 = -32768;
384 let y: Decimal = x.saturating_convert();
385 assert_eq!(y.to_string(), "-32768");
386 }
387
388 #[test]
389 fn test_wrapping_convert() {
390 let x: i16 = 32767;
391 let y: Decimal = x.wrapping_convert();
392 assert_eq!(y.to_string(), "32767");
393 }
394 }
395
396 mod int {
397 use super::*;
398 use crate::value::int::Int;
399
400 #[test]
401 fn test_checked_convert() {
402 let x: i16 = -32768;
403 let y: Option<Int> = x.checked_convert();
404 assert!(y.is_some());
405 assert_eq!(y.unwrap().to_string(), "-32768");
406 }
407
408 #[test]
409 fn test_saturating_convert() {
410 let x: i16 = 32767;
411 let y: Int = x.saturating_convert();
412 assert_eq!(y.to_string(), "32767");
413 }
414
415 #[test]
416 fn test_wrapping_convert() {
417 let x: i16 = -1;
418 let y: Int = x.wrapping_convert();
419 assert_eq!(y.to_string(), "-1");
420 }
421 }
422
423 mod uint {
424 use super::*;
425 use crate::value::uint::Uint;
426
427 #[test]
428 fn test_checked_convert_positive() {
429 let x: i16 = 42;
430 let y: Option<Uint> = x.checked_convert();
431 assert!(y.is_some());
432 assert_eq!(y.unwrap().to_string(), "42");
433 }
434
435 #[test]
436 fn test_checked_convert_negative() {
437 let x: i16 = -1;
438 let y: Option<Uint> = x.checked_convert();
439 assert!(y.is_none());
440 }
441
442 #[test]
443 fn test_saturating_convert() {
444 let x: i16 = -1;
445 let y: Uint = x.saturating_convert();
446 assert_eq!(y.to_string(), "0");
447 }
448
449 #[test]
450 fn test_wrapping_convert() {
451 let x: i16 = -1;
452 let y: Uint = x.wrapping_convert();
453 assert_eq!(y.to_string(), "65535");
454 }
455 }
456}