-
Notifications
You must be signed in to change notification settings - Fork 0
Test 2.1 - Task 2 - [Hashset]
#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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ project(test_2_1) | |
| set(testName "${PROJECT_NAME}") | ||
|
|
||
| add_subdirectory(task_1) | ||
| add_subdirectory(task_2) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| project("${testName}_task_2") | ||
|
|
||
| add_library(hashset hashset.c) | ||
|
|
||
| add_executable(${PROJECT_NAME}_test test.c) | ||
| target_link_libraries(${PROJECT_NAME}_test hashset) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #include "hashset.h" | ||
|
|
||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| typedef struct Hashset { | ||
| char *elements[HASHSET_SIZE]; | ||
| } Hashset; | ||
|
|
||
| bool createHashset(Hashset **hashset) { | ||
| *hashset = malloc(sizeof(Hashset)); | ||
| if (*hashset == NULL) { | ||
| return false; | ||
| } | ||
|
|
||
| for (int i = 0; i < HASHSET_SIZE; ++i) { | ||
| (*hashset)->elements[i] = NULL; | ||
ilya-krivtsov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| static unsigned int getStringHash(const char *string) { | ||
| unsigned int hash = 0; | ||
| for (int i = 0; string[i] != '\0'; ++i) { | ||
| hash = hash * 57 + string[i]; | ||
| } | ||
| return hash; | ||
| } | ||
|
|
||
| bool addToHashset(Hashset *hashset, const char *string) { | ||
| if (string == NULL) { | ||
ilya-krivtsov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return false; | ||
| } | ||
|
|
||
| unsigned int elementIndex = getStringHash(string) % HASHSET_SIZE; | ||
|
|
||
| int counter = 0; | ||
| while (hashset->elements[elementIndex] != NULL) { | ||
ilya-krivtsov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| elementIndex = (elementIndex + 1) % HASHSET_SIZE; | ||
| ++counter; | ||
| if (counter >= HASHSET_SIZE) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| char *stringCopy = strdup(string); | ||
| if (stringCopy == NULL) { | ||
| return false; | ||
| } | ||
|
|
||
| hashset->elements[elementIndex] = stringCopy; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool containedInHashset(Hashset *hashset, const char *string) { | ||
| unsigned int elementIndex = getStringHash(string) % HASHSET_SIZE; | ||
|
|
||
| if (hashset->elements[elementIndex] == NULL) { | ||
| return false; | ||
| } | ||
|
|
||
| int counter = 0; | ||
| while (strcmp(hashset->elements[elementIndex], string) != 0) { | ||
| elementIndex = (elementIndex + 1) % HASHSET_SIZE; | ||
| ++counter; | ||
| if (counter >= HASHSET_SIZE) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| void disposeHashset(Hashset *hashset) { | ||
| for (int i = 0; i < HASHSET_SIZE; ++i) { | ||
| free(hashset->elements[i]); | ||
| } | ||
| free(hashset); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #pragma once | ||
|
|
||
| #define HASHSET_SIZE 256 | ||
|
|
||
| /// @brief Hashset of strings | ||
| typedef struct Hashset Hashset; | ||
|
|
||
| /// @brief Creates new hashset | ||
| /// @param hashset Pointer to store new hashset to | ||
| /// @return `true` if created successfully, `false` otherwise (allocation failed) | ||
| bool createHashset(Hashset **hashset); | ||
|
|
||
| /// @brief Adds string to hashtable | ||
| /// @param hashset Hashset to add string to | ||
| /// @param string String to add | ||
| /// @return `true` if added successfully, `false` otherwise (string was `NULL`, allocation failed, no space for new entries left) | ||
| bool addToHashset(Hashset *hashset, const char *string); | ||
|
|
||
| /// @brief Searchs for specified string | ||
| /// @param hashset Hashset to search string in | ||
| /// @param string String to search | ||
| /// @return `true` if found, `false` otherwise | ||
| bool containedInHashset(Hashset *hashset, const char *string); | ||
|
|
||
| /// @brief Disposes hashtable | ||
| /// @param hashset Hashtable to dispose | ||
| void disposeHashset(Hashset *hashset); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #define CTEST_MAIN | ||
| #define CTEST_SEGFAULT | ||
| #include "../../ctest/ctest.h" | ||
|
|
||
| #include <stdlib.h> | ||
|
|
||
| #include "hashset.h" | ||
|
|
||
| int main(int argc, const char *argv[]) { | ||
| return ctest_main(argc, argv); | ||
| } | ||
|
|
||
| Hashset *createNewHashset(void) { | ||
| Hashset *hashset = NULL; | ||
| ASSERT_TRUE(createHashset(&hashset)); | ||
| return hashset; | ||
| } | ||
|
|
||
| CTEST(hashsetTests, createTest) { | ||
| Hashset *hashset = createNewHashset(); | ||
| disposeHashset(hashset); | ||
| } | ||
|
|
||
| CTEST(hashsetTests, singleElemenTest) { | ||
| Hashset *hashset = createNewHashset(); | ||
|
|
||
| ASSERT_FALSE(containedInHashset(hashset, "element")); | ||
| ASSERT_TRUE(addToHashset(hashset, "element")); | ||
| ASSERT_TRUE(containedInHashset(hashset, "element")); | ||
| ASSERT_FALSE(containedInHashset(hashset, "not_element")); | ||
|
|
||
| disposeHashset(hashset); | ||
| } | ||
|
|
||
| CTEST(hashsetTests, randomElementsTest) { | ||
| char *elements[HASHSET_SIZE] = { 0 }; | ||
|
|
||
| const char *elementTemplate = "key_000"; | ||
| const int elementOffset = strlen("key_"); | ||
| const int digitsCount = strlen(elementTemplate) - elementOffset; | ||
|
|
||
| for (int i = 0; i < HASHSET_SIZE; ++i) { | ||
| char *element = strdup(elementTemplate); | ||
|
|
||
| ASSERT_NOT_NULL(element); | ||
|
|
||
| elements[i] = element; | ||
|
|
||
| int number = i; | ||
| for (int j = digitsCount - 1; j >= 0; --j) { | ||
| char digit = (number % 10) + '0'; | ||
| element[elementOffset + j] = digit; | ||
| number /= 10; | ||
| } | ||
| } | ||
|
|
||
| Hashset *hashset = createNewHashset(); | ||
|
|
||
| srand(453563458); | ||
| for (int i = 0; i < HASHSET_SIZE; ++i) { | ||
| int index = (rand() % (HASHSET_SIZE - i)) + i; | ||
|
|
||
| ASSERT_TRUE(addToHashset(hashset, elements[i])); | ||
| } | ||
|
|
||
| for (int i = 0; i < HASHSET_SIZE; ++i) { | ||
| ASSERT_TRUE(containedInHashset(hashset, elements[i])); | ||
| } | ||
|
|
||
| ASSERT_FALSE(containedInHashset(hashset, "random_element")); | ||
| ASSERT_FALSE(containedInHashset(hashset, "does_not_exist")); | ||
|
|
||
| for (int i = 0; i < HASHSET_SIZE; ++i) { | ||
| free(elements[i]); | ||
| } | ||
|
|
||
| disposeHashset(hashset); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.