diff --git a/aux_lib.c b/aux_lib.c index 469bd42..631d9d2 100644 --- a/aux_lib.c +++ b/aux_lib.c @@ -1,4 +1,5 @@ #include "aux_lib.h" +#include /* * Функция возвращает n-ный элемент последовательности фибоначи @@ -6,8 +7,22 @@ int fibonacci(int nitem) { - //YOUR_CODE - return 42; + if (nitem < 0) + return -1; + + if (nitem == 0) + return 0; + if (nitem == 1) + return 1; + + int a = 0, b = 1; + for (int i = 2; i <= nitem; i++) { + int next = a + b; + a = b; + b = next; + } + + return b; } /* @@ -17,6 +32,12 @@ fibonacci(int nitem) bool sum_is_odd(int *arr, int arrsz) { - //YOUR_CODE - return false; + if (arr == NULL || arrsz <= 0) + return false; + + long sum = 0; + for (int i = 0; i < arrsz; i++) { + sum += arr[i]; + } + return (sum % 2 != 0); } diff --git a/aux_test.c b/aux_test.c index 0de7fbf..927f189 100644 --- a/aux_test.c +++ b/aux_test.c @@ -2,14 +2,102 @@ #include "munit.h" #include "aux_lib.h" -static const MunitSuite test_suite = { - //FILL ME +/****************** TEST fibonacci()*********************/ +static MunitResult +test_fibonacci_basic(const MunitParameter params[], void *data) +{ + (void) params; + (void) data; + + munit_assert_int(fibonacci(0), ==, 0); + munit_assert_int(fibonacci(1), ==, 1); + munit_assert_int(fibonacci(2), ==, 1); + munit_assert_int(fibonacci(3), ==, 2); + munit_assert_int(fibonacci(4), ==, 3); + munit_assert_int(fibonacci(5), ==, 5); + munit_assert_int(fibonacci(10), ==, 55); + + return MUNIT_OK; +} + +static MunitResult +test_fibonacci_negative(const MunitParameter params[], void *data) +{ + (void) params; + (void) data; + + munit_assert_int(fibonacci(-1), ==, -1); + munit_assert_int(fibonacci(-5), ==, -1); + + return MUNIT_OK; +} + +/************* TEST sum_is_odd() *************/ + +static MunitResult +test_sum_is_odd_basic(const MunitParameter params[], void *data) +{ + (void) params; + (void) data; + + int a1[] = {1}; + munit_assert_true(sum_is_odd(a1, 1) == true); + + int a2[] = {2}; + munit_assert_true(sum_is_odd(a2, 1) == false); + + int a3[] = {1, 2, 3}; // сумма = 6 + munit_assert_false(sum_is_odd(a3, 3)); + + int a4[] = {1, 2, 4}; // сумма = 7 + munit_assert_true(sum_is_odd(a4, 3)); + + return MUNIT_OK; +} + +static MunitResult +test_sum_is_odd_edge_cases(const MunitParameter params[], void *data) +{ + (void) params; + (void) data; + + munit_assert_false(sum_is_odd(NULL, 10)); + munit_assert_false(sum_is_odd(NULL, -1)); + + int arr[] = {1}; + munit_assert_false(sum_is_odd(arr, 0)); + + return MUNIT_OK; +} + +/*************** suit ***************/ + +#define TEST_ITEM(func) { #func, func, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } + +static MunitTest test_suite_tests[] = { + TEST_ITEM(test_fibonacci_basic), + TEST_ITEM(test_fibonacci_negative), + TEST_ITEM(test_sum_is_odd_basic), + TEST_ITEM(test_sum_is_odd_edge_cases), + + { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; +static const MunitSuite test_suite = { + (char *) "", + test_suite_tests, + NULL, + 1, + MUNIT_SUITE_OPTION_NONE +}; + +/********************** MAIN **********************/ + int main(int argc, const char *argv[]) { - munit_suite_main(&test_suite, (void *) "string library test", argc, (char * const*) argv); - return 0; + return munit_suite_main(&test_suite, + (void *) "aux library test", + argc, + (char* const*) argv); } -