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