Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
947771abe3 | ||
|
|
d8cd2b9316 | ||
|
|
98dabf1f73 |
18
Makefile
18
Makefile
@@ -1,16 +1,22 @@
|
|||||||
CFLAGS=-Wall -I munit -ggdb
|
CFLAGS=-Wall -I munit -ggdb
|
||||||
unittest_obj=munit/munit.o
|
unittest_obj=munit/munit.o
|
||||||
|
|
||||||
all: str_bin str_test
|
all: aux_bin aux_test bit_test # ДОБАВЛЕНО 'bit_test'
|
||||||
|
|
||||||
str_test: $(unittest_obj) str_lib.o str_test.o
|
aux_bin: aux_bin.o aux_lib.o
|
||||||
$(CC) $(CFLAGS) -o $@ $^
|
$(CC) $(CFLAGS) -o $@ $^
|
||||||
|
|
||||||
str_bin: str_lib.o str_bin.o
|
aux_test: $(unittest_obj) aux_lib.o aux_test.o
|
||||||
$(CC) $(CFLAGS) -o $@ $^
|
$(CC) $(CFLAGS) -o $@ $^
|
||||||
|
|
||||||
test: ./str_test
|
bit_test: $(unittest_obj) bit_lib.o bit_test.o
|
||||||
./str_test
|
$(CC) $(CFLAGS) -o $@ $^
|
||||||
|
|
||||||
|
test_bit: bit_test
|
||||||
|
./bit_test
|
||||||
|
|
||||||
|
test: ./aux_test
|
||||||
|
./aux_test
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm *_bin *.o $(unittest_obj) str_test
|
rm *_bin *.o $(unittest_obj) aux_test bit_test # ДОБАВЛЕНО 'bit_test'
|
||||||
|
|||||||
18
aux_bin.c
Normal file
18
aux_bin.c
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
47
aux_lib.c
Normal file
47
aux_lib.c
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include "aux_lib.h"
|
||||||
|
#include <stdbool.h> // Добавлен для bool
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Функция возвращает n-ный элемент последовательности фибоначи
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
fibonacci(int nitem)
|
||||||
|
{
|
||||||
|
//YOUR_CODE
|
||||||
|
if (nitem <= 0) {
|
||||||
|
return 0; // F(0)
|
||||||
|
}
|
||||||
|
if (nitem == 1) {
|
||||||
|
return 1; // F(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
int a = 0; // F(n-2)
|
||||||
|
int b = 1; // F(n-1)
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
for (int i = 2; i <= nitem; i++) {
|
||||||
|
result = a + b;
|
||||||
|
a = b;
|
||||||
|
b = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result; // Убираем 'return 42;'
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Функция считает сумму элементов массива и возвращает информацию
|
||||||
|
* является ли эта сумма нечётным числом.
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
sum_is_odd(int *arr, int arrsz)
|
||||||
|
{
|
||||||
|
//YOUR_CODE
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = 0; i < arrsz; i++) {
|
||||||
|
sum += arr[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// (sum % 2 != 0) вернет true, если нечетное,
|
||||||
|
// и false, если четное
|
||||||
|
return (sum % 2 != 0); // Убираем 'return false;'
|
||||||
|
}
|
||||||
7
aux_lib.h
Normal file
7
aux_lib.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
int fibonacci(int nitem);
|
||||||
|
|
||||||
|
bool sum_is_odd(int *arr, int arrsz);
|
||||||
|
|
||||||
70
aux_test.c
Normal file
70
aux_test.c
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "munit.h"
|
||||||
|
#include "aux_lib.h"
|
||||||
|
#include <stdbool.h> // Для bool, true, false
|
||||||
|
|
||||||
|
/* --- Тест для fibonacci --- */
|
||||||
|
static MunitResult
|
||||||
|
test_fibonacci(const MunitParameter params[], void* fixture)
|
||||||
|
{
|
||||||
|
// Проверяем базовые случаи
|
||||||
|
munit_assert_int(fibonacci(0), ==, 0);
|
||||||
|
munit_assert_int(fibonacci(1), ==, 1);
|
||||||
|
munit_assert_int(fibonacci(2), ==, 1);
|
||||||
|
|
||||||
|
// Проверяем несколько значений из последовательности
|
||||||
|
munit_assert_int(fibonacci(5), ==, 5);
|
||||||
|
munit_assert_int(fibonacci(10), ==, 55);
|
||||||
|
munit_assert_int(fibonacci(13), ==, 233);
|
||||||
|
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --- Тест для sum_is_odd --- */
|
||||||
|
static MunitResult
|
||||||
|
test_sum_is_odd(const MunitParameter params[], void* fixture)
|
||||||
|
{
|
||||||
|
// 1. Тест на нечетную сумму
|
||||||
|
int arr_odd[] = {1, 2, 4}; // Сумма = 7
|
||||||
|
munit_assert_true(sum_is_odd(arr_odd, 3));
|
||||||
|
|
||||||
|
// 2. Тест на четную сумму
|
||||||
|
int arr_even[] = {1, 2, 3}; // Сумма = 6
|
||||||
|
munit_assert_false(sum_is_odd(arr_even, 3));
|
||||||
|
|
||||||
|
// 3. Тест на пустой массив
|
||||||
|
int arr_empty[] = {}; // Сумма = 0 (четное)
|
||||||
|
munit_assert_false(sum_is_odd(arr_empty, 0));
|
||||||
|
|
||||||
|
// 4. Тест на один нечетный элемент
|
||||||
|
int arr_single_odd[] = {7}; // Сумма = 7
|
||||||
|
munit_assert_true(sum_is_odd(arr_single_odd, 1));
|
||||||
|
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Массив тестов */
|
||||||
|
static MunitTest tests[] = {
|
||||||
|
{ "/test_fibonacci", test_fibonacci, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||||
|
{ "/test_sum_is_odd", test_sum_is_odd, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||||
|
/* Обязательный NULL-терминатор в конце массива */
|
||||||
|
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static const MunitSuite test_suite = {
|
||||||
|
//FILL ME
|
||||||
|
"/aux_tests", /* Имя сюиты */
|
||||||
|
tests, /* Массив тестов */
|
||||||
|
NULL, /* Вложенные сюиты */
|
||||||
|
1, /* 1 итерация */
|
||||||
|
MUNIT_SUITE_OPTION_NONE /* Опции */
|
||||||
|
};
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
|
/* Меняем "string library test" на "aux library test" для понятности */
|
||||||
|
munit_suite_main(&test_suite, (void *) "aux library test", argc, (char * const*) argv);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BIN
aux_test.o
Normal file
BIN
aux_test.o
Normal file
Binary file not shown.
23
bit_lib.c
Normal file
23
bit_lib.c
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#include "bit_lib.h"
|
||||||
|
|
||||||
|
int bit_count(unsigned int number)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
// Цикл продолжается, пока в 'number' есть хотя бы один единичный бит
|
||||||
|
while (number > 0) {
|
||||||
|
// (number & 1) проверяет, является ли *последний* бит единицей.
|
||||||
|
// (1 & 1) -> 1
|
||||||
|
// (0 & 1) -> 0
|
||||||
|
if ((number & 1) == 1) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// >> 1 — это побитовый сдвиг вправо.
|
||||||
|
// Он "отбрасывает" последний бит и сдвигает все остальные.
|
||||||
|
// 1011 (11) >> 1 станет 0101 (5)
|
||||||
|
number = number >> 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
11
bit_lib.h
Normal file
11
bit_lib.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef BIT_LIB_H
|
||||||
|
#define BIT_LIB_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Функция, считающая количество установленных (единичных) битов
|
||||||
|
* @param number Входное беззнаковое число
|
||||||
|
* @return int Количество единичных битов
|
||||||
|
*/
|
||||||
|
int bit_count(unsigned int number);
|
||||||
|
|
||||||
|
#endif //BIT_LIB_H
|
||||||
63
bit_test.c
Normal file
63
bit_test.c
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "munit.h"
|
||||||
|
#include "bit_lib.h" // Подключаем наш новый модуль
|
||||||
|
|
||||||
|
/* --- Тест для bit_count --- */
|
||||||
|
static MunitResult
|
||||||
|
test_bit_count(const MunitParameter params[], void* fixture)
|
||||||
|
{
|
||||||
|
// 1. Тест на ноль
|
||||||
|
// 0 в двоичной ...0000
|
||||||
|
munit_assert_int(bit_count(0), ==, 0);
|
||||||
|
|
||||||
|
// 2. Тест на 1
|
||||||
|
// 1 в двоичной ...0001
|
||||||
|
munit_assert_int(bit_count(1), ==, 1);
|
||||||
|
|
||||||
|
// 3. Тест на степень двойки
|
||||||
|
// 8 в двоичной ...1000
|
||||||
|
munit_assert_int(bit_count(8), ==, 1);
|
||||||
|
|
||||||
|
// 1024 в двоичной ...10000000000
|
||||||
|
munit_assert_int(bit_count(1024), ==, 1);
|
||||||
|
|
||||||
|
// 4. Тест на число с несколькими битами
|
||||||
|
// 7 в двоичной ...0111
|
||||||
|
munit_assert_int(bit_count(7), ==, 3);
|
||||||
|
|
||||||
|
// 13 в двоичной ...1101
|
||||||
|
munit_assert_int(bit_count(13), ==, 3);
|
||||||
|
|
||||||
|
// 254 в двоичной ...11111110
|
||||||
|
munit_assert_int(bit_count(254), ==, 7);
|
||||||
|
|
||||||
|
// 5. Тест на 'все единицы' (для unsigned int, 32 бита)
|
||||||
|
// 0xFFFFFFFF (или 4294967295)
|
||||||
|
munit_assert_int(bit_count(0xFFFFFFFF), ==, 32);
|
||||||
|
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Массив тестов */
|
||||||
|
static MunitTest tests[] = {
|
||||||
|
{ "/test_bit_count", test_bit_count, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||||
|
/* Обязательный NULL-терминатор в конце массива */
|
||||||
|
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static const MunitSuite test_suite = {
|
||||||
|
"/bit_tests", /* Имя сюиты */
|
||||||
|
tests, /* Массив тестов */
|
||||||
|
NULL, /* Вложенные сюиты */
|
||||||
|
1, /* 1 итерация */
|
||||||
|
MUNIT_SUITE_OPTION_NONE /* Опции */
|
||||||
|
};
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
|
munit_suite_main(&test_suite, (void *) "bit library test", argc, (char * const*) argv);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BIN
bit_test.o
Normal file
BIN
bit_test.o
Normal file
Binary file not shown.
BIN
munit/example
Executable file
BIN
munit/example
Executable file
Binary file not shown.
BIN
munit/munit.o
Normal file
BIN
munit/munit.o
Normal file
Binary file not shown.
21
str_bin.c
21
str_bin.c
@@ -1,21 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
23
str_lib.c
23
str_lib.c
@@ -1,23 +0,0 @@
|
|||||||
#include <string.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Вернуть длину строки.
|
|
||||||
* Строки в C -- это массив символов, в конце которого находится нулевой символ ( '\0')
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
mystrlen(const char *s)
|
|
||||||
{
|
|
||||||
// <YOURCODE>
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Найти индекс, с которого строка s2 присутствует в строке s1
|
|
||||||
* или -1
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
mystr_idx(const char *s1, const char *s2)
|
|
||||||
{
|
|
||||||
// <YOURCODE>
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
int mystrlen(const char *s);
|
|
||||||
|
|
||||||
int mystr_idx(const char *s1, const char *s2);
|
|
||||||
|
|
||||||
67
str_test.c
67
str_test.c
@@ -1,67 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
|
|
||||||
BIN
str_test.o
Normal file
BIN
str_test.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user