Debian Codegraph @

tests/test_arbitrary_precision.rs

raw symbols SCIP
1 #![cfg(feature = "arbitrary_precision")]
2
3 use pyo3::prelude::*;
4 use pythonize::{depythonize, pythonize};
5 use serde_json::Value;
6
7 #[test]
8 fn test_greater_than_u64_max() {
9 Python::attach(|py| {
10 let json_str = r#"18446744073709551616"#;
11 let value: Value = serde_json::from_str(json_str).unwrap();
12 let result = pythonize(py, &value).unwrap();
13 let number_str = result.str().unwrap().to_string();
14
15 assert!(result.is_instance_of::<pyo3::types::PyInt>());
16 assert_eq!(number_str, "18446744073709551616");
17 });
18 }
19
20 #[test]
21 fn test_less_than_i64_min() {
22 Python::attach(|py| {
23 let json_str = r#"-9223372036854775809"#;
24 let value: Value = serde_json::from_str(json_str).unwrap();
25 let result = pythonize(py, &value).unwrap();
26 let number_str = result.str().unwrap().to_string();
27
28 assert!(result.is_instance_of::<pyo3::types::PyInt>());
29 assert_eq!(number_str, "-9223372036854775809");
30 });
31 }
32
33 #[test]
34 fn test_float() {
35 Python::attach(|py| {
36 let json_str = r#"3.141592653589793238"#;
37 let value: Value = serde_json::from_str(json_str).unwrap();
38 let result = pythonize(py, &value).unwrap();
39 let num: f32 = result.extract().unwrap();
40
41 assert!(result.is_instance_of::<pyo3::types::PyFloat>());
42 assert_eq!(num, 3.141592653589793238); // not {'$serde_json::private::Number': ...}
43 });
44 }
45
46 #[test]
47 fn test_int() {
48 Python::attach(|py| {
49 let json_str = r#"2"#;
50 let value: Value = serde_json::from_str(json_str).unwrap();
51 let result = pythonize(py, &value).unwrap();
52 let num: i32 = result.extract().unwrap();
53
54 assert!(result.is_instance_of::<pyo3::types::PyInt>());
55 assert_eq!(num, 2); // not {'$serde_json::private::Number': '2'}
56 });
57 }
58
59 #[test]
60 fn test_serde_error_if_token_empty() {
61 let json_str = r#"{"$serde_json::private::Number": ""}"#;
62 let result: Result<Value, _> = serde_json::from_str(json_str);
63
64 assert!(result.is_err());
65 assert!(result
66 .unwrap_err()
67 .to_string()
68 .contains("EOF while parsing a value"));
69 }
70
71 #[test]
72 fn test_serde_error_if_token_invalid() {
73 let json_str = r#"{"$serde_json::private::Number": 2}"#;
74 let result: Result<Value, _> = serde_json::from_str(json_str);
75
76 assert!(result.is_err());
77 assert!(result
78 .unwrap_err()
79 .to_string()
80 .contains("invalid type: integer `2`, expected string containing a number"));
81 }
82
83 #[test]
84 fn test_token_valid() {
85 Python::attach(|py| {
86 let json_str = r#"{"$serde_json::private::Number": "2"}"#;
87 let value: Value = serde_json::from_str(json_str).unwrap();
88 let result = pythonize(py, &value).unwrap();
89 let num: i32 = result.extract().unwrap();
90
91 assert!(result.is_instance_of::<pyo3::types::PyInt>());
92 assert_eq!(num, 2);
93 });
94 }
95
96 #[test]
97 fn test_depythonize_greater_than_u128_max() {
98 Python::attach(|py| {
99 // u128::MAX + 1
100 let py_int = py
101 .eval(c"340282366920938463463374607431768211456", None, None)
102 .unwrap();
103 let value: Value = depythonize(&py_int).unwrap();
104
105 assert!(value.is_number());
106 assert_eq!(value.to_string(), "340282366920938463463374607431768211456");
107 });
108 }