diff --git a/Makefile b/Makefile index 61636ef..139f1ab 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/aux_test b/aux_test new file mode 100755 index 0000000..0bca9a8 Binary files /dev/null and b/aux_test differ diff --git a/bit_lib.c b/bit_lib.c new file mode 100644 index 0000000..1364747 --- /dev/null +++ b/bit_lib.c @@ -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; +} diff --git a/bit_lib.h b/bit_lib.h new file mode 100644 index 0000000..bdaa546 --- /dev/null +++ b/bit_lib.h @@ -0,0 +1,6 @@ +#ifndef BIT_LIB_H +#define BIT_LIB_H + +int bit_count(unsigned int number); + +#endif diff --git a/bit_lib.o b/bit_lib.o new file mode 100644 index 0000000..50c1f15 Binary files /dev/null and b/bit_lib.o differ diff --git a/bit_test b/bit_test new file mode 100755 index 0000000..19c0b78 Binary files /dev/null and b/bit_test differ diff --git a/bit_test.c b/bit_test.c new file mode 100644 index 0000000..03dcbe5 --- /dev/null +++ b/bit_test.c @@ -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); +} diff --git a/bit_test.o b/bit_test.o new file mode 100644 index 0000000..c216203 Binary files /dev/null and b/bit_test.o differ diff --git a/munit/example b/munit/example new file mode 100755 index 0000000..a720acc 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..d90ef7a Binary files /dev/null and b/munit/munit.o differ diff --git a/str_bin b/str_bin new file mode 100755 index 0000000..3aa2cd4 Binary files /dev/null and b/str_bin differ diff --git a/str_bin.o b/str_bin.o new file mode 100644 index 0000000..ff314bd Binary files /dev/null and b/str_bin.o differ diff --git a/str_lib.o b/str_lib.o new file mode 100644 index 0000000..92f0f79 Binary files /dev/null and b/str_lib.o differ diff --git a/str_test b/str_test new file mode 100755 index 0000000..17548b3 Binary files /dev/null and b/str_test differ diff --git a/str_test.o b/str_test.o new file mode 100644 index 0000000..e11d4ef Binary files /dev/null and b/str_test.o differ