From 260589cd7fe962b2b2b3e78aca182e626b0c73f8 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Thu, 12 Jan 2023 15:53:33 -0600 Subject: [PATCH 1/2] pass clippy --- packages/core/tests/fuzzing.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/core/tests/fuzzing.rs b/packages/core/tests/fuzzing.rs index 949a0b941..be8505413 100644 --- a/packages/core/tests/fuzzing.rs +++ b/packages/core/tests/fuzzing.rs @@ -83,7 +83,7 @@ fn create_random_template_node( fn generate_paths( node: &TemplateNode<'static>, - current_path: &Vec, + current_path: &[u8], node_paths: &mut Vec>, attr_paths: &mut Vec>, ) { @@ -93,22 +93,22 @@ fn generate_paths( match attr { TemplateAttribute::Static { .. } => {} TemplateAttribute::Dynamic { .. } => { - attr_paths.push(current_path.clone()); + attr_paths.push(current_path.to_vec()); } } } for (i, child) in children.iter().enumerate() { - let mut current_path = current_path.clone(); + let mut current_path = current_path.to_vec(); current_path.push(i as u8); generate_paths(child, ¤t_path, node_paths, attr_paths); } } TemplateNode::Text { .. } => {} TemplateNode::DynamicText { .. } => { - node_paths.push(current_path.clone()); + node_paths.push(current_path.to_vec()); } TemplateNode::Dynamic { .. } => { - node_paths.push(current_path.clone()); + node_paths.push(current_path.to_vec()); } } } @@ -119,12 +119,12 @@ fn create_random_template(name: &'static str) -> Template<'static> { let roots = (0..(1 + rand::random::() % 5)) .map(|_| create_random_template_node(&mut template_idx, &mut attr_idx, 0)) .collect::>(); - assert!(roots.len() > 0); + assert!(!roots.is_empty()); let roots = Box::leak(roots.into_boxed_slice()); let mut node_paths = Vec::new(); let mut attr_paths = Vec::new(); for (i, root) in roots.iter().enumerate() { - generate_paths(root, &vec![i as u8], &mut node_paths, &mut attr_paths); + generate_paths(root, &[i as u8], &mut node_paths, &mut attr_paths); } let node_paths = Box::leak( node_paths @@ -143,7 +143,7 @@ fn create_random_template(name: &'static str) -> Template<'static> { Template { name, roots, node_paths, attr_paths } } -fn create_random_dynamic_node<'a>(cx: &'a ScopeState, depth: usize) -> DynamicNode<'a> { +fn create_random_dynamic_node(cx: &ScopeState, depth: usize) -> DynamicNode { let range = if depth > 10 { 2 } else { 4 }; match rand::random::() % range { 0 => DynamicNode::Text(VText { @@ -177,7 +177,7 @@ fn create_random_dynamic_node<'a>(cx: &'a ScopeState, depth: usize) -> DynamicNo } } -fn create_random_dynamic_attr<'a>(cx: &'a ScopeState) -> Attribute<'a> { +fn create_random_dynamic_attr(cx: &ScopeState) -> Attribute { let value = match rand::random::() % 6 { 0 => AttributeValue::Text(Box::leak( format!("{}", rand::random::()).into_boxed_str(), @@ -207,7 +207,7 @@ struct DepthProps { root: bool, } -fn create_random_element<'a>(cx: Scope<'a, DepthProps>) -> Element<'a> { +fn create_random_element(cx: Scope) -> Element { cx.needs_update(); let range = if cx.props.root { 2 } else { 3 }; let node = match rand::random::() % range { @@ -226,7 +226,7 @@ fn create_random_element<'a>(cx: Scope<'a, DepthProps>) -> Element<'a> { ) .into_boxed_str(), )); - println!("{:#?}", template); + println!("{template:#?}"); let node = VNode { key: None, parent: None, @@ -248,7 +248,7 @@ fn create_random_element<'a>(cx: Scope<'a, DepthProps>) -> Element<'a> { } _ => None, }; - println!("{:#?}", node); + println!("{node:#?}"); node } From a01a65a0ebe2a78fa0215d605df3c13760701c31 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Thu, 12 Jan 2023 16:34:46 -0600 Subject: [PATCH 2/2] seperate text and other dynamic nodes --- packages/core/tests/fuzzing.rs | 64 ++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/packages/core/tests/fuzzing.rs b/packages/core/tests/fuzzing.rs index be8505413..75dd84c33 100644 --- a/packages/core/tests/fuzzing.rs +++ b/packages/core/tests/fuzzing.rs @@ -32,6 +32,7 @@ fn create_random_attribute(attr_idx: &mut usize) -> TemplateAttribute<'static> { } fn create_random_template_node( + dynamic_node_types: &mut Vec, template_idx: &mut usize, attr_idx: &mut usize, depth: usize, @@ -49,11 +50,18 @@ fn create_random_template_node( namespace: random_ns(), attrs, children: { - if depth > 10 { + if depth > 4 { &[] } else { let children: Vec<_> = (0..(rand::random::() % 3)) - .map(|_| create_random_template_node(template_idx, attr_idx, depth + 1)) + .map(|_| { + create_random_template_node( + dynamic_node_types, + template_idx, + attr_idx, + depth + 1, + ) + }) .collect(); Box::leak(children.into_boxed_slice()) } @@ -67,6 +75,7 @@ fn create_random_template_node( id: { let old_idx = *template_idx; *template_idx += 1; + dynamic_node_types.push(DynamicNodeType::Text); old_idx }, }, @@ -74,6 +83,7 @@ fn create_random_template_node( id: { let old_idx = *template_idx; *template_idx += 1; + dynamic_node_types.push(DynamicNodeType::Other); old_idx }, }, @@ -113,11 +123,19 @@ fn generate_paths( } } -fn create_random_template(name: &'static str) -> Template<'static> { +enum DynamicNodeType { + Text, + Other, +} + +fn create_random_template(name: &'static str) -> (Template<'static>, Vec) { + let mut dynamic_node_type = Vec::new(); let mut template_idx = 0; let mut attr_idx = 0; let roots = (0..(1 + rand::random::() % 5)) - .map(|_| create_random_template_node(&mut template_idx, &mut attr_idx, 0)) + .map(|_| { + create_random_template_node(&mut dynamic_node_type, &mut template_idx, &mut attr_idx, 0) + }) .collect::>(); assert!(!roots.is_empty()); let roots = Box::leak(roots.into_boxed_slice()); @@ -140,18 +158,17 @@ fn create_random_template(name: &'static str) -> Template<'static> { .collect::>() .into_boxed_slice(), ); - Template { name, roots, node_paths, attr_paths } + ( + Template { name, roots, node_paths, attr_paths }, + dynamic_node_type, + ) } fn create_random_dynamic_node(cx: &ScopeState, depth: usize) -> DynamicNode { - let range = if depth > 10 { 2 } else { 4 }; + let range = if depth > 3 { 1 } else { 3 }; match rand::random::() % range { - 0 => DynamicNode::Text(VText { - value: Box::leak(format!("{}", rand::random::()).into_boxed_str()), - id: Default::default(), - }), - 1 => DynamicNode::Placeholder(Default::default()), - 2 => cx.make_node((0..(rand::random::() % 5)).map(|_| VNode { + 0 => DynamicNode::Placeholder(Default::default()), + 1 => cx.make_node((0..(rand::random::() % 5)).map(|_| VNode { key: None, parent: Default::default(), template: Cell::new(Template { @@ -168,7 +185,7 @@ fn create_random_dynamic_node(cx: &ScopeState, depth: usize) -> DynamicNode { )]), dynamic_attrs: &[], })), - 3 => cx.component( + 2 => cx.component( create_random_element, DepthProps { depth, root: false }, "create_random_element", @@ -212,7 +229,7 @@ fn create_random_element(cx: Scope) -> Element { let range = if cx.props.root { 2 } else { 3 }; let node = match rand::random::() % range { 0 | 1 => { - let template = create_random_template(Box::leak( + let (template, dynamic_node_types) = create_random_template(Box::leak( format!( "{}{}", concat!(file!(), ":", line!(), ":", column!(), ":"), @@ -233,8 +250,19 @@ fn create_random_element(cx: Scope) -> Element { template: Cell::new(template), root_ids: Default::default(), dynamic_nodes: { - let dynamic_nodes: Vec<_> = (0..template.node_paths.len()) - .map(|_| create_random_dynamic_node(cx, cx.props.depth + 1)) + let dynamic_nodes: Vec<_> = dynamic_node_types + .iter() + .map(|ty| match ty { + DynamicNodeType::Text => DynamicNode::Text(VText { + value: Box::leak( + format!("{}", rand::random::()).into_boxed_str(), + ), + id: Default::default(), + }), + DynamicNodeType::Other => { + create_random_dynamic_node(cx, cx.props.depth + 1) + } + }) .collect(); cx.bump().alloc(dynamic_nodes) }, @@ -255,7 +283,7 @@ fn create_random_element(cx: Scope) -> Element { // test for panics when creating random nodes and templates #[test] fn create() { - for _ in 0..1000 { + for _ in 0..100 { let mut vdom = VirtualDom::new_with_props(create_random_element, DepthProps { depth: 0, root: true }); let _ = vdom.rebuild(); @@ -266,7 +294,7 @@ fn create() { // This test will change the template every render which is not very realistic, but it helps stress the system #[test] fn diff() { - for _ in 0..100 { + for _ in 0..10 { let mut vdom = VirtualDom::new_with_props(create_random_element, DepthProps { depth: 0, root: true }); let _ = vdom.rebuild();