2023-02-16 21:43:43 +08:00
|
|
|
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 {
|
2022-12-26 00:22:25 +08:00
|
|
|
None,
|
|
|
|
Single(f32),
|
|
|
|
Multiple(f32, String),
|
2022-09-26 03:09:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn struct_config_should_impl_serde() {
|
|
|
|
let config = TestStructConfig::new(2, 3.0, "Allo".to_string(), TestEmptyStructConfig::new());
|
|
|
|
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() {
|
|
|
|
let config = TestStructConfig::new(2, 3.0, "Allo".to_string(), TestEmptyStructConfig::new());
|
|
|
|
assert_eq!(config, config.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn struct_config_should_impl_display() {
|
|
|
|
let config = TestStructConfig::new(2, 3.0, "Allo".to_string(), TestEmptyStructConfig::new());
|
|
|
|
assert_eq!(burn::config::config_to_json(&config), config.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enum_config_no_value_should_impl_serde() {
|
2022-12-26 00:22:25 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enum_config_one_value_should_impl_serde() {
|
2022-12-26 00:22:25 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enum_config_multiple_values_should_impl_serde() {
|
2022-12-26 00:22:25 +08:00
|
|
|
let config = TestEnumConfig::Multiple(42.0, "Allo".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() {
|
2022-12-26 00:22:25 +08:00
|
|
|
let config = TestEnumConfig::Multiple(42.0, "Allo".to_string());
|
2022-09-26 03:09:08 +08:00
|
|
|
assert_eq!(config, config.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enum_config_should_impl_display() {
|
2022-12-26 00:22:25 +08:00
|
|
|
let config = TestEnumConfig::Multiple(42.0, "Allo".to_string());
|
2022-09-26 03:09:08 +08:00
|
|
|
assert_eq!(burn::config::config_to_json(&config), config.to_string());
|
|
|
|
}
|
2023-02-16 21:43:43 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn struct_config_can_load_binary() {
|
|
|
|
let config = TestStructConfig::new(2, 3.0, "Allo".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);
|
|
|
|
}
|