test_misc.py 4.5 KB

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