algorithm_playground/
lib.rs

1/// Declare the algorithms module, which contains various searching algorithms.
2pub mod algorithms {
3    /// Import individual modules from the algorithms directory.
4    pub mod searching;
5    pub mod sorting;
6}
7
8/// Re-export the public functions from the searching module.
9pub use crate::algorithms::searching::searching::{linear_search, binary_search, Graph, TreeNode};
10pub use crate::algorithms::sorting::sorting::{merge_sort, heap_sort, quick_sort, insertion_sort, selection_sort, bubble_sort};
11
12/// Module containing unit tests for the searching algorithms.
13#[cfg(test)]
14mod search_tests {
15    use super::*;
16    use super::Graph;
17    use super::TreeNode;
18
19    /// Unit tests for the linear_search function.
20    #[test]
21    fn test_linear_search() {
22        // Test with integers
23        let arr_int = vec![1, 2, 3, 4, 5];
24        assert_eq!(linear_search(&arr_int, &3), Some(2));
25    
26        // Test with strings
27        let arr_str = vec!["apple", "banana", "cherry", "date"];
28        assert_eq!(linear_search(&arr_str, &"cherry"), Some(2));
29
30        // Test with characters
31        let arr_char = vec!['a', 'b', 'c', 'd', 'e'];
32        assert_eq!(linear_search(&arr_char, &'c'), Some(2));
33
34        // Test with custom struct
35        #[derive(Debug, PartialEq)]
36        struct Point {
37            x: i32,
38            y: i32,
39        }
40        
41        let arr_custom = vec![
42            Point { x: 1, y: 1 },
43            Point { x: 2, y: 2 },
44            Point { x: 3, y: 3 },
45        ];
46        assert_eq!(linear_search(&arr_custom, &Point { x: 3, y: 3 }), Some(2));
47    }
48    
49    /// Unit tests for the binary_search function.
50    #[test]
51    fn test_binary_search() {
52        // Test with integers
53        let arr_int = vec![1, 2, 3, 4, 5];
54        assert_eq!(binary_search(&arr_int, &3), Some(2));
55    
56        // Test with strings
57        let arr_str = vec!["apple", "banana", "cherry", "date"];
58        assert_eq!(binary_search(&arr_str, &"cherry"), Some(2));
59
60        // Test with characters
61        let arr_char = vec!['a', 'b', 'c', 'd', 'e'];
62        assert_eq!(binary_search(&arr_char, &'c'), Some(2)); 
63
64        // Test with custom struct
65        #[derive(Debug, PartialEq, Eq, Ord, PartialOrd)]
66        struct Point {
67            x: i32,
68            y: i32,
69        }
70        
71        let arr_custom = vec![
72            Point { x: 1, y: 1 },
73            Point { x: 2, y: 2 },
74            Point { x: 3, y: 3 },
75        ];
76        assert_eq!(binary_search(&arr_custom, &Point { x: 3, y: 3 }), Some(2));
77    } 
78
79    #[test]
80    fn test_graph_dfs_and_bfs() {
81        // Test Graph with usize
82        let mut graph_usize = Graph::new(5);
83        graph_usize.add_edge(0, 1);
84        graph_usize.add_edge(0, 2);
85        graph_usize.add_edge(1, 3);
86        graph_usize.add_edge(2, 4);
87        assert!(graph_usize.dfs(0, 4));
88        assert!(graph_usize.bfs(0, 4));
89
90        // Test Graph with char
91        let mut graph_char = Graph::new(5);
92        graph_char.add_edge(0, 1);
93        graph_char.add_edge(0, 2);
94        graph_char.add_edge(1, 3);
95        graph_char.add_edge(2, 4);
96        assert!(graph_char.dfs(0, 4));
97        assert!(graph_char.bfs(0, 4));
98    }
99
100    #[test]
101    fn test_tree_contains() {
102        // Test TreeNode with usize
103        let mut root_usize = TreeNode::new(5);
104        let left_child_usize = TreeNode::new(3);
105        let right_child_usize = TreeNode::new(7);
106        root_usize.left = Some(Box::new(left_child_usize));
107        root_usize.right = Some(Box::new(right_child_usize));
108        assert!(root_usize.contains(&3));
109        assert!(!root_usize.contains(&4));
110
111        // Test TreeNode with char
112        let mut root_char = TreeNode::new('c');
113        let left_child_char = TreeNode::new('b');
114        let right_child_char = TreeNode::new('d');
115        root_char.left = Some(Box::new(left_child_char));
116        root_char.right = Some(Box::new(right_child_char));
117        assert!(root_char.contains(&'b'));
118        assert!(!root_char.contains(&'a'));
119    }
120}
121
122#[cfg(test)]
123mod sort_tests {
124    use super::*;
125
126    #[test]
127    fn test_merge_sort() {
128        let mut arr = [3, 2, 1];
129        merge_sort(&mut arr);
130        assert_eq!(arr, [1, 2, 3]);
131
132        let mut arr = ['c', 'b', 'a'];
133        merge_sort(&mut arr);
134        assert_eq!(arr, ['a', 'b', 'c']);
135    }
136
137    #[test]
138    fn test_heap_sort() {
139        let mut arr = [3, 2, 1];
140        heap_sort(&mut arr);
141        assert_eq!(arr, [1, 2, 3]);
142
143        let mut arr = ['c', 'b', 'a'];
144        heap_sort(&mut arr);
145        assert_eq!(arr, ['a', 'b', 'c']); 
146    }
147
148    #[test]
149    fn test_quick_sort() {
150        let mut arr = [3, 2, 1];
151        quick_sort(&mut arr);
152        assert_eq!(arr, [1, 2, 3]);
153
154        let mut arr = ['c', 'b', 'a'];
155        quick_sort(&mut arr);
156        assert_eq!(arr, ['a', 'b', 'c']);
157    }
158
159    #[test]
160    fn test_insertion_sort() {
161        let mut arr = [3, 2, 1];
162        insertion_sort(&mut arr);
163        assert_eq!(arr, [1, 2, 3]);
164
165        let mut arr = ['c', 'b', 'a'];
166        insertion_sort(&mut arr);
167        assert_eq!(arr, ['a', 'b', 'c']);
168    }
169
170    #[test]
171    fn test_selection_sort() {
172        let mut arr = [3, 2, 1];
173        selection_sort(&mut arr);
174        assert_eq!(arr, [1, 2, 3]);
175
176        let mut arr = ['c', 'b', 'a'];
177        selection_sort(&mut arr);
178        assert_eq!(arr, ['a', 'b', 'c']);
179    }
180
181    #[test]
182    fn test_bubble_sort() {
183        let mut arr = [3, 2, 1];
184        bubble_sort(&mut arr);
185        assert_eq!(arr, [1, 2, 3]);
186
187        let mut arr = ['c', 'b', 'a'];
188        bubble_sort(&mut arr);
189        assert_eq!(arr, ['a', 'b', 'c']);
190    }
191}