Skip to content

Commit

Permalink
test: add doctests
Browse files Browse the repository at this point in the history
- Add doctest for `ops::count_all_edges::CountAllEdges`.
- Add doctest for `ops::count_all_vertices::CountAllVertices`.
- Add doctest for `ops::edge_weight::EdgeWeight`.
- Add documentation for `ops::count_all_edges`.
- Add documentation for `ops::count_all_vertices`.
- Add documentation for `ops::edge_weight`.
  • Loading branch information
basdirks-purple committed Apr 5, 2024
1 parent 74741f0 commit ba5e0de
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 9 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
- Add `dijkstra::sssp`.
- Add doctest for `AdjacencyMatrix::new`.
- Add doctest for `AdjacencyMatrix::toggle`.
- Add doctest for `CountAllEdges`.
- Add doctest for `CountAllVertices`.
- Add doctest for `EdgeWeight`.
- Add doctest for `Indegree`.
- Add doctest for `IsEdge`.
- Add doctest for `IterAllEdges`.
- Add doctest for `IterAllWeightedEdges`.
Expand All @@ -25,7 +23,13 @@
### Added

- Add doctest for `ops::add_weighted_edge::AddWeightedEdge`.
- Add doctest for `ops::count_all_edges::CountAllEdges`.
- Add doctest for `ops::count_all_vertices::CountAllVertices`.
- Add doctest for `ops::edge_weight::EdgeWeight`.
- 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`.

## [0.5.2] - 2024-04-04

Expand Down
22 changes: 22 additions & 0 deletions src/ops/count_all_edges.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
//! A trait to count all edges in a graph
//!
//! # Examples
//!
//! ```
//! use graaf::ops::CountAllEdges;
//!
//! let graph: Vec<Vec<usize>> = vec![vec![1, 2], vec![0, 2, 3], vec![0, 1, 3], vec![1, 2]];
//!
//! assert_eq!(graph.count_all_edges(), 10);
//! ```
use {
core::hash::BuildHasher,
std::collections::{
Expand All @@ -7,6 +19,16 @@ use {
};

/// A trait to count all edges in a graph
///
/// # Examples
///
/// ```
/// use graaf::ops::CountAllEdges;
///
/// let graph: Vec<Vec<usize>> = vec![vec![1, 2], vec![0, 2, 3], vec![0, 1, 3], vec![1, 2]];
///
/// assert_eq!(graph.count_all_edges(), 10);
/// ```
pub trait CountAllEdges {
/// Counts all edges.
fn count_all_edges(&self) -> usize;
Expand Down
21 changes: 21 additions & 0 deletions src/ops/count_all_vertices.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
//! A trait to count all vertices in a graph
//!
//! # Example
//!
//! ```
//! use graaf::ops::CountAllVertices;
//!
//! let graph: Vec<Vec<usize>> = vec![vec![1, 2], vec![0, 2, 3], vec![0, 1, 3], vec![1, 2]];
//!
//! assert_eq!(graph.count_all_vertices(), 4);
//! ```
use {
core::hash::{
BuildHasher,
Expand All @@ -7,6 +18,16 @@ use {
};

/// A trait to count all vertices in a graph
///
/// # Example
///
/// ```
/// use graaf::ops::CountAllVertices;
///
/// let graph: Vec<Vec<usize>> = vec![vec![1, 2], vec![0, 2, 3], vec![0, 1, 3], vec![1, 2]];
///
/// assert_eq!(graph.count_all_vertices(), 4);
/// ```
pub trait CountAllVertices {
/// Counts all vertices.
fn count_all_vertices(&self) -> usize;
Expand Down
50 changes: 50 additions & 0 deletions src/ops/edge_weight.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,59 @@
//! A trait to get the weight of a given edge
//!
//! # Examples
//!
//! ```
//! use {
//! graaf::ops::EdgeWeight,
//! std::collections::HashMap,
//! };
//!
//! let graph = vec![
//! HashMap::from([(1, 2), (2, 3)]),
//! HashMap::from([(0, 4)]),
//! HashMap::from([(0, 7), (1, 8)]),
//! ];
//!
//! assert_eq!(graph.edge_weight(0, 0), None);
//! assert_eq!(graph.edge_weight(0, 1), Some(&2));
//! assert_eq!(graph.edge_weight(0, 2), Some(&3));
//! assert_eq!(graph.edge_weight(1, 0), Some(&4));
//! assert_eq!(graph.edge_weight(1, 1), None);
//! assert_eq!(graph.edge_weight(2, 0), Some(&7));
//! assert_eq!(graph.edge_weight(2, 1), Some(&8));
//! assert_eq!(graph.edge_weight(2, 2), None);
//! ```
use {
core::hash::BuildHasher,
std::collections::HashMap,
};

/// A trait to get the weight of a given edge
///
/// # Examples
///
/// ```
/// use {
/// graaf::ops::EdgeWeight,
/// std::collections::HashMap,
/// };
///
/// let graph = vec![
/// HashMap::from([(1, 2), (2, 3)]),
/// HashMap::from([(0, 4)]),
/// HashMap::from([(0, 7), (1, 8)]),
/// ];
///
/// assert_eq!(graph.edge_weight(0, 0), None);
/// assert_eq!(graph.edge_weight(0, 1), Some(&2));
/// assert_eq!(graph.edge_weight(0, 2), Some(&3));
/// assert_eq!(graph.edge_weight(1, 0), Some(&4));
/// assert_eq!(graph.edge_weight(1, 1), None);
/// assert_eq!(graph.edge_weight(2, 0), Some(&7));
/// assert_eq!(graph.edge_weight(2, 1), Some(&8));
/// assert_eq!(graph.edge_weight(2, 2), None);
/// ```
pub trait EdgeWeight<W> {
/// Get the weight of the edge from `s` to `t`.
///
Expand Down
6 changes: 0 additions & 6 deletions src/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
pub mod add_edge;
pub mod add_weighted_edge;

/// A trait to count all edges in a graph
pub mod count_all_edges;

/// A trait to count all vertices in a graph
pub mod count_all_vertices;

/// A trait to get the weight of a given edge
pub mod edge_weight;

/// A trait to get the indegree of a given vertex
Expand Down

0 comments on commit ba5e0de

Please sign in to comment.