Skip to content

Commit

Permalink
test: add doctests
Browse files Browse the repository at this point in the history
- Add doctest for `ops::indegree::Indegree`.
- Add doctest for `ops::is_edge::IsEdge`.
- Add doctest for `ops::iter_all_edges::IterAllEdges`.
- Add doctest for `ops::iter_all_weighted_edges::IterAllWeightedEdges`.
- Add doctest for `ops::iter_edges::IterEdges`.
- Add doctest for `ops::iter_weighted_edges::IterWeightedEdges`.
- Add doctest for `ops::iter_vertices::IterVertices`.
- Add doctest for `ops::outdegree::OutDegree`.
- Add doctest for `ops::remove_edge::RemoveEdge`.
- Add documentation for `ops::indegree`.
- Add documentation for `ops::is_edge`.
- Add documentation for `ops::iter_all_edges`.
- Add documentation for `ops::iter_all_weighted_edges`.
- Add documentation for `ops::iter_edges`.
- Add documentation for `ops::iter_vertices`.
- Add documentation for `ops::iter_weighted_edges`.
- Add documentation for `ops::outdegree`.
- Add documentation for `ops::remove_edge`.
  • Loading branch information
basdirks-purple committed Apr 5, 2024
1 parent ba5e0de commit 97591f5
Show file tree
Hide file tree
Showing 13 changed files with 621 additions and 55 deletions.
39 changes: 31 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@
- Add `dfs::*`.
- Add `dijkstra::mssp`.
- Add `dijkstra::sssp`.
- Add implementation documentation for `AddEdge`.
- Add implementation documentation for `AddWeightedEdge`.
- Add implementation documentation for `CountAllEdges`.
- Add implementation documentation for `CountAllVertices`.
- Add implementation documentation for `EdgeWeight`.
- Add implementation documentation for `Indegree`.
- Add implementation documentation for `IsEdge`.
- Add implementation documentation for `IterAllEdges`.
- Add implementation documentation for `IterAllWeightedEdges`.
- Add implementation documentation for `IterEdges`.
- Add implementation documentation for `IterWeightedEdges`.
- Add implementation documentation for `Outdegree`.
- Add implementation documentation for `RemoveEdge`.
- Add doctest for `AdjacencyMatrix::new`.
- Add doctest for `AdjacencyMatrix::toggle`.
- Add doctest for `Indegree`.
- Add doctest for `IsEdge`.
- Add doctest for `IterAllEdges`.
- Add doctest for `IterAllWeightedEdges`.
- Add doctest for `IterEdges`.
- Add doctest for `IterWeightedEdges`.
- Add doctest for `Outdegree`.
- Add doctest for `RemoveEdge`.

## [0.5.3] - Unreleased

Expand All @@ -26,10 +31,28 @@
- Add doctest for `ops::count_all_edges::CountAllEdges`.
- Add doctest for `ops::count_all_vertices::CountAllVertices`.
- Add doctest for `ops::edge_weight::EdgeWeight`.
- Add doctest for `ops::indegree::Indegree`.
- Add doctest for `ops::is_edge::IsEdge`.
- Add doctest for `ops::iter_all_edges::IterAllEdges`.
- Add doctest for `ops::iter_all_weighted_edges::IterAllWeightedEdges`.
- Add doctest for `ops::iter_edges::IterEdges`.
- Add doctest for `ops::iter_weighted_edges::IterWeightedEdges`.
- Add doctest for `ops::iter_vertices::IterVertices`.
- Add doctest for `ops::outdegree::OutDegree`.
- Add doctest for `ops::remove_edge::RemoveEdge`.
- Add documentation for `ops::add_weighted_edge`.
- Add documentation for `ops::count_all_edges`.
- Add documentation for `ops::count_all_vertices`.
- Add documentation for `ops::edge_weight`.
- Add documentation for `ops::indegree`.
- Add documentation for `ops::is_edge`.
- Add documentation for `ops::iter_all_edges`.
- Add documentation for `ops::iter_all_weighted_edges`.
- Add documentation for `ops::iter_edges`.
- Add documentation for `ops::iter_vertices`.
- Add documentation for `ops::iter_weighted_edges`.
- Add documentation for `ops::outdegree`.
- Add documentation for `ops::remove_edge`.

