burn/burn-core/tests/derive_config.rs

108 lines
3.0 KiB
Rust
Raw Normal View History

use burn::config::{config_to_json, Config};
2023-01-02 08:21:08 +08:00
use burn_core as burn;
2022-09-26 03:09:08 +08:00
#[derive(Config, Debug, PartialEq, Eq)]
pub struct TestEmptyStructConfig {}
#[derive(Config, Debug, PartialEq)]
pub struct TestStructConfig {
int: i32,
#[config(default = 2)]
int_default: i32,
float: f32,
#[config(default = 2.0)]
float_default: f32,
string: String,
other_config: TestEmptyStructConfig,
}
#[derive(Config, Debug, PartialEq)]
pub enum TestEnumConfig {
None,
Single(f32),
Multiple(f32, String),
Named { first: f32, second: String },
2022-09-26 03:09:08 +08:00
}
#[cfg(feature = "std")]
2022-09-26 03:09:08 +08:00
#[test]
fn struct_config_should_impl_serde() {
2023-08-09 05:57:51 +08:00
let config = TestStructConfig::new(2, 3.0, "Allow".to_string(), TestEmptyStructConfig::new());
2022-09-26 03:09:08 +08:00
let file_path = "/tmp/test_struct_config.json";
config.save(file_path).unwrap();
let config_loaded = TestStructConfig::load(file_path).unwrap();
assert_eq!(config, config_loaded);
}
#[test]
fn struct_config_should_impl_clone() {
2023-08-09 05:57:51 +08:00
let config = TestStructConfig::new(2, 3.0, "Allow".to_string(), TestEmptyStructConfig::new());
2022-09-26 03:09:08 +08:00
assert_eq!(config, config.clone());
}
#[test]
fn struct_config_should_impl_display() {
2023-08-09 05:57:51 +08:00
let config = TestStructConfig::new(2, 3.0, "Allow".to_string(), TestEmptyStructConfig::new());
2022-09-26 03:09:08 +08:00
assert_eq!(burn::config::config_to_json(&config), config.to_string());
}
#[cfg(feature = "std")]
2022-09-26 03:09:08 +08:00
#[test]
fn enum_config_no_value_should_impl_serde() {
let config = TestEnumConfig::None;
2022-09-26 03:09:08 +08:00
let file_path = "/tmp/test_enum_no_value_config.json";
config.save(file_path).unwrap();
let config_loaded = TestEnumConfig::load(file_path).unwrap();
assert_eq!(config, config_loaded);
}
#[cfg(feature = "std")]
2022-09-26 03:09:08 +08:00
#[test]
fn enum_config_one_value_should_impl_serde() {
let config = TestEnumConfig::Single(42.0);
2022-09-26 03:09:08 +08:00
let file_path = "/tmp/test_enum_one_value_config.json";
config.save(file_path).unwrap();
let config_loaded = TestEnumConfig::load(file_path).unwrap();
assert_eq!(config, config_loaded);
}
#[cfg(feature = "std")]
2022-09-26 03:09:08 +08:00
#[test]
fn enum_config_multiple_values_should_impl_serde() {
2023-08-09 05:57:51 +08:00
let config = TestEnumConfig::Multiple(42.0, "Allow".to_string());
2022-09-26 03:09:08 +08:00
let file_path = "/tmp/test_enum_multiple_values_config.json";
config.save(file_path).unwrap();
let config_loaded = TestEnumConfig::load(file_path).unwrap();
assert_eq!(config, config_loaded);
}
#[test]
fn enum_config_should_impl_clone() {
2023-08-09 05:57:51 +08:00
let config = TestEnumConfig::Multiple(42.0, "Allow".to_string());
2022-09-26 03:09:08 +08:00
assert_eq!(config, config.clone());
}
#[test]
fn enum_config_should_impl_display() {
2023-08-09 05:57:51 +08:00
let config = TestEnumConfig::Multiple(42.0, "Allow".to_string());
2022-09-26 03:09:08 +08:00
assert_eq!(burn::config::config_to_json(&config), config.to_string());
}
#[test]
fn struct_config_can_load_binary() {
2023-08-09 05:57:51 +08:00
let config = TestStructConfig::new(2, 3.0, "Allow".to_string(), TestEmptyStructConfig::new());
let binary = config_to_json(&config).as_bytes().to_vec();
let config_loaded = TestStructConfig::load_binary(&binary).unwrap();
assert_eq!(config, config_loaded);
}