From 8d359e4385052f012d8d0c2e57a0bcfe54462d44 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 2 May 2023 11:01:58 +1000 Subject: [PATCH] Move some `Encodable`/`Decodable` tests. Round-trip encoding/decoding of many types is tested in `compiler/rustc_serialize/tests/opaque.rs`. There is also a small amount of encoding/decoding testing in three files in `tests/ui-fulldeps`. There is no obvious reason why these three files are necessary. They were originally added in 2014. Maybe it wasn't possible for a proc macro to run in a unit test back then? This commit just moves the testing from those three files into the unit test. --- compiler/rustc_serialize/tests/opaque.rs | 39 ++++++++++++++++ .../deriving-encodable-decodable-box.rs | 34 -------------- ...riving-encodable-decodable-cell-refcell.rs | 44 ------------------- tests/ui-fulldeps/issue-14021.rs | 33 -------------- 4 files changed, 39 insertions(+), 111 deletions(-) delete mode 100644 tests/ui-fulldeps/deriving-encodable-decodable-box.rs delete mode 100644 tests/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs delete mode 100644 tests/ui-fulldeps/issue-14021.rs diff --git a/compiler/rustc_serialize/tests/opaque.rs b/compiler/rustc_serialize/tests/opaque.rs index 5e7dd18aa84..7a7db99b168 100644 --- a/compiler/rustc_serialize/tests/opaque.rs +++ b/compiler/rustc_serialize/tests/opaque.rs @@ -251,3 +251,42 @@ 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, + bar: RefCell, + } + + let obj = B { foo: Cell::new(true), bar: RefCell::new(A { baz: 2 }) }; + check_round_trip(vec![obj]); +} + diff --git a/tests/ui-fulldeps/deriving-encodable-decodable-box.rs b/tests/ui-fulldeps/deriving-encodable-decodable-box.rs deleted file mode 100644 index 1c376f59e51..00000000000 --- a/tests/ui-fulldeps/deriving-encodable-decodable-box.rs +++ /dev/null @@ -1,34 +0,0 @@ -// run-pass - -#![allow(unused_imports)] -#![feature(rustc_private)] - -extern crate rustc_macros; -extern crate rustc_serialize; - -// Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta -// files. -#[allow(unused_extern_crates)] -extern crate rustc_driver; - -use rustc_macros::{Decodable, Encodable}; -use rustc_serialize::opaque::{MemDecoder, MemEncoder}; -use rustc_serialize::{Decodable, Encodable, Encoder}; - -#[derive(Encodable, Decodable)] -struct A { - foo: Box<[bool]>, -} - -fn main() { - let obj = A { foo: Box::new([true, false]) }; - - let mut encoder = MemEncoder::new(); - obj.encode(&mut encoder); - let data = encoder.finish(); - - let mut decoder = MemDecoder::new(&data, 0); - let obj2 = A::decode(&mut decoder); - - assert_eq!(obj.foo, obj2.foo); -} diff --git a/tests/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs b/tests/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs deleted file mode 100644 index 844d40f2ecd..00000000000 --- a/tests/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs +++ /dev/null @@ -1,44 +0,0 @@ -// run-pass - -#![allow(unused_imports)] -// This briefly tests the capability of `Cell` and `RefCell` to implement the -// `Encodable` and `Decodable` traits via `#[derive(Encodable, Decodable)]` -#![feature(rustc_private)] - -extern crate rustc_macros; -extern crate rustc_serialize; - -// Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta -// files. -#[allow(unused_extern_crates)] -extern crate rustc_driver; - -use rustc_macros::{Decodable, Encodable}; -use rustc_serialize::opaque::{MemDecoder, MemEncoder}; -use rustc_serialize::{Decodable, Encodable, Encoder}; -use std::cell::{Cell, RefCell}; - -#[derive(Encodable, Decodable)] -struct A { - baz: isize, -} - -#[derive(Encodable, Decodable)] -struct B { - foo: Cell, - bar: RefCell, -} - -fn main() { - let obj = B { foo: Cell::new(true), bar: RefCell::new(A { baz: 2 }) }; - - let mut encoder = MemEncoder::new(); - obj.encode(&mut encoder); - let data = encoder.finish(); - - let mut decoder = MemDecoder::new(&data, 0); - let obj2 = B::decode(&mut decoder); - - assert_eq!(obj.foo.get(), obj2.foo.get()); - assert_eq!(obj.bar.borrow().baz, obj2.bar.borrow().baz); -} diff --git a/tests/ui-fulldeps/issue-14021.rs b/tests/ui-fulldeps/issue-14021.rs deleted file mode 100644 index 309b5c4a03d..00000000000 --- a/tests/ui-fulldeps/issue-14021.rs +++ /dev/null @@ -1,33 +0,0 @@ -// run-pass - -#![allow(unused_mut)] -#![allow(unused_imports)] -#![feature(rustc_private)] - -extern crate rustc_macros; -extern crate rustc_serialize; - -// Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta -// files. -#[allow(unused_extern_crates)] -extern crate rustc_driver; - -use rustc_macros::{Decodable, Encodable}; -use rustc_serialize::opaque::{MemDecoder, MemEncoder}; -use rustc_serialize::{Decodable, Encodable, Encoder}; - -#[derive(Encodable, Decodable, PartialEq, Debug)] -struct UnitLikeStruct; - -pub fn main() { - let obj = UnitLikeStruct; - - let mut encoder = MemEncoder::new(); - obj.encode(&mut encoder); - let data = encoder.finish(); - - let mut decoder = MemDecoder::new(&data, 0); - let obj2 = UnitLikeStruct::decode(&mut decoder); - - assert_eq!(obj, obj2); -}