Skip to content

Commit dd413a8

Browse files
author
studentfedorov
committed
Lab2: Added makefile, functions and tests for them
1 parent 701e833 commit dd413a8

File tree

7 files changed

+167
-18
lines changed

7 files changed

+167
-18
lines changed

Lab1/makefile

Lines changed: 0 additions & 18 deletions
This file was deleted.

Lab2/include/str.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef __STR_H
2+
#define __STR_H
3+
#include "stddef.h"
4+
char * strcpy(char * destination, const char * source);
5+
char * strcat(char * destination, const char * source);
6+
int strcmp(const char * str1, const char * str2);
7+
size_t strlen(const char * str);
8+
9+
#endif

Lab2/include/test_str.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __TEST_STR_H
2+
#define __TEST_STR_H
3+
#include "stddef.h"
4+
#include "stdbool.h"
5+
6+
bool equal(const char *a, const char* b);
7+
char* test_strcpy(char *destination, const char *source, const char *ans);
8+
char* test_strcat(char *destination, const char *source, const char *ans);
9+
char* test_strcmp(const char *str1, const char *str2, int ans);
10+
char* test_strlen(const char *str, size_t ans);
11+
12+
#endif

Lab2/makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
test: ./bin/test_str.o ./bin/str.o ./bin/test.o
2+
gcc ./bin/test_str.o ./bin/str.o ./bin/test.o -o test
3+
4+
./bin/test_str.o: bin ./src/test_str.c ./include/test_str.h
5+
gcc -c -o ./bin/test_str.o ./src/test_str.c
6+
7+
./bin/test.o: bin ./src/test.c
8+
gcc -c -o ./bin/test.o ./src/test.c
9+
10+
./bin/str.o: bin ./src/str.c ./include/str.h
11+
gcc -c -o ./bin/str.o ./src/str.c
12+
13+
clean:
14+
rm -rf ./bin
15+
rm -rf test
16+
17+
bin:
18+
mkdir bin -p

Lab2/src/str.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "stddef.h"
2+
char * strcpy(char * destination, const char * source){
3+
char *pos = destination;
4+
while (*source != '\0'){
5+
*pos = *source;
6+
pos++;
7+
source++;
8+
}
9+
*pos = '\0';
10+
return destination;
11+
}
12+
13+
char * strcat(char * destination, const char * source){
14+
char *pos = destination;
15+
while (*pos != '\0')
16+
pos++;
17+
while (*source != '\0')
18+
*pos = *source, source++, pos++;
19+
*pos = '\0';
20+
return destination;
21+
}
22+
23+
int strcmp(const char * str1, const char * str2){
24+
while (*str1 == *str2 && *str1 != '\0')
25+
str1++, str2++;
26+
return (int)*str1 - (int)*str2;
27+
}
28+
29+
size_t strlen(const char * str){
30+
size_t res = 0;
31+
while (*str)
32+
res++, str++;
33+
return res;
34+
}

