summaryrefslogtreecommitdiff
path: root/compiler/rustc_serialize/tests/opaque.rs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_serialize/tests/opaque.rs')
-rw-r--r--compiler/rustc_serialize/tests/opaque.rs56
1 files changed, 49 insertions, 7 deletions
diff --git a/compiler/rustc_serialize/tests/opaque.rs b/compiler/rustc_serialize/tests/opaque.rs
index 5e7dd18aa84..861091688bb 100644
--- a/compiler/rustc_serialize/tests/opaque.rs
+++ b/compiler/rustc_serialize/tests/opaque.rs
@@ -1,9 +1,10 @@
#![allow(rustc::internal)]
use rustc_macros::{Decodable, Encodable};
-use rustc_serialize::opaque::{MemDecoder, MemEncoder};
+use rustc_serialize::opaque::{MemDecoder, FileEncoder};
use rustc_serialize::{Decodable, Encodable};
use std::fmt::Debug;
+use std::fs;
#[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
struct Struct {
@@ -27,18 +28,21 @@ struct Struct {
}
fn check_round_trip<
- T: Encodable<MemEncoder> + for<'a> Decodable<MemDecoder<'a>> + PartialEq + Debug,
+ T: Encodable<FileEncoder> + for<'a> Decodable<MemDecoder<'a>> + PartialEq + Debug,
>(
values: Vec<T>,
) {
- let mut encoder = MemEncoder::new();
+ let tmpfile = tempfile::NamedTempFile::new().unwrap();
+ let tmpfile = tmpfile.path();
+
+ let mut encoder = FileEncoder::new(&tmpfile).unwrap();
for value in &values {
Encodable::encode(value, &mut encoder);
}
+ encoder.finish().unwrap();
- let data = encoder.finish();
+ let data = fs::read(&tmpfile).unwrap();
let mut decoder = MemDecoder::new(&data[..], 0);
-
for value in values {
let decoded = Decodable::decode(&mut decoder);
assert_eq!(value, decoded);
@@ -61,7 +65,7 @@ fn test_u8() {
#[test]
fn test_u16() {
- for i in u16::MIN..u16::MAX {
+ for i in [u16::MIN, 111, 3333, 55555, u16::MAX] {
check_round_trip(vec![1, 2, 3, i, i, i]);
}
}
@@ -92,7 +96,7 @@ fn test_i8() {
#[test]
fn test_i16() {
- for i in i16::MIN..i16::MAX {
+ for i in [i16::MIN, -100, 0, 101, i16::MAX] {
check_round_trip(vec![-1, 2, -3, i, i, i, 2]);
}
}
@@ -251,3 +255,41 @@ fn test_tuples() {
check_round_trip(vec![(1234567isize, 100000000000000u64, 99999999999999i64)]);
check_round_trip(vec![(String::new(), "some string".to_string())]);
}
+
+#[test]
+fn test_unit_like_struct() {
+ #[derive(Encodable, Decodable, PartialEq, Debug)]
+ struct UnitLikeStruct;
+
+ check_round_trip(vec![UnitLikeStruct]);
+}
+
+#[test]
+fn test_box() {
+ #[derive(Encodable, Decodable, PartialEq, Debug)]
+ struct A {
+ foo: Box<[bool]>,
+ }
+
+ let obj = A { foo: Box::new([true, false]) };
+ check_round_trip(vec![obj]);
+}
+
+#[test]
+fn test_cell() {
+ use std::cell::{Cell, RefCell};
+
+ #[derive(Encodable, Decodable, PartialEq, Debug)]
+ struct A {
+ baz: isize,
+ }
+
+ #[derive(Encodable, Decodable, PartialEq, Debug)]
+ struct B {
+ foo: Cell<bool>,
+ bar: RefCell<A>,
+ }
+
+ let obj = B { foo: Cell::new(true), bar: RefCell::new(A { baz: 2 }) };
+ check_round_trip(vec![obj]);
+}