5 Commits

Author SHA1 Message Date
ed3307e16e Update Makefile 2025-11-01 12:49:06 +03:00
9d1d42b494 Update str_test.c 2025-11-01 12:47:18 +03:00
fc6cef7cf7 Update str_test.c 2025-11-01 12:46:55 +03:00
0e15f39bbb Update str_lib.h 2025-11-01 12:45:21 +03:00
5b9d9b6ad2 Update str_lib.c 2025-11-01 12:43:42 +03:00
4 changed files with 98 additions and 61 deletions

View File

@@ -1,16 +1,32 @@
CFLAGS=-Wall -I munit -ggdb CC = gcc
unittest_obj=munit/munit.o CFLAGS = -Wall -Wextra -std=c99 -I.
MUNIT_DIR = munit
all: str_bin str_test # Основные цели
all: test example
str_test: $(unittest_obj) str_lib.o str_test.o # Сборка библиотеки munit
$(CC) $(CFLAGS) -o $@ $^ $(MUNIT_DIR)/munit.o: $(MUNIT_DIR)/munit.c $(MUNIT_DIR)/munit.h
$(CC) $(CFLAGS) -c $(MUNIT_DIR)/munit.c -o $(MUNIT_DIR)/munit.o
str_bin: str_lib.o str_bin.o # Сборка библиотеки строк
$(CC) $(CFLAGS) -o $@ $^ str_lib.o: str_lib.c str_lib.h
$(CC) $(CFLAGS) -c str_lib.c -o str_lib.o
test: ./str_test # Сборка тестов
str_test: str_test.c str_lib.o $(MUNIT_DIR)/munit.o
$(CC) $(CFLAGS) str_test.c str_lib.o $(MUNIT_DIR)/munit.o -o str_test
# Сборка примера из munit
example: example.c $(MUNIT_DIR)/munit.o
$(CC) $(CFLAGS) example.c $(MUNIT_DIR)/munit.o -o example
# Запуск тестов
test: str_test
./str_test ./str_test
# Очистка
clean: clean:
rm *_bin *.o $(unittest_obj) str_test rm -f *.o str_test example $(MUNIT_DIR)/*.o
.PHONY: all test clean

View File

@@ -4,20 +4,48 @@
* Вернуть длину строки. * Вернуть длину строки.
* Строки в C -- это массив символов, в конце которого находится нулевой символ ( '\0') * Строки в C -- это массив символов, в конце которого находится нулевой символ ( '\0')
*/ */
int int mystrlen(const char *s)
mystrlen(const char *s)
{ {
// <YOURCODE> int length = 0;
while (s[length] != '\0') {
length++;
}
return length;
} }
/* /*
* Найти индекс, с которого строка s2 присутствует в строке s1 * Найти индекс, с которого строка s2 присутствует в строке s1
* или -1 * или -1
*/ */
int int mystr_idx(const char *s1, const char *s2)
mystr_idx(const char *s1, const char *s2)
{ {
// <YOURCODE> int len1 = mystrlen(s1);
return -1; int len2 = mystrlen(s2);
}
// Если вторая строка пустая, возвращаем 0
if (len2 == 0) {
return 0;
}
// Если первая строка короче второй, точно не найдем
if (len1 < len2) {
return -1;
}
// Проходим по первой строке
for (int i = 0; i <= len1 - len2; i++) {
int j;
// Сравниваем подстроку начиная с позиции i
for (j = 0; j < len2; j++) {
if (s1[i + j] != s2[j]) {
break;
}
}
// Если прошли всю вторую строку - нашли вхождение
if (j == len2) {
return i;
}
}
return -1;
}

View File

@@ -1,6 +1,7 @@
#pragma once #ifndef STR_LIB_H
#define STR_LIB_H
int mystrlen(const char *s); int mystrlen(const char *s);
int mystr_idx(const char *s1, const char *s2); int mystr_idx(const char *s1, const char *s2);
#endif

View File

