diff --git a/aux_bin b/aux_bin new file mode 100755 index 0000000..71abefa Binary files /dev/null and b/aux_bin differ diff --git a/aux_bin.o b/aux_bin.o new file mode 100644 index 0000000..90e4273 Binary files /dev/null and b/aux_bin.o differ diff --git a/aux_lib.c b/aux_lib.c index 469bd42..6740812 100644 --- a/aux_lib.c +++ b/aux_lib.c @@ -6,8 +6,18 @@ int fibonacci(int nitem) { - //YOUR_CODE - return 42; + if (nitem <= 0) return 0; + if (nitem == 1) return 1; + + int prev = 0; + int curr = 1; + + for (int i = 2; i <= nitem; i++) { + int next = prev + curr; + prev = curr; + curr = next; + } + return curr; } /* @@ -17,6 +27,20 @@ fibonacci(int nitem) bool sum_is_odd(int *arr, int arrsz) { - //YOUR_CODE - return false; + int sum = 0; + 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) { + count += number & 1; + number >>= 1; + } + return count; +} + diff --git a/aux_lib.h b/aux_lib.h index bcf878c..6143968 100644 --- a/aux_lib.h +++ b/aux_lib.h @@ -5,3 +5,5 @@ int fibonacci(int nitem); bool sum_is_odd(int *arr, int arrsz); +int bit_count(unsigned int number); + diff --git a/aux_lib.o b/aux_lib.o new file mode 100644 index 0000000..256feea Binary files /dev/null and b/aux_lib.o differ diff --git a/aux_test b/aux_test new file mode 100755 index 0000000..735572d Binary files /dev/null and b/aux_test differ diff --git a/aux_test.c b/aux_test.c index 0de7fbf..04b6d96 100644 --- a/aux_test.c +++ b/aux_test.c @@ -1,15 +1,67 @@ #include -#include "munit.h" +#include "munit/munit.h" #include "aux_lib.h" +#include + +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 = { - //FILL ME + (char*) "", + test_suite_tests, + NULL, // Вложенные наборы те�tov + 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; + munit_suite_main(&test_suite, (void *) "auxiliary library test", argc, (char * const*) argv); + return 0; } - diff --git a/aux_test.o b/aux_test.o new file mode 100644 index 0000000..6a159a8 Binary files /dev/null and b/aux_test.o differ diff --git a/munit/example b/munit/example new file mode 100755 index 0000000..39d51d4 Binary files /dev/null and b/munit/example differ diff --git a/munit/munit.o b/munit/munit.o new file mode 100644 index 0000000..82bbbd1 Binary files /dev/null and b/munit/munit.o differ diff --git a/str_test.c b/str_test.c new file mode 100644 index 0000000..074f9f4 --- /dev/null +++ b/str_test.c @@ -0,0 +1,67 @@ +#include +#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; +} +