-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
222 lines (196 loc) · 6.95 KB
/
test.py
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
from __future__ import print_function
import unittest
import tempfile
import os
import ctypes
import imp
import shutil
import subprocess
import sys
TGREP_DIR = os.path.dirname(os.path.realpath(__file__))
TGREP_FILE = os.path.join(TGREP_DIR, '4grep')
tgrep = imp.load_source('4grep', TGREP_FILE)
TRUNC = 0
MTCH = 1
NO_MTCH = 2
class TestFiltering(unittest.TestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.tempindex = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.tempdir)
shutil.rmtree(self.tempindex)
def test_filter(self):
index = tgrep.StringIndex([[str(10 ** tgrep.NGRAM_CHARS)]])
c_index = index.get_index_struct()
for i in range(10):
name = os.path.join(self.tempdir, '{}.txt'.format(i))
f = open(name, 'w')
f.write(str(i * 10 ** tgrep.NGRAM_CHARS))
f.close()
c_filename = ctypes.c_char_p(name)
# first run through: no bitmaps, 2nd bit should be set
ret = tgrep.start_filter(c_index, c_filename, self.tempindex)
if i == 1:
self.assertEqual(ret, 3)
else:
self.assertEqual(ret, 4)
# 2nd run through: all bitmaps should be cached, 2nd bit unset
ret = tgrep.start_filter(c_index, c_filename, self.tempindex)
if i == 1:
self.assertEqual(ret, 1)
else:
self.assertEqual(ret, 2)
def test_filter_deletedfiles(self):
index = tgrep.StringIndex([[str(10 ** tgrep.NGRAM_CHARS)]])
c_index = index.get_index_struct()
for i in range(10):
name = os.path.join(self.tempdir, '{}.txt'.format(i))
c_filename = ctypes.c_char_p(name)
# no files exist, so should filter files out
ret = tgrep.start_filter(c_index, c_filename, self.tempindex)
self.assertEqual(ret, -1)
def test_filter_modifiedfiles(self):
index = tgrep.StringIndex([[str(10 ** tgrep.NGRAM_CHARS)]])
c_index = index.get_index_struct()
# write garbage to each file with an old modification time
for i in range(10):
name = os.path.join(self.tempdir, '{}.txt'.format(i))
f = open(name, 'w')
f.write(str(i * 9 ** tgrep.NGRAM_CHARS))
f.close()
os.utime(name, (100, 100))
# make sure nothing is found when we search
for i in range(10):
name = os.path.join(self.tempdir, '{}.txt'.format(i))
c_filename = ctypes.c_char_p(name)
ret = tgrep.start_filter(c_index, c_filename, self.tempindex)
self.assertEqual(ret, 4)
# modify each file with mtime=real, current time
for i in range(10):
name = os.path.join(self.tempdir, '{}.txt'.format(i))
f = open(name, 'w')
f.write(str(i * 10 ** tgrep.NGRAM_CHARS))
f.close()
# the query should now match file 1.
for i in range(10):
name = os.path.join(self.tempdir, '{}.txt'.format(i))
c_filename = ctypes.c_char_p(name)
ret = tgrep.start_filter(c_index, c_filename, self.tempindex)
if i == 1:
self.assertEqual(ret, 3)
else:
self.assertEqual(ret, 4)
class TestIndexAutodetection(unittest.TestCase):
def test_parsable_chars(self):
self.assertEqual(
tgrep.get_index_from_regex('12345.54321'),
tgrep.StringIndex([['12345', '54321']]))
self.assertEqual(
tgrep.get_index_from_regex('12345+54321'),
tgrep.StringIndex([['12345', '54321']]))
self.assertEqual(
tgrep.get_index_from_regex('12345.*54321'),
tgrep.StringIndex([['12345', '54321']]))
def test_short(self):
self.assertEqual(
tgrep.get_index_from_regex('1234'),
tgrep.empty_index())
self.assertEqual(
tgrep.get_index_from_regex(''),
tgrep.empty_index())
self.assertEqual(
tgrep.get_index_from_regex('1234|4321'),
tgrep.empty_index())
self.assertEqual(
tgrep.get_index_from_regex('1234|4321.*4321'),
tgrep.empty_index())
def test_regex_or(self):
self.assertEqual(
tgrep.get_index_from_regex('one111|two22|three'),
tgrep.StringIndex([['one111'], ['two22'], ['three']]))
self.assertTrue(
tgrep.get_index_from_regex('one***111|two22|three').empty())
self.assertEqual(
tgrep.get_index_from_regex('12345.54321|two22|three'),
tgrep.StringIndex([['12345', '54321'], ['two22'], ['three']]))
def test_literal(self):
self.assertEqual(
tgrep.get_index_from_regex('qwertyuiop'),
tgrep.StringIndex([['qwertyuiop']]))
def test_regex_question_mark(self):
self.assertTrue(
tgrep.get_index_from_regex('12345?').empty())
def test_regex_star(self):
self.assertTrue(
tgrep.get_index_from_regex('12345*').empty())
def test_regex_curly_braces(self):
self.assertTrue(
tgrep.get_index_from_regex('12345{0,9}').empty())
class TestStringIndex(unittest.TestCase):
def test_get_index_struct(self):
si = tgrep.StringIndex([['aaaaa']])
struct = si.get_index_struct()
self.assertEqual(struct.num_rows, 1)
self.assertEqual(struct.rows[0].length, 1)
self.assertEqual(struct.rows[0].data[0], 0b00010001000100010001)
si = tgrep.StringIndex([['aaaaa'], ['bbbbb']])
struct = si.get_index_struct()
self.assertEqual(struct.num_rows, 2)
self.assertEqual(struct.rows[0].length, 1)
self.assertEqual(struct.rows[0].data[0], 0b00010001000100010001)
self.assertEqual(struct.rows[1].length, 1)
self.assertEqual(struct.rows[1].data[0], 0b00100010001000100010)
si = tgrep.StringIndex([['aaaaa', 'bbbbb'], ['bbbbb']])
struct = si.get_index_struct()
self.assertEqual(struct.num_rows, 2)
self.assertEqual(struct.rows[0].length, 2)
self.assertEqual(struct.rows[0].data[0], 0b00010001000100010001)
self.assertEqual(struct.rows[0].data[1], 0b00100010001000100010)
class TestTgrep(unittest.TestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.tempdir)
def test_tgrep(self):
index = str(10 ** tgrep.NGRAM_CHARS)
for i in range(10):
name = os.path.join(self.tempdir, '{}.txt'.format(i))
f = open(name, 'w')
f.write(str(i * 10 ** tgrep.NGRAM_CHARS))
f.close()
search = str(10 ** tgrep.NGRAM_CHARS)
command = "{} {} {}/*.txt".format(
TGREP_FILE, search, self.tempdir)
out = subprocess.check_output(command, shell=True)
self.assertEqual(out.strip(), self.tempdir + '/1.txt:' + search)
def test_tgrep_or_regex(self):
str1 = str(10 ** tgrep.NGRAM_CHARS)
str2 = str(2 * 10 ** tgrep.NGRAM_CHARS)
search = "{}|{}".format(str1, str2)
for i in range(10):
name = os.path.join(self.tempdir, '{}.txt'.format(i))
f = open(name, 'w')
f.write(str(i * 10 ** tgrep.NGRAM_CHARS))
f.close()
command = "{} -E '{}' {}/*.txt".format(
TGREP_FILE, search, self.tempdir)
out = subprocess.check_output(command, shell=True)
self.assertEqual(out.strip(),
self.tempdir + '/1.txt:' + str1 + "\n"
+ self.tempdir + "/2.txt:" + str2)
def test_tgrep_unindexed(self):
index = str(10 ** tgrep.NGRAM_CHARS)
c_index = ctypes.c_char_p(index)
for i in range(10):
name = os.path.join(self.tempdir, '{}.txt'.format(i))
f = open(name, 'w')
f.write(str(i * 10 ** tgrep.NGRAM_CHARS))
f.close()
search = str(10 ** tgrep.NGRAM_CHARS)
command = "{} --filter='' {} {}/*.txt".format(
TGREP_FILE, search, self.tempdir)
out = subprocess.check_output(command, shell=True)
self.assertEqual(out.strip(), self.tempdir + '/1.txt:' + search)
if __name__ == '__main__':
unittest.main()