@@ -1,67 +1,59 @@
#include <stdio.h> #include <stdio.h>
#include "munit.h" #include "munit/munit.h"
#include "str_lib.h" #include "str_lib.h"
static MunitResult static MunitResult
test_mystrlen(const MunitParameter params[], void * data) test_mystrlen(const MunitParameter params[], void * data)
{ {
munit_assert_true(mystrlen("") == 0); munit_assert_int(mystrlen(""), ==, 0);
munit_assert_true(mystrlen("333") == 3); munit_assert_int(mystrlen("333"), ==, 3);
munit_assert_true(mystrlen("helloworld") == 10); munit_assert_int(mystrlen("helloworld"), ==, 10);
return MUNIT_OK; return MUNIT_OK;
} }
static MunitResult static MunitResult
test_mystr_idx(const MunitParameter params[], void * data) test_mystr_idx(const MunitParameter params[], void * data)
{ {
munit_assert_true(mystr_idx("", "") == 0); munit_assert_int(mystr_idx("", ""), ==, 0);
munit_assert_true(mystr_idx("h", "h") == 0); munit_assert_int(mystr_idx("h", "h"), ==, 0);
munit_assert_true(mystr_idx("ah", "h") == 1); munit_assert_int(mystr_idx("ah", "h"), ==, 1);
munit_assert_true(mystr_idx("ah", "hh") == -1); munit_assert_int(mystr_idx("ah", "hh"), ==, -1);
munit_assert_true(mystr_idx("ahh", "hh") == 1); munit_assert_int(mystr_idx("ahh", "hh"), ==, 1);
char *cap = "London is the capital of Great Britan"; char *cap = "London is the capital of Great Britan";
munit_assert_true(mystr_idx(cap, "python") == -1); munit_assert_int(mystr_idx(cap, "python"), ==, -1);
munit_assert_true(mystr_idx(cap, "London") == 0); munit_assert_int(mystr_idx(cap, "London"), ==, 0);
munit_assert_true(mystr_idx(cap, "o") == 1); munit_assert_int(mystr_idx(cap, "o"), ==, 1);
munit_assert_true(mystr_idx(cap, "is") == 7); munit_assert_int(mystr_idx(cap, "is"), ==, 7);
return MUNIT_OK; return MUNIT_OK;
} }
static MunitResult static MunitResult
test_mystr_idx_largestr(const MunitParameter params[], void * data) test_mystr_idx_largestr(const MunitParameter params[], void * data)
{ {
munit_assert_true(mystr_idx("", "LARGE STRING") == -1); munit_assert_int(mystr_idx("", "LARGE STRING"), ==, -1);
munit_assert_true(mystr_idx("ARGE_STRING", "LARGE STRING") == -1); munit_assert_int(mystr_idx("ARGE_STRING", "LARGE STRING"), ==, -1);
munit_assert_true(mystr_idx("ARGE STRING", "LARGE STRING") == -1); munit_assert_int(mystr_idx("ARGE STRING", "LARGE STRING"), ==, -1);
munit_assert_true(mystr_idx("RGE STRING", "LARGE STRING") == -1); munit_assert_int(mystr_idx("RGE STRING", "LARGE STRING"), ==, -1);
return MUNIT_OK; return MUNIT_OK;
} }
#define TEST_ITEM(func) {#func, func, NULL, NULL, MUNIT_TEST_OPTION_NONE } #define TEST_ITEM(func) {#func, func, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}
static MunitTest test_suite_tests[] = { static MunitTest test_suite_tests[] = {
TEST_ITEM(test_mystrlen), TEST_ITEM(test_mystrlen),
TEST_ITEM(test_mystr_idx),
TEST_ITEM(test_mystr_idx), TEST_ITEM(test_mystr_idx_largestr),
TEST_ITEM(test_mystr_idx_largestr), { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
//TEST_ITEM(test_pvector_init),
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
}; };
static const MunitSuite test_suite = { static const MunitSuite test_suite = {
(char *) "", (char *) "",
test_suite_tests, test_suite_tests,
NULL, NULL,
1, 1,
MUNIT_SUITE_OPTION_NONE MUNIT_SUITE_OPTION_NONE
}; };
int main(int argc, const char *argv[])
int
main(int argc, const char *argv[])
{ {
munit_suite_main(&test_suite, (void *) "string library test", argc, (char * const*) argv); return munit_suite_main(&test_suite, (void *) "string library test", argc, (char **)argv);
return 0;
} }