Skip to content

Commit 842c4ab

Browse files
Panquesito7github-actions[bot]
andauthored
docs: add self-test examples (#2452)
* docs: add self-test examples * updating DIRECTORY.md --------- Co-authored-by: github-actions[bot] <[email protected]>
1 parent 29f5439 commit 842c4ab

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

CONTRIBUTING.md

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ You can add new algorithms or data structures that are **not present in the repo
3333
- Make sure the file extensions are `*.hpp`, `*.h` or `*.cpp`.
3434
- Don't use **`bits/stdc++.h`** because this is quite Linux-specific and slows down the compilation process.
3535
- Organize your code using **`struct`**, **`class`**, and/or **`namespace`** keywords.
36-
- If an implementation of the algorithm already exists, please refer to the [file-name section below](#new-file-name-guidelines).
36+
- If an implementation of the algorithm already exists, please refer to the [file-name section below](#file-name-guidelines).
3737
- You can suggest reasonable changes to existing algorithms.
3838
- Strictly use snake_case (underscore_separated) in filenames.
3939
- If you have added or modified code, please make sure the code compiles before submitting.
@@ -54,13 +54,85 @@ You can add new algorithms or data structures that are **not present in the repo
5454
- Make sure to add examples and test cases in your `main()` function.
5555
- If you find an algorithm or document without tests, please feel free to create a pull request or issue describing suggested changes.
5656
- Please try to add one or more `test()` functions that will invoke the algorithm implementation on random test data with the expected output. Use the `assert()` function to confirm that the tests will pass. Requires including the `cassert` library.
57+
- Test cases should fully verify that your program works as expected. Rather than asking the user for input, it's best to make sure the given output is the correct output.
58+
59+
##### Self-test examples
60+
61+
1. [Quick sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/quick_sort.cpp#L137) testing (complex).
62+
63+
```cpp
64+
// Let's make sure the array of numbers is ordered after calling the function.
65+
std::vector<uint64_t> arr = {5, 3, 8, 12, 14, 16, 28, 96, 2, 5977};
66+
std::vector<uint64_t> arr_sorted = sorting::quick_sort::quick_sort(
67+
arr, 0, int(std::end(arr) - std::begin(arr)) - 1);
68+
69+
assert(std::is_sorted(std::begin(arr_sorted), std::end(arr_sorted)));
70+
```
71+
72+
2. [Subset Sum](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/subset_sum.cpp#L58) testing (medium).
73+
74+
```cpp
75+
std::vector<int32_t> array1 = {-7, -3, -2, 5, 8}; // input array
76+
assert(backtracking::subset_sum::number_of_subsets(0, array1) ==
77+
2); // first argument in subset_sum function is the required sum and
78+
// second is the input array
79+
```
80+
81+
3. Small C++ program that showcases and explains the use of tests.
82+
83+
```cpp
84+
#include <vector> /// for std::vector
85+
#include <cassert> /// for assert
86+
87+
/**
88+
* @brief Verifies if the given array
89+
* contains the given number on it.
90+
* @tparam T the type of array (e.g., `int`, `float`, etc.)
91+
* @param arr the array to be used for checking
92+
* @param number the number to check if it's inside the array
93+
* @return false if the number was NOT found in the array
94+
* @return true if the number WAS found in the array
95+
*/
96+
template <typename T>
97+
bool is_number_on_array(const std::vector<T> &arr, const int &number) {
98+
for (int i = 0; i < sizeof(arr) / sizeof(int); i++) {
99+
if (arr[i] == number) {
100+
return true;
101+
}
102+
else {
103+
return false;
104+
}
105+
}
106+
107+
return false;
108+
}
109+
110+
/**
111+
* @brief Self-test implementations
112+
* @returns void
113+
*/
114+
static void tests() {
115+
std::vector<int> arr = { 9, 14, 21, 98, 67 };
116+
117+
assert(is_number_on_array(arr, 9) == true);
118+
assert(is_number_on_array(arr, 4) == false);
119+
}
120+
121+
/**
122+
* @brief Main function
123+
* @returns 0 on exit
124+
*/
125+
int main() {
126+
tests(); // run self-test implementations
127+
}
128+
```
57129
58130
#### Typical structure of a program
59131
60132
```cpp
61133
/**
62134
* @file
63-
* @brief Add one line description here. Should contain a Wikipedia
135+
* @brief Add one-line description here. Should contain a Wikipedia
64136
* link or another source explaining the algorithm/implementation.
65137
* @details
66138
* This is a multi-line
@@ -74,7 +146,7 @@ You can add new algorithms or data structures that are **not present in the repo
74146
#include /// for `some function here`
75147
76148
/**
77-
* @namespace <folder name>
149+
* @namespace
78150
* @brief <namespace description>
79151
*/
80152
namespace name {

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@
374374
* [Shell Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/shell_sort.cpp)
375375
* [Shell Sort2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/shell_sort2.cpp)
376376
* [Slow Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/slow_sort.cpp)
377+
* [Stooge Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/stooge_sort.cpp)
377378
* [Strand Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/strand_sort.cpp)
378379
* [Swap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/swap_sort.cpp)
379380
* [Tim Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/tim_sort.cpp)

0 commit comments

Comments
 (0)