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