str_ut.pyx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # cython: c_string_type=str, c_string_encoding=utf8
  2. from cython.operator cimport dereference
  3. from util.generic.ptr cimport THolder
  4. from util.generic.string cimport TString, TStringBuf
  5. from util.stream.str cimport TStringOutput, TStringOutputPtr
  6. import unittest
  7. class TestStringOutput(unittest.TestCase):
  8. def test_ctor1(self):
  9. cdef TStringOutput output
  10. def test_ctor2(self):
  11. cdef TString string
  12. cdef THolder[TStringOutput] string_output = THolder[TStringOutput](new TStringOutput(string))
  13. def test_write_char(self):
  14. cdef TString string
  15. cdef TStringOutputPtr string_output = TStringOutputPtr(new TStringOutput(string))
  16. self.assertEqual(string, "")
  17. dereference(string_output.Get()).WriteChar('1')
  18. self.assertEqual(string, "1")
  19. dereference(string_output.Get()).WriteChar('2')
  20. self.assertEqual(string, "12")
  21. dereference(string_output.Get()).WriteChar('3')
  22. self.assertEqual(string, "123")
  23. def test_write_void(self):
  24. cdef TString string
  25. cdef TStringOutputPtr string_output = TStringOutputPtr(new TStringOutput(string))
  26. self.assertEqual(string, "")
  27. dereference(string_output.Get()).Write("1", 1)
  28. self.assertEqual(string, "1")
  29. dereference(string_output.Get()).Write("2", 1)
  30. self.assertEqual(string, "12")
  31. dereference(string_output.Get()).Write("34", 2)
  32. self.assertEqual(string, "1234")
  33. def test_write_buf(self):
  34. cdef TString string
  35. cdef TStringOutputPtr string_output = TStringOutputPtr(new TStringOutput(string))
  36. self.assertEqual(string, "")
  37. dereference(string_output.Get()).WriteBuf(TStringBuf("1"))
  38. self.assertEqual(string, "1")
  39. dereference(string_output.Get()).WriteBuf(TStringBuf("2"))
  40. self.assertEqual(string, "12")
  41. dereference(string_output.Get()).WriteBuf(TStringBuf("34"))
  42. self.assertEqual(string, "1234")
  43. def test_reserve(self):
  44. cdef TString string
  45. cdef TStringOutputPtr string_output = TStringOutputPtr(new TStringOutput(string))
  46. self.assertEqual(string, "")
  47. dereference(string_output.Get()).Reserve(50)
  48. self.assertEqual(string, "")
  49. self.assertLessEqual(50, string.capacity())