Lab2/src/test.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "../include/test_str.h"
2+
#include "stdio.h"
3+
4+
int main(){
5+
{
6+
char a1[] = "", a2[] = "a", a3[] = "abcd";
7+
printf("Testing strlen\n");
8+
printf("Test 1(str = %s): %s\n", a1, test_strlen(a1, 0));
9+
printf("Test 2(str = %s): %s\n", a2, test_strlen(a2, 1));
10+
printf("Test 3(str = %s): %s\n", a3, test_strlen(a3, 4));
11+
}
12+
{
13+
char s1[] = "", s2[] = "a", s3[] = "abc", s4[] = "b";
14+
printf("Testing strcmp\n");
15+
printf("Test 1(str1 = %s, str2 = %s): %s\n", s1, s1, test_strcmp(s1, s1, 0));
16+
printf("Test 2(str1 = %s, str2 = %s): %s\n", s3, s4, test_strcmp(s3, s4, -1));
17+
printf("Test 3(str1 = %s, str2 = %s): %s\n", s4, s3, test_strcmp(s4, s3, 1));
18+
printf("Test 4(str1 = %s, str2 = %s): %s\n", s1, s2, test_strcmp(s1, s2, -1));
19+
printf("Test 5(str1 = %s, str2 = %s): %s\n", s3, s3, test_strcmp(s3, s3, 0));
20+
}
21+
{
22+
char s1[] = "", s2[] = "a", s3[] = "abc";
23+
char d1[10] = "", d2[10] = "a", d3[10] = "abc", d4[10] = "", d5[10] = "a";
24+
char a1[] = "", a2[] = "abc", a3[] = "aa", a4[] = "abc", a5[] = "aabc";
25+
printf("Testing strcat\n");
26+
printf("Test 1(destination = %s, source = %s): ", d1, s1);
27+
printf("%s\n", test_strcat(d1, s1, a1));
28+
printf("Test 2(destination = %s, source = %s): ", d4, s3);
29+
printf("%s\n", test_strcat(d4, s3, a2));
30+
printf("Test 3(destination = %s, source = %s): ", d2, s2);
31+
printf("%s\n", test_strcat(d2, s2, a3));
32+
printf("Test 4(destination = %s, source = %s): ", d3, s1);
33+
printf("%s\n", test_strcat(d3, s1, a4));
34+
printf("Test 5(destination = %s, source = %s): ", d5, s3);
35+
printf("%s\n", test_strcat(d5, s3, a5));
36+
}
37+
{
38+
char s1[] = "", s2[] = "a", s3[] = "abc";
39+
char d1[10] = "", d2[10] = "a", d3[10] = "abc", d4[10] = "", d5[10] = "a";
40+
char a1[] = "", a2[] = "a", a3[] = "abc";
41+
printf("Testing strcpy\n");
42+
printf("Test 1(destination = %s, source = %s): ", d1, s1);
43+
printf("%s\n", test_strcpy(d1, s1, a1));
44+
printf("Test 2(destination = %s, source = %s): ", d2, s2);
45+
printf("%s\n", test_strcpy(d2, s2, a2));
46+
printf("Test 3(destination = %s, source = %s): ", d3, s2);
47+
printf("%s\n", test_strcpy(d3, s2, a2));
48+
printf("Test 4(destination = %s, source = %s): ", d4, s2);
49+
printf("%s\n", test_strcpy(d4, s2, a2));
50+
printf("Test 5(destination = %s, source = %s): ", d5, s3);
51+
printf("%s\n", test_strcpy(d5, s3, a3));
52+
}
53+
return 0;
54+
}

Lab2/src/test_str.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "../include/str.h"
2+
#include "stddef.h"
3+
#include "stdio.h"
4+
#include "stdbool.h"
5+
6+
bool equal(const char *a, const char* b){
7+
while (*a == *b && *a != '\0')
8+
a++, b++;
9+
return (*a == *b && *a == '\0');
10+
}
11+
12+
char* test_strcpy(char *destination, const char *source, const char* ans){
13+
if (equal(ans, strcpy(destination, source)))
14+
return "OK";
15+
else
16+
return "Error";
17+
}
18+
19+
char* test_strcat(char *destination, const char *source, const char* ans){
20+
if (equal(ans, strcat(destination, source)))
21+
return "OK";
22+
else
23+
return "Error";
24+
}
25+
26+
char* test_strcmp(const char *str1, const char *str2, int ans){
27+
int res = strcmp(str1, str2);
28+
if ((ans == 0 && res == 0) || ans * res > 0)
29+
return "OK";
30+
else
31+
return "Error";
32+
}
33+
34+
char* test_strlen(const char *str, size_t ans){
35+
size_t a = strlen(str);
36+
if (ans == a)
37+
return "OK";
38+
else
39+
return "Error";
40+
}

0 commit comments

Comments
 (0)