aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/string_ut.pyx
blob: 2149748e33e9922cac256ac8d2308da90ef56c31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# cython: c_string_type=str, c_string_encoding=utf8

from libcpp.string cimport string as std_string
from util.generic.string cimport TString, npos 

import pytest
import unittest

import sys


class TestStroka(unittest.TestCase):
    def test_unicode(self):
        cdef TString x = "привет"
        self.assertEquals(x, "привет")


    def test_ctor1(self):
        cdef TString tmp = TString() 
        cdef TString tmp2 = TString(tmp) 
        self.assertEquals(tmp2, "")

    def test_ctor2(self):
        cdef std_string tmp = b"hello"
        cdef TString tmp2 = TString(tmp) 
        self.assertEquals(tmp2, "hello")

    def test_ctor3(self):
        cdef TString tmp = b"hello"
        cdef TString tmp2 = TString(tmp, 0, 4) 
        self.assertEquals(tmp2, "hell")

    def test_ctor4(self):
        cdef TString tmp = TString(<char*>b"hello") 
        self.assertEquals(tmp, "hello")

    def test_ctor5(self):
        cdef TString tmp = TString(<char*>b"hello", 4) 
        self.assertEquals(tmp, "hell")

    def test_ctor6(self):
        cdef TString tmp = TString(<char*>b"hello", 1, 3) 
        self.assertEquals(tmp, "ell")

    def test_ctor7(self):
        cdef TString tmp = TString(3, <char>'x') 
        self.assertEquals(tmp, "xxx")

    def test_ctor8(self):
        cdef bytes tmp = b"hello"
        cdef TString tmp2 = TString(<char*>tmp, <char*>tmp + 4) 
        self.assertEquals(tmp2, "hell")

    def test_compare(self):
        cdef TString tmp1 = b"abacab"
        cdef TString tmp2 = b"abacab"
        cdef TString tmp3 = b"abacac"

        self.assertTrue(tmp1.compare(tmp2) == 0)
        self.assertTrue(tmp1.compare(tmp3) < 0)
        self.assertTrue(tmp3.compare(tmp1) > 0)

        self.assertTrue(tmp1 == tmp2)
        self.assertTrue(tmp1 != tmp3)

        self.assertTrue(tmp1 < tmp3)
        self.assertTrue(tmp1 <= tmp3)

        self.assertTrue(tmp3 > tmp1)
        self.assertTrue(tmp3 >= tmp1)

    def test_operator_assign(self):
        cdef TString tmp = b"hello"
        cdef TString tmp2 = tmp 
        self.assertEquals(tmp2, "hello")

    def test_operator_plus(self):
        cdef TString tmp = TString(b"hello ") + TString(b"world")
        self.assertEquals(tmp, "hello world")

    def test_c_str(self):
        cdef TString tmp = b"hello"
        if sys.version_info.major == 2:
            self.assertEquals(bytes(tmp.c_str()), b"hello")
        else:
            self.assertEquals(bytes(tmp.c_str(), 'utf8'), b"hello")

    def test_length(self):
        cdef TString tmp = b"hello"
        self.assertEquals(tmp.size(), tmp.length())

    def test_index(self):
        cdef TString tmp = b"hello"

        self.assertEquals(<bytes>tmp[0], b'h')
        self.assertEquals(<bytes>tmp.at(0), b'h')

        self.assertEquals(<bytes>tmp[4], b'o')
        self.assertEquals(<bytes>tmp.at(4), b'o')

        # Actually, TString::at() is noexcept 
        # with pytest.raises(IndexError):
        #     tmp.at(100)

    def test_append(self):
        cdef TString tmp 
        cdef TString tmp2 = b"fuu"

        tmp.append(tmp2)
        self.assertEquals(tmp, "fuu")

        tmp.append(tmp2, 1, 2)
        self.assertEquals(tmp, "fuuuu")

        tmp.append(<char*>"ll ")
        self.assertEquals(tmp, "fuuuull ")

        tmp.append(<char*>"of greatness", 4)
        self.assertEquals(tmp, "fuuuull of g")

        tmp.append(2, <char>b'o')
        self.assertEquals(tmp, "fuuuull of goo")

        tmp.push_back(b'z')
        self.assertEquals(tmp, "fuuuull of gooz")

    def test_assign(self):
        cdef TString tmp 

        tmp.assign(b"one")
        self.assertEquals(tmp, "one")

        tmp.assign(b"two hundred", 0, 3)
        self.assertEquals(tmp, "two")

        tmp.assign(<char*>b"three")
        self.assertEquals(tmp, "three")

        tmp.assign(<char*>b"three fiddy", 5)
        self.assertEquals(tmp, "three")

    def test_insert(self):
        cdef TString tmp 

        tmp = b"xx"
        tmp.insert(1, b"foo")
        self.assertEquals(tmp, "xfoox")

        tmp = b"xx"
        tmp.insert(1, b"haxor", 1, 3)
        self.assertEquals(tmp, "xaxox")

        tmp = b"xx"
        tmp.insert(1, <char*>b"foo")
        self.assertEquals(tmp, "xfoox")

        tmp = b"xx"
        tmp.insert(1, <char*>b"foozzy", 3)
        self.assertEquals(tmp, "xfoox")

        tmp = b"xx"
        tmp.insert(1, 2, <char>b'u')
        self.assertEquals(tmp, "xuux")

    def test_copy(self):
        cdef char buf[16]
        cdef TString tmp = b"hello"
        tmp.copy(buf, 5, 0)
        self.assertEquals(buf[:5], "hello")

    def test_find(self):
        cdef TString haystack = b"whole lotta bytes"
        cdef TString needle = "hole"

        self.assertEquals(haystack.find(needle), 1)
        self.assertEquals(haystack.find(needle, 3), npos)

        self.assertEquals(haystack.find(<char>b'h'), 1)
        self.assertEquals(haystack.find(<char>b'h', 3), npos)

    def test_rfind(self):
        cdef TString haystack = b"whole lotta bytes"
        cdef TString needle = b"hole"

        self.assertEquals(haystack.rfind(needle), 1)
        self.assertEquals(haystack.rfind(needle, 0), npos)

        self.assertEquals(haystack.rfind(<char>b'h'), 1)
        self.assertEquals(haystack.rfind(<char>b'h', 0), npos)

    def test_find_first_of(self):
        cdef TString haystack = b"whole lotta bytes"
        cdef TString cset = b"hxz"

        self.assertEquals(haystack.find_first_of(<char>b'h'), 1)
        self.assertEquals(haystack.find_first_of(<char>b'h', 3), npos)

        self.assertEquals(haystack.find_first_of(cset), 1)
        self.assertEquals(haystack.find_first_of(cset, 3), npos)

    def test_first_not_of(self):
        cdef TString haystack = b"whole lotta bytes"
        cdef TString cset = b"wxz"

        self.assertEquals(haystack.find_first_not_of(<char>b'w'), 1)
        self.assertEquals(haystack.find_first_not_of(<char>b'w', 3), 3)

        self.assertEquals(haystack.find_first_not_of(cset), 1)
        self.assertEquals(haystack.find_first_not_of(cset, 3), 3)

    def test_find_last_of(self):
        cdef TString haystack = b"whole lotta bytes"
        cdef TString cset = b"hxz"

        self.assertEquals(haystack.find_last_of(<char>b'h'), 1)
        self.assertEquals(haystack.find_last_of(<char>b'h', 0), npos)

        self.assertEquals(haystack.find_last_of(cset), 1)
        self.assertEquals(haystack.find_last_of(cset, 0), npos)

    def test_substr(self):
        cdef TString tmp = b"foobar"

        self.assertEquals(tmp.substr(1), "oobar")
        self.assertEquals(tmp.substr(1, 4), "ooba")