From 3972cb7600a35f3af2bbb0ef4029f394f8d99de8 Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Sun, 26 Jan 2025 17:48:14 +0200 Subject: [PATCH] test(starknet_os): test hint strings are unique --- Cargo.lock | 2 ++ crates/starknet_os/Cargo.toml | 9 +++++++ .../starknet_os/src/hints/enum_definition.rs | 4 ++++ .../src/hints/enum_definition_test.rs | 24 +++++++++++++++++++ .../starknet_os/src/hints/enum_generation.rs | 1 + 5 files changed, 40 insertions(+) create mode 100644 crates/starknet_os/src/hints/enum_definition_test.rs diff --git a/Cargo.lock b/Cargo.lock index a5dda7413dd..d575c5c1727 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11220,6 +11220,8 @@ dependencies = [ "cairo-vm", "indoc 2.0.5", "starknet-types-core", + "strum 0.25.0", + "strum_macros 0.25.3", "thiserror 1.0.69", ] diff --git a/crates/starknet_os/Cargo.toml b/crates/starknet_os/Cargo.toml index 93112aea041..839828d83f1 100644 --- a/crates/starknet_os/Cargo.toml +++ b/crates/starknet_os/Cargo.toml @@ -7,11 +7,20 @@ license.workspace = true license-file.workspace = true description = "The Starknet OS." +[features] +testing = ["dep:strum", "dep:strum_macros"] + [dependencies] cairo-vm.workspace = true indoc.workspace = true starknet-types-core.workspace = true +strum = { workspace = true, optional = true } +strum_macros = { workspace = true, optional = true } thiserror.workspace = true +[dev-dependencies] +strum.workspace = true +strum_macros.workspace = true + [lints] workspace = true diff --git a/crates/starknet_os/src/hints/enum_definition.rs b/crates/starknet_os/src/hints/enum_definition.rs index c54a9b3a938..1881abf8c72 100644 --- a/crates/starknet_os/src/hints/enum_definition.rs +++ b/crates/starknet_os/src/hints/enum_definition.rs @@ -210,6 +210,10 @@ use crate::hints::transaction_hash::{additional_data_new_segment, data_to_hash_n use crate::hints::types::{HintEnum, HintExtensionImplementation, HintImplementation}; use crate::{define_hint_enum, define_hint_extension_enum}; +#[cfg(test)] +#[path = "enum_definition_test.rs"] +pub mod test; + define_hint_enum!( Hint, ( diff --git a/crates/starknet_os/src/hints/enum_definition_test.rs b/crates/starknet_os/src/hints/enum_definition_test.rs new file mode 100644 index 00000000000..caa45dac768 --- /dev/null +++ b/crates/starknet_os/src/hints/enum_definition_test.rs @@ -0,0 +1,24 @@ +use std::collections::HashSet; + +use strum::IntoEnumIterator; + +use super::{Hint, HintExtension}; +use crate::hints::types::HintEnum; + +#[test] +fn test_hint_strings_are_unique() { + let hint_strings = Hint::iter().map(|hint| hint.to_str()).collect::>(); + let hint_extension_strings = + HintExtension::iter().map(|hint| hint.to_str()).collect::>(); + let hint_strings_set: HashSet<&&str> = HashSet::from_iter(hint_strings.iter()); + let hint_extension_strings_set = HashSet::from_iter(hint_extension_strings.iter()); + assert_eq!(hint_strings.len(), hint_strings_set.len(), "Duplicate hint strings."); + assert_eq!( + hint_extension_strings.len(), + hint_extension_strings_set.len(), + "Duplicate hint extension strings." + ); + let ambiguous_strings = + hint_strings_set.intersection(&hint_extension_strings_set).collect::>(); + assert!(ambiguous_strings.is_empty(), "Ambiguous hint strings: {ambiguous_strings:?}"); +} diff --git a/crates/starknet_os/src/hints/enum_generation.rs b/crates/starknet_os/src/hints/enum_generation.rs index 262d022d92b..783e26a3587 100644 --- a/crates/starknet_os/src/hints/enum_generation.rs +++ b/crates/starknet_os/src/hints/enum_generation.rs @@ -1,6 +1,7 @@ #[macro_export] macro_rules! define_hint_enum_base { ($enum_name:ident, $(($hint_name:ident, $hint_str:expr)),+ $(,)?) => { + #[cfg_attr(any(test, feature = "testing"), derive(strum_macros::EnumIter))] pub enum $enum_name { $($hint_name),+ }