From 662817e6367a865a2d86a99581172cc45f585807 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Thu, 4 May 2023 13:02:39 +0800 Subject: Completed the procedural macro for deriving Graphs. The macro `graph_derive` can automatically write the boiler-plate codes for wrapper types one of whose sub-fields implements the `Graph` trait. The generated implementation will delegate the `Graph` operations to the sub-field which implements the `Graph` trait. I plan to add more macros, corresponding to various other graph-related traits, so that no such boiler-plate codes are needed, at least for my use-cases. --- graph_macro/tests/works.rs | 95 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 90 insertions(+), 5 deletions(-) (limited to 'graph_macro/tests') diff --git a/graph_macro/tests/works.rs b/graph_macro/tests/works.rs index fe8a853..a57e866 100644 --- a/graph_macro/tests/works.rs +++ b/graph_macro/tests/works.rs @@ -1,13 +1,98 @@ #![allow(dead_code)] -use graph_macro::Testing; +use graph_macro::Graph; -#[derive(Debug, Testing)] -struct Haha { - size: usize, +#[derive(Debug, Graph, Default)] +pub(crate) struct Haha { + test: String, + #[graph] + little_graph: graph::ALGraph, +} + +#[derive(Debug)] +/// Testing docs +#[allow(unused)] +#[derive(Default, Graph)] +pub struct HahaU(pub(crate) graph::ALGraph); + +#[derive(Debug, Graph)] +pub struct HahaG { + graph: graph::DLGraph, +} + +impl Default for HahaG { + fn default() -> Self { + Self { + graph: Default::default(), + } + } +} + +#[derive(Debug, Graph)] +pub struct HahaW +where + T: graph::GraphLabel, +{ + name: String, + #[graph] + graph: graph::DLGraph, +} + +impl Default for HahaW { + fn default() -> Self { + Self { + name: Default::default(), + graph: Default::default(), + } + } +} + +impl Haha { + fn new(test: impl ToString) -> Self { + use graph::builder::Builder; + + let test = test.to_string(); + let mut little_graph_builder: graph::adlist::ALGBuilder = Default::default(); + + little_graph_builder.add_vertex(); + little_graph_builder.add_vertex(); + little_graph_builder.add_edge(0, 1, ()).unwrap(); + + let little_graph = little_graph_builder.build(); + + Self { little_graph, test } + } } #[test] fn it_works() { - println!("it works: {:?}", Haha { size: 0 }); + use graph::{builder::Builder, Graph}; + let haha = Haha::new("test"); + + assert!(!haha.is_empty()); + + assert_eq!(haha.has_edge(0, 1), Ok(true)); + + let hahaw = HahaW::::default(); + + assert!(hahaw.is_empty()); + + assert!(matches!( + hahaw.has_edge(0, 1), + Err(graph::error::Error::IndexOutOfBounds(0, 0)) + )); + + let mut builder: graph::adlist::ALGBuilder = Default::default(); + + let first = builder.add_vertex(); + let second = builder.add_vertex(); + builder.add_edge(first, second, ()).unwrap(); + + let hahau = HahaU(builder.build()); + + assert!(!hahau.is_empty()); + + assert_eq!(hahau.has_edge(0, 1), Ok(true)); + + dbg!(haha, hahaw, hahau); } -- cgit v1.2.3-18-g5258