fixedpoint_test.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import numpy as np
  17. import unittest
  18. from federatedml.secureprotol.fixedpoint import FixedPointNumber
  19. class TestFixedPointNumber(unittest.TestCase):
  20. def setUp(self):
  21. unittest.TestCase.setUp(self)
  22. def tearDown(self):
  23. unittest.TestCase.tearDown(self)
  24. def test_encode_decode(self):
  25. for i in range(100):
  26. en_i = FixedPointNumber.encode(i)
  27. de_en_i = en_i.decode()
  28. self.assertEqual(de_en_i, i)
  29. en_i = FixedPointNumber.encode(-i)
  30. de_en_i = en_i.decode()
  31. self.assertEqual(de_en_i, -i)
  32. for i in range(100):
  33. x = i * 0.6
  34. en_x = FixedPointNumber.encode(x)
  35. de_en_x = en_x.decode()
  36. self.assertAlmostEqual(de_en_x, x)
  37. elem = np.ones(100) * np.random.rand()
  38. for x in elem:
  39. en_x = FixedPointNumber.encode(x)
  40. de_en_x = en_x.decode()
  41. self.assertAlmostEqual(de_en_x, x)
  42. elem = np.ones(100) * np.random.randint(100)
  43. for x in elem:
  44. en_x = FixedPointNumber.encode(x)
  45. de_en_x = en_x.decode()
  46. self.assertAlmostEqual(de_en_x, x)
  47. def test_add(self):
  48. x_li = np.ones(100) * np.random.randint(100)
  49. y_li = np.ones(100) * np.random.randint(1000)
  50. z_li = np.ones(100) * np.random.rand()
  51. t_li = range(100)
  52. for i in range(x_li.shape[0]):
  53. x = x_li[i]
  54. y = y_li[i]
  55. z = z_li[i]
  56. t = t_li[i]
  57. en_x = FixedPointNumber.encode(x)
  58. en_y = FixedPointNumber.encode(y)
  59. en_z = FixedPointNumber.encode(-z)
  60. en_t = FixedPointNumber.encode(-t)
  61. en_res = en_x + en_y + en_z + en_t
  62. res = x + y + (-z) + (-t)
  63. de_en_res = en_res.decode()
  64. self.assertAlmostEqual(de_en_res, res)
  65. def test_sub(self):
  66. x_li = np.ones(100) * np.random.randint(100)
  67. y_li = np.ones(100) * np.random.randint(1000)
  68. z_li = np.ones(100) * np.random.rand()
  69. t_li = range(100)
  70. for i in range(x_li.shape[0]):
  71. x = x_li[i]
  72. y = y_li[i]
  73. z = z_li[i]
  74. t = t_li[i]
  75. en_x = FixedPointNumber.encode(x)
  76. en_y = FixedPointNumber.encode(y)
  77. en_z = FixedPointNumber.encode(z)
  78. en_t = FixedPointNumber.encode(t)
  79. en_res = en_x - en_y - en_z - en_t
  80. res = x - y - z - t
  81. de_en_res = en_res.decode()
  82. self.assertAlmostEqual(de_en_res, res)
  83. def test_mul(self):
  84. x_li = np.ones(100) * np.random.randint(100)
  85. y_li = np.ones(100) * np.random.randint(1000) * -1
  86. z_li = np.ones(100) * np.random.rand()
  87. t_li = range(0, 100)
  88. for i in range(x_li.shape[0]):
  89. x = x_li[i]
  90. y = y_li[i]
  91. z = z_li[i]
  92. t = t_li[i]
  93. en_x = FixedPointNumber.encode(x)
  94. en_res = (en_x * y + z) * t
  95. res = (x * y + z) * t
  96. de_en_res = en_res.decode()
  97. self.assertAlmostEqual(de_en_res, res)
  98. x = 9
  99. en_x = FixedPointNumber.encode(x)
  100. for i in range(100):
  101. en_x = en_x + 5000 - 0.2
  102. x = x + 5000 - 0.2
  103. de_en_x = en_x.decode()
  104. self.assertAlmostEqual(de_en_x, x)
  105. def test_div(self):
  106. for i in range(100):
  107. x = np.random.randn() * 100
  108. y = np.random.randn() * 100
  109. en_x = FixedPointNumber.encode(x)
  110. en_y = FixedPointNumber.encode(y)
  111. z = x / y
  112. en_z = en_x / en_y
  113. de_en_z = en_z.decode()
  114. self.assertAlmostEqual(de_en_z, z)
  115. def test_lt(self):
  116. for i in range(100):
  117. x = np.random.randn() * 100
  118. y = np.random.randn() * 100
  119. en_x = FixedPointNumber.encode(x)
  120. en_y = FixedPointNumber.encode(y)
  121. z = x < y
  122. en_z = en_x < en_y
  123. self.assertEqual(en_z, z)
  124. def test_gt(self):
  125. for i in range(100):
  126. x = np.random.randn() * 100
  127. y = np.random.randn() * 100
  128. en_x = FixedPointNumber.encode(x)
  129. en_y = FixedPointNumber.encode(y)
  130. z = x > y
  131. en_z = en_x > en_y
  132. self.assertEqual(en_z, z)
  133. def test_le(self):
  134. for i in range(100):
  135. x = np.random.randint(10)
  136. y = np.random.randint(10)
  137. en_x = FixedPointNumber.encode(x)
  138. en_y = FixedPointNumber.encode(y)
  139. z = x <= y
  140. en_z = en_x <= en_y
  141. self.assertEqual(en_z, z)
  142. def test_ge(self):
  143. for i in range(100):
  144. x = np.random.randint(10)
  145. y = np.random.randint(10)
  146. en_x = FixedPointNumber.encode(x)
  147. en_y = FixedPointNumber.encode(y)
  148. z = x >= y
  149. en_z = en_x >= en_y
  150. self.assertEqual(en_z, z)
  151. def test_eq(self):
  152. for i in range(100):
  153. x = np.random.randint(10)
  154. y = np.random.randint(10)
  155. en_x = FixedPointNumber.encode(x)
  156. en_y = FixedPointNumber.encode(y)
  157. z = x == y
  158. en_z = en_x == en_y
  159. self.assertEqual(en_z, z)
  160. def test_ne(self):
  161. for i in range(100):
  162. x = np.random.randint(10)
  163. y = np.random.randint(10)
  164. en_x = FixedPointNumber.encode(x)
  165. en_y = FixedPointNumber.encode(y)
  166. z = x != y
  167. en_z = en_x != en_y
  168. self.assertEqual(en_z, z)
  169. if __name__ == '__main__':
  170. unittest.main()