eina: add test case for eina_hash_crc function.
authorvivek <vivek.ellur@samsung.com>
Tue, 28 Apr 2015 21:34:34 +0000 (23:34 +0200)
committerCedric BAIL <cedric@osg.samsung.com>
Thu, 7 May 2015 07:53:11 +0000 (09:53 +0200)
Summary:
Added test case for eina_hash_crc function which uses crc32 for hashing
algorithm

Signed-off-by: vivek <vivek.ellur@samsung.com>
Reviewers: cedric

Subscribers: cedric

Differential Revision: https://phab.enlightenment.org/D2296

Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
src/tests/eina/eina_test_hash.c

index 0471d5e..b2f9704 100644 (file)
 
 #include "eina_suite.h"
 #include "Eina.h"
+#include "eina_hash.h"
+
+#define EINA_HASH_BUCKET_SIZE       8
+
+static unsigned int
+_eina_string_key_length(const char *key)
+{
+   if (!key)
+     return 0;
+
+   return (int)strlen(key) + 1;
+}
+
+static int
+_eina_string_key_cmp(const char *key1, int key1_length,
+                     const char *key2, int key2_length)
+{
+   int delta;
+
+   delta = key1_length - key2_length;
+   if (delta) return delta;
+   return strcmp(key1, key2);
+}
 
 static Eina_Bool
 eina_foreach_check(EINA_UNUSED const Eina_Hash *hash,
@@ -103,6 +126,67 @@ START_TEST(eina_hash_simple)
 }
 END_TEST
 
+START_TEST(eina_test_hash_crc)
+{
+   Eina_Hash *hash = NULL;
+   int *test;
+   int array[] = { 1, 42, 4, 5, 6 };
+
+   fail_if(eina_init() != 2);
+
+   hash = eina_hash_new(EINA_KEY_LENGTH(_eina_string_key_length),
+                        EINA_KEY_CMP(_eina_string_key_cmp),
+                        EINA_KEY_HASH(eina_hash_crc),
+                        NULL,
+                        EINA_HASH_BUCKET_SIZE);
+   fail_if(hash == NULL);
+
+   fail_if(eina_hash_add(hash, "1", &array[0]) != EINA_TRUE);
+   fail_if(eina_hash_add(hash, "42", &array[1]) != EINA_TRUE);
+   fail_if(eina_hash_direct_add(hash, "4", &array[2]) != EINA_TRUE);
+   fail_if(eina_hash_direct_add(hash, "5", &array[3]) != EINA_TRUE);
+   fail_if(eina_hash_add(hash, "", "") != EINA_TRUE);
+
+   test = eina_hash_find(hash, "4");
+   fail_if(!test);
+   fail_if(*test != 4);
+
+   test = eina_hash_find(hash, "42");
+   fail_if(!test);
+   fail_if(*test != 42);
+
+   eina_hash_foreach(hash, eina_foreach_check, NULL);
+
+   test = eina_hash_modify(hash, "5", &array[4]);
+   fail_if(!test);
+   fail_if(*test != 5);
+
+   test = eina_hash_find(hash, "5");
+   fail_if(!test);
+   fail_if(*test != 6);
+
+   fail_if(eina_hash_population(hash) != 5);
+
+   fail_if(eina_hash_find(hash, "120") != NULL);
+
+   fail_if(eina_hash_del(hash, "5", NULL) != EINA_TRUE);
+   fail_if(eina_hash_find(hash, "5") != NULL);
+
+   fail_if(eina_hash_del(hash, NULL, &array[2]) != EINA_TRUE);
+   fail_if(eina_hash_find(hash, "4") != NULL);
+
+   fail_if(eina_hash_del(hash, NULL, &array[2]) != EINA_FALSE);
+
+   fail_if(eina_hash_del(hash, "1", NULL) != EINA_TRUE);
+   fail_if(eina_hash_del(hash, "42", NULL) != EINA_TRUE);
+
+   eina_hash_free(hash);
+
+   /* Same comment as eina_init */
+        fail_if(eina_shutdown() != 1);
+}
+END_TEST
+
 START_TEST(eina_hash_extended)
 {
    Eina_Hash *hash = NULL;
@@ -337,6 +421,7 @@ END_TEST
 void eina_test_hash(TCase *tc)
 {
    tcase_add_test(tc, eina_hash_simple);
+   tcase_add_test(tc, eina_test_hash_crc);
    tcase_add_test(tc, eina_hash_extended);
    tcase_add_test(tc, eina_hash_double_item);
    tcase_add_test(tc, eina_hash_all_int);