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.
sledge/runtime/Utest_py/ctest.py

54 lines
1.4 KiB

6 months ago
import unittest,importlib,BeautifulReport
import cffi
6 months ago
def load():
6 months ago
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()
6 months ago
builder = cffi.FFI()
builder.cdef(inc)
6 months ago
builder.set_source("hashlib",src)
6 months ago
builder.compile()
6 months ago
md = importlib.import_module("hashlib")
6 months ago
return md.lib
md = load()
6 months ago
class HashTableTestCase(unittest.TestCase):
6 months ago
def test_case1(self):
'''
6 months ago
测试添加和查找功能
6 months ago
'''
6 months ago
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)
6 months ago
def test_case2(self):
'''
6 months ago
测试查找不存在的键
6 months ago
'''
6 months ago
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)
6 months ago
sut = unittest.TestSuite()
6 months ago
sut.addTest(unittest.makeSuite(HashTableTestCase))
6 months ago
run = BeautifulReport.BeautifulReport(sut)
6 months ago
run.report(filename="test.html",description="单元测试")
6 months ago