blob_ut.pyx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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
  4. from util.memory.blob cimport TBlob
  5. import pytest
  6. import unittest
  7. class TestBlob(unittest.TestCase):
  8. def test_ctor(self):
  9. cdef TBlob tmp = TBlob()
  10. cdef TBlob tmp2 = TBlob(tmp)
  11. self.assertEquals(tmp.Size(), 0)
  12. self.assertEquals(tmp2.Size(), 0)
  13. def test_empty_data(self):
  14. cdef TBlob tmp = TBlob()
  15. self.assertEquals(tmp.Data() == NULL, True)
  16. self.assertEquals(tmp.AsCharPtr() == NULL, True)
  17. self.assertEquals(tmp.AsUnsignedCharPtr() == NULL, True)
  18. self.assertEquals(tmp.Empty(), True)
  19. self.assertEquals(tmp.IsNull(), True)
  20. def test_empty_is_null(self):
  21. cdef TBlob tmp = TBlob.NoCopy("", 0)
  22. self.assertEquals(tmp.Empty(), True)
  23. self.assertEquals(tmp.IsNull(), False)
  24. def test_data_types(self):
  25. cdef const char* char_data = TBlob().AsCharPtr()
  26. cdef const unsigned char* uchar_data = TBlob().AsUnsignedCharPtr()
  27. cdef const void* void_data = TBlob().Data()
  28. def test_no_copy(self):
  29. cdef const char* txt = "hello world"
  30. cdef TBlob tmp = TBlob.NoCopy(txt, len(txt))
  31. self.assertEquals(tmp.AsCharPtr() - txt, 0)
  32. self.assertEquals(tmp.Size(), 11)
  33. self.assertEquals(tmp.AsCharPtr()[:tmp.Size()], "hello world")
  34. self.assertEquals(tmp.Empty(), False)
  35. self.assertEquals(tmp.IsNull(), False)
  36. def test_copy(self):
  37. cdef const char* txt = "hello world"
  38. cdef TBlob tmp = TBlob.Copy(txt, len(txt))
  39. self.assertNotEquals(tmp.AsCharPtr() - txt, 0)
  40. self.assertEquals(tmp.Size(), 11)
  41. self.assertEquals(tmp.AsCharPtr()[:tmp.Size()], "hello world")
  42. self.assertEquals(tmp.Empty(), False)
  43. self.assertEquals(tmp.IsNull(), False)
  44. def test_from_string(self):
  45. cdef TBlob tmp = TBlob.FromString(TString("hello world"))
  46. self.assertEquals(tmp.Size(), 11)
  47. self.assertEquals(tmp.AsCharPtr()[:tmp.Size()], "hello world")
  48. self.assertEquals(tmp.Empty(), False)
  49. self.assertEquals(tmp.IsNull(), False)
  50. def test_from_file(self):
  51. with open("file", "w") as f:
  52. f.write("hello world")
  53. cdef TBlob tmp = TBlob.FromFile("file")
  54. self.assertEquals(tmp.Size(), 11)
  55. self.assertEquals(tmp.AsCharPtr()[:tmp.Size()], "hello world")
  56. self.assertEquals(tmp.Empty(), False)
  57. self.assertEquals(tmp.IsNull(), False)
  58. def test_precharged_from_file(self):
  59. with open("precharged", "w") as f:
  60. f.write("hello world")
  61. cdef TBlob tmp = TBlob.PrechargedFromFile("precharged")
  62. self.assertEquals(tmp.Size(), 11)
  63. self.assertEquals(tmp.AsCharPtr()[:tmp.Size()], "hello world")
  64. self.assertEquals(tmp.Empty(), False)
  65. self.assertEquals(tmp.IsNull(), False)
  66. def test_swap_drop(self):
  67. cdef TBlob tmp = TBlob.NoCopy("hello world", 11)
  68. cdef TBlob tmp2
  69. tmp2.Swap(tmp)
  70. self.assertEquals(tmp2.Size(), 11)
  71. self.assertEquals(tmp.Size(), 0)
  72. self.assertEquals(tmp2.AsCharPtr()[:tmp2.Size()], "hello world")
  73. tmp2.Swap(tmp)
  74. self.assertEquals(tmp2.Size(), 0)
  75. self.assertEquals(tmp.Size(), 11)
  76. tmp.Drop()
  77. self.assertEquals(tmp.Size(), 0)
  78. def test_operator_brackets(self):
  79. cdef TBlob tmp = TBlob.NoCopy("hello world", 11)
  80. self.assertEquals(tmp[0], ord('h'))
  81. self.assertEquals(tmp[1], ord('e'))
  82. self.assertEquals(tmp[2], ord('l'))
  83. self.assertEquals(tmp[3], ord('l'))
  84. self.assertEquals(tmp[4], ord('o'))
  85. self.assertEquals(tmp[5], ord(' '))
  86. self.assertEquals(tmp[6], ord('w'))
  87. self.assertEquals(tmp[7], ord('o'))
  88. self.assertEquals(tmp[8], ord('r'))
  89. self.assertEquals(tmp[9], ord('l'))
  90. self.assertEquals(tmp[10], ord('d'))
  91. def test_operator_equal(self):
  92. cdef TBlob foo = TBlob.NoCopy("foo", 3)
  93. cdef TBlob bar = TBlob.NoCopy("bar", 3)
  94. self.assertEquals(foo.AsCharPtr(), "foo")
  95. self.assertEquals(bar.AsCharPtr(), "bar")
  96. bar = foo
  97. self.assertEquals(foo.AsCharPtr(), "foo")
  98. self.assertEquals(bar.AsCharPtr(), "foo")
  99. def test_sub_blob(self):
  100. cdef TBlob tmp = TBlob.NoCopy("hello world", 11)
  101. self.assertEquals(tmp.SubBlob(0).Size(), 0)
  102. self.assertEquals(tmp.SubBlob(1).Size(), 1)
  103. self.assertEquals(tmp.SubBlob(5).Size(), 5)
  104. self.assertEquals(tmp.AsCharPtr() - tmp.SubBlob(0).AsCharPtr(), 0)
  105. self.assertEquals(tmp.SubBlob(0, 0).Size(), 0)
  106. self.assertEquals(tmp.SubBlob(0, 1).Size(), 1)
  107. self.assertEquals(tmp.SubBlob(0, 5).Size(), 5)
  108. self.assertEquals(tmp.AsCharPtr() - tmp.SubBlob(0, 0).AsCharPtr(), 0)
  109. self.assertEquals(tmp.SubBlob(1, 1).Size(), 0)
  110. self.assertEquals(tmp.SubBlob(1, 2).Size(), 1)
  111. self.assertEquals(tmp.SubBlob(1, 6).Size(), 5)
  112. self.assertEquals(tmp.SubBlob(1, 1).AsCharPtr() - tmp.AsCharPtr(), 1)
  113. with self.assertRaises(Exception):
  114. tmp.SubBlob(2, 1)
  115. def test_deep_copy(self):
  116. cdef TBlob tmp = TBlob.NoCopy("hello world", 11)
  117. cdef TBlob tmp2 = tmp.DeepCopy()
  118. self.assertEquals(tmp.AsCharPtr()[:tmp.Size()], "hello world")
  119. self.assertEquals(tmp2.AsCharPtr()[:tmp2.Size()], "hello world")
  120. self.assertNotEquals(tmp2.AsCharPtr() - tmp.AsCharPtr(), 0)