forked from 131/lab3_test
написал bit_count, тесты и добавил цель в Makefile
This commit is contained in:
17
Makefile
17
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
|
||||
|
||||
10
bit_lib.c
Normal file
10
bit_lib.c
Normal 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
6
bit_lib.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef BIT_LIB_H
|
||||
#define BIT_LIB_H
|
||||
|
||||
int bit_count(unsigned int number);
|
||||
|
||||
#endif
|
||||
30
bit_test.c
Normal file
30
bit_test.c
Normal 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
BIN
bit_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.
BIN
str_test.o
Normal file
BIN
str_test.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user