Git Mailing List Archive mirror
 help / color / mirror / Atom feed
From: Siddharth Singh <siddhartth@google.com>
To: git@vger.kernel.org
Cc: Siddharth Singh <siddhartth@google.com>
Subject: [RFC PATCH 1/1] khash_test.c: add unit tests
Date: Wed, 31 May 2023 17:51:42 +0200	[thread overview]
Message-ID: <20230531155142.3359886-2-siddhartth@google.com> (raw)
In-Reply-To: <20230531155142.3359886-1-siddhartth@google.com>

The tests check if a hashmap
1. Gets hash value for a string
2. Has correct value for '__ac_HASH_UPPER'
3. Initializes succesfully.
4. Put a key value pair.
5. Gets the value from key.
6. Delete the key-value pair.
7. Updates the value after deleting it.
8. Update Value of before deleting.
9. Contains the right number of elements.

Signed-off-by: Siddharth Singh <siddhartth@google.com>
---
 Makefile       |   1 +
 t/khash_test.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 174 insertions(+)
 create mode 100644 t/khash_test.c

diff --git a/Makefile b/Makefile
index 660c72d72e..59cb76e5e5 100644
--- a/Makefile
+++ b/Makefile
@@ -3852,6 +3852,7 @@ $(UNIT_TEST_PROGS): $(CTAP_OBJS) $(LIBS)
 	$(QUIET)mkdir -p t/unit-tests
 	$(QUIET_CC)$(CC) -It -o t/unit-tests/unit-test-t t/unit-test.c $(CTAP_OBJS)
 	$(QUIET_CC)$(CC) -It -o t/unit-tests/strbuf-t t/strbuf-test.c -DSKIP_COMMON_MAIN common-main.c $(CTAP_OBJS) $(LIBS)
+	$(QUIET_CC)$(CC) -It -o t/unit-tests/khash-t t/khash_test.c -DSKIP_COMMON_MAIN common-main.c $(CTAP_OBJS) $(LIBS)
 
 .PHONY: unit-tests
 unit-tests: $(UNIT_TEST_PROGS) $(UNIT_TEST_RUNNER)
