Skip to content

Commit d07ab7d

Browse files
Panquesito7github-actions[bot]
and
github-actions[bot]
authored
docs: add self-test examples (#1250)
* docs: add self-test examples * updating DIRECTORY.md --------- Co-authored-by: github-actions[bot] <[email protected]>
1 parent 3ba43b7 commit d07ab7d

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

CONTRIBUTING.md

+96
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,102 @@ For LeetCode solutions, please check its [**guide**](https://github.com/TheAlgor
5757
- Make sure to add examples and test cases in your `main()` function.
5858
- If you find an algorithm or document without tests, please feel free to create a pull request or issue describing suggested changes.
5959
- 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 `assert.h` library.
60+
- 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.
61+
62+
##### Self-test examples
63+
64+
1. [ROT13 Cipher](https://github.com/TheAlgorithms/C/blob/master/cipher/rot13.c) (complex).
65+
66+
```c
67+
// NOTE: the `rot13` function is defined in another part of the code.
68+
69+
char test_01[] = "The more I C, the less I see.";
70+
rot13(test_01);
71+
assert(strcmp(test_01, "Gur zber V P, gur yrff V frr.") == 0);
72+
73+
char test_02[] = "Which witch switched the Swiss wristwatches?";
74+
rot13(test_02);
75+
assert(strcmp(test_02, "Juvpu jvgpu fjvgpurq gur Fjvff jevfgjngpurf?") == 0);
76+
77+
char test_03[] = "Juvpu jvgpu fjvgpurq gur Fjvff jevfgjngpurf?";
78+
rot13(test_03);
79+
assert(strcmp(test_03, "Which witch switched the Swiss wristwatches?") == 0);
80+
```
81+
82+
2. [Sudoku Solver](https://github.com/TheAlgorithms/C/blob/master/misc/sudoku_solver.c) (medium).
83+
84+
```c
85+
uint8_t test_array[] = {3, 0, 6, 5, 0, 8, 4, 0, 0, 5, 2, 0, 0, 0, 0, 0, 0,
86+
0, 0, 8, 7, 0, 0, 0, 0, 3, 1, 0, 0, 3, 0, 1, 0, 0,
87+
8, 0, 9, 0, 0, 8, 6, 3, 0, 0, 5, 0, 5, 0, 0, 9, 0,
88+
6, 0, 0, 1, 3, 0, 0, 0, 0, 2, 5, 0, 0, 0, 0, 0, 0,
89+
0, 0, 7, 4, 0, 0, 5, 2, 0, 6, 3, 0, 0};
90+
struct sudoku a = {.N = 9, .N2 = 3, .a = test_array};
91+
assert(solve(&a)); // ensure that solution is obtained
92+
// NOTE: `solve` is defined in another part of the code.
93+
94+
uint8_t expected[] = {3, 1, 6, 5, 7, 8, 4, 9, 2, 5, 2, 9, 1, 3, 4, 7, 6,
95+
8, 4, 8, 7, 6, 2, 9, 5, 3, 1, 2, 6, 3, 4, 1, 5, 9,
96+
8, 7, 9, 7, 4, 8, 6, 3, 1, 2, 5, 8, 5, 1, 7, 9, 2,
97+
6, 4, 3, 1, 3, 8, 9, 4, 7, 2, 5, 6, 6, 9, 2, 3, 5,
98+
1, 8, 7, 4, 7, 4, 5, 2, 8, 6, 3, 1, 9};
99+
for (int i = 0; i < a.N; i++)
100+
for (int j = 0; j < a.N; j++)
101+
assert(a.a[i * a.N + j] == expected[i * a.N + j]);
102+
```
103+
104+
3. Small C program that showcases and explains the use of tests.
105+
106+
```c
107+
#include <stdio.h> /// for IO operations
108+
#include <assert.h> /// for assert
109+
#include <stdbool.h> /// for bool
110+
111+
/**
112+
* @brief Verifies if the given array
113+
* contains the given number on it.
114+
* @param arr the array to be used for checking
115+
* @param number the number to check if it's inside the array
116+
* @return false if the number was NOT found in the array
117+
* @return true if the number WAS found in the array
118+
*/
119+
bool is_number_on_array(const int *arr, const int number) {
120+
for (int i = 0; i < sizeof(arr); i++) {
121+
if (arr[i] == number) {
122+
return true;
123+
}
124+
else {
125+
// Number not in the current index, keep searching.
126+
}
127+
}
128+
129+
return false;
130+
}
131+
132+
/**
133+
* @brief Self-test implementations
134+
* @returns void
135+
*/
136+
static void tests() {
137+
int arr[] = { 9, 14, 21, 98, 67 };
138+
139+
assert(is_number_on_array(arr, 9) == true);
140+
assert(is_number_on_array(arr, 4) == false);
141+
assert(is_number_on_array(arr, 98) == true);
142+
assert(is_number_on_array(arr, 512) == false);
143+
144+
printf("All tests have successfully passed!\n");
145+
}
146+
147+
/**
148+
* @brief Main function
149+
* @returns 0 on exit
150+
*/
151+
int main() {
152+
tests(); // run self-test implementations
153+
return 0;
154+
}
155+
```
60156
61157
#### Typical structure of a program
62158

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [Alaw](https://github.com/TheAlgorithms/C/blob/HEAD/audio/alaw.c)
44

55
## Cipher
6+
* [Affine](https://github.com/TheAlgorithms/C/blob/HEAD/cipher/affine.c)
67
* [Rot13](https://github.com/TheAlgorithms/C/blob/HEAD/cipher/rot13.c)
78

89
## Client Server
@@ -182,6 +183,7 @@
182183
* [Cartesian To Polar](https://github.com/TheAlgorithms/C/blob/HEAD/math/cartesian_to_polar.c)
183184
* [Catalan](https://github.com/TheAlgorithms/C/blob/HEAD/math/catalan.c)
184185
* [Collatz](https://github.com/TheAlgorithms/C/blob/HEAD/math/collatz.c)
186+
* [Euclidean Algorithm Extended](https://github.com/TheAlgorithms/C/blob/HEAD/math/euclidean_algorithm_extended.c)
185187
* [Factorial](https://github.com/TheAlgorithms/C/blob/HEAD/math/factorial.c)
186188
* [Factorial Large Number](https://github.com/TheAlgorithms/C/blob/HEAD/math/factorial_large_number.c)
187189
* [Factorial Trailing Zeroes](https://github.com/TheAlgorithms/C/blob/HEAD/math/factorial_trailing_zeroes.c)

0 commit comments

Comments
 (0)