From f766d17fb0a27c9f6bfef535135c8382a7969007 Mon Sep 17 00:00:00 2001 From: hwwang Date: Fri, 14 Jun 2024 17:36:00 +0800 Subject: [PATCH] add all --- runtime/Utest_py/ctest.py | 46 +- runtime/Utest_py/hash | Bin 0 -> 22632 bytes runtime/Utest_py/test.c | 49 + runtime/Utest_py/test.html | 7392 ++++++++++++++++++++++++++++++++++++ runtime/include/hash.h | 144 + runtime/tests/tree/main.c | 0 6 files changed, 7613 insertions(+), 18 deletions(-) create mode 100755 runtime/Utest_py/hash create mode 100644 runtime/Utest_py/test.c create mode 100644 runtime/Utest_py/test.html create mode 100644 runtime/include/hash.h create mode 100644 runtime/tests/tree/main.c diff --git a/runtime/Utest_py/ctest.py b/runtime/Utest_py/ctest.py index 8c88eff..fa34f24 100644 --- a/runtime/Utest_py/ctest.py +++ b/runtime/Utest_py/ctest.py @@ -1,43 +1,53 @@ -import unittest,importlib,BeautifulReport,cffi +import unittest,importlib,BeautifulReport +import cffi def load(): - src = open("add.c").read() - inc = open("add.h").read() + with open("/home/hai/sledge-serverless-framework/runtime/include/hash.h", "r") as f: + inc = "" + for line in f: + if not line.strip().startswith('#'): + inc += line + src = open("/home/hai/sledge-serverless-framework/runtime/Utest_py/hash.c").read() builder = cffi.FFI() builder.cdef(inc) - builder.set_source("addLib",src) + builder.set_source("hashlib",src) builder.compile() - md = importlib.import_module("addLib") + md = importlib.import_module("hashlib") return md.lib md = load() - -class AddTestCase(unittest.TestCase): +class HashTableTestCase(unittest.TestCase): def test_case1(self): ''' - 第1个case - :return: + 测试添加和查找功能 ''' - self.assertEqual(md.addition(1,2),1+2) - print('md.addition(1,2),1+2') + table = md.create_table() + md.add_item(table, b"key1", "Hello World") + value = md.find_value(table, b"key1") + self.assertEqual(value, "Hello World") + print('Value for "key1":', value) def test_case2(self): ''' - 第2个case - :return: + 测试查找不存在的键 ''' - self.assertEqual(md.addition(1,5),1+2) - - print('md.addition(1,5),1+2') + table = md.create_table() + value = md.find_value(table, b"nonexistent") + self.assertIsNone(value) + print('Value for "nonexistent":', value) + + def test_case3(self): + # 确保每个测试后表被释放 + md.free_table(self.table) sut = unittest.TestSuite() -sut.addTest(unittest(AddTestCase)) +sut.addTest(unittest.makeSuite(HashTableTestCase)) run = BeautifulReport.BeautifulReport(sut) -run.report(filename="test.html",description="add单元测试") +run.report(filename="test.html",description="单元测试") diff --git a/runtime/Utest_py/hash b/runtime/Utest_py/hash new file mode 100755 index 0000000000000000000000000000000000000000..b6872dd6dc54b3e1ae06f2ccb669ede8a4890f5e GIT binary patch literal 22632 zcmeHPd2m$6neR8FmPU*;x{#0nqY;C08CP*{N930-* zV9WMek*NZ2oH$ivm!z^~uS2ppo1|>x*doaqvL;#VN(4EomUY>VRCab1L)OIC?C-2ZOSWw2P;@&W=~Yr@95_>wnTEQA1W9kARGcq48nR-ElOCt6!maQ- zPAR9M=BNC*C7)T~&8qwwX6jLF_S8weJ*%?eDHWFs3TizXKjcG_eDBbo(Jn%p9w@vBYStIROVL+0ZhT7k)(4)Bg zSCek$?aJRo?QlZHUqcmMR^JoqXj)v~6R7J6g%bzr4lHe|Yg+7yMLY{d5vW{l{HafE z+_ICw%n;F37@6&`#I-%*O;vo7kDYUM-E-IZTTB1yM-@-s_~*svb6;3QGE{ETAsI?! zPj?Z*DNjR_WJElS`;cQcqLt$RO7IuG%ibB(!-jZSsSp^+S<^NU#-k5|?zRd8C;#Kg zQ@P`bZoyFi~C@q*B1)IK_D27g5f{lGdn|JUr*@npv6Kc z9E!8PXeb=-v~Ut(VZC#IG!zet!uIwtazR6-iEV6eTi0SP^ekGO$}aRQVP@No%_wX% z*cFP!gV7zETY4hl;0|9$57n=$HxgDQG^Oacf(Voi_tUOOb_AAVPpS9!bp0)*6naYHFCF_k2QUdqC1*}8q;)*Us@ySG*)$4h;liD zuICJ*k7UrN5J9D}47y7b0k37y)hSzWbw8r{DYJhbQ1W@8(-_w2a0Z>`A)Ox1po{aT zVjRt&i+M=VAJ3qZ51meA&~=Q6_hbfL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$)hA+6`Zhs*m)~EH{%$rcR$nOS3Mc9t z7wLUAQc!QYxlXVngxAT1t&bI{k00y{W(zX4fd{XR2ZEj0$%DXSLy(v@Xk+A`+t3$b3S=dIsOY_#>@)vQj=U1Dr>& z?4slrM_dj9jwe40bQ>#XV{{iPBe{_$x?5csXx1V{*Z27~ofdBf-s`VzmJ})=aw<7_eW8uZQU8P1;Ky z(KV6*r%ym%IRX84(8({e_@$fr6AAwHwDz?~} zf_chK0+uuv^zrJ|0{U!=ETMOiybYt!@?0n|ft`NP$Ndh;ZT@Jy(G$Um+1C-mDYJ`# z5Q`@|J8@o1f6Bv*_nLnCm5E2v2eFQcM{y&K&WvUt(c6n93l=gd%%sI_mZ&<_3C_!`!iUU3;r395m}MtBB93SU>8L;^Mn2ANQ>lxeiQ8lP&ttOcucm ziVGysEb1FDS^Vq;pZWq#mPFPUYb+eHEx+}mR&W-6_P1cnKrCW*`@#YGP7JNZ;(<`u zq&2KwQ>YK&$hB*hEqQ^GMN;eJj#x|;K`fQ!w`{TnO?;w8w-4@lIo*=GK#o+D^z?#OS4LQoChR(y4uQvo^QXS36 zJeXd3F>8Pp>_~J$(iiT+T%^!YxHF>jzK#yM##S7BQ15pkSxU;BL~#59{+Ag^8tQd{F29!3@LuTC-V}|G zC_xQ%`&rAc3vdJ(Y7?!m_seK_NeOEI*7|={>913AdjE}vZdGB@r!_ZR3TG;s74BE*z*bg=>Z&`afvZVn)*6cf0xl(UX5$`N9fQq z4>jyl*UjB^WG&raC6>mu{)b>t-_`p1`c8jmQ-4pB#vdiEr`z${pivdIzTWS)r=Fy- z;YA$N2d%GRE|fF%S+kODkWl;47POwm>47lOw10QAff&S`c(!&bfv6Vxse#-><2Ukdi zhjm~l(5LMdWb?Xuu8|6zmI0&vYbft4T(95NBK0?EL5qJ)CTOXmkbGvnROsUp&H +typedef struct test +{ + char *name; +}test; + +int main(int argc, char *argv[]) { + test *test1 = malloc(sizeof(test)); + test *test2 = malloc(sizeof(test)); + test1->name = "test1"; + test2->name = "test2"; + HashTable *myTable = create_table(); + add_item(myTable, "key1", test1); + add_item(myTable, "key2", test2); + char *input = argv[1]; + test *value = find_value(myTable, input); + if (value) { + printf("Test Passed: %s\n", value->name); + } else { + printf("Test Failed: Key not found.\n"); + } + remove_item(myTable, "key1"); + printf("remove key1.\n"); + test *value2 = find_value(myTable, input); + if (value2) { + printf("Test Passed: %s\n", value2->name); + } else { + printf("Test Failed: Key not found.\n"); + } + add_item(myTable, "key1", test1); + test *value4 = find_value(myTable, "key1"); + if (value4) { + printf("Test Passed: %s\n", value4->name); + } else { + printf("Test Failed: Key not found.\n"); + } + test *value3 = find_value(myTable, "key2"); + if (value3) { + printf("Test Passed: %s\n", value3->name); + } else { + printf("Test Failed: Key not found.\n"); + } + free(test1); + free(test2); + free_table(myTable); + return 0; +} diff --git a/runtime/Utest_py/test.html b/runtime/Utest_py/test.html new file mode 100644 index 0000000..1d356f2 --- /dev/null +++ b/runtime/Utest_py/test.html @@ -0,0 +1,7392 @@ + + + + + 测试报告 + + + + + + + + +
+
+ 测试报告 +
+
+
+
+
+
+
+
报告汇总
+ +
+
+
+
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+
+
+
+
+
+
+
+
+ + +
+
+
+
+
详细数据
+ +
+
+
+ + + + +
+ +
+
+ + + + + + + + + + + + + + +
编号测试类测试方法用例描述运行时长结果操作
+
+
+
+
+
+ + + + + + + diff --git a/runtime/include/hash.h b/runtime/include/hash.h new file mode 100644 index 0000000..76ae5e9 --- /dev/null +++ b/runtime/include/hash.h @@ -0,0 +1,144 @@ +#ifndef HASH_H +#define HASH_H + +#include +#include +#include +#include +#include +#define TABLE_SIZE 32 // the size of hash table +#define MAX_KEY_LENGTH 32 // the maximum length of key +typedef struct { + char key[MAX_KEY_LENGTH]; + void *value; + int is_deleted; // Flag to mark items as deleted +} HashItem; + +typedef struct { + HashItem **items; + pthread_mutex_t lock; +} HashTable; + +static inline unsigned long +hash_function(const char *str) { + unsigned long i = 0; + for (int j = 0; str[j]; j++) + i += str[j]; + return i % TABLE_SIZE; +} + +static inline HashTable +*create_table() { + HashTable *table = (HashTable *)malloc(sizeof(HashTable)); + if (!table) { + fprintf(stderr, "Failed to allocate memory for hash table struct.\n"); + return NULL; + } + + table->items = (HashItem **)malloc(sizeof(HashItem*) * TABLE_SIZE); + if (!table->items) { + fprintf(stderr, "Failed to allocate memory for items.\n"); + free(table); // Free the table if item allocation fails + return NULL; + } + + pthread_mutex_init(&table->lock, NULL); + + for (int i = 0; i < TABLE_SIZE; i++) { + table->items[i] = NULL; + } + return table; +} + +static inline void +add_item(HashTable *table, const char *key, void *value) { + assert(table != NULL); + assert(key != NULL); + assert(value != NULL); + + pthread_mutex_lock(&table->lock); + + unsigned long index = hash_function(key); + HashItem *item = malloc(sizeof(HashItem)); + if (!item) { + fprintf(stderr, "Failed to allocate memory for a new item.\n"); + pthread_mutex_unlock(&table->lock); + return; + } + strcpy(item->key, key); + item->value = value; + item->is_deleted = 0; + + while (table->items[index] != NULL && !table->items[index]->is_deleted && strcmp(table->items[index]->key, key) != 0) { + index = (index + 1) % TABLE_SIZE; + } + + free(table->items[index]); // Free the existing item if overwriting + table->items[index] = item; + + pthread_mutex_unlock(&table->lock); +} + +static inline void +remove_item(HashTable *table, const char *key) { + assert(table != NULL); + assert(key != NULL); + + pthread_mutex_lock(&table->lock); + + unsigned long index = hash_function(key); + while (table->items[index] != NULL && strcmp(table->items[index]->key, key) != 0) { + index = (index + 1) % TABLE_SIZE; + } + + if (table->items[index] != NULL) { + table->items[index]->is_deleted = 1; // Mark as deleted + } + + pthread_mutex_unlock(&table->lock); +} + +static inline void +*find_value(HashTable *table, const char *key) { + assert(table != NULL); + assert(key != NULL); + + pthread_mutex_lock(&table->lock); + + unsigned long index = hash_function(key); + int count = 0; + while (table->items[index] != NULL && count < TABLE_SIZE && (table->items[index]->is_deleted || strcmp(table->items[index]->key, key) != 0)) { + index = (index + 1) % TABLE_SIZE; + count++; + } + + if (table->items[index] == NULL || table->items[index]->is_deleted) + { + pthread_mutex_unlock(&table->lock); + return NULL; + } + else { + pthread_mutex_unlock(&table->lock); + return table->items[index]->value; + } +} + +static inline void +free_table(HashTable *table) { + assert(table != NULL); + + pthread_mutex_lock(&table->lock); + + for (int i = 0; i < TABLE_SIZE; i++) { + if (table->items[i] != NULL) { + free(table->items[i]); // Free each item + } + } + free(table->items); + free(table); + + pthread_mutex_unlock(&table->lock); + pthread_mutex_destroy(&table->lock); +} + +#endif diff --git a/runtime/tests/tree/main.c b/runtime/tests/tree/main.c new file mode 100644 index 0000000..e69de29