summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde_cbor/tests/value.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/serde_cbor/tests/value.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/serde_cbor/tests/value.rs')
-rw-r--r--third_party/rust/serde_cbor/tests/value.rs100
1 files changed, 100 insertions, 0 deletions
diff --git a/third_party/rust/serde_cbor/tests/value.rs b/third_party/rust/serde_cbor/tests/value.rs
new file mode 100644
index 0000000000..554d74266c
--- /dev/null
+++ b/third_party/rust/serde_cbor/tests/value.rs
@@ -0,0 +1,100 @@
+#[macro_use]
+extern crate serde_derive;
+
+#[cfg(feature = "std")]
+mod std_tests {
+ use serde_cbor;
+
+ use std::collections::BTreeMap;
+
+ #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
+ struct TupleStruct(String, i32, u64);
+
+ #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
+ struct UnitStruct;
+
+ #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
+ struct Struct<'a> {
+ tuple_struct: TupleStruct,
+ tuple: (String, f32, f64),
+ map: BTreeMap<String, String>,
+ bytes: &'a [u8],
+ array: Vec<String>,
+ unit_array: Vec<UnitStruct>,
+ }
+
+ use serde_cbor::value::Value;
+ use std::iter::FromIterator;
+
+ #[test]
+ fn serde() {
+ let tuple_struct = TupleStruct(format!("test"), -60, 3000);
+
+ let tuple = (format!("hello"), -50.0040957, -12.094635556478);
+
+ let map = BTreeMap::from_iter(
+ [
+ (format!("key1"), format!("value1")),
+ (format!("key2"), format!("value2")),
+ (format!("key3"), format!("value3")),
+ (format!("key4"), format!("value4")),
+ ]
+ .iter()
+ .cloned(),
+ );
+
+ let bytes = b"test byte string";
+
+ let array = vec![format!("one"), format!("two"), format!("three")];
+ let unit_array = vec![UnitStruct, UnitStruct, UnitStruct];
+
+ let data = Struct {
+ tuple_struct,
+ tuple,
+ map,
+ bytes,
+ array,
+ unit_array,
+ };
+
+ let value = serde_cbor::value::to_value(data.clone()).unwrap();
+ println!("{:?}", value);
+
+ let data_ser = serde_cbor::to_vec(&value).unwrap();
+ let data_de_value: Value = serde_cbor::from_slice(&data_ser).unwrap();
+
+ fn as_object(value: &Value) -> &BTreeMap<Value, Value> {
+ if let Value::Map(ref v) = value {
+ return v;
+ }
+ panic!()
+ }
+
+ for ((k1, v1), (k2, v2)) in as_object(&value)
+ .iter()
+ .zip(as_object(&data_de_value).iter())
+ {
+ assert_eq!(k1, k2);
+ assert_eq!(v1, v2);
+ }
+
+ assert_eq!(value, data_de_value);
+ }
+
+ #[derive(Debug, Deserialize, Serialize)]
+ struct SmallStruct {
+ spam: u32,
+ eggs: u32,
+ }
+
+ #[test]
+ fn small_struct() {
+ // Test whether the packed format works.
+ // Field names should not be serialized,
+ // instead field indizes are serialized.
+ let value = SmallStruct { spam: 17, eggs: 42 };
+ let data = serde_cbor::ser::to_vec_packed(&value).unwrap();
+ let reference = b"\xa2\x00\x11\x01\x18\x2a";
+ assert_eq!(data, reference);
+ }
+}