Skip to content

Commit a6c9d15

Browse files
committed
first implementation.
1 parent 91d381a commit a6c9d15

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

compile_flags.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-std=c17
2+
-I.
3+

makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.DEFAULT_GOAL := run
2+
3+
CC?=clang
4+
5+
CFLAGS=-g -I.
6+
7+
SOURCES=$(wildcard *.c)
8+
OBJECTS=$(SOURCES:%.c=%.o)
9+
10+
%.o: %.c
11+
$(CC) $(CFLAGS) -c -o $@ $<
12+
13+
clean:
14+
rm -rf $(OBJECTS) tests
15+
16+
tests: ./t/tests.c $(OBJECTS)
17+
$(CC) $(CFLAGS) -o $@ $< $(OBJECTS)
18+
19+
run: clean $(OBJECTS) tests
20+
./tests

struppercase.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <assert.h>
2+
#include <ctype.h>
3+
4+
void struppercase(char* str) {
5+
assert(str);
6+
while(*str != 0) {
7+
*(str) = toupper(*str);
8+
++str;
9+
}
10+
}
11+
12+
void strnuppercase(char* str, size_t length) {
13+
assert(str);
14+
assert(length >= 0);
15+
while(*str != 0 && length-- > 0) {
16+
*(str) = toupper(*str);
17+
++str;
18+
}
19+
}

struppercase.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef _STRUPPERCASE_
2+
#define _STRUPPERCASE_ 1
3+
4+
#include <stddef.h>
5+
6+
void struppercase(char* const str);
7+
void strnuppercase(char* str, size_t length);
8+
9+
#endif // _STRUPPERCASE_

t/tests.c

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
#include "struppercase.h"
7+
8+
void test_upcase_single_char() {
9+
char* word = strdup("a");
10+
struppercase(word);
11+
assert(memcmp(word, "A", 2) == 0);
12+
free(word);
13+
}
14+
15+
void test_upcase_last_char() {
16+
char* word = strdup("aB");
17+
struppercase(word);
18+
assert(memcmp(word, "AB", 3) == 0);
19+
free(word);
20+
}
21+
22+
void test_upcase_first_char() {
23+
char* word = strdup("Ab");
24+
struppercase(word);
25+
assert(memcmp(word, "AB", 3) == 0);
26+
free(word);
27+
}
28+
29+
void test_upcase_whole_string() {
30+
char* word = strdup("ab");
31+
struppercase(word);
32+
assert(memcmp(word, "AB", 3) == 0);
33+
free(word);
34+
}
35+
36+
void test_no_change_if_already_uppercase() {
37+
char* word = strdup("AB");
38+
struppercase(word);
39+
assert(memcmp(word, "AB", 3) == 0);
40+
free(word);
41+
}
42+
43+
void test_noop_if_empty_string() {
44+
char* word = strdup("");
45+
struppercase(word);
46+
assert(memcmp(word, "", 1) == 0);
47+
free(word);
48+
}
49+
50+
void test_must_not_upper_case_after_length() {
51+
char* word = strdup("abc");
52+
strnuppercase(word, 2);
53+
assert(memcmp(word, "ABc", 4) == 0);
54+
free(word);
55+
}
56+
57+
void test_must_not_end_if_word_is_smaller_than_length() {
58+
char* word = strdup("abc");
59+
strnuppercase(word, 6);
60+
assert(memcmp(word, "ABC", 4) == 0);
61+
free(word);
62+
}
63+
64+
void test_must_not_uppercase_if_length_is_zero() {
65+
char* word = strdup("abc");
66+
strnuppercase(word, 0);
67+
assert(memcmp(word, "abc", 4) == 0);
68+
free(word);
69+
}
70+
71+
int main(void) {
72+
printf("test struppercase\n");
73+
test_upcase_single_char();
74+
test_upcase_last_char();
75+
test_upcase_first_char();
76+
test_upcase_whole_string();
77+
test_no_change_if_already_uppercase();
78+
test_noop_if_empty_string();
79+
test_must_not_upper_case_after_length();
80+
test_must_not_end_if_word_is_smaller_than_length();
81+
test_must_not_uppercase_if_length_is_zero();
82+
printf("done\n");
83+
return 0;
84+
}

0 commit comments

Comments
 (0)