-
Notifications
You must be signed in to change notification settings - Fork 0
Test2. Kalsina Yana. #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e492802
6333ba5
0c8df21
4618865
caa5ac3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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() |
| 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. И, кстати, числа-то неотрицательные, почему тогда здесь везде |
||
|
|
||
| /* | ||
| функция перевода массива с булевыми элементами в целочисленные 0 и 1 | ||
| на вход принимает указатель на массив и его длину | ||
| возвращает указатель на новый массив. | ||
| */ | ||
| int* convertBoolElementsToInt(bool* arr, int length); | ||
|
Comment on lines
+13
to
+18
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ненужная функция. При касте bool превращается в 0 и 1 соответственно, т.е. их |
||
|
|
||
| /* функция (собирающая все другие функции) | ||
| она сравнивает два числа | ||
| на вход пинимает два указателя на массивы с булевыми значениями | ||
| возвращает 1, -1, 0 в зависимости от результата сравнения. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А какая зависимость? Это нужно описывать. |
||
| */ | ||
| int compareTwoDecimalNumbers(bool* arr1, bool* arr2); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А что говорилось про смысл выражений типа |
||
| 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не-а. Сравнивать нужно было массивы |
||
| 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[]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачем такие сложности, если без флага |
||
| { | ||
| 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; | ||
| } | ||
| 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; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А почему у хедера нет файла с реализацией?