You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.4 KiB
54 lines
1.4 KiB
import unittest,importlib,BeautifulReport
|
|
import cffi
|
|
|
|
def load():
|
|
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("hashlib",src)
|
|
builder.compile()
|
|
|
|
md = importlib.import_module("hashlib")
|
|
|
|
return md.lib
|
|
|
|
md = load()
|
|
class HashTableTestCase(unittest.TestCase):
|
|
|
|
def test_case1(self):
|
|
'''
|
|
测试添加和查找功能
|
|
'''
|
|
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):
|
|
'''
|
|
测试查找不存在的键
|
|
'''
|
|
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.makeSuite(HashTableTestCase))
|
|
run = BeautifulReport.BeautifulReport(sut)
|
|
|
|
run.report(filename="test.html",description="单元测试")
|
|
|