-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.rs
236 lines (212 loc) · 6.33 KB
/
load.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#![allow(clippy::approx_constant)]
use marshal_rs::{load, StringMode};
#[cfg(not(feature = "sonic"))]
use serde_json::json;
#[cfg(feature = "sonic")]
use sonic_rs::json;
#[test]
#[should_panic(expected = "Incompatible Marshal file format or version.")]
fn invalid_marshal_version() {
load(b"\x04\x090", None, None).unwrap();
}
#[test]
fn null() {
assert_eq!(load(b"\x04\x080", None, None).unwrap(), json!(null));
}
#[test]
fn boolean() {
assert_eq!(load(b"\x04\x08T", None, None).unwrap(), json!(true));
assert_eq!(load(b"\x04\x08F", None, None).unwrap(), json!(false));
}
#[test]
fn fixnum_positive() {
assert_eq!(load(b"\x04\x08i\0", None, None).unwrap(), json!(0));
assert_eq!(load(b"\x04\x08i\x0A", None, None).unwrap(), json!(5));
assert_eq!(
load(b"\x04\x08i\x02\x2C\x01", None, None).unwrap(),
json!(300)
);
assert_eq!(
load(b"\x04\x08i\x03\x70\x11\x01", None, None).unwrap(),
json!(70000)
);
assert_eq!(
load(b"\x04\x08i\x04\0\0\0\x01", None, None).unwrap(),
json!(16777216)
);
}
#[test]
fn fixnum_negative() {
assert_eq!(load(b"\x04\x08i\xF6", None, None).unwrap(), json!(-5));
assert_eq!(
load(b"\x04\x08i\xFE\xD4\xFE", None, None).unwrap(),
json!(-300)
);
assert_eq!(
load(b"\x04\x08i\xFD\x90\xEE\xFE", None, None).unwrap(),
json!(-70000)
);
assert_eq!(
load(b"\x04\x08i\xFD\0\0\0", None, None).unwrap(),
json!(-16777216)
);
}
#[test]
fn bignum_positive() {
assert_eq!(
load(b"\x04\x08l+\n\0\0\0\0\0\0\0\0\x02\0", None, None).unwrap(),
json!({"__type": "bigint", "value": "36893488147419103232"})
);
assert_eq!(
load(b"\x04\x08l+\n\0\0\0\0\0\0\0\0\x04\0", None, None).unwrap(),
json!({"__type": "bigint", "value": "73786976294838206464"})
);
assert_eq!(
load(b"\x04\x08l+\n\0\0\0\0\0\0\0\0\x08\0", None, None).unwrap(),
json!({"__type": "bigint", "value": "147573952589676412928"})
);
}
#[test]
fn bignum_negative() {
assert_eq!(
load(b"\x04\x08l-\n\0\0\0\0\0\0\0\0\x02\0", None, None).unwrap(),
json!({"__type": "bigint", "value": "-36893488147419103232"})
);
assert_eq!(
load(b"\x04\x08l-\n\0\0\0\0\0\0\0\0\x04\0", None, None).unwrap(),
json!({"__type": "bigint", "value": "-73786976294838206464"})
);
assert_eq!(
load(b"\x04\x08l-\n\0\0\0\0\0\0\0\0\x08\0", None, None).unwrap(),
json!({"__type": "bigint", "value": "-147573952589676412928"})
);
}
#[test]
fn float() {
assert_eq!(load(b"\x04\x08f\x06\x30", None, None).unwrap(), json!(0.0));
assert_eq!(load(b"\x04\x08f\x07-0", None, None).unwrap(), json!(-0.0));
assert_eq!(
load(b"\x04\x08f\x0C\x33\x2E\x31\x34\x31\x35\x39", None, None).unwrap(),
json!(3.14159)
);
assert_eq!(
load(b"\x04\x08f\x0D\x2D\x32\x2E\x37\x31\x38\x32\x38", None, None).unwrap(),
json!(-2.71828)
);
}
#[test]
fn string_utf8() {
assert_eq!(
load(b"\x04\x08I\"\x11Short string\x06:\x06ET", None, None).unwrap(),
json!("Short string")
);
assert_eq!(
load(
b"\x04\x08I\"\x01\xdcLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong string\x06:\x06ET",
None, None
).unwrap(),
"Long string".repeat(20),
);
}
#[test]
fn string_nonutf8() {
assert_eq!(
load(
b"\x04\x08I\"\x0b\xBA\xBA\xD7\xD6\xC4\xDA\x06:\rencoding\"\x08GBK",
None,
None
)
.unwrap(),
json!("汉字内")
);
}
#[test]
fn string_binary() {
assert_eq!(
load(
b"\x04\x08I\"\x11Short string\x06:\x06ET",
Some(StringMode::Binary),
None
)
.unwrap(),
json!({"__type": "bytes", "data": "Short string".as_bytes()})
);
assert_eq!(
load(
b"\x04\x08I\"\x01\xdcLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong string\x06:\x06ET",
Some(StringMode::Binary),
None
).unwrap(),
json!({"__type": "bytes", "data": "Long string".repeat(20).as_bytes()}),
);
}
#[test]
#[should_panic(expected = "Marshal data is too short.")]
fn invalid_string() {
// length of string is 4, which is equal to 0x09, but 0x10 length is passed
load(b"\x04\x08\"\x10\xf0(\x8c(", None, None).unwrap();
}
#[test]
fn links() {
assert_eq!(
load(
b"\x04\x08[\x08[\x08f\x080.1@\x07@\x07[\x08f\x080.2@\x09@\x09[\x08f\x080.3@\x0b@\x0b",
None,
None
)
.unwrap(),
json!([[0.1, 0.1, 0.1], [0.2, 0.2, 0.2], [0.3, 0.3, 0.3]])
);
}
#[test]
fn array() {
assert_eq!(
load(
b"\x04\x08[\x0ai\x06I\"\x08two\x06:\x06ETf\x063[\x06i\x09{\x06i\x0ai\x0b",
None,
None
)
.unwrap(),
json!([1, "two", 3.0, [4], {"__integer__5": 6}])
);
}
#[test]
fn hash() {
assert_eq!(
load(
b"\x04\x08{\x08i\x06I\"\x08one\x06:\x06ETI\"\x08two\x06;\0Ti\x07o:\x0bObject\x000",
None,
None
)
.unwrap(),
json!({"__integer__1": "one", "two": 2, r#"__object__{"__class":"__symbol__Object","__type":"object"}"#: null})
);
assert_eq!(
load(b"\x04\x08}\0I\"\x0cdefault\x06:\x06ET", None, None).unwrap(),
json!({"__ruby_default__": "default"})
);
}
#[test]
fn ruby_struct() {
assert_eq!(
load(
b"\x04\x08S:\x0bPerson\x07:\x09nameI\"\x0aAlice\x06:\x06ET:\x08agei#",
None,
None
)
.unwrap(),
json!({"__class": "__symbol__Person", "__members": {"__symbol__age": 30, "__symbol__name": "Alice"}, "__type": "struct"})
);
}
#[test]
fn object() {
assert_eq!(
load(
b"\x04\x08o:\x11CustomObject\x06:\x0a@dataI\"\x10object data\x06:\x06ET",
None,
None
)
.unwrap(),
json!({"__class": "__symbol__CustomObject", "__symbol__@data": "object data", "__type": "object"})
);
}