-
Notifications
You must be signed in to change notification settings - Fork 0
Homework tests #7
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
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 |
|---|---|---|
|
|
@@ -53,3 +53,5 @@ dkms.conf | |
|
|
||
| # debug information files | ||
| *.dwo | ||
|
|
||
| *build/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #include <stdio.h> | ||
| int main() | ||
| { | ||
| printf("Hello, world!\n"); | ||
|
|
||
|
|
||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #include "list.h" | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| bool testInsertIntoEmpty(void) | ||
| { | ||
| Node* head = NULL; | ||
| insertSorted(&head, 10); | ||
| bool result = (head != NULL && head->value == 10 && head->next == NULL); | ||
| freeList(head); | ||
| return result; | ||
| } | ||
|
|
||
| bool testInsertCorrectOrder(void) | ||
| { | ||
| Node* head = NULL; | ||
| insertSorted(&head, 5); | ||
| insertSorted(&head, 3); | ||
| insertSorted(&head, 7); | ||
| insertSorted(&head, 5); | ||
|
|
||
| bool result = head != NULL && head->value == 3 && head->next != NULL && head->next->value == 5 && head->next->next != NULL && head->next->next->value == 5 && head->next->next->next != NULL && head->next->next->next->value == 7 && head->next->next->next->next == NULL; | ||
|
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. Ой нет. Напишите функцию, которая сравнивает список с массивом. Так будет сиииильно лучше. |
||
|
|
||
| freeList(head); | ||
| return result; | ||
| } | ||
|
|
||
| bool testDeleteExisting(void) | ||
| { | ||
| Node* head = NULL; | ||
| insertSorted(&head, 1); | ||
| insertSorted(&head, 3); | ||
| insertSorted(&head, 5); | ||
|
|
||
| bool result = deleteValue(&head, 3) == 1 && head != NULL && head->value == 1 && head->next != NULL && head->next->value == 5 && head->next->next == NULL; | ||
|
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. Аналогично
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. И далее тоже |
||
|
|
||
| freeList(head); | ||
| return result; | ||
| } | ||
|
|
||
| bool testDeleteNonExisting(void) | ||
| { | ||
| Node* head = NULL; | ||
| insertSorted(&head, 4); | ||
| insertSorted(&head, 6); | ||
|
|
||
| bool result = deleteValue(&head, 100) == 0; | ||
| freeList(head); | ||
| return result; | ||
| } | ||
|
|
||
| bool testDeleteFromEmpty(void) | ||
| { | ||
| Node* head = NULL; | ||
| return deleteValue(&head, 5) == 0; | ||
| } | ||
|
|
||
| bool testDeleteFirstLast(void) | ||
| { | ||
| Node* head = NULL; | ||
| insertSorted(&head, 2); | ||
| insertSorted(&head, 4); | ||
| insertSorted(&head, 6); | ||
|
|
||
| bool result = deleteValue(&head, 2) == 1 && deleteValue(&head, 6) == 1 && head != NULL && head->value == 4 && head->next == NULL; | ||
|
|
||
| freeList(head); | ||
| return result; | ||
| } | ||
|
|
||
| int main(int argc, char* argv[]) | ||
| { | ||
| if (argc >1 && strcmp(argv[1], "--test")==0){ | ||
| if (!testInsertIntoEmpty() || | ||
| !testInsertCorrectOrder() || | ||
| !testDeleteExisting() || | ||
| !testDeleteNonExisting() || | ||
| !testDeleteFromEmpty() || | ||
| !testDeleteFirstLast()) { | ||
| printf("Tests failed\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| printf("All tests passed\n"); | ||
| return 0; | ||
|
|
||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include "list.h" | ||
|
|
||
|
|
||
| void insertSorted(Node** head, int value) { | ||
| Node* newNode = malloc(sizeof(Node)); | ||
| newNode->value = value; | ||
| newNode->next = NULL; | ||
|
|
||
| if (*head == NULL || value < (*head)->value) { | ||
| newNode->next = *head; | ||
| *head = newNode; | ||
| return; | ||
| } | ||
|
|
||
|
|
||
| Node* current = *head; | ||
| while (current->next != NULL && current->next->value < value) { | ||
| current = current->next; | ||
| } | ||
|
|
||
| newNode->next = current->next; | ||
| current->next = newNode; | ||
| } | ||
|
|
||
| int deleteValue(Node** head, int value) { | ||
| Node* current = *head; | ||
| Node* prev = NULL; | ||
|
|
||
| while (current != NULL && current->value != value) { | ||
| prev = current; | ||
| current = current->next; | ||
| } | ||
|
|
||
| if (current == NULL) return 0; | ||
|
|
||
| if (prev == NULL) *head = current->next; | ||
| else prev->next = current->next; | ||
|
|
||
| free(current); | ||
| return 1; | ||
| } | ||
|
|
||
| void printList(Node* head) { | ||
| if (head == NULL) { | ||
| printf("Список пуст.\n"); | ||
| return; | ||
| } | ||
|
|
||
| printf("Список: "); | ||
| while (head != NULL) { | ||
| printf("%d ", head->value); | ||
| head = head->next; | ||
| } | ||
| printf("\n"); | ||
| } | ||
|
|
||
|
|
||
| void freeList(Node* head) { | ||
| while (head != NULL) { | ||
| Node* temp = head; | ||
| head = head->next; | ||
| free(temp); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #pragma once | ||
|
|
||
| typedef struct Node { | ||
| int value; | ||
| struct Node* next; | ||
| } Node; | ||
|
|
||
| void insertSorted(Node** head, int value); | ||
| int deleteValue(Node** head, int value); | ||
| void printList(Node* head); | ||
| void freeList(Node* head); | ||
|
|
||
|
|
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.
Вообще этого здесь быть не должно