test_misc.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from textwrap import dedent
  2. import sys
  3. from subprocess import Popen, PIPE
  4. import os
  5. from sympy.core.singleton import S
  6. from sympy.testing.pytest import (raises, warns_deprecated_sympy,
  7. skip_under_pyodide)
  8. from sympy.utilities.misc import (translate, replace, ordinal, rawlines,
  9. strlines, as_int, find_executable)
  10. from sympy.external import import_module
  11. pyodide_js = import_module('pyodide_js')
  12. def test_translate():
  13. abc = 'abc'
  14. assert translate(abc, None, 'a') == 'bc'
  15. assert translate(abc, None, '') == 'abc'
  16. assert translate(abc, {'a': 'x'}, 'c') == 'xb'
  17. assert translate(abc, {'a': 'bc'}, 'c') == 'bcb'
  18. assert translate(abc, {'ab': 'x'}, 'c') == 'x'
  19. assert translate(abc, {'ab': ''}, 'c') == ''
  20. assert translate(abc, {'bc': 'x'}, 'c') == 'ab'
  21. assert translate(abc, {'abc': 'x', 'a': 'y'}) == 'x'
  22. u = chr(4096)
  23. assert translate(abc, 'a', 'x', u) == 'xbc'
  24. assert (u in translate(abc, 'a', u, u)) is True
  25. def test_replace():
  26. assert replace('abc', ('a', 'b')) == 'bbc'
  27. assert replace('abc', {'a': 'Aa'}) == 'Aabc'
  28. assert replace('abc', ('a', 'b'), ('c', 'C')) == 'bbC'
  29. def test_ordinal():
  30. assert ordinal(-1) == '-1st'
  31. assert ordinal(0) == '0th'
  32. assert ordinal(1) == '1st'
  33. assert ordinal(2) == '2nd'
  34. assert ordinal(3) == '3rd'
  35. assert all(ordinal(i).endswith('th') for i in range(4, 21))
  36. assert ordinal(100) == '100th'
  37. assert ordinal(101) == '101st'
  38. assert ordinal(102) == '102nd'
  39. assert ordinal(103) == '103rd'
  40. assert ordinal(104) == '104th'
  41. assert ordinal(200) == '200th'
  42. assert all(ordinal(i) == str(i) + 'th' for i in range(-220, -203))
  43. def test_rawlines():
  44. assert rawlines('a a\na') == "dedent('''\\\n a a\n a''')"
  45. assert rawlines('a a') == "'a a'"
  46. assert rawlines(strlines('\\le"ft')) == (
  47. '(\n'
  48. " '(\\n'\n"
  49. ' \'r\\\'\\\\le"ft\\\'\\n\'\n'
  50. " ')'\n"
  51. ')')
  52. def test_strlines():
  53. q = 'this quote (") is in the middle'
  54. # the following assert rhs was prepared with
  55. # print(rawlines(strlines(q, 10)))
  56. assert strlines(q, 10) == dedent('''\
  57. (
  58. 'this quo'
  59. 'te (") i'
  60. 's in the'
  61. ' middle'
  62. )''')
  63. assert q == (
  64. 'this quo'
  65. 'te (") i'
  66. 's in the'
  67. ' middle'
  68. )
  69. q = "this quote (') is in the middle"
  70. assert strlines(q, 20) == dedent('''\
  71. (
  72. "this quote (') is "
  73. "in the middle"
  74. )''')
  75. assert strlines('\\left') == (
  76. '(\n'
  77. "r'\\left'\n"
  78. ')')
  79. assert strlines('\\left', short=True) == r"r'\left'"
  80. assert strlines('\\le"ft') == (
  81. '(\n'
  82. 'r\'\\le"ft\'\n'
  83. ')')
  84. q = 'this\nother line'
  85. assert strlines(q) == rawlines(q)
  86. def test_translate_args():
  87. try:
  88. translate(None, None, None, 'not_none')
  89. except ValueError:
  90. pass # Exception raised successfully
  91. else:
  92. assert False
  93. assert translate('s', None, None, None) == 's'
  94. try:
  95. translate('s', 'a', 'bc')
  96. except ValueError:
  97. pass # Exception raised successfully
  98. else:
  99. assert False
  100. @skip_under_pyodide("Cannot create subprocess under pyodide.")
  101. def test_debug_output():
  102. env = os.environ.copy()
  103. env['SYMPY_DEBUG'] = 'True'
  104. cmd = 'from sympy import *; x = Symbol("x"); print(integrate((1-cos(x))/x, x))'
  105. cmdline = [sys.executable, '-c', cmd]
  106. proc = Popen(cmdline, env=env, stdout=PIPE, stderr=PIPE)
  107. out, err = proc.communicate()
  108. out = out.decode('ascii') # utf-8?
  109. err = err.decode('ascii')
  110. expected = 'substituted: -x*(1 - cos(x)), u: 1/x, u_var: _u'
  111. assert expected in err, err
  112. def test_as_int():
  113. raises(ValueError, lambda : as_int(True))
  114. raises(ValueError, lambda : as_int(1.1))
  115. raises(ValueError, lambda : as_int([]))
  116. raises(ValueError, lambda : as_int(S.NaN))
  117. raises(ValueError, lambda : as_int(S.Infinity))
  118. raises(ValueError, lambda : as_int(S.NegativeInfinity))
  119. raises(ValueError, lambda : as_int(S.ComplexInfinity))
  120. # for the following, limited precision makes int(arg) == arg
  121. # but the int value is not necessarily what a user might have
  122. # expected; Q.prime is more nuanced in its response for
  123. # expressions which might be complex representations of an
  124. # integer. This is not -- by design -- as_ints role.
  125. raises(ValueError, lambda : as_int(1e23))
  126. raises(ValueError, lambda : as_int(S('1.'+'0'*20+'1')))
  127. assert as_int(True, strict=False) == 1
  128. def test_deprecated_find_executable():
  129. with warns_deprecated_sympy():
  130. find_executable('python')