Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc08212e54 | ||
|
|
93c460eef6 | ||
|
|
e4f271ba85 |
12
Makefile
12
Makefile
@@ -1,16 +1,16 @@
|
|||||||
CFLAGS=-Wall -I munit -ggdb
|
CFLAGS=-Wall -I munit -ggdb
|
||||||
unittest_obj=munit/munit.o
|
unittest_obj=munit/munit.o
|
||||||
|
|
||||||
all: aux_bin aux_test
|
all: str_bin str_test
|
||||||
|
|
||||||
aux_bin: aux_bin.o aux_lib.o
|
str_test: $(unittest_obj) str_lib.o str_test.o
|
||||||
$(CC) $(CFLAGS) -o $@ $^
|
$(CC) $(CFLAGS) -o $@ $^
|
||||||
|
|
||||||
aux_test: $(unittest_obj) aux_lib.o aux_test.o
|
str_bin: str_lib.o str_bin.o
|
||||||
$(CC) $(CFLAGS) -o $@ $^
|
$(CC) $(CFLAGS) -o $@ $^
|
||||||
|
|
||||||
test: ./aux_test
|
test: ./str_test
|
||||||
./aux_test
|
./str_test
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm *_bin *.o $(unittest_obj) aux_test
|
rm *_bin *.o $(unittest_obj) str_test
|
||||||
|
|||||||
18
aux_bin.c
18
aux_bin.c
@@ -1,18 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include "aux_lib.h"
|
|
||||||
|
|
||||||
#define ARRSZ(arr) (sizeof(arr) / sizeof(arr[0]))
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, const char *argv[])
|
|
||||||
{
|
|
||||||
int n = 5;
|
|
||||||
printf("fibonacci(%d) == %d\n", n, fibonacci(n));
|
|
||||||
|
|
||||||
int arr[] = {1,2,3,4,5};
|
|
||||||
|
|
||||||
bool res = sum_is_odd(arr, ARRSZ(arr));
|
|
||||||
printf("sum_is_odd() == %s\n", res ? "true" : "false");
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
22
aux_lib.c
22
aux_lib.c
@@ -1,22 +0,0 @@
|
|||||||
#include "aux_lib.h"
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Функция возвращает n-ный элемент последовательности фибоначи
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
fibonacci(int nitem)
|
|
||||||
{
|
|
||||||
//YOUR_CODE
|
|
||||||
return 42;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Функция считает сумму элементов массива и возвращает информацию
|
|
||||||
* является ли эта сумма нечётным числом.
|
|
||||||
*/
|
|
||||||
bool
|
|
||||||
sum_is_odd(int *arr, int arrsz)
|
|
||||||
{
|
|
||||||
//YOUR_CODE
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
int fibonacci(int nitem);
|
|
||||||
|
|
||||||
bool sum_is_odd(int *arr, int arrsz);
|
|
||||||
|
|
||||||
15
aux_test.c
15
aux_test.c
@@ -1,15 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include "munit.h"
|
|
||||||
#include "aux_lib.h"
|
|
||||||
|
|
||||||
static const MunitSuite test_suite = {
|
|
||||||
//FILL ME
|
|
||||||
};
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, const char *argv[])
|
|
||||||
{
|
|
||||||
munit_suite_main(&test_suite, (void *) "string library test", argc, (char * const*) argv);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
33
munit/str_lib.c
Normal file
33
munit/str_lib.c
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#include "str_lib.h"
|
||||||
|
|
||||||
|
size_t mystrlen(const char *str) {
|
||||||
|
if (!str) return 0;
|
||||||
|
size_t len = 0;
|
||||||
|
while (str[len] != '\0') {
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mystr_idx(const char *str, const char *substr) {
|
||||||
|
if (!str || !substr) return -1;
|
||||||
|
|
||||||
|
size_t str_len = mystrlen(str);
|
||||||
|
size_t substr_len = mystrlen(substr);
|
||||||
|
|
||||||
|
if (substr_len == 0) return 0;
|
||||||
|
if (substr_len > str_len) return -1;
|
||||||
|
|
||||||
|
for (size_t i = 0; i <= str_len - substr_len; i++) {
|
||||||
|
int found = 1;
|
||||||
|
for (size_t j = 0; j < substr_len; j++) {
|
||||||
|
if (str[i + j] != substr[j]) {
|
||||||
|
found = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (found) return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
21
str_bin.c
Normal file
21
str_bin.c
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "str_lib.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
|
const char *s1 = "hello";
|
||||||
|
const char *s2 = "ello";
|
||||||
|
|
||||||
|
if (argc > 2) {
|
||||||
|
s1 = argv[1];
|
||||||
|
s2 = argv[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
// binary for manual testing
|
||||||
|
printf("\033[1;30mUse str_test for actual testing\033[0m\n");
|
||||||
|
|
||||||
|
printf("mystrlen(\"%s\") == %d\n", s1, mystrlen(s1));
|
||||||
|
printf("str_idx(\"%s\", \"%s\") == %d\n", s1, s2, mystr_idx(s1, s2));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
50
str_lib.c
Normal file
50
str_lib.c
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Вернуть длину строки.
|
||||||
|
* Строки в C -- это массив символов, в конце которого находится нулевой символ ( '\0')
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
mystrlen(const char *s)
|
||||||
|
{
|
||||||
|
// <YOURCODE>
|
||||||
|
int len = 0;
|
||||||
|
while (s[len] != '\0') {
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Найти индекс, с которого строка s2 присутствует в строке s1
|
||||||
|
* или -1
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
mystr_idx(const char *s1, const char *s2)
|
||||||
|
{
|
||||||
|
// <YOURCODE>
|
||||||
|
int i = 0; // Индекс для s1
|
||||||
|
|
||||||
|
// Если s2 пустая строка, возвращаем 0 (поведение как у strstr)
|
||||||
|
if (s2[0] == '\0') {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (s1[i] != '\0') {
|
||||||
|
int j = 0; // Индекс для s2
|
||||||
|
|
||||||
|
// Внутренний цикл: сравниваем символы
|
||||||
|
while (s1[i + j] != '\0' && s2[j] != '\0' && s1[i + j] == s2[j]) {
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Если j дошел до конца s2, значит s2 найдена
|
||||||
|
if (s2[j] == '\0') {
|
||||||
|
return i; // Возвращаем индекс начала вхождения
|
||||||
|
}
|
||||||
|
|
||||||
|
i++; // Сдвигаем i, пробуем найти со следующего символа s1
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
6
str_lib.h
Normal file
6
str_lib.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
int mystrlen(const char *s);
|
||||||
|
|
||||||
|
int mystr_idx(const char *s1, const char *s2);
|
||||||
|
|
||||||
67
str_test.c
Normal file
67
str_test.c
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "munit.h"
|
||||||
|
#include "str_lib.h"
|
||||||
|
|
||||||
|
static MunitResult
|
||||||
|
test_mystrlen(const MunitParameter params[], void * data)
|
||||||
|
{
|
||||||
|
munit_assert_true(mystrlen("") == 0);
|
||||||
|
munit_assert_true(mystrlen("333") == 3);
|
||||||
|
munit_assert_true(mystrlen("helloworld") == 10);
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static MunitResult
|
||||||
|
test_mystr_idx(const MunitParameter params[], void * data)
|
||||||
|
{
|
||||||
|
munit_assert_true(mystr_idx("", "") == 0);
|
||||||
|
munit_assert_true(mystr_idx("h", "h") == 0);
|
||||||
|
munit_assert_true(mystr_idx("ah", "h") == 1);
|
||||||
|
munit_assert_true(mystr_idx("ah", "hh") == -1);
|
||||||
|
munit_assert_true(mystr_idx("ahh", "hh") == 1);
|
||||||
|
char *cap = "London is the capital of Great Britan";
|
||||||
|
munit_assert_true(mystr_idx(cap, "python") == -1);
|
||||||
|
munit_assert_true(mystr_idx(cap, "London") == 0);
|
||||||
|
munit_assert_true(mystr_idx(cap, "o") == 1);
|
||||||
|
munit_assert_true(mystr_idx(cap, "is") == 7);
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static MunitResult
|
||||||
|
test_mystr_idx_largestr(const MunitParameter params[], void * data)
|
||||||
|
{
|
||||||
|
munit_assert_true(mystr_idx("", "LARGE STRING") == -1);
|
||||||
|
munit_assert_true(mystr_idx("ARGE_STRING", "LARGE STRING") == -1);
|
||||||
|
munit_assert_true(mystr_idx("ARGE STRING", "LARGE STRING") == -1);
|
||||||
|
munit_assert_true(mystr_idx("RGE STRING", "LARGE STRING") == -1);
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST_ITEM(func) {#func, func, NULL, NULL, MUNIT_TEST_OPTION_NONE }
|
||||||
|
static MunitTest test_suite_tests[] = {
|
||||||
|
TEST_ITEM(test_mystrlen),
|
||||||
|
|
||||||
|
TEST_ITEM(test_mystr_idx),
|
||||||
|
TEST_ITEM(test_mystr_idx_largestr),
|
||||||
|
//TEST_ITEM(test_pvector_init),
|
||||||
|
|
||||||
|
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static const MunitSuite test_suite = {
|
||||||
|
(char *) "",
|
||||||
|
test_suite_tests,
|
||||||
|
NULL,
|
||||||
|
1,
|
||||||
|
MUNIT_SUITE_OPTION_NONE
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
|
munit_suite_main(&test_suite, (void *) "string library test", argc, (char * const*) argv);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user