From 0f960b0ecaa30be08d859bf5ca910b7603e66a10 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Tue, 18 Jul 2023 16:31:05 +0800 Subject: chain/src/item/default/mod.rs: Add a unit test for `set_pos` The function `set_pos` is kind of subtle and its behaviour needs a unit test so that we can be sure that it does not accidentally set the ending positions in a careless manner. --- chain/src/item/default/mod.rs | 98 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) (limited to 'chain/src/item/default') diff --git a/chain/src/item/default/mod.rs b/chain/src/item/default/mod.rs index 5971985..e0e2296 100644 --- a/chain/src/item/default/mod.rs +++ b/chain/src/item/default/mod.rs @@ -1584,4 +1584,102 @@ mod item_test { Ok(()) } + + #[test] + fn test_set_pos() -> Result<(), Box> { + let grammar_file = std::fs::read_to_string( + "/Users/durand/Desktop/Centre/A propos de programmes\ + /Rust/rep/grammar/abnf grammars/test.abnf", + )?; + + let grammar: grammar::Grammar = grammar_file.parse()?; + + let atom = DefaultAtom::from_grammar(grammar)?; + + // atom.print_nullables(); + + let mut forest: DefaultForest> = DefaultForest::new_leaf_raw( + ForestLabel::new(GrammarLabel::new(TNT::Non(0), 0), ForestLabelType::Plain), + ); + + forest.plant( + 0, + DefaultForest::new_leaf_raw(ForestLabel::new( + GrammarLabel::new(1, 0), + ForestLabelType::Plain, + )), + false, + )?; + + forest.plant( + 1, + DefaultForest::new_leaf_raw(ForestLabel::new( + GrammarLabel::new(TNT::Non(1), 0), + ForestLabelType::Plain, + )), + false, + )?; + + forest.plant( + 2, + DefaultForest::new_leaf_raw(ForestLabel::new( + GrammarLabel::new(104, 0), + ForestLabelType::Plain, + )), + false, + )?; + + forest.plant( + 3, + DefaultForest::new_leaf_raw(ForestLabel::new( + GrammarLabel::new(TNT::Non(2), 0), + ForestLabelType::Plain, + )), + false, + )?; + + forest.plant( + 4, + DefaultForest::new_leaf_raw(ForestLabel::new( + GrammarLabel::new(2, 0), + ForestLabelType::Plain, + )), + false, + )?; + + forest.plant( + 5, + DefaultForest::new_leaf_raw(ForestLabel::new( + GrammarLabel::new(TNT::Ter(0), 0), + ForestLabelType::Plain, + )), + false, + )?; + + // forest.print_viz("test.gv")?; + + forest.set_pos(&atom, 1, true)?; + + // forest.print_viz("test set.gv")?; + + for node in 0..=6 { + let label = forest.vertex_label(node)?.expect("node no label?"); + + assert_eq!(label.label().start(), 1); + } + + for node in 0..3 { + let label = forest.vertex_label(node)?.expect("node no label?"); + + assert!(label.label().end().is_none()); + } + + for node in 3..=6 { + let label = forest.vertex_label(node)?.expect("node no label?"); + + assert_eq!(label.label().end(), Some(2)); + } + + Ok(()) + } } -- cgit v1.2.3-18-g5258