-
Notifications
You must be signed in to change notification settings - Fork 206
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
Solving problems: key duplication, incorrect hashmap size change on e… #15
Open
kostalski
wants to merge
2
commits into
petewarden:master
Choose a base branch
from
kostalski:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 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
This file contains 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,114 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <assert.h> | ||
|
||
#include "hashmap.h" | ||
|
||
/* | ||
* This test cover problem reported at https://github.com/petewarden/c_hashmap/issues/6 . | ||
*/ | ||
void test_key_duplication_issue() | ||
{ | ||
int status; | ||
map_t hmap; | ||
unsigned int colliding_keys_hash; | ||
char *colliding_key1 = "123"; | ||
char *colliding_key2 = "157"; | ||
char *res; | ||
|
||
printf("Running test_key_duplication_issue..."); | ||
|
||
hmap = hashmap_new(); | ||
|
||
/* 1. Preparation: */ | ||
/* - Check if two provided keys are colliding (same hash value). */ | ||
colliding_keys_hash = hashmap_hash_int(hmap, colliding_key1); | ||
|
||
/* - If assertion below is failing, this mean that hash function */ | ||
/* has beed updated and keys are not collidinig any more. */ | ||
/* Provided keys colliding_key1, colliding_key2 have to be updated, */ | ||
/* so they collide again. */ | ||
assert(hashmap_hash_int(hmap, colliding_key2) == colliding_keys_hash); | ||
|
||
/* 2. Setup data in hash map: */ | ||
/* - Put test data into map. Two colliding keys (same hash value). */ | ||
status = hashmap_put(hmap, colliding_key1, colliding_key1); | ||
assert(status==MAP_OK); | ||
|
||
status = hashmap_put(hmap, colliding_key2, colliding_key2); | ||
assert(status==MAP_OK); | ||
|
||
/* - Check if keys exists in hash map. */ | ||
assert(hashmap_get(hmap, colliding_key1, (void **)(&res)) == MAP_OK); | ||
assert(res == colliding_key1); | ||
|
||
assert(hashmap_get(hmap, colliding_key2, (void **)(&res)) == MAP_OK); | ||
assert(res == colliding_key2); | ||
|
||
|
||
/* 3. Remove first key - colliding_key1. */ | ||
assert(hashmap_remove(hmap, colliding_key1) == MAP_OK); | ||
|
||
/* - Check if colliding_key2 is present. */ | ||
assert(hashmap_get(hmap, colliding_key2, (void **)(&res)) == MAP_OK); | ||
|
||
/* 4. Put again colliding_key2. If problem occur then in hash map there */ | ||
/* can be placed key duplicate. */ | ||
assert(hashmap_put(hmap, colliding_key2, colliding_key2) == MAP_OK); | ||
|
||
/* 5. Remove colliding_key2 from hash map. Only first instance will be removed. */ | ||
assert(hashmap_remove(hmap, colliding_key2) == MAP_OK); | ||
|
||
/* 6. Check if colliding_key2 exists in hash map. */ | ||
/* After calling hashmap_remove() user expect not to find key. */ | ||
/* Should be removed, but still present in map. */ | ||
assert(hashmap_get(hmap, colliding_key2, (void **)(&res)) == MAP_MISSING); | ||
|
||
/* Cleanup. */ | ||
hashmap_free(hmap); | ||
|
||
printf("ok\n"); | ||
} | ||
|
||
/* | ||
* This test cover problem reported at https://github.com/petewarden/c_hashmap/issues/8 . | ||
*/ | ||
void test_map_size_increase_on_entry_overwrite_issue() | ||
{ | ||
int status; | ||
map_t hmap; | ||
char *test_key = "123"; | ||
|
||
printf("Running test_map_size_increase_on_entry_overwrite_issue..."); | ||
|
||
/* 1. Check initial size of hashmap. */ | ||
hmap = hashmap_new(); | ||
assert(hashmap_length(hmap) == 0); | ||
|
||
/* 2. Add single element and check map size. */ | ||
status = hashmap_put(hmap, test_key, test_key); | ||
assert(status==MAP_OK); | ||
assert(hashmap_length(hmap) == 1); | ||
|
||
/* 3. Add again same element. */ | ||
status = hashmap_put(hmap, test_key, test_key); | ||
assert(status==MAP_OK); | ||
|
||
/* 4. No change in size is expected on same key insert. */ | ||
assert(hashmap_length(hmap) == 1); | ||
|
||
/* Cleanup. */ | ||
hashmap_free(hmap); | ||
|
||
printf("ok\n"); | ||
} | ||
|
||
/* | ||
* For quick run all tests call in command line: | ||
* gcc tests.c hashmap.c -o tests && ./tests | ||
*/ | ||
int main(char* argv, int argc) | ||
{ | ||
test_key_duplication_issue(); | ||
test_map_size_increase_on_entry_overwrite_issue(); | ||
} |
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.
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.
Why do I need different colliding keys for different architectures?
If I run this test on an iPhone Simulator it works, if I run it in an Android Simulator it doesn't ("717" works in Android Simulator)?
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.
Hi Andreas,
It's problem of big-endianess and some settings for Android NDK.
Functions:
are designed for calculating hash for strings and assume, they are working on lilte-endian platform. They will return different results for big-endian platforms and rather not correct ;)
Android supports little and big endianess. Try to setup to little-endian - it should solve the problem (more info: https://stackoverflow.com/questions/6212951/endianness-of-android-ndk).