summaryrefslogtreecommitdiff
path: root/chain/src
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2023-07-18 16:31:05 +0800
committerJSDurand <mmemmew@gmail.com>2023-07-18 16:31:05 +0800
commit0f960b0ecaa30be08d859bf5ca910b7603e66a10 (patch)
tree130db3e40212db376f5d79b1c1563cc7299af130 /chain/src
parent5a40d2cb79a791a46a579fdf6532e1b49053e96c (diff)
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.
Diffstat (limited to 'chain/src')
-rw-r--r--chain/src/item/default/mod.rs98
1 files changed, 98 insertions, 0 deletions
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<dyn std::error::Error>> {
+ 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<ForestLabel<GrammarLabel>> = 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(())
+ }
}