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