Skip to content

Commit 45fd548

Browse files
committed
Add tests for check_whitespace
This adds GTest tests for both the `strip()` and `check_whitespace()` functions. I had to do some restructuring here, creating `check_whitespace.h` and moving `main()` out to `main.c`. I also had a bunch of compiler warnings about type conversions; changing a bunch of things from `char*` to `char const *` made that better. This is the important first step in adding GitHub Actions for this lab.
1 parent 937f926 commit 45fd548

File tree

6 files changed

+88
-28
lines changed

6 files changed

+88
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Don't commit the executable
22
check_whitespace
3+
check_whitespace_test
34

45
# Created by https://www.toptal.com/developers/gitignore/api/c,code,emacs,vim
56
# Edit at https://www.toptal.com/developers/gitignore?templates=c,code,emacs,vim

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"calloc",
55
"cmockery",
66
"malloc",
7+
"STREQ",
78
"structs",
89
"valgrind"
910
]

check_whitespace.c

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Strips spaces from both the front and back of a string,
77
* leaving any internal spaces alone.
88
*/
9-
char* strip(char* str) {
9+
char const *strip(char const *str) {
1010
int size = strlen(str);
1111

1212
// This counts the number of leading and trailing spaces
@@ -33,7 +33,7 @@ char* strip(char* str) {
3333

3434
// Allocate a slot for all the "saved" characters
3535
// plus one extra for the null terminator.
36-
char* result = calloc(size-num_spaces+1, sizeof(char));
36+
char* result = (char*) calloc(size-num_spaces+1, sizeof(char));
3737

3838
// Copy in the "saved" characters.
3939
int i;
@@ -50,10 +50,10 @@ char* strip(char* str) {
5050
* Return true (1) if the given string is "clean", i.e., has
5151
* no spaces at the front or the back of the string.
5252
*/
53-
int is_clean(char* str) {
53+
int is_clean(char const *str) {
5454
// We check if it's clean by calling strip and seeing if the
5555
// result is the same as the original string.
56-
char* cleaned = strip(str);
56+
char const *cleaned = strip(str);
5757

5858
// strcmp compares two strings, returning a negative value if
5959
// the first is less than the second (in alphabetical order),
@@ -63,27 +63,3 @@ int is_clean(char* str) {
6363

6464
return result == 0;
6565
}
66-
67-
int main() {
68-
int NUM_STRINGS = 7;
69-
// Makes an array of 7 string constants for testing.
70-
char* strings[] = {
71-
"Morris",
72-
" stuff",
73-
"Minnesota",
74-
"nonsense ",
75-
"USA",
76-
" ",
77-
" silliness "
78-
};
79-
80-
for (int i = 0; i < NUM_STRINGS; ++i) {
81-
if (is_clean(strings[i])) {
82-
printf("The string '%s' is clean.\n", strings[i]);
83-
} else {
84-
printf("The string '%s' is NOT clean.\n", strings[i]);
85-
}
86-
}
87-
88-
return 0;
89-
}

check_whitespace.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef CHECK_WHITESPACE_H_GUARD
2+
#define CHECK_WHITESPACE_H_GUARD
3+
4+
char* strip(char const *str);
5+
int is_clean(char const *str);
6+
7+
#endif

check_whitespace_test.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "check_whitespace.h"
4+
5+
TEST(Strip, EmptyString) {
6+
ASSERT_STREQ("", strip(""));
7+
}
8+
9+
TEST(Strip, NoWhitespace) {
10+
ASSERT_STREQ("frog", strip("frog"));
11+
}
12+
13+
TEST(Strip, WhitespaceOnFront) {
14+
ASSERT_STREQ("frog", strip(" frog"));
15+
}
16+
17+
TEST(Strip, WhitespaceOnBack) {
18+
ASSERT_STREQ("frog", strip("frog "));
19+
}
20+
21+
TEST(Strip, WhitespaceOnBothEnds) {
22+
ASSERT_STREQ("frog", strip(" frog "));
23+
}
24+
25+
TEST(IsClean, EmptyString) {
26+
ASSERT_TRUE(is_clean(""));
27+
}
28+
29+
TEST(IsClean, NoWhitespace) {
30+
ASSERT_TRUE(is_clean("University of Minnesota Morris"));
31+
}
32+
33+
TEST(IsClean, WhitespaceOnFront) {
34+
ASSERT_FALSE(is_clean(" University of Minnesota Morris"));
35+
}
36+
37+
TEST(IsClean, WhitespaceOnBack) {
38+
ASSERT_FALSE(is_clean("University of Minnesota Morris "));
39+
}
40+
41+
TEST(IsClean, WhitespaceOnBothEnds) {
42+
ASSERT_FALSE(is_clean(" University of Minnesota Morris" ));
43+
}
44+
45+
int main(int argc, char *argv[]) {
46+
::testing::InitGoogleTest(&argc, argv);
47+
return RUN_ALL_TESTS();
48+
}

main.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <stdio.h>
2+
3+
#include "check_whitespace.h"
4+
5+
int main() {
6+
int NUM_STRINGS = 7;
7+
// Makes an array of 7 string constants for testing.
8+
char const *strings[] = {
9+
"Morris",
10+
" stuff",
11+
"Minnesota",
12+
"nonsense ",
13+
"USA",
14+
" ",
15+
" silliness "
16+
};
17+
18+
for (int i = 0; i < NUM_STRINGS; ++i) {
19+
if (is_clean(strings[i])) {
20+
printf("The string '%s' is clean.\n", strings[i]);
21+
} else {
22+
printf("The string '%s' is NOT clean.\n", strings[i]);
23+
}
24+
}
25+
26+
return 0;
27+
}

0 commit comments

Comments
 (0)