forked from 131/lab3_test
Implement fibonacci, sum_is_odd, and bit_count functions with tests in aux branch
This commit is contained in:
38
aux_lib.c
38
aux_lib.c
@@ -1,22 +1,38 @@
|
|||||||
#include "aux_lib.h"
|
#include "aux_lib.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
/*
|
|
||||||
* Функция возвращает n-ный элемент последовательности фибоначи
|
|
||||||
*/
|
|
||||||
int
|
int
|
||||||
fibonacci(int nitem)
|
fibonacci(int nitem)
|
||||||
{
|
{
|
||||||
//YOUR_CODE
|
if (nitem <= 1) {
|
||||||
return 42;
|
return nitem;
|
||||||
|
}
|
||||||
|
int a = 0, b = 1, next;
|
||||||
|
for (int i = 2; i <= nitem; ++i) {
|
||||||
|
next = a + b;
|
||||||
|
a = b;
|
||||||
|
b = next;
|
||||||
|
}
|
||||||
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Функция считает сумму элементов массива и возвращает информацию
|
|
||||||
* является ли эта сумма нечётным числом.
|
|
||||||
*/
|
|
||||||
bool
|
bool
|
||||||
sum_is_odd(int *arr, int arrsz)
|
sum_is_odd(int *arr, int arrsz)
|
||||||
{
|
{
|
||||||
//YOUR_CODE
|
long long sum = 0;
|
||||||
return false;
|
for (int i = 0; i < arrsz; i++) {
|
||||||
|
sum += arr[i];
|
||||||
|
}
|
||||||
|
return sum % 2 != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int bit_count(unsigned int number) {
|
||||||
|
int count = 0;
|
||||||
|
while (number > 0) {
|
||||||
|
if (number % 2 == 1) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
number = number / 2;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,3 +5,4 @@ int fibonacci(int nitem);
|
|||||||
|
|
||||||
bool sum_is_odd(int *arr, int arrsz);
|
bool sum_is_odd(int *arr, int arrsz);
|
||||||
|
|
||||||
|
int bit_count(unsigned int number);
|
||||||
|
|||||||
60
aux_test.c
60
aux_test.c
@@ -1,15 +1,67 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "munit.h"
|
#include "munit/munit.h"
|
||||||
#include "aux_lib.h"
|
#include "aux_lib.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
static MunitResult test_fibonacci_base(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(3), ==, 2);
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static MunitResult test_bit_count(const MunitParameter params[], void* fixture) {
|
||||||
|
munit_assert_int(bit_count(0), ==, 0);
|
||||||
|
munit_assert_int(bit_count(1), ==, 1);
|
||||||
|
munit_assert_int(bit_count(3), ==, 2);
|
||||||
|
munit_assert_int(bit_count(8), ==, 1);
|
||||||
|
munit_assert_int(bit_count(255), ==, 8);
|
||||||
|
munit_assert_int(bit_count(0xFFFFFFFF), ==, 32);
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static MunitResult test_fibonacci_advanced(const MunitParameter params[], void* fixture) {
|
||||||
|
munit_assert_int(fibonacci(10), ==, 55);
|
||||||
|
munit_assert_int(fibonacci(20), ==, 6765);
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static MunitResult test_sum_odd(const MunitParameter params[], void* fixture) {
|
||||||
|
int arr[] = {1, 2, 3, 4, 5};
|
||||||
|
munit_assert_true(sum_is_odd(arr, 5));
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static MunitResult test_sum_even(const MunitParameter params[], void* fixture) {
|
||||||
|
int arr[] = {1, 2, 3, 4, 5, 1};
|
||||||
|
munit_assert_false(sum_is_odd(arr, 6));
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static MunitTest test_suite_tests[] = {
|
||||||
|
{ (char*) "fibonacci base cases", test_fibonacci_base, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||||
|
{ (char*) "fibonacci advanced cases", test_fibonacci_advanced, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||||
|
{ (char*) "sum odd array", test_sum_odd, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||||
|
{ (char*) "sum even array", test_sum_even, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||||
|
{ (char*) "bit count", test_bit_count, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
||||||
|
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
static const MunitSuite test_suite = {
|
static const MunitSuite test_suite = {
|
||||||
//FILL ME
|
(char*) "",
|
||||||
|
test_suite_tests,
|
||||||
|
NULL, // Вложенные наборы тесtov
|
||||||
|
1, // Количество прогонов
|
||||||
|
MUNIT_SUITE_OPTION_NONE
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, const char *argv[])
|
main(int argc, const char *argv[])
|
||||||
{
|
{
|
||||||
munit_suite_main(&test_suite, (void *) "string library test", argc, (char * const*) argv);
|
munit_suite_main(&test_suite, (void *) "auxiliary library test", argc, (char * const*) argv);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user