添加简单测试代码
This commit is contained in:
parent
2e0b552a50
commit
b2354738fe
44
test_CoreAlgo.py
Normal file
44
test_CoreAlgo.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import unittest
|
||||||
|
import CoreAlgorithm as paillier
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
|
|
||||||
|
def generate_paillier_keypair() -> (
|
||||||
|
Tuple[paillier.PaillierPublicKey, paillier.PaillierPrivateKey]
|
||||||
|
):
|
||||||
|
"""生成Paillier公钥和私钥对."""
|
||||||
|
public_key, private_key = paillier.generate_paillier_keypair()
|
||||||
|
return public_key, private_key
|
||||||
|
|
||||||
|
|
||||||
|
class TestPaillierEncryption(unittest.TestCase):
|
||||||
|
"""测试Paillier加密算法的不同运算功能."""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
cls.public_key, cls.private_key = generate_paillier_keypair()
|
||||||
|
cls.x = 90000.23
|
||||||
|
cls.y = 90
|
||||||
|
cls.z = 0.5
|
||||||
|
cls.x_encrypted = cls.public_key.encrypt(cls.x)
|
||||||
|
cls.y_encrypted = cls.public_key.encrypt(cls.y)
|
||||||
|
cls.z_encrypted = cls.public_key.encrypt(cls.z)
|
||||||
|
|
||||||
|
def test_encryption_and_basic_arithmetic(self):
|
||||||
|
"""Test encrypted values with basic arithmetic operations."""
|
||||||
|
t_encrypted = self.x_encrypted + self.y_encrypted * 0.5 # Perform encrypted arithmetic operations
|
||||||
|
t_decrypted = self.private_key.decrypt(t_encrypted)
|
||||||
|
self.assertAlmostEqual(t_decrypted, self.x + self.y * self.z, places=5, msg="Encrypted arithmetic result is incorrect")
|
||||||
|
|
||||||
|
def test_comparisons(self):
|
||||||
|
"""Test encrypted value comparisons."""
|
||||||
|
self.assertNotEqual(self.x_encrypted, self.y_encrypted, msg="Encrypted values should not be equal")
|
||||||
|
self.assertFalse(self.x_encrypted == self.y_encrypted, msg="Equality comparison failed")
|
||||||
|
self.assertTrue(self.x_encrypted != self.y_encrypted, msg="Inequality comparison failed")
|
||||||
|
|
||||||
|
|
||||||
|
# 这里可以继续添加更多测试用例,例如对自增、自减、比较操作等的测试
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user