Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
947771abe3 |
10
Makefile
10
Makefile
@@ -1,7 +1,7 @@
|
||||
CFLAGS=-Wall -I munit -ggdb
|
||||
unittest_obj=munit/munit.o
|
||||
|
||||
all: aux_bin aux_test
|
||||
all: aux_bin aux_test bit_test # ДОБАВЛЕНО 'bit_test'
|
||||
|
||||
aux_bin: aux_bin.o aux_lib.o
|
||||
$(CC) $(CFLAGS) -o $@ $^
|
||||
@@ -9,8 +9,14 @@ aux_bin: aux_bin.o aux_lib.o
|
||||
aux_test: $(unittest_obj) aux_lib.o aux_test.o
|
||||
$(CC) $(CFLAGS) -o $@ $^
|
||||
|
||||
bit_test: $(unittest_obj) bit_lib.o bit_test.o
|
||||
$(CC) $(CFLAGS) -o $@ $^
|
||||
|
||||
test_bit: bit_test
|
||||
./bit_test
|
||||
|
||||
test: ./aux_test
|
||||
./aux_test
|
||||
|
||||
clean:
|
||||
rm *_bin *.o $(unittest_obj) aux_test
|
||||
rm *_bin *.o $(unittest_obj) aux_test bit_test # ДОБАВЛЕНО 'bit_test'
|
||||
|
||||
33
aux_lib.c
33
aux_lib.c
@@ -1,4 +1,5 @@
|
||||
#include "aux_lib.h"
|
||||
#include <stdbool.h> // Добавлен для bool
|
||||
|
||||
/*
|
||||
* Функция возвращает n-ный элемент последовательности фибоначи
|
||||
@@ -6,8 +7,25 @@
|
||||
int
|
||||
fibonacci(int nitem)
|
||||
{
|
||||
//YOUR_CODE
|
||||
return 42;
|
||||
//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;'
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -17,6 +35,13 @@ fibonacci(int nitem)
|
||||
bool
|
||||
sum_is_odd(int *arr, int arrsz)
|
||||
{
|
||||
//YOUR_CODE
|
||||
return false;
|
||||
//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;'
|
||||
}
|
||||
|
||||
63
aux_test.c
63
aux_test.c
@@ -1,15 +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
|
||||
//FILL ME
|
||||
"/aux_tests", /* Имя сюиты */
|
||||
tests, /* Массив тестов */
|
||||
NULL, /* Вложенные сюиты */
|
||||
1, /* 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;
|
||||
/* Меняем "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.
BIN
str_test.o
Normal file
BIN
str_test.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user