Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59d2d3e1cd | ||
|
|
d8cd2b9316 | ||
|
|
98dabf1f73 |
12
Makefile
12
Makefile
@@ -1,16 +1,16 @@
|
||||
CFLAGS=-Wall -I munit -ggdb
|
||||
unittest_obj=munit/munit.o
|
||||
|
||||
all: str_bin str_test
|
||||
all: aux_bin aux_test
|
||||
|
||||
str_test: $(unittest_obj) str_lib.o str_test.o
|
||||
aux_bin: aux_bin.o aux_lib.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 $@ $^
|
||||
|
||||
test: ./str_test
|
||||
./str_test
|
||||
test: ./aux_test
|
||||
./aux_test
|
||||
|
||||
clean:
|
||||
rm *_bin *.o $(unittest_obj) str_test
|
||||
rm *_bin *.o $(unittest_obj) aux_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;
|
||||
}
|
||||
46
aux_lib.c
Normal file
46
aux_lib.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "aux_lib.h"
|
||||
|
||||
/*
|
||||
* Функция возвращает n-ный элемент последовательности фибоначи
|
||||
*/
|
||||
int
|
||||
fibonacci(int nitem)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
* Функция считает сумму элементов массива и возвращает информацию
|
||||
* является ли эта сумма нечётным числом.
|
||||
*/
|
||||
bool
|
||||
sum_is_odd(int *arr, int arrsz)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
9
aux_lib.h
Normal file
9
aux_lib.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
|
||||
int fibonacci(int nitem);
|
||||
|
||||
bool sum_is_odd(int *arr, int arrsz);
|
||||
|
||||
int bit_count(unsigned int number);
|
||||
|
||||
67
aux_test.c
Normal file
67
aux_test.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include <stdio.h>
|
||||
#include "munit/munit.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 = {
|
||||
(char*) "",
|
||||
test_suite_tests,
|
||||
NULL, // Вложенные наборы теÑ<C2B5>tov
|
||||
1, // КоличеÑ<C2B5>тво прогонов
|
||||
MUNIT_SUITE_OPTION_NONE
|
||||
};
|
||||
|
||||
|
||||
int
|
||||
main(int argc, const char *argv[])
|
||||
{
|
||||
munit_suite_main(&test_suite, (void *) "auxiliary library test", argc, (char * const*) argv);
|
||||
return 0;
|
||||
}
|
||||
BIN
aux_test.o
Normal file
BIN
aux_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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user