Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,5 @@ WhitespaceSensitiveMacros:
- BOOST_PP_STRINGIZE
- CF_SWIFT_NAME
- NS_SWIFT_NAME
- PP_STRINGIZE
- STRINGIZE
- STRINGIZE

9 changes: 9 additions & 0 deletions src/Test2Dir/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.25)

project(ProgressTest2 C)

add_compile_options(-Wall -Wextra -Wpedantic)

add_executable(main main.c tests.c)

enable_testing()
25 changes: 25 additions & 0 deletions src/Test2Dir/lib.h
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А почему у хедера нет файла с реализацией?

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include <stdbool.h>

// Функция запуска всех тестов, ничего не принимает, ничего не возвращает
bool tests();

/*
* функция перевода 8ми битного числа в десятичную систему счисления
* принимает на ход указатель на массив с целочисленными значениями 0 и 1
*/
int convertToDecimal8bits(int* binary);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И, кстати, числа-то неотрицательные, почему тогда здесь везде int?


/*
функция перевода массива с булевыми элементами в целочисленные 0 и 1
на вход принимает указатель на массив и его длину
возвращает указатель на новый массив.
*/
int* convertBoolElementsToInt(bool* arr, int length);
Comment on lines +13 to +18
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ненужная функция. При касте bool превращается в 0 и 1 соответственно, т.е. их bool можно использовать там, где ожидается int.


/* функция (собирающая все другие функции)
она сравнивает два числа
на вход пинимает два указателя на массивы с булевыми значениями
возвращает 1, -1, 0 в зависимости от результата сравнения.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А какая зависимость? Это нужно описывать.

*/
int compareTwoDecimalNumbers(bool* arr1, bool* arr2);
85 changes: 85 additions & 0 deletions src/Test2Dir/main.c
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь как-то грустненько :( Но код Вы написали и он даже как-то работает. 4 балла.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "lib.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int convertToDecimal8bits(int* binary)
{
int decimal = 0;
int power = 128;
for (int i = 0; i < 8; i++) {
if (binary[i] == 1) {
decimal += power;
}
power /= 2;
}
return decimal;
}

int* convertBoolElementsToInt(bool* arr, int length)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если у Вас до этого функция работает только с 8битными числами, зачем здесь произвольная длина? (Или там тогда зачем ограничение?)

{
int* newArr = malloc(sizeof(int) * length);
for (int i = 0; i < length; i++) {
if (arr[i] == false) {
newArr[i] = 0;
} else {
newArr[i] = 1;
}
}
Comment on lines +24 to +29
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А что говорилось про смысл выражений типа true == true?

return newArr;
}

int compareTwoDecimalNumbers(bool* arr1, bool* arr2)
{
bool* p1 = arr1;
bool* p2 = arr2;
int* newArr1 = convertBoolElementsToInt(p1, 8);
int decimal1 = convertToDecimal8bits(newArr1);
int* newArr2 = convertBoolElementsToInt(p2, 8);
int decimal2 = convertToDecimal8bits(newArr2);
Comment on lines +37 to +40
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не-а. Сравнивать нужно было массивы bool.

free(newArr1);
free(newArr2);
if (decimal1 > decimal2) {
return 1;
} else if (decimal2 > decimal1) {
return -1;
} else {
return 0;
}
}

void printResults(int res)
{
if (res == 1) {
printf("Первое число больше чем второе число\n");
} else if (res == -1) {
printf("Второе число больше чем первое число\n");
} else if (res == 0) {
printf("Числа равны");
}
return;
}

// так как в условии ничего не сказано про длину масиива, пусть она будет всего 8 элементов (8 бит)
int main(int argc, char* argv[])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем такие сложности, если без флага --test Вы всё равно запускаете тест, просто всего один?

{
if (argc == 2 && strcmp(argv[1], "--test") == 0) {
bool allTestsPassed = tests();
if (allTestsPassed) {
printf("Все тесты прошли успешно");
} else {
printf("Некоторые тесты не были пройдены");
}
printf("\n");
return 0;
}

bool arr1[] = { false, false, false, false, true, false, false, false };
bool arr2[] = { false, false, false, false, false, false, true, false };
bool* p1 = arr1;
bool* p2 = arr2;
int res = compareTwoDecimalNumbers(p1, p2);
printResults(res);
return 0;
}
92 changes: 92 additions & 0 deletions src/Test2Dir/tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "lib.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool testConverToDecimal8bits()
{

int num1[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
int* p1 = num1;
if (convertToDecimal8bits(p1) != 0) {
printf("Тесты сломались на проверке перевода в десятичную сс (00000000 -> 0)\n");
return false;
}

int num2[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
int* p2 = num2;
if (convertToDecimal8bits(p2) != 255) {
printf("Тесты сломались на проверке перевода в десятичную сс (11111111 -> 255)\n");
return false;
}

int num3[] = { 0, 0, 0, 0, 0, 1, 0, 1 };
int* p3 = num3;
if (convertToDecimal8bits(p3) != 5) {
printf("Тесты сломались на проверке перевода в десятичную сс (00000101 -> 5)\n");
return false;
}
return true;
}

bool testConvertBoolToInt()
{

bool arr1[] = { false, true, false, true, false, true, false, true };
int correctArr1[] = { 0, 1, 0, 1, 0, 1, 0, 1 };
int* pointet_to_correctArr = correctArr1;
bool* p1 = arr1;
int* testArr = convertBoolElementsToInt(p1, 8);

for (int i = 0; i < 8; i++) {
if (testArr[i] != pointet_to_correctArr[i]) {
printf("Тесты сломались на проверке перевода массивов с булевыми значениями в целочисленные\n");
return false;
}
}
return true;
}

bool testcompareElements()
{
bool arr1[] = { false, false, false, false, false, false, false, true };
bool arr2[] = { false, false, false, false, false, false, true, false };
bool* p1 = arr1;
bool* p2 = arr2;
int res1 = compareTwoDecimalNumbers(p1, p2);
if (res1 != -1) {
printf("Тесты сломались на стандартной проверке (первое число больше второго)\n");
return false;
}

bool arr3[] = { false, false, false, false, true, true, false, false };
bool arr4[] = { false, false, false, false, false, false, true, false };
bool* p3 = arr3;
bool* p4 = arr4;
int res2 = compareTwoDecimalNumbers(p3, p4);
if (res2 != 1) {
printf("Тесты сломались на стандартной проверке (второе число больше первого)\n");
return false;
}

bool arr5[] = { false, false, false, false, true, true, true, false };
bool arr6[] = { false, false, false, false, true, true, true, false };
bool* p5 = arr5;
bool* p6 = arr6;
int res3 = compareTwoDecimalNumbers(p5, p6);
if (res3 != 0) {
printf("Тесты сломались на стандартной проверке (числа равны)\n");
return false;
}
return true;
}

bool tests()
{
bool allTestsPassed = false;
if (testConvertBoolToInt() && testcompareElements() && testConverToDecimal8bits()) {
allTestsPassed = true;
}
return allTestsPassed;
}