forked from 131/lab3_test
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1a60ece40f | ||
|
|
d8cd2b9316 | ||
|
|
98dabf1f73 |
23
Makefile
23
Makefile
@@ -1,27 +1,16 @@
|
|||||||
CFLAGS=-Wall -I munit -ggdb
|
CFLAGS=-Wall -I munit -ggdb
|
||||||
unittest_obj=munit/munit.o
|
unittest_obj=munit/munit.o
|
||||||
|
|
||||||
all: str_bin str_test bit_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 $@ $^
|
$(CC) $(CFLAGS) -o $@ $^
|
||||||
|
|
||||||
str_bin: str_lib.o str_bin.o
|
aux_test: $(unittest_obj) aux_lib.o aux_test.o
|
||||||
$(CC) $(CFLAGS) -o $@ $^
|
$(CC) $(CFLAGS) -o $@ $^
|
||||||
|
|
||||||
bit_test: $(unittest_obj) bit_lib.o bit_test.o
|
test: ./aux_test
|
||||||
$(CC) $(CFLAGS) -o $@ $^
|
./aux_test
|
||||||
|
|
||||||
bit_lib.o: bit_lib.c bit_lib.h
|
|
||||||
str_lib.o: str_lib.c str_lib.h
|
|
||||||
str_bin.o: str_bin.c
|
|
||||||
bit_test.o: bit_test.c bit_lib.h
|
|
||||||
|
|
||||||
all: str_bin str_test bit_test
|
|
||||||
|
|
||||||
test: all
|
|
||||||
./str_test
|
|
||||||
./bit_test
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *_bin *.o $(unittest_obj) str_test bit_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;
|
||||||
|
}
|
||||||
43
aux_lib.c
Normal file
43
aux_lib.c
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#include "aux_lib.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Функция возвращает n-ный элемент последовательности фибоначи
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
fibonacci(int nitem)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Функция считает сумму элементов массива и возвращает информацию
|
||||||
|
* является ли эта сумма нечётным числом.
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
sum_is_odd(int *arr, int arrsz)
|
||||||
|
{
|
||||||
|
if (arr == NULL || arrsz <= 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
long sum = 0;
|
||||||
|
for (int i = 0; i < arrsz; i++) {
|
||||||
|
sum += arr[i];
|
||||||
|
}
|
||||||
|
return (sum % 2 != 0);
|
||||||
|
}
|
||||||
7
aux_lib.h
Normal file
7
aux_lib.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
int fibonacci(int nitem);
|
||||||
|
|
||||||
|
bool sum_is_odd(int *arr, int arrsz);
|
||||||
|
|
||||||
103
aux_test.c
Normal file
103
aux_test.c
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "munit.h"
|
||||||
|
#include "aux_lib.h"
|
||||||
|
|
||||||
|
/****************** 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[])
|
||||||
|
{
|
||||||
|
return munit_suite_main(&test_suite,
|
||||||
|
(void *) "aux library test",
|
||||||
|
argc,
|
||||||
|
(char* const*) argv);
|
||||||
|
}
|
||||||
10
bit_lib.c
10
bit_lib.c
@@ -1,10 +0,0 @@
|
|||||||
#include "bit_lib.h"
|
|
||||||
|
|
||||||
int bit_count(unsigned int number) {
|
|
||||||
int count = 0;
|
|
||||||
while (number) {
|
|
||||||
count += number & 1; // если младший бит = 1 → +1
|
|
||||||
number >>= 1; // сдвиг вправо
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
#ifndef BIT_LIB_H
|
|
||||||
#define BIT_LIB_H
|
|
||||||
|
|
||||||
int bit_count(unsigned int number);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
30
bit_test.c
30
bit_test.c
@@ -1,30 +0,0 @@
|
|||||||
#include "munit.h"
|
|
||||||
#include "bit_lib.h"
|
|
||||||
|
|
||||||
static MunitResult
|
|
||||||
test_bit_count_basic(const MunitParameter params[], void *data) {
|
|
||||||
(void) params; (void) data;
|
|
||||||
|
|
||||||
munit_assert_int(bit_count(0), ==, 0); // 0b0
|
|
||||||
munit_assert_int(bit_count(1), ==, 1); // 0b1
|
|
||||||
munit_assert_int(bit_count(2), ==, 1); // 0b10
|
|
||||||
munit_assert_int(bit_count(3), ==, 2); // 0b11
|
|
||||||
munit_assert_int(bit_count(15), ==, 4); // 0b1111
|
|
||||||
munit_assert_int(bit_count(16), ==, 1); // 0b10000
|
|
||||||
munit_assert_int(bit_count(255), ==, 8); // 0b11111111
|
|
||||||
|
|
||||||
return MUNIT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const MunitTest tests[] = {
|
|
||||||
{ "/bit_count/basic", test_bit_count_basic, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
|
|
||||||
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
|
||||||
};
|
|
||||||
|
|
||||||
static const MunitSuite suite = {
|
|
||||||
NULL, tests, NULL, 1, MUNIT_SUITE_OPTION_NONE
|
|
||||||
};
|
|
||||||
|
|
||||||
int main(int argc, const char *argv[]) {
|
|
||||||
return munit_suite_main(&suite, (void*) "", argc, (char * const*) argv);
|
|
||||||
}
|
|
||||||
BIN
bit_test.o
BIN
bit_test.o
Binary file not shown.
BIN
munit/example
BIN
munit/example
Binary file not shown.
BIN
munit/munit.o
BIN
munit/munit.o
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;
|
|
||||||
}
|
|
||||||
49
str_lib.c
49
str_lib.c
@@ -1,49 +0,0 @@
|
|||||||
#include <string.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Вернуть длину строки.
|
|
||||||
* Строки в C -- это массив символов, в конце которого находится нулевой символ ( '\0')
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
mystrlen(const char *s)
|
|
||||||
{
|
|
||||||
int len = 0;
|
|
||||||
while (s[len] != '\0') {
|
|
||||||
len++;
|
|
||||||
}
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Найти индекс, с которого строка s2 присутствует в строке s1
|
|
||||||
* или -1
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
mystr_idx(const char *s1, const char *s2)
|
|
||||||
{
|
|
||||||
if (s1 == NULL || s2 == NULL)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (s2[0] == '\0')
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
int len1 = mystrlen(s1);
|
|
||||||
int len2 = mystrlen(s2);
|
|
||||||
|
|
||||||
if (len2 > len1)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
for (int i = 0; i <=len1 - len2; i++) {
|
|
||||||
int j = 0;
|
|
||||||
while (j < len2 && s1[i + j] == s2[j]) {
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
if (j == len2)
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
int mystrlen(const char *s);
|
|
||||||
|
|
||||||
int mystr_idx(const char *s1, const char *s2);
|
|
||||||
|
|
||||||
67
str_test.c
67
str_test.c
@@ -1,67 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#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;
|
|
||||||
}
|
|
||||||
|
|
||||||
BIN
str_test.o
BIN
str_test.o
Binary file not shown.
Reference in New Issue
Block a user