string_ut.pyx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # cython: c_string_type=str, c_string_encoding=utf8
  2. from libcpp.string cimport string as std_string
  3. from util.generic.string cimport TString, npos
  4. import pytest
  5. import unittest
  6. import sys
  7. class TestStroka(unittest.TestCase):
  8. def test_unicode(self):
  9. cdef TString x = "привет"
  10. self.assertEqual(x, "привет")
  11. def test_ctor1(self):
  12. cdef TString tmp = TString()
  13. cdef TString tmp2 = TString(tmp)
  14. self.assertEqual(tmp2, "")
  15. def test_ctor2(self):
  16. cdef std_string tmp = b"hello"
  17. cdef TString tmp2 = TString(tmp)
  18. self.assertEqual(tmp2, "hello")
  19. def test_ctor3(self):
  20. cdef TString tmp = b"hello"
  21. cdef TString tmp2 = TString(tmp, 0, 4)
  22. self.assertEqual(tmp2, "hell")
  23. def test_ctor4(self):
  24. cdef TString tmp = TString(<char*>b"hello")
  25. self.assertEqual(tmp, "hello")
  26. def test_ctor5(self):
  27. cdef TString tmp = TString(<char*>b"hello", 4)
  28. self.assertEqual(tmp, "hell")
  29. def test_ctor6(self):
  30. cdef TString tmp = TString(<char*>b"hello", 1, 3)
  31. self.assertEqual(tmp, "ell")
  32. def test_ctor7(self):
  33. cdef TString tmp = TString(3, <char>'x')
  34. self.assertEqual(tmp, "xxx")
  35. def test_ctor8(self):
  36. cdef bytes tmp = b"hello"
  37. cdef TString tmp2 = TString(<char*>tmp, <char*>tmp + 4)
  38. self.assertEqual(tmp2, "hell")
  39. def test_compare(self):
  40. cdef TString tmp1 = b"abacab"
  41. cdef TString tmp2 = b"abacab"
  42. cdef TString tmp3 = b"abacac"
  43. self.assertTrue(tmp1.compare(tmp2) == 0)
  44. self.assertTrue(tmp1.compare(tmp3) < 0)
  45. self.assertTrue(tmp3.compare(tmp1) > 0)
  46. self.assertTrue(tmp1 == tmp2)
  47. self.assertTrue(tmp1 != tmp3)
  48. self.assertTrue(tmp1 < tmp3)
  49. self.assertTrue(tmp1 <= tmp3)
  50. self.assertTrue(tmp3 > tmp1)
  51. self.assertTrue(tmp3 >= tmp1)
  52. def test_operator_assign(self):
  53. cdef TString tmp = b"hello"
  54. cdef TString tmp2 = tmp
  55. self.assertEqual(tmp2, "hello")
  56. def test_operator_plus(self):
  57. cdef TString tmp = TString(b"hello ") + TString(b"world")
  58. self.assertEqual(tmp, "hello world")
  59. def test_c_str(self):
  60. cdef TString tmp = b"hello"
  61. if sys.version_info.major == 2:
  62. self.assertEqual(bytes(tmp.c_str()), b"hello")
  63. else:
  64. self.assertEqual(bytes(tmp.c_str(), 'utf8'), b"hello")
  65. def test_length(self):
  66. cdef TString tmp = b"hello"
  67. self.assertEqual(tmp.size(), tmp.length())
  68. def test_index(self):
  69. cdef TString tmp = b"hello"
  70. self.assertEqual(<bytes>tmp[0], b'h')
  71. self.assertEqual(<bytes>tmp.at(0), b'h')
  72. self.assertEqual(<bytes>tmp[4], b'o')
  73. self.assertEqual(<bytes>tmp.at(4), b'o')
  74. # Actually, TString::at() is noexcept
  75. # with pytest.raises(IndexError):
  76. # tmp.at(100)
  77. def test_append(self):
  78. cdef TString tmp
  79. cdef TString tmp2 = b"fuu"
  80. tmp.append(tmp2)
  81. self.assertEqual(tmp, "fuu")
  82. tmp.append(tmp2, 1, 2)
  83. self.assertEqual(tmp, "fuuuu")
  84. tmp.append(<char*>"ll ")
  85. self.assertEqual(tmp, "fuuuull ")
  86. tmp.append(<char*>"of greatness", 4)
  87. self.assertEqual(tmp, "fuuuull of g")
  88. tmp.append(2, <char>b'o')
  89. self.assertEqual(tmp, "fuuuull of goo")
  90. tmp.push_back(b'z')
  91. self.assertEqual(tmp, "fuuuull of gooz")
  92. def test_assign(self):
  93. cdef TString tmp
  94. tmp.assign(b"one")
  95. self.assertEqual(tmp, "one")
  96. tmp.assign(b"two hundred", 0, 3)
  97. self.assertEqual(tmp, "two")
  98. tmp.assign(<char*>b"three")
  99. self.assertEqual(tmp, "three")
  100. tmp.assign(<char*>b"three fiddy", 5)
  101. self.assertEqual(tmp, "three")
  102. def test_insert(self):
  103. cdef TString tmp
  104. tmp = b"xx"
  105. tmp.insert(1, b"foo")
  106. self.assertEqual(tmp, "xfoox")
  107. tmp = b"xx"
  108. tmp.insert(1, b"haxor", 1, 3)
  109. self.assertEqual(tmp, "xaxox")
  110. tmp = b"xx"
  111. tmp.insert(1, <char*>b"foo")
  112. self.assertEqual(tmp, "xfoox")
  113. tmp = b"xx"
  114. tmp.insert(1, <char*>b"foozzy", 3)
  115. self.assertEqual(tmp, "xfoox")
  116. tmp = b"xx"
  117. tmp.insert(1, 2, <char>b'u')
  118. self.assertEqual(tmp, "xuux")
  119. def test_copy(self):
  120. cdef char buf[16]
  121. cdef TString tmp = b"hello"
  122. tmp.copy(buf, 5, 0)
  123. self.assertEqual(buf[:5], "hello")
  124. def test_find(self):
  125. cdef TString haystack = b"whole lotta bytes"
  126. cdef TString needle = "hole"
  127. self.assertEqual(haystack.find(needle), 1)
  128. self.assertEqual(haystack.find(needle, 3), npos)
  129. self.assertEqual(haystack.find(<char>b'h'), 1)
  130. self.assertEqual(haystack.find(<char>b'h', 3), npos)
  131. def test_rfind(self):
  132. cdef TString haystack = b"whole lotta bytes"
  133. cdef TString needle = b"hole"
  134. self.assertEqual(haystack.rfind(needle), 1)
  135. self.assertEqual(haystack.rfind(needle, 0), npos)
  136. self.assertEqual(haystack.rfind(<char>b'h'), 1)
  137. self.assertEqual(haystack.rfind(<char>b'h', 0), npos)
  138. def test_find_first_of(self):
  139. cdef TString haystack = b"whole lotta bytes"
  140. cdef TString cset = b"hxz"
  141. self.assertEqual(haystack.find_first_of(<char>b'h'), 1)
  142. self.assertEqual(haystack.find_first_of(<char>b'h', 3), npos)
  143. self.assertEqual(haystack.find_first_of(cset), 1)
  144. self.assertEqual(haystack.find_first_of(cset, 3), npos)
  145. def test_first_not_of(self):
  146. cdef TString haystack = b"whole lotta bytes"
  147. cdef TString cset = b"wxz"
  148. self.assertEqual(haystack.find_first_not_of(<char>b'w'), 1)
  149. self.assertEqual(haystack.find_first_not_of(<char>b'w', 3), 3)
  150. self.assertEqual(haystack.find_first_not_of(cset), 1)
  151. self.assertEqual(haystack.find_first_not_of(cset, 3), 3)
  152. def test_find_last_of(self):
  153. cdef TString haystack = b"whole lotta bytes"
  154. cdef TString cset = b"hxz"
  155. self.assertEqual(haystack.find_last_of(<char>b'h'), 1)
  156. self.assertEqual(haystack.find_last_of(<char>b'h', 0), npos)
  157. self.assertEqual(haystack.find_last_of(cset), 1)
  158. self.assertEqual(haystack.find_last_of(cset, 0), npos)
  159. def test_substr(self):
  160. cdef TString tmp = b"foobar"
  161. self.assertEqual(tmp.substr(1), "oobar")
  162. self.assertEqual(tmp.substr(1, 4), "ooba")