## [0.5.2] - 2024-04-04

Expand Down
34 changes: 18 additions & 16 deletions src/ops/add_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,23 @@ use {

/// A trait to add an edge to an unweighted graph
///
/// # `AddEdge` and [`crate::ops::RemoveEdge`]
/// # Examples
///
/// ```
/// use graaf::ops::AddEdge;
///
/// let mut graph = vec![Vec::new(); 3];
///
/// graph.add_edge(0, 1);
/// graph.add_edge(0, 2);
/// graph.add_edge(2, 0);
///
/// assert_eq!(graph, vec![vec![1, 2], Vec::new(), vec![0]]);
/// ```
///
/// # Properties
///
/// ## `AddEdge` and [`crate::ops::RemoveEdge`]
///
/// Types that also implement [`crate::ops::RemoveEdge`] should ensure that the
/// following property holds for every `graph`, `s`, and `t` of the given types:
Expand All @@ -48,7 +64,7 @@ use {
/// }
/// ```
///
/// # `AddEdge` and [`crate::ops::IsEdge`]
/// ## `AddEdge` and [`crate::ops::IsEdge`]
///
/// Types that also implement [`crate::ops::IsEdge`] should ensure that the
/// following property holds for every `graph`, `s`, and `t` of the given types:
Expand All @@ -75,20 +91,6 @@ pub trait AddEdge {
///
/// * `s`: The source vertex.
/// * `t`: The target vertex.
///
/// # Examples
///
/// ```
/// use graaf::ops::AddEdge;
///
/// let mut graph = vec![Vec::new(); 3];
///
/// graph.add_edge(0, 1);
/// graph.add_edge(0, 2);
/// graph.add_edge(2, 0);
///
/// assert_eq!(graph, vec![vec![1, 2], Vec::new(), vec![0]]);
/// ```
fn add_edge(&mut self, s: usize, t: usize);
}

Expand Down
36 changes: 19 additions & 17 deletions src/ops/add_weighted_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,23 @@ use {

/// A trait to add an edge to a weighted graph
///
/// # `AddWeightedEdge` and [`crate::ops::RemoveEdge`]
/// # Examples
///
/// ```
/// use graaf::ops::AddWeightedEdge;
///
/// let mut graph: Vec<Vec<(usize, i32)>> = vec![Vec::new(); 3];
///
/// graph.add_weighted_edge(0, 1, 2);
/// graph.add_weighted_edge(0, 2, 1);
/// graph.add_weighted_edge(1, 2, -3);
///
/// assert_eq!(graph, vec![vec![(1, 2), (2, 1)], vec![(2, -3)], Vec::new()]);
/// ```
///
/// # Properties
///
/// ## `AddWeightedEdge` and [`crate::ops::RemoveEdge`]
///
/// Types that also implement [`crate::ops::RemoveEdge`] should ensure that the
/// following property holds for every `graph`, `s`, `t`, and `w` of the given
Expand All @@ -52,7 +68,7 @@ use {
/// }
/// ```
///
/// # `AddWeightedEdge` and [`crate::ops::IsEdge`]
/// ## `AddWeightedEdge` and [`crate::ops::IsEdge`]
///
/// Types that also implement [`crate::ops::IsEdge`] should ensure that the
/// following property holds for every `graph`, `s`, `t`, and `w` of the given
Expand All @@ -64,7 +80,7 @@ use {
/// IsEdge,
/// };
///
/// fn prop_add_edge_is_edge<G, W>(graph: &mut G, s: usize, t: usize, w: W) -> bool
/// fn prop_add_edge_weighted_is_edge<G, W>(graph: &mut G, s: usize, t: usize, w: W) -> bool
/// where
/// G: AddWeightedEdge<W> + IsEdge,
/// {
Expand All @@ -81,20 +97,6 @@ pub trait AddWeightedEdge<W> {
/// * `s`: The source vertex.
/// * `t`: The target vertex.
/// * `w`: The weight of the edge.
///
/// # Examples
///
/// ```
/// use graaf::ops::AddWeightedEdge;
///
/// let mut graph: Vec<Vec<(usize, i32)>> = vec![Vec::new(); 3];
///
/// graph.add_weighted_edge(0, 1, 2);
/// graph.add_weighted_edge(0, 2, 1);
/// graph.add_weighted_edge(1, 2, -3);
///
/// assert_eq!(graph, vec![vec![(1, 2), (2, 1)], vec![(2, -3)], Vec::new()]);
/// ```
fn add_weighted_edge(&mut self, s: usize, t: usize, w: W);
}

Expand Down
34 changes: 34 additions & 0 deletions src/ops/indegree.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
//! A trait to get the indegree of a given vertex
//!
//! # Examples
//!
//! ```
//! use {
//! graaf::ops::Indegree,
//! std::collections::HashSet,
//! };
//!
//! let graph: Vec<HashSet<usize>> =
//! vec![HashSet::from([1, 2]), HashSet::from([2]), HashSet::new()];
//!
//! assert_eq!(graph.indegree(0), 0);
//! assert_eq!(graph.indegree(1), 1);
//! assert_eq!(graph.indegree(2), 2);
//! ```
use {
core::hash::BuildHasher,
std::collections::{
Expand All @@ -7,6 +25,22 @@ use {
};

/// A trait to get the indegree of a given vertex
///
/// # Examples
///
/// ```
/// use {
/// graaf::ops::Indegree,
/// std::collections::HashSet,
/// };
///
/// let graph: Vec<HashSet<usize>> =
/// vec![HashSet::from([1, 2]), HashSet::from([2]), HashSet::new()];
///
/// assert_eq!(graph.indegree(0), 0);
/// assert_eq!(graph.indegree(1), 1);
/// assert_eq!(graph.indegree(2), 2);
/// ```
pub trait Indegree {
/// Returns the indegree of a vertex.
///
Expand Down
118 changes: 118 additions & 0 deletions src/ops/is_edge.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
//! A trait to check if an edge exists between two vertices
//!
//! # Examples
//!
//! ```
//! use {
//! graaf::ops::IsEdge,
//! std::collections::HashSet,
//! };
//!
//! let graph = vec![
//! HashSet::from([1, 2]),
//! HashSet::from([0]),
//! HashSet::from([0, 1]),
//! ];
//!
//! assert!(!graph.is_edge(0, 0));
//! assert!(graph.is_edge(0, 1));
//! assert!(graph.is_edge(0, 2));
//! assert!(graph.is_edge(1, 0));
//! assert!(!graph.is_edge(1, 1));
//! assert!(!graph.is_edge(1, 2));
//! assert!(graph.is_edge(2, 0));
//! assert!(graph.is_edge(2, 1));
//! assert!(!graph.is_edge(2, 2));
//! ```
use {
core::hash::BuildHasher,
std::collections::{
Expand All @@ -7,6 +34,97 @@ use {
};

/// A trait to check if an edge exists between two vertices
///
/// # Examples
///
/// ```
/// use {
/// graaf::ops::IsEdge,
/// std::collections::HashSet,
/// };
///
/// let graph = vec![
/// HashSet::from([1, 2]),
/// HashSet::from([0]),
/// HashSet::from([0, 1]),
/// ];
///
/// assert!(!graph.is_edge(0, 0));
/// assert!(graph.is_edge(0, 1));
/// assert!(graph.is_edge(0, 2));
/// assert!(graph.is_edge(1, 0));
/// assert!(!graph.is_edge(1, 1));
/// assert!(!graph.is_edge(1, 2));
/// assert!(graph.is_edge(2, 0));
/// assert!(graph.is_edge(2, 1));
/// assert!(!graph.is_edge(2, 2));
/// ```
///
/// # Properties
///
/// ## `IsEdge` and [`crate::ops::AddEdge`]
///
/// Types that also implement [`crate::ops::AddEdge`] should ensure that the
/// following property holds for every `graph`, `s`, and `t` of the given types:
///
/// ```
/// use graaf::ops::{
/// AddEdge,
/// IsEdge,
/// };
///
/// fn prop_add_edge_is_edge<G, W>(graph: &mut G, s: usize, t: usize) -> bool
/// where
/// G: AddEdge + IsEdge,
/// {
/// graph.add_edge(s, t);
///
/// graph.is_edge(s, t)
/// }
/// ```
///
/// ## `IsEdge` and [`crate::ops::AddWeightedEdge`]
///
/// Types that also implement [`crate::ops::AddWeightedEdge`] should ensure that
/// the following property holds for every `graph`, `s`, `t`, and `w` of the
/// given types:
///
/// ```
/// use graaf::ops::{
/// AddWeightedEdge,
/// IsEdge,
/// };
///
/// fn prop_add_weighted_edge_is_edge<G, W>(graph: &mut G, s: usize, t: usize, w: W) -> bool
/// where
/// G: AddWeightedEdge<W> + IsEdge,
/// {
/// graph.add_weighted_edge(s, t, w);
///
/// graph.is_edge(s, t)
/// }
/// ```
///
/// ## `IsEdge` and [`crate::ops::RemoveEdge`]
///
/// Types that also implement [`crate::ops::RemoveEdge`] should ensure that the
/// following property holds for every `graph`, `s`, and `t` of the given types:
///
/// ```
/// use graaf::ops::{
/// IsEdge,
/// RemoveEdge,
/// };
///
/// fn prop_remove_edge_is_edge<G, W>(graph: &mut G, s: usize, t: usize) -> bool
/// where
/// G: IsEdge + RemoveEdge,
/// {
/// graph.remove_edge(s, t);
///
/// !graph.is_edge(s, t)
/// }
/// ```
pub trait IsEdge {
/// Checks if there is an edge from `s` to `t`.
///
Expand Down
30 changes: 30 additions & 0 deletions src/ops/iter_all_edges.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
//! A trait to iterate over all unweighted edges in a graph
//!
//! # Examples
//!
//! ```
//! use graaf::ops::IterAllEdges;
//!
//! let graph = vec![(0, 1), (1, 2), (2, 0)];
//! let mut iter = graph.iter_all_edges();
//!
//! assert_eq!(iter.next(), Some(&(0, 1)));
//! assert_eq!(iter.next(), Some(&(1, 2)));
//! assert_eq!(iter.next(), Some(&(2, 0)));
//! assert_eq!(iter.next(), None);
//! ```
use {
core::hash::BuildHasher,
std::collections::HashSet,
};

/// A trait to iterate over all unweighted edges in a graph
///
/// # Examples
///
/// ```
/// use graaf::ops::IterAllEdges;
///
/// let graph = vec![(0, 1), (1, 2), (2, 0)];
/// let mut iter = graph.iter_all_edges();
///
/// assert_eq!(iter.next(), Some(&(0, 1)));
/// assert_eq!(iter.next(), Some(&(1, 2)));
/// assert_eq!(iter.next(), Some(&(2, 0)));
/// assert_eq!(iter.next(), None);
/// ```
pub trait IterAllEdges {
/// Returns an iterator that iterates over all edges in a graph.
fn iter_all_edges(&self) -> impl Iterator<Item = &(usize, usize)>;
Expand Down
Loading

0 comments on commit 97591f5

Please sign in to comment.