图片解析应用
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.

146 lines
4.4 KiB

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