diff --git a/t/khash_test.c b/t/khash_test.c
new file mode 100644
index 0000000000..cd27618d4d
--- /dev/null
+++ b/t/khash_test.c
@@ -0,0 +1,173 @@
+#include <tap/basic.h>
+#include "../git-compat-util.h"
+
+#include "../khash.h"
+
+int khash_testACX31HashString(){
+    const char * str = "foobar";
+    khint_t value = __ac_X31_hash_string(str);
+    khint_t expected = 3026088333;
+    if(value == expected)
+        return 1;
+    return 0;
+}
+
+int khash_testAC_HASH_UPPER(){
+    const double expected = 0.77;
+    if(expected == __ac_HASH_UPPER){
+        return 1;
+    }
+    return 0;
+}
+
+KHASH_INIT(str, const char *, int *, 1, kh_str_hash_func, kh_str_hash_equal);
+
+int khash_testInit(){
+    kh_str_t * hashmap = kh_init_str();
+    if(hashmap != NULL){
+        return 1;
+    }
+    return 0;
+}
+
+int khash_testPut(){
+    kh_str_t * hashmap = kh_init_str();
+    int ret;
+    int pos = kh_put_str(hashmap,"test_key",&ret);
+    int value = 14;
+    if(ret){
+        kh_key(hashmap, pos) = xstrdup("test_key");
+		kh_value(hashmap, pos) = &value;
+    }else return 0;
+    if(*kh_value(hashmap,pos) == value){
+        return 1;
+    }
+    return 0;
+}
+
+int khash_testGet(){
+    kh_str_t * hashmap = kh_init_str();
+    int ret;
+    int pos = kh_put_str(hashmap,"test_key",&ret);
+    int value = 14;
+    if(ret){
+        kh_key(hashmap, pos) = xstrdup("test_key");
+		kh_value(hashmap, pos) = &value;
+    }else return 0;
+    int value_pos = kh_get_str(hashmap,"test_key");    
+    int returned_value = *kh_value(hashmap,value_pos);
+    if(returned_value == value){
+        return 1;
+    }
+    return 0;
+}
+
+int khash_testDelete(){
+    kh_str_t * hashmap = kh_init_str();
+    int ret;
+    int pos = kh_put_str(hashmap,"test_key",&ret);
+    int value = 14;
+    if(ret){
+        kh_key(hashmap, pos) = xstrdup("test_key");
+		kh_value(hashmap, pos) = &value;
+    }else return 0;
+    int current_size = hashmap->size;
+    kh_del_str(hashmap,pos);
+    if(hashmap->size + 1 == current_size && !kh_exist(hashmap, pos)){
+        return 1;
+    }
+    return 0;
+}
+
+int khash_testUpdateValueAfterDeleting(){
+    kh_str_t * hashmap = kh_init_str();
+    int ret;
+    int pos1 = kh_put_str(hashmap,"test_key",&ret);
+    int value1 = 14;
+    if(ret){
+        kh_key(hashmap, pos1) = xstrdup("test_key");
+		kh_value(hashmap, pos1) = &value1;
+    }else return 0;
+
+    kh_del_str(hashmap,pos1);
+    int pos2 = kh_put_str(hashmap,"test_key",&ret);
+    int value2 = 15;
+    
+    if(ret){
+        kh_key(hashmap, pos2) = xstrdup("test_key");
+		kh_value(hashmap, pos2) = &value2;
+    }else return 0;
+
+    int value_pos = kh_get_str(hashmap,"test_key");    
+    int returned_value = *kh_value(hashmap,value_pos);
+    if(returned_value == value2){
+        return 1;
+    }
+    return 0;
+}
+
+int khash_testUpdateValueBeforeDeleting(){
+    kh_str_t * hashmap = kh_init_str();
+    int ret;
+    int pos1 = kh_put_str(hashmap,"test_key",&ret);
+    int value1 = 14;
+    if(ret){
+        kh_key(hashmap, pos1) = xstrdup("test_key");
+		kh_value(hashmap, pos1) = &value1;
+    }else return 0;
+
+    int pos2 = kh_put_str(hashmap,"test_key",&ret);
+    int value2 = 15;
+    
+    if(ret == 0){
+        return 1;
+    }
+    return 0;
+}
+
+int khash_testSize(){
+    kh_str_t * hashmap = kh_init_str();
+    int ret;
+    int pos1 = kh_put_str(hashmap,"test_key1",&ret);
+    int value1 = 14;
+    if(ret){
+        kh_key(hashmap, pos1) = xstrdup("test_key1");
+		kh_value(hashmap, pos1) = &value1;
+    }else return 0;
+
+    int pos2 = kh_put_str(hashmap,"test_key2",&ret);
+    int value2 = 15;
+    if(ret){
+        kh_key(hashmap, pos2) = xstrdup("test_key2");
+		kh_value(hashmap, pos2) = &value2;
+    }else return 0;
+
+
+    int pos3 = kh_put_str(hashmap,"test_key3",&ret);
+    int value3 = 16;
+    if(ret){
+        kh_key(hashmap, pos3) = xstrdup("test_key3");
+		kh_value(hashmap, pos3) = &value3;
+    }else return 0;
+
+    if(kh_size(hashmap) == 3){
+        return 1;
+    }
+    return 0;
+}
+
+int main(void){
+    plan(9);
+    
+    ok(khash_testACX31HashString(),"__ac_X31_hash_string works");
+    ok(khash_testAC_HASH_UPPER(),"__ac_HASH_UPPER has correct value");
+    ok(khash_testInit(), "Initialize hashmap");
+    ok(khash_testPut(), "Put Key-Value pair");
+    ok(khash_testGet(), "Get the value from hashmap");
+    ok(khash_testDelete(), "Delete the key-value from hashmap");
+    ok(khash_testUpdateValueAfterDeleting(), "Update Value of a Key after deleting");
+    ok(khash_testUpdateValueBeforeDeleting(), "Update Value of a Key before deleting");
+    ok(khash_testSize(),"Test if 3 elements are present in hashmap");
+
+    return 0;
+}
\ No newline at end of file
-- 
2.40.1.606.ga4b1b128d6-goog


  reply	other threads:[~2023-05-31 15:53 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-31 15:51 [RFC PATCH 0/1] Unit tests for khash.h Siddharth Singh
2023-05-31 15:51 ` Siddharth Singh [this message]
2023-05-31 22:35 ` Taylor Blau
2023-06-01  6:26   ` Junio C Hamano
2023-06-01 13:02   ` Phillip Wood
2023-05-31 23:35 ` Eric Wong
2023-06-01 13:23 ` Phillip Wood
2023-06-27 13:20 ` [RFC PATCH v2] khash_test.c : add unit tests Siddharth Singh
2023-06-30  0:53   ` Glen Choo
2023-07-07 15:04     ` Siddharth Singh
2023-07-07 21:12       ` Glen Choo
2023-06-30  1:05   ` Glen Choo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230531155142.3359886-2-siddhartth@google.com \
    --to=siddhartth@google.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).