Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bfa5b6faef | ||
|
|
48d46880ea |
17
Makefile
17
Makefile
@@ -1,7 +1,7 @@
|
|||||||
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
|
all: str_bin str_test bit_test
|
||||||
|
|
||||||
str_test: $(unittest_obj) str_lib.o str_test.o
|
str_test: $(unittest_obj) str_lib.o str_test.o
|
||||||
$(CC) $(CFLAGS) -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
|
str_bin: str_lib.o str_bin.o
|
||||||
$(CC) $(CFLAGS) -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
|
./str_test
|
||||||
|
./bit_test
|
||||||
|
|
||||||
clean:
|
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.
30
str_lib.c
30
str_lib.c
@@ -7,7 +7,11 @@
|
|||||||
int
|
int
|
||||||
mystrlen(const char *s)
|
mystrlen(const char *s)
|
||||||
{
|
{
|
||||||
// <YOURCODE>
|
int len = 0;
|
||||||
|
while (s[len] != '\0') {
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -17,7 +21,29 @@ mystrlen(const char *s)
|
|||||||
int
|
int
|
||||||
mystr_idx(const char *s1, const char *s2)
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
BIN
str_test.o
Normal file
BIN
str_test.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user