From a03fde22df60ddae26614e008ec78cda0e37407f Mon Sep 17 00:00:00 2001 From: schilkp Date: Tue, 7 May 2024 08:42:17 +0200 Subject: [PATCH] rust-rewrite: Ported c-funcpack generator tests to rust integration tests --- .github/workflows/ci.yml | 8 +- .gitignore | 2 + Cargo.lock | 60 ++++ justfile | 11 +- reginald_codegen/Cargo.toml | 1 + .../generator_tests/run_tests.bash | 4 - reginald_codegen/src/cli/cmd/completion.rs | 2 +- reginald_codegen/src/cli/cmd/gen.rs | 10 +- reginald_codegen/src/cli/cmd/tool.rs | 2 +- reginald_codegen/src/cli/mod.rs | 2 +- reginald_codegen/src/lib.rs | 3 + reginald_codegen/src/main.rs | 4 +- .../generator_c_funcpack}/.gitignore | 2 +- .../tests/generator_c_funcpack/mod.rs | 287 ++++++++++++++++++ .../resources}/Unity/LICENSE.txt | 0 .../resources}/Unity/README.md | 0 .../resources}/Unity/unity.c | 0 .../resources}/Unity/unity.h | 0 .../resources}/Unity/unity_internals.h | 0 .../resources/compile_flags.txt | 2 + .../generator_c_funcpack/resources}/test.c | 4 +- .../generator_c_funcpack}/run_test.bash | 2 - reginald_codegen/tests/generator_tests.rs | 15 + reginald_codegen/tests/map.yaml | 231 ++++++++++++++ 24 files changed, 628 insertions(+), 24 deletions(-) rename reginald_codegen/{generator_tests/c_funcpack => tests/generator_c_funcpack}/.gitignore (73%) create mode 100644 reginald_codegen/tests/generator_c_funcpack/mod.rs rename reginald_codegen/{generator_tests/c_funcpack => tests/generator_c_funcpack/resources}/Unity/LICENSE.txt (100%) rename reginald_codegen/{generator_tests/c_funcpack => tests/generator_c_funcpack/resources}/Unity/README.md (100%) rename reginald_codegen/{generator_tests/c_funcpack => tests/generator_c_funcpack/resources}/Unity/unity.c (100%) rename reginald_codegen/{generator_tests/c_funcpack => tests/generator_c_funcpack/resources}/Unity/unity.h (100%) rename reginald_codegen/{generator_tests/c_funcpack => tests/generator_c_funcpack/resources}/Unity/unity_internals.h (100%) create mode 100644 reginald_codegen/tests/generator_c_funcpack/resources/compile_flags.txt rename reginald_codegen/{generator_tests/c_funcpack => tests/generator_c_funcpack/resources}/test.c (98%) rename reginald_codegen/{generator_tests/c_funcpack => tests/generator_c_funcpack}/run_test.bash (99%) create mode 100644 reginald_codegen/tests/generator_tests.rs create mode 100644 reginald_codegen/tests/map.yaml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8c5d4bd..2481fa7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,5 +45,9 @@ jobs: with: toolchain: stable - uses: carlosperate/arm-none-eabi-gcc-action@v1 - - name: Run Generator tests. - run: bash ${{github.workspace}}/reginald_codegen/generator_tests/run_tests.bash + - name: Run old generator tests. + run: + bash ${{github.workspace}}/reginald_codegen/generator_tests/run_tests.bash + - name: Run generator tests. + run: + cargo test -- --ignored diff --git a/.gitignore b/.gitignore index ee86644..e15b0c6 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ out.c out.rs testproj/ + +tarpaulin-report.html diff --git a/Cargo.lock b/Cargo.lock index 6830a25..2e81a5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,18 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "bitflags" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "clap" version = "4.5.1" @@ -154,6 +166,22 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "errno" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "fastrand" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" + [[package]] name = "hashbrown" version = "0.14.3" @@ -194,6 +222,12 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "linux-raw-sys" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" + [[package]] name = "memchr" version = "2.7.1" @@ -279,6 +313,7 @@ dependencies = [ "serde", "serde_yaml", "similar", + "tempfile", "thiserror", ] @@ -301,6 +336,19 @@ dependencies = [ "pretty_assertions", ] +[[package]] +name = "rustix" +version = "0.38.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "ryu" version = "1.0.17" @@ -363,6 +411,18 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tempfile" +version = "3.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" +dependencies = [ + "cfg-if", + "fastrand", + "rustix", + "windows-sys", +] + [[package]] name = "thiserror" version = "1.0.57" diff --git a/justfile b/justfile index e23d953..6659744 100644 --- a/justfile +++ b/justfile @@ -1,9 +1,18 @@ +full_check: + cargo test --workspace -- --include-ignored + ./reginald_codegen/generator_tests/run_tests.bash + cargo fmt --check + cargo clippy --all-features --workspace + @echo "ALL CHECKS OK" + check: cargo test --workspace - ./reginald_codegen/generator_tests/run_tests.bash cargo fmt --check cargo clippy --all-features --workspace @echo "ALL CHECKS OK" tokei: tokei --exclude Unity + +cov: + cargo tarpaulin --all-targets --doc --out Html -- --include-ignored diff --git a/reginald_codegen/Cargo.toml b/reginald_codegen/Cargo.toml index 7c07d89..b5e8a79 100644 --- a/reginald_codegen/Cargo.toml +++ b/reginald_codegen/Cargo.toml @@ -28,4 +28,5 @@ similar = { version = "2.4.0", features = ["inline"], optional = true } console = { version = "0.15.8", features = [], optional = true } [dev-dependencies] +tempfile = "3.10.1" pretty_assertions = "1.4.0" diff --git a/reginald_codegen/generator_tests/run_tests.bash b/reginald_codegen/generator_tests/run_tests.bash index 35e16bb..567de20 100755 --- a/reginald_codegen/generator_tests/run_tests.bash +++ b/reginald_codegen/generator_tests/run_tests.bash @@ -21,10 +21,6 @@ echo_cyan "=== COMPILE ===" cargo build echo_green "DONE" -echo -echo_cyan "=== TESTING C FUNCPACK ===" -bash ./c_funcpack/run_test.bash || fail - echo echo_cyan "=== TESTING RS STRUCTS ===" bash ./rs_structs/run_test.bash || fail diff --git a/reginald_codegen/src/cli/cmd/completion.rs b/reginald_codegen/src/cli/cmd/completion.rs index 753ee7a..4ebdc41 100644 --- a/reginald_codegen/src/cli/cmd/completion.rs +++ b/reginald_codegen/src/cli/cmd/completion.rs @@ -1,7 +1,7 @@ use std::io; +use crate::error::Error; use clap::{CommandFactory, Parser}; -use reginald_codegen::error::Error; use crate::cli::Cli; diff --git a/reginald_codegen/src/cli/cmd/gen.rs b/reginald_codegen/src/cli/cmd/gen.rs index 6bdda29..4671cce 100644 --- a/reginald_codegen/src/cli/cmd/gen.rs +++ b/reginald_codegen/src/cli/cmd/gen.rs @@ -1,12 +1,12 @@ use std::fs; use std::path::PathBuf; +use crate::builtin::c; +use crate::builtin::md; +use crate::builtin::rs; +use crate::error::Error; +use crate::regmap::RegisterMap; use clap::Parser; -use reginald_codegen::builtin::c; -use reginald_codegen::builtin::md; -use reginald_codegen::builtin::rs; -use reginald_codegen::error::Error; -use reginald_codegen::regmap::RegisterMap; use crate::cli::diff; diff --git a/reginald_codegen/src/cli/cmd/tool.rs b/reginald_codegen/src/cli/cmd/tool.rs index 4ad3756..82cb6fc 100644 --- a/reginald_codegen/src/cli/cmd/tool.rs +++ b/reginald_codegen/src/cli/cmd/tool.rs @@ -1,7 +1,7 @@ use std::{fs, path::PathBuf}; +use crate::{builtin::rs::CONVERSION_TRAITS, error::Error}; use clap::Parser; -use reginald_codegen::{builtin::rs::CONVERSION_TRAITS, error::Error}; #[derive(Parser, Debug)] #[command(about = "Built-in tools and utilities")] diff --git a/reginald_codegen/src/cli/mod.rs b/reginald_codegen/src/cli/mod.rs index ec3cf7c..80cc602 100644 --- a/reginald_codegen/src/cli/mod.rs +++ b/reginald_codegen/src/cli/mod.rs @@ -1,4 +1,4 @@ -mod cmd; +pub mod cmd; mod diff; use std::process::ExitCode; diff --git a/reginald_codegen/src/lib.rs b/reginald_codegen/src/lib.rs index 0ba11dd..4f54bcd 100644 --- a/reginald_codegen/src/lib.rs +++ b/reginald_codegen/src/lib.rs @@ -7,3 +7,6 @@ pub mod error; pub mod regmap; pub mod utils; pub mod writer; + +#[cfg(feature = "cli")] +pub mod cli; diff --git a/reginald_codegen/src/main.rs b/reginald_codegen/src/main.rs index 8d18a10..a926a91 100644 --- a/reginald_codegen/src/main.rs +++ b/reginald_codegen/src/main.rs @@ -1,8 +1,6 @@ +use reginald_codegen::cli; use std::process::ExitCode; -#[cfg(feature = "cli")] -mod cli; - #[cfg(feature = "cli")] fn main() -> ExitCode { cli::cli_main() diff --git a/reginald_codegen/generator_tests/c_funcpack/.gitignore b/reginald_codegen/tests/generator_c_funcpack/.gitignore similarity index 73% rename from reginald_codegen/generator_tests/c_funcpack/.gitignore rename to reginald_codegen/tests/generator_c_funcpack/.gitignore index f682627..388b79c 100644 --- a/reginald_codegen/generator_tests/c_funcpack/.gitignore +++ b/reginald_codegen/tests/generator_c_funcpack/.gitignore @@ -1,3 +1,3 @@ -output/ +artifacts/ .cache/ compile_commands.json diff --git a/reginald_codegen/tests/generator_c_funcpack/mod.rs b/reginald_codegen/tests/generator_c_funcpack/mod.rs new file mode 100644 index 0000000..35ab9ee --- /dev/null +++ b/reginald_codegen/tests/generator_c_funcpack/mod.rs @@ -0,0 +1,287 @@ +use std::{fs, path::PathBuf, process::Command}; + +use tempfile::{tempdir, TempDir}; + +use reginald_codegen::{ + builtin::c::funcpack::{Element, GeneratorOpts}, + cli::cmd::gen::{self, Generator}, + utils::Endianess, +}; + +use crate::TEST_MAP_FILE; + +// ==== Utils ================================================================== + +fn test_generated_code(test_dir: &TempDir, extra_cflags: &[&str], extra_sources: &[&str]) { + let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let resources_dir = manifest_dir.join(PathBuf::from("tests/generator_c_funcpack/resources")); + + let mut compile_args = vec![]; + + // Sources: + compile_args.push(resources_dir.join("test.c").to_str().unwrap().to_string()); + compile_args.push(resources_dir.join("Unity/unity.c").to_str().unwrap().to_string()); + compile_args.append(&mut extra_sources.iter().map(|x| x.to_string()).collect()); + + // c flags: + compile_args.push("-Wall".to_string()); + compile_args.push("-Wextra".to_string()); + compile_args.push("-Wpedantic".to_string()); + compile_args.push("-Wconversion".to_string()); + compile_args.push("-Warith-conversion".to_string()); + compile_args.push("-Wint-conversion".to_string()); + compile_args.push("-Werror".to_string()); + compile_args.push("-fsanitize=undefined".to_string()); + + // include resources dir (for test framework): + compile_args.push(format!("-I{}", resources_dir.to_str().unwrap())); + + // include test dir (for generated files): + compile_args.push(format!("-I{}", test_dir.path().to_str().unwrap())); + + // Extra c flags: + compile_args.append(&mut extra_cflags.iter().map(|x| x.to_string()).collect()); + + // output: + let test_exe = test_dir.path().join("test.out").to_str().unwrap().to_string(); + compile_args.push("-o".to_string()); + compile_args.push(test_exe.to_string()); + + println!("Compiling..."); + let compile_output = Command::new("gcc").args(&compile_args).output().unwrap(); + println!("STDOUT:"); + println!("{}", String::from_utf8(compile_output.stdout.clone()).unwrap()); + println!("STDERR:"); + println!("{}", String::from_utf8(compile_output.stderr.clone()).unwrap()); + println!("EXIT STATUS:"); + println!("{}", &compile_output.status); + assert!(compile_output.status.success()); + + println!("Testing..."); + let test_output = Command::new(test_exe).output().unwrap(); + println!("STDOUT:"); + println!("{}", String::from_utf8(test_output.stdout.clone()).unwrap()); + println!("STDERR:"); + println!("{}", String::from_utf8(test_output.stderr.clone()).unwrap()); + println!("EXIT STATUS:"); + println!("{}", &test_output.status); + assert!(test_output.status.success()); +} + +fn finish_test(d: TempDir) { + let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let artifacts_dir = manifest_dir.join(PathBuf::from("tests/generator_c_funcpack/artifacts")); + fs::create_dir_all(&artifacts_dir).unwrap(); + + for entry in fs::read_dir(d.path()).unwrap() { + let entry = entry.unwrap(); + if !entry.file_type().unwrap().is_dir() { + fs::copy(entry.path(), artifacts_dir.join(entry.file_name())).unwrap(); + } + } + + d.close().unwrap(); +} + +fn run_reginald(d: &TempDir, output_name: &str, opts: GeneratorOpts) { + gen::cmd(gen::Command { + input: TEST_MAP_FILE.to_owned(), + output: d.path().to_owned().join(PathBuf::from(output_name)), + overwrite_map_name: None, + verify: false, + generator: Generator::CFuncpack(opts), + }) + .unwrap(); +} + +const GENERATOR_OPTS_DEFAULT: GeneratorOpts = GeneratorOpts { + endian: vec![], + defer_to_endian: None, + registers_as_bitfields: true, + max_enum_bitwidth: 31, + add_include: vec![], + funcs_static_inline: true, + funcs_as_prototypes: false, + clang_format_guard: true, + include_guards: true, + only_generate: vec![], + dont_generate: vec![], +}; + +// ==== Tests ================================================================== + +#[test] +#[ignore] +fn generator_c_funcpack_c99() { + let d = tempdir().unwrap(); + + run_reginald( + &d, + "out.h", + GeneratorOpts { + dont_generate: vec![Element::GenericMacros], + ..GENERATOR_OPTS_DEFAULT + }, + ); + + test_generated_code(&d, &["-DTEST_SKIP_GENERIC_MACROS", "-std=c99", "-fanalyzer"], &[]); + + finish_test(d); +} + +#[test] +#[ignore] +fn generator_c_funcpack_c11() { + let d = tempdir().unwrap(); + + run_reginald( + &d, + "out.h", + GeneratorOpts { + ..GENERATOR_OPTS_DEFAULT + }, + ); + + test_generated_code(&d, &["-std=c11", "-fanalyzer"], &[]); + + finish_test(d); +} + +#[test] +#[ignore] +fn generator_c_funcpack_defer_to_le() { + let d = tempdir().unwrap(); + + run_reginald( + &d, + "out.h", + GeneratorOpts { + defer_to_endian: Some(Endianess::Little), + ..GENERATOR_OPTS_DEFAULT + }, + ); + + test_generated_code(&d, &["-std=c11", "-fanalyzer"], &[]); + + finish_test(d); +} + +#[test] +#[ignore] +fn generator_c_funcpack_defer_to_be() { + let d = tempdir().unwrap(); + + run_reginald( + &d, + "out.h", + GeneratorOpts { + defer_to_endian: Some(Endianess::Big), + ..GENERATOR_OPTS_DEFAULT + }, + ); + + test_generated_code(&d, &["-std=c11", "-fanalyzer"], &[]); + + finish_test(d); +} + +#[test] +#[ignore] +fn generator_c_funcpack_split_header_source() { + let d = tempdir().unwrap(); + + run_reginald( + &d, + "out.h", + GeneratorOpts { + funcs_as_prototypes: true, + funcs_static_inline: false, + ..GENERATOR_OPTS_DEFAULT + }, + ); + + run_reginald( + &d, + "out.c", + GeneratorOpts { + funcs_static_inline: false, + add_include: vec!["out.h".to_string()], + include_guards: false, + only_generate: vec![Element::StructConversionFuncs], + ..GENERATOR_OPTS_DEFAULT + }, + ); + + test_generated_code(&d, &["-std=c11", "-fanalyzer"], &[d.path().join("out.c").to_str().unwrap()]); + + finish_test(d); +} + +#[test] +#[ignore] +fn generator_c_funcpack_split_headers() { + let d = tempdir().unwrap(); + + run_reginald( + &d, + "out_enum.h", + GeneratorOpts { + only_generate: vec![Element::Enums], + ..GENERATOR_OPTS_DEFAULT + }, + ); + + run_reginald( + &d, + "out_enum_validation.h", + GeneratorOpts { + only_generate: vec![Element::EnumValidationMacros], + add_include: vec!["out_enum.h".to_string()], + ..GENERATOR_OPTS_DEFAULT + }, + ); + + run_reginald( + &d, + "out_structs.h", + GeneratorOpts { + only_generate: vec![Element::Structs], + add_include: vec!["out_enum_validation.h".to_string()], + ..GENERATOR_OPTS_DEFAULT + }, + ); + + run_reginald( + &d, + "out_struct_conv.h", + GeneratorOpts { + only_generate: vec![Element::StructConversionFuncs], + add_include: vec!["out_structs.h".to_string()], + ..GENERATOR_OPTS_DEFAULT + }, + ); + + run_reginald( + &d, + "out_reg_props.h", + GeneratorOpts { + only_generate: vec![Element::RegisterProperties], + add_include: vec!["out_struct_conv.h".to_string()], + ..GENERATOR_OPTS_DEFAULT + }, + ); + + run_reginald( + &d, + "out.h", + GeneratorOpts { + only_generate: vec![Element::GenericMacros], + add_include: vec!["out_reg_props.h".to_string()], + ..GENERATOR_OPTS_DEFAULT + }, + ); + + test_generated_code(&d, &["-std=c11", "-fanalyzer"], &[]); + + finish_test(d); +} diff --git a/reginald_codegen/generator_tests/c_funcpack/Unity/LICENSE.txt b/reginald_codegen/tests/generator_c_funcpack/resources/Unity/LICENSE.txt similarity index 100% rename from reginald_codegen/generator_tests/c_funcpack/Unity/LICENSE.txt rename to reginald_codegen/tests/generator_c_funcpack/resources/Unity/LICENSE.txt diff --git a/reginald_codegen/generator_tests/c_funcpack/Unity/README.md b/reginald_codegen/tests/generator_c_funcpack/resources/Unity/README.md similarity index 100% rename from reginald_codegen/generator_tests/c_funcpack/Unity/README.md rename to reginald_codegen/tests/generator_c_funcpack/resources/Unity/README.md diff --git a/reginald_codegen/generator_tests/c_funcpack/Unity/unity.c b/reginald_codegen/tests/generator_c_funcpack/resources/Unity/unity.c similarity index 100% rename from reginald_codegen/generator_tests/c_funcpack/Unity/unity.c rename to reginald_codegen/tests/generator_c_funcpack/resources/Unity/unity.c diff --git a/reginald_codegen/generator_tests/c_funcpack/Unity/unity.h b/reginald_codegen/tests/generator_c_funcpack/resources/Unity/unity.h similarity index 100% rename from reginald_codegen/generator_tests/c_funcpack/Unity/unity.h rename to reginald_codegen/tests/generator_c_funcpack/resources/Unity/unity.h diff --git a/reginald_codegen/generator_tests/c_funcpack/Unity/unity_internals.h b/reginald_codegen/tests/generator_c_funcpack/resources/Unity/unity_internals.h similarity index 100% rename from reginald_codegen/generator_tests/c_funcpack/Unity/unity_internals.h rename to reginald_codegen/tests/generator_c_funcpack/resources/Unity/unity_internals.h diff --git a/reginald_codegen/tests/generator_c_funcpack/resources/compile_flags.txt b/reginald_codegen/tests/generator_c_funcpack/resources/compile_flags.txt new file mode 100644 index 0000000..4e390d2 --- /dev/null +++ b/reginald_codegen/tests/generator_c_funcpack/resources/compile_flags.txt @@ -0,0 +1,2 @@ +-I. +-I../artifacts/ diff --git a/reginald_codegen/generator_tests/c_funcpack/test.c b/reginald_codegen/tests/generator_c_funcpack/resources/test.c similarity index 98% rename from reginald_codegen/generator_tests/c_funcpack/test.c rename to reginald_codegen/tests/generator_c_funcpack/resources/test.c index 3dbfa92..f1bab31 100644 --- a/reginald_codegen/generator_tests/c_funcpack/test.c +++ b/reginald_codegen/tests/generator_c_funcpack/resources/test.c @@ -1,6 +1,6 @@ #include "Unity/unity.h" #include "Unity/unity_internals.h" -#include "output/generated.h" +#include "out.h" #include void reverse_array(uint8_t *from, uint8_t *to, size_t len) { @@ -55,7 +55,6 @@ void test_basic_reg1(void) { // Try unpacking: memset(®, 0, sizeof(struct chip_reg1)); - printf("0x%x\n", packed_reg_le); #ifdef TEST_SKIP_GENERIC_MACROS TEST_ASSERT_EQUAL(0, chip_reg1_try_unpack_le(&packed_reg_le, ®)); #else @@ -93,7 +92,6 @@ void test_basic_reg2(void) { ), [1] = ((0xA) << 0), // Field 4 }; - printf("0: 0x%x, 1: 0x%x\r\n", expected_packed_reg_le[0], expected_packed_reg_le[1]); uint8_t expected_packed_reg_be[2] = {0}; reverse_array(expected_packed_reg_le, expected_packed_reg_be, 2); diff --git a/reginald_codegen/generator_tests/c_funcpack/run_test.bash b/reginald_codegen/tests/generator_c_funcpack/run_test.bash similarity index 99% rename from reginald_codegen/generator_tests/c_funcpack/run_test.bash rename to reginald_codegen/tests/generator_c_funcpack/run_test.bash index 5597c45..7ad6e4e 100755 --- a/reginald_codegen/generator_tests/c_funcpack/run_test.bash +++ b/reginald_codegen/tests/generator_c_funcpack/run_test.bash @@ -59,8 +59,6 @@ echo "Generating..." cargo run --quiet --color always -- gen -i ../map.yaml -o output/generated.h c-funcpack test_generated_code "-std=c11" -exit - # #### Defer-to-LE ########################################################## start_test "Defer-to-LE Test" diff --git a/reginald_codegen/tests/generator_tests.rs b/reginald_codegen/tests/generator_tests.rs new file mode 100644 index 0000000..3522c9e --- /dev/null +++ b/reginald_codegen/tests/generator_tests.rs @@ -0,0 +1,15 @@ +mod generator_c_funcpack; + +use std::path::PathBuf; + +use lazy_static::lazy_static; + +lazy_static! { + pub static ref TEST_MAP_FILE: PathBuf = find_test_map_file(); +} + +fn find_test_map_file() -> PathBuf { + let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + d.push("tests/map.yaml"); + d +} diff --git a/reginald_codegen/tests/map.yaml b/reginald_codegen/tests/map.yaml new file mode 100644 index 0000000..9c30c0d --- /dev/null +++ b/reginald_codegen/tests/map.yaml @@ -0,0 +1,231 @@ +--- +name: chip +defaults: + layout_bitwidth: 8 + +registers: + REG1: !Register + adr: 0x00 + reset_val: 0x00 + bitwidth: 8 + layout: !Layout + FIELD1: + bits: ["2-5"] + accepts: !UInt + FIELD0: + bits: [0] + accepts: !Bool + + REG2: !Register + adr: 0x00 + reset_val: 0x0043 + bitwidth: 16 + layout: !Layout + FIELD1: + bits: [7, 6] + access: [R] + accepts: !SharedEnum STAT + FIELD2: + bits: ["1-0"] + access: [R] + accepts: !Enum + EN: + val: 0x3 + FIELD3: + bits: [2] + access: [R] + accepts: !Bool + FIELD4: + bits: ["8-12"] + access: [R] + RESERVED: + bits: ["4-5"] + accepts: !Fixed 0x1 + + REG3: !Register + adr: 0x10 + bitwidth: 64 + layout: !Layout + FIELD0: + bits: ["0-15"] + FIELD1: + bits: ["63-56"] + + REG_EMPTY: !Register + adr: 0x10 + reset_val: 0x00 + bitwidth: 8 + + REG_SHARED_LAYOUT_BASIC_1: !Register + adr: 0x10 + reset_val: 0x0 + layout: !SharedLayout BASIC_SHARED_LAYOUT + + REG_SHARED_LAYOUT_BASIC_2: !Register + adr: 0x10 + layout: !SharedLayout BASIC_SHARED_LAYOUT + + REG_FIXED_ACROSS_BYTES: !Register + adr: 0x12 + bitwidth: 16 + layout: !Layout + F1: + bits: ["6-9"] + accepts: !Fixed 0x9 + + REG_SHARED_LAYOUT: !Register + adr: 0x21 + layout: !SharedLayout BASIC_SHARED_LAYOUT + + REG_LAYOUT_FIELD: !Register + adr: 0x20 + bitwidth: 16 + reset_val: 0xFD03 + layout: !Layout + LAYOUT_FIELD: + bits: ["0-15"] + accepts: !Layout + F1: + bits: [0] + F2: + bits: ["2-9"] + accepts: !Layout + F22: + bits: ["0-7"] + F3: + bits: [13] + accepts: !Layout + + REG_NESTED_ONLY_FIXED: !Register + adr: 0x20 + layout: !Layout + LAYOUT_FIELD_1: + bits: ["0-7"] + accepts: !Layout + LAYOUT_FIELD_2: + bits: ["0-7"] + accepts: !Fixed 0xAB + + REG_SPLIT_FIELD: !Register + adr: 0x20 + layout: !Layout + SPLIT_FIELD_1: + bits: ["0-1", "4-5"] + SPLIT_FIELD_2: + bits: ["2-3", "6-7"] + + REG_SPLIT_ENUM: !Register + adr: 0x20 + layout: !Layout + SPLIT_ENUM: + bits: [0, 2] + accepts: !Enum + SE_0: + val: 0x0 + SE_1: + val: 0x1 + SE_2: + val: 0x4 + SE_3: + val: 0x5 + + REG_SPLIT_LAYOUT: !Register + adr: 0x21 + layout: !Layout + SPLIT_LAYOUT: + bits: ["1-3", "5-7"] + accepts: !Layout + F1: + bits: ["1-2"] + F2: + bits: ["4-6"] + + REG_FIELD_BIGGER_THAN_ENUM_1: !Register + adr: 0x21 + bitwidth: 32 + layout: !Layout + TINY_ENUM_1: + bits: ["0-3"] + accepts: !Enum + F0: + val: 0 + F1: + val: 1 + + REG_FIELD_BIGGER_THAN_ENUM_2: !Register + adr: 0x21 + bitwidth: 32 + layout: !Layout + TINY_ENUM_2: + bits: ["0-31"] + accepts: !Enum + F0: + val: 0 + F1: + val: 1 + + REG_FIELD_BIGGER_THAN_ENUM_3: !Register + adr: 0x21 + bitwidth: 64 + layout: !Layout + TINY_ENUM_3: + bits: ["0-63"] + accepts: !Enum + F0: + val: 0 + F1: + val: 1 + + REG_FIELD_HUGE_ENUM: !Register + adr: 0x21 + bitwidth: 64 + layout: !Layout + HUGE_ENUM: + bits: ["0-63"] + accepts: !Enum + F0: + val: 0x0 + F1: + val: 0xFFFFFFFFFFFFFFFF + + BLOCK: !RegisterBlock + instances: + BLOCK1: { adr: 0x10 } + BLOCK2: { adr: 0x20 } + + registers: + MEMBER_A: + offset: 0x00 + reset_val: 0x1B + layout: !Layout + VAL: + bits: [7-0] + MEMBER_B: + offset: 0x01 + layout: !Layout + VAL: + bits: [6-0] + +enums: + STAT: + enum: + COOL: + val: 0x1 + NOT_COOL: + val: 0x2 + HOT: + val: 0x3 + +layouts: + BASIC_SHARED_LAYOUT: + bitwidth: 14 + layout: + SHARED_FIELD1: + bits: ["4-1"] + SHARED_FIELD2: + bits: [10] + accepts: !Enum + IS_ONE: + val: 0x1 + IS_ZERO: + val: 0x0