2 Commits

Author SHA1 Message Date
alexey
bfa5b6faef написал bit_count, тесты и добавил цель в Makefile 2025-12-05 01:03:04 +03:00
alexey
48d46880ea Допилил файлик mystrlen, mystr_idx 2025-12-04 23:55:35 +03:00
16 changed files with 88 additions and 5 deletions

View File

@@ -1,7 +1,7 @@
CFLAGS=-Wall -I munit -ggdb
unittest_obj=munit/munit.o
all: str_bin str_test
all: str_bin str_test bit_test
str_test: $(unittest_obj) str_lib.o str_test.o
$(CC) $(CFLAGS) -o $@ $^
@@ -9,8 +9,19 @@ str_test: $(unittest_obj) str_lib.o str_test.o
str_bin: str_lib.o str_bin.o
$(CC) $(CFLAGS) -o $@ $^
test: ./str_test
bit_test: $(unittest_obj) bit_lib.o bit_test.o
$(CC) $(CFLAGS) -o $@ $^
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:
rm *_bin *.o $(unittest_obj) str_test
rm -f *_bin *.o $(unittest_obj) str_test bit_test

BIN
aux_test Executable file

Binary file not shown.

10
bit_lib.c Normal file
View File

@@ -0,0 +1,10 @@
#include "bit_lib.h"
int bit_count(unsigned int number) {
int count = 0;
while (number) {
count += number & 1; // если младший бит = 1 → +1
number >>= 1; // сдвиг вправо
}
return count;
}

6
bit_lib.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef BIT_LIB_H
#define BIT_LIB_H
int bit_count(unsigned int number);
#endif

BIN
bit_lib.o Normal file

Binary file not shown.

BIN
bit_test Executable file

Binary file not shown.

30
bit_test.c Normal file
View File

@@ -0,0 +1,30 @@
#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 Normal file

Binary file not shown.

BIN
munit/example Executable file

Binary file not shown.

BIN
munit/munit.o Normal file

Binary file not shown.

BIN
str_bin Executable file

Binary file not shown.

BIN
str_bin.o Normal file

Binary file not shown.

View File

@@ -7,7 +7,11 @@
int
mystrlen(const char *s)
{
// <YOURCODE>
int len = 0;
while (s[len] != '\0') {
len++;
}
return len;
}
/*
@@ -17,7 +21,29 @@ mystrlen(const char *s)
int
mystr_idx(const char *s1, const char *s2)
{
// <YOURCODE>
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;
}

BIN
str_lib.o Normal file

Binary file not shown.

BIN
str_test Executable file

Binary file not shown.

BIN
str_test.o Normal file

Binary file not shown.