Development of an internal social media platform with personalised dashboards for students
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.

unittest_brain_numpy.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #-*- encoding=utf-8 -*-
  2. # Copyright (c) 2017 Guillaume Peillex <guillaume.peillex@gmail.com>
  3. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  4. # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
  5. import unittest
  6. import contextlib
  7. try:
  8. import numpy # pylint: disable=unused-import
  9. HAS_NUMPY = True
  10. except ImportError:
  11. HAS_NUMPY = False
  12. from astroid import builder
  13. from astroid import nodes
  14. class SubTestWrapper(unittest.TestCase):
  15. """
  16. A class for supporting all unittest version wether or not subTest is available
  17. """
  18. def subTest(self, msg=None, **params):
  19. try:
  20. # For python versions above 3.5 this should be ok
  21. return super(SubTestWrapper, self).subTest(msg, **params)
  22. except AttributeError:
  23. # For python versions below 3.5
  24. return subTestMock(msg)
  25. @contextlib.contextmanager
  26. def subTestMock(msg=None):
  27. """
  28. A mock for subTest which do nothing
  29. """
  30. yield msg
  31. @unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.")
  32. class NumpyBrainCoreUmathTest(SubTestWrapper):
  33. """
  34. Test of all members of numpy.core.umath module
  35. """
  36. no_arg_ufunc = ('geterrobj',)
  37. one_arg_ufunc_spec = ('seterrobj',)
  38. one_arg_ufunc = (
  39. 'arccos', 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctanh',
  40. 'cbrt', 'conj', 'conjugate', 'cosh', 'deg2rad',
  41. 'degrees', 'exp2', 'expm1', 'fabs', 'frexp', 'isfinite',
  42. 'isinf', 'log', 'log1p', 'log2', 'logical_not', 'modf',
  43. 'negative', 'rad2deg', 'radians', 'reciprocal', 'rint',
  44. 'sign', 'signbit', 'spacing', 'square', 'tan', 'tanh',
  45. 'trunc',)
  46. two_args_ufunc = (
  47. 'bitwise_and', 'bitwise_or', 'bitwise_xor',
  48. 'copysign', 'divide', 'equal', 'float_power',
  49. 'floor_divide', 'fmax', 'fmin', 'fmod', 'greater',
  50. 'hypot', 'ldexp', 'left_shift', 'less', 'logaddexp',
  51. 'logaddexp2', 'logical_and', 'logical_or', 'logical_xor',
  52. 'maximum', 'minimum', 'nextafter', 'not_equal', 'power',
  53. 'remainder', 'right_shift', 'subtract', 'true_divide',)
  54. all_ufunc = no_arg_ufunc + one_arg_ufunc_spec + one_arg_ufunc + two_args_ufunc
  55. constants = ('e', 'euler_gamma')
  56. def _inferred_numpy_attribute(self, func_name):
  57. node = builder.extract_node("""
  58. import numpy.core.umath as tested_module
  59. func = tested_module.{:s}
  60. func""".format(func_name))
  61. return next(node.infer())
  62. def test_numpy_core_umath_constants(self):
  63. """
  64. Test that constants have Const type.
  65. """
  66. for const in self.constants:
  67. with self.subTest(const=const):
  68. inferred = self._inferred_numpy_attribute(const)
  69. self.assertIsInstance(inferred, nodes.Const)
  70. def test_numpy_core_umath_constants_values(self):
  71. """
  72. Test the values of the constants.
  73. """
  74. exact_values = {'e': 2.718281828459045,
  75. 'euler_gamma': 0.5772156649015329}
  76. for const in self.constants:
  77. with self.subTest(const=const):
  78. inferred = self._inferred_numpy_attribute(const)
  79. self.assertEqual(inferred.value, exact_values[const])
  80. def test_numpy_core_umath_functions(self):
  81. """
  82. Test that functions have FunctionDef type.
  83. """
  84. for func in self.all_ufunc:
  85. with self.subTest(func=func):
  86. inferred = self._inferred_numpy_attribute(func)
  87. self.assertIsInstance(inferred, nodes.FunctionDef)
  88. def test_numpy_core_umath_functions_no_arg(self):
  89. """
  90. Test that functions with no arguments have really no arguments.
  91. """
  92. for func in self.no_arg_ufunc:
  93. with self.subTest(func=func):
  94. inferred = self._inferred_numpy_attribute(func)
  95. self.assertFalse(inferred.argnames())
  96. def test_numpy_core_umath_functions_one_arg_spec(self):
  97. """
  98. Test the arguments names of functions.
  99. """
  100. exact_arg_names = ['errobj']
  101. for func in self.one_arg_ufunc_spec:
  102. with self.subTest(func=func):
  103. inferred = self._inferred_numpy_attribute(func)
  104. self.assertEqual(inferred.argnames(), exact_arg_names)
  105. def test_numpy_core_umath_functions_one_arg(self):
  106. """
  107. Test the arguments names of functions.
  108. """
  109. exact_arg_names = ['x', 'out', 'where', 'casting', 'order', 'dtype', 'subok']
  110. for func in self.one_arg_ufunc:
  111. with self.subTest(func=func):
  112. inferred = self._inferred_numpy_attribute(func)
  113. self.assertEqual(inferred.argnames(), exact_arg_names)
  114. def test_numpy_core_umath_functions_two_args(self):
  115. """
  116. Test the arguments names of functions.
  117. """
  118. exact_arg_names = ['x1', 'x2', 'out', 'where', 'casting', 'order', 'dtype', 'subok']
  119. for func in self.two_args_ufunc:
  120. with self.subTest(func=func):
  121. inferred = self._inferred_numpy_attribute(func)
  122. self.assertEqual(inferred.argnames(), exact_arg_names)
  123. def test_numpy_core_umath_functions_kwargs_default_values(self):
  124. """
  125. Test the default values for keyword arguments.
  126. """
  127. exact_kwargs_default_values = [None, True, 'same_kind', 'K', None, True]
  128. for func in self.one_arg_ufunc + self.two_args_ufunc:
  129. with self.subTest(func=func):
  130. inferred = self._inferred_numpy_attribute(func)
  131. default_args_values = [default.value for default in inferred.args.defaults]
  132. self.assertEqual(default_args_values, exact_kwargs_default_values)
  133. @unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.")
  134. class NumpyBrainRandomMtrandTest(SubTestWrapper):
  135. """
  136. Test of all the functions of numpy.random.mtrand module.
  137. """
  138. # Map between functions names and arguments names and default values
  139. all_mtrand = {
  140. 'beta': (['a', 'b', 'size'], [None]),
  141. 'binomial': (['n', 'p', 'size'], [None]),
  142. 'bytes': (['length',], []),
  143. 'chisquare': (['df', 'size'], [None]),
  144. 'choice': (['a', 'size', 'replace', 'p'], [None, True, None]),
  145. 'dirichlet': (['alpha', 'size'], [None]),
  146. 'exponential': (['scale', 'size'], [1.0, None]),
  147. 'f': (['dfnum', 'dfden', 'size'], [None]),
  148. 'gamma': (['shape', 'scale', 'size'], [1.0, None]),
  149. 'geometric': (['p', 'size'], [None]),
  150. 'get_state': ([], []),
  151. 'gumbel': (['loc', 'scale', 'size'], [0.0, 1.0, None]),
  152. 'hypergeometric': (['ngood', 'nbad', 'nsample', 'size'], [None]),
  153. 'laplace': (['loc', 'scale', 'size'], [0.0, 1.0, None]),
  154. 'logistic': (['loc', 'scale', 'size'], [0.0, 1.0, None]),
  155. 'lognormal': (['mean', 'sigma', 'size'], [0.0, 1.0, None]),
  156. 'logseries': (['p', 'size'], [None]),
  157. 'multinomial': (['n', 'pvals', 'size'], [None]),
  158. 'multivariate_normal': (['mean', 'cov', 'size'], [None]),
  159. 'negative_binomial': (['n', 'p', 'size'], [None]),
  160. 'noncentral_chisquare': (['df', 'nonc', 'size'], [None]),
  161. 'noncentral_f': (['dfnum', 'dfden', 'nonc', 'size'], [None]),
  162. 'normal': (['loc', 'scale', 'size'], [0.0, 1.0, None]),
  163. 'pareto': (['a', 'size'], [None]),
  164. 'permutation': (['x'], []),
  165. 'poisson': (['lam', 'size'], [1.0, None]),
  166. 'power': (['a', 'size'], [None]),
  167. 'rand': (['args'], []),
  168. 'randint': (['low', 'high', 'size', 'dtype'], [None, None, 'l']),
  169. 'randn': (['args'], []),
  170. 'random_integers': (['low', 'high', 'size'], [None, None]),
  171. 'random_sample': (['size'], [None]),
  172. 'rayleigh': (['scale', 'size'], [1.0, None]),
  173. 'seed': (['seed'], [None]),
  174. 'set_state': (['state'], []),
  175. 'shuffle': (['x'], []),
  176. 'standard_cauchy': (['size'], [None]),
  177. 'standard_exponential': (['size'], [None]),
  178. 'standard_gamma': (['shape', 'size'], [None]),
  179. 'standard_normal': (['size'], [None]),
  180. 'standard_t': (['df', 'size'], [None]),
  181. 'triangular': (['left', 'mode', 'right', 'size'], [None]),
  182. 'uniform': (['low', 'high', 'size'], [0.0, 1.0, None]),
  183. 'vonmises': (['mu', 'kappa', 'size'], [None]),
  184. 'wald': (['mean', 'scale', 'size'], [None]),
  185. 'weibull': (['a', 'size'], [None]),
  186. 'zipf': (['a', 'size'], [None])}
  187. def _inferred_numpy_attribute(self, func_name):
  188. node = builder.extract_node("""
  189. import numpy.random.mtrand as tested_module
  190. func = tested_module.{:s}
  191. func""".format(func_name))
  192. return next(node.infer())
  193. def test_numpy_random_mtrand_functions(self):
  194. """
  195. Test that all functions have FunctionDef type.
  196. """
  197. for func in self.all_mtrand:
  198. with self.subTest(func=func):
  199. inferred = self._inferred_numpy_attribute(func)
  200. self.assertIsInstance(inferred, nodes.FunctionDef)
  201. def test_numpy_random_mtrand_functions_signature(self):
  202. """
  203. Test the arguments names and default values.
  204. """
  205. for func, (exact_arg_names, exact_kwargs_default_values) in self.all_mtrand.items():
  206. with self.subTest(func=func):
  207. inferred = self._inferred_numpy_attribute(func)
  208. self.assertEqual(inferred.argnames(), exact_arg_names)
  209. default_args_values = [default.value for default in inferred.args.defaults]
  210. self.assertEqual(default_args_values, exact_kwargs_default_values)
  211. @unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.")
  212. class NumpyBrainCoreNumericTypesTest(SubTestWrapper):
  213. """
  214. Test of all the missing types defined in numerictypes module.
  215. """
  216. all_types = ['uint16', 'uint32', 'uint64', 'int128', 'uint128',
  217. 'float16', 'float32', 'float64', 'float80', 'float96',
  218. 'float128', 'float256', 'complex32', 'complex64', 'complex128',
  219. 'complex160', 'complex192', 'complex256', 'complex512',
  220. 'timedelta64', 'datetime64', 'unicode_', 'string_', 'object_']
  221. def _inferred_numpy_attribute(self, attrib):
  222. node = builder.extract_node("""
  223. import numpy.core.numerictypes as tested_module
  224. missing_type = tested_module.{:s}""".format(attrib))
  225. return next(node.value.infer())
  226. def test_numpy_core_types(self):
  227. """
  228. Test that all defined types have ClassDef type.
  229. """
  230. for typ in self.all_types:
  231. with self.subTest(typ=typ):
  232. inferred = self._inferred_numpy_attribute(typ)
  233. self.assertIsInstance(inferred, nodes.ClassDef)
  234. if __name__ == '__main__':
  235. unittest.main()