robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
9.8 KiB375 linesraw
1local t = require('test.unit.testutil')
2local describe = t.describe
3local itp = t.gen_itp(t.it)
4
5local cimport = t.cimport
6local internalize = t.internalize
7local eq = t.eq
8local neq = t.neq
9local ffi = t.ffi
10local to_cstr = t.to_cstr
11local NULL = t.NULL
12
13local garray = cimport('./src/nvim/garray.h')
14
15local itemsize = 14
16local growsize = 95
17
18-- define a basic interface to garray. We could make it a lot nicer by
19-- constructing a class wrapper around garray. It could for example associate
20-- ga_clear_strings to the underlying garray cdata if the garray is a string
21-- array. But for now I estimate that that kind of magic might make testing
22-- less "transparent" (i.e.: the interface would become quite different as to
23-- how one would use it from C.
24
25-- accessors
26local ga_len = function(garr)
27  return garr[0].ga_len
28end
29
30local ga_maxlen = function(garr)
31  return garr[0].ga_maxlen
32end
33
34local ga_itemsize = function(garr)
35  return garr[0].ga_itemsize
36end
37
38local ga_growsize = function(garr)
39  return garr[0].ga_growsize
40end
41
42local ga_data = function(garr)
43  return garr[0].ga_data
44end
45
46-- derived accessors
47local ga_size = function(garr)
48  return ga_len(garr) * ga_itemsize(garr)
49end
50
51local ga_maxsize = function(garr) -- luacheck: ignore
52  return ga_maxlen(garr) * ga_itemsize(garr)
53end
54
55local ga_data_as_bytes = function(garr)
56  return ffi.cast('uint8_t *', ga_data(garr))
57end
58
59local ga_data_as_strings = function(garr)
60  return ffi.cast('char **', ga_data(garr))
61end
62
63local ga_data_as_ints = function(garr)
64  return ffi.cast('int *', ga_data(garr))
65end
66
67-- garray manipulation
68local ga_init = function(garr, itemsize_, growsize_)
69  return garray.ga_init(garr, itemsize_, growsize_)
70end
71
72local ga_clear = function(garr)
73  return garray.ga_clear(garr)
74end
75
76local ga_clear_strings = function(garr)
77  t.ok(ga_itemsize(garr) == ffi.sizeof('char *'))
78  return garray.ga_clear_strings(garr)
79end
80
81local ga_grow = function(garr, n)
82  return garray.ga_grow(garr, n)
83end
84
85local ga_concat = function(garr, str)
86  return garray.ga_concat(garr, to_cstr(str))
87end
88
89local ga_append = function(garr, b)
90  if type(b) == 'string' then
91    return garray.ga_append(garr, string.byte(b))
92  else
93    return garray.ga_append(garr, b)
94  end
95end
96
97local ga_concat_strings = function(garr, sep)
98  return internalize(garray.ga_concat_strings(garr, to_cstr(sep)))
99end
100
101local ga_remove_duplicate_strings = function(garr)
102  return garray.ga_remove_duplicate_strings(garr)
103end
104
105-- derived manipulators
106local ga_set_len = function(garr, len)
107  t.ok(len <= ga_maxlen(garr))
108  garr[0].ga_len = len
109end
110
111local ga_inc_len = function(garr, by)
112  return ga_set_len(garr, ga_len(garr) + by)
113end
114
115-- custom append functions
116-- not the C ga_append, which only works for bytes
117local ga_append_int = function(garr, it)
118  t.ok(ga_itemsize(garr) == ffi.sizeof('int'))
119  ga_grow(garr, 1)
120  local data = ga_data_as_ints(garr)
121  data[ga_len(garr)] = it
122  return ga_inc_len(garr, 1)
123end
124
125local ga_append_string = function(garr, it)
126  t.ok(ga_itemsize(garr) == ffi.sizeof('char *'))
127  -- make a non-garbage collected string and copy the lua string into it,
128  -- TODO(aktau): we should probably call xmalloc here, though as long as
129  -- xmalloc is based on malloc it should work.
130  local mem = ffi.C.malloc(string.len(it) + 1)
131  ffi.copy(mem, it)
132  ga_grow(garr, 1)
133  local data = ga_data_as_strings(garr)
134  data[ga_len(garr)] = mem
135  return ga_inc_len(garr, 1)
136end
137
138local ga_append_strings = function(garr, ...)
139  local prevlen = ga_len(garr)
140  local len = select('#', ...)
141  for i = 1, len do
142    ga_append_string(garr, select(i, ...))
143  end
144  return eq(prevlen + len, ga_len(garr))
145end
146
147local ga_append_ints = function(garr, ...)
148  local prevlen = ga_len(garr)
149  local len = select('#', ...)
150  for i = 1, len do
151    ga_append_int(garr, select(i, ...))
152  end
153  return eq(prevlen + len, ga_len(garr))
154end
155
156-- enhanced constructors
157local garray_ctype = function(...)
158  return ffi.typeof('garray_T[1]')(...)
159end
160local new_garray = function()
161  local garr = garray_ctype()
162  return ffi.gc(garr, ga_clear)
163end
164
165local new_string_garray = function()
166  local garr = garray_ctype()
167  ga_init(garr, ffi.sizeof('unsigned char *'), 1)
168  return ffi.gc(garr, ga_clear_strings)
169end
170
171local randomByte = function()
172  return ffi.cast('uint8_t', math.random(0, 255))
173end
174
175-- scramble the data in a garray
176local ga_scramble = function(garr)
177  local size, bytes = ga_size(garr), ga_data_as_bytes(garr)
178  for i = 0, size - 1 do
179    bytes[i] = randomByte()
180  end
181end
182
183describe('garray', function()
184  describe('ga_init', function()
185    itp('initializes the values of the garray', function()
186      local garr = new_garray()
187      ga_init(garr, itemsize, growsize)
188      eq(0, ga_len(garr))
189      eq(0, ga_maxlen(garr))
190      eq(growsize, ga_growsize(garr))
191      eq(itemsize, ga_itemsize(garr))
192      eq(NULL, ga_data(garr))
193    end)
194  end)
195
196  describe('ga_grow', function()
197    local function new_and_grow(itemsize_, growsize_, req)
198      local garr = new_garray()
199      ga_init(garr, itemsize_, growsize_)
200      eq(0, ga_size(garr)) -- should be 0 at first
201      eq(NULL, ga_data(garr)) -- should be NULL
202      ga_grow(garr, req) -- add space for `req` items
203      return garr
204    end
205
206    itp('grows by growsize items if num < growsize', function()
207      itemsize = 16
208      growsize = 4
209      local grow_by = growsize - 1
210      local garr = new_and_grow(itemsize, growsize, grow_by)
211      neq(NULL, ga_data(garr)) -- data should be a ptr to memory
212      eq(growsize, ga_maxlen(garr)) -- we requested LESS than growsize, so...
213    end)
214
215    itp('grows by num items if num > growsize', function()
216      itemsize = 16
217      growsize = 4
218      local grow_by = growsize + 1
219      local garr = new_and_grow(itemsize, growsize, grow_by)
220      neq(NULL, ga_data(garr)) -- data should be a ptr to memory
221      eq(grow_by, ga_maxlen(garr)) -- we requested MORE than growsize, so...
222    end)
223
224    itp('does not grow when nothing is requested', function()
225      local garr = new_and_grow(16, 4, 0)
226      eq(NULL, ga_data(garr))
227      eq(0, ga_maxlen(garr))
228    end)
229  end)
230
231  describe('ga_clear', function()
232    itp('clears an already allocated array', function()
233      -- allocate and scramble an array
234      local garr = garray_ctype()
235      ga_init(garr, itemsize, growsize)
236      ga_grow(garr, 4)
237      ga_set_len(garr, 4)
238      ga_scramble(garr)
239
240      -- clear it and check
241      ga_clear(garr)
242      eq(NULL, ga_data(garr))
243      eq(0, ga_maxlen(garr))
244      eq(0, ga_len(garr))
245    end)
246  end)
247
248  describe('ga_append', function()
249    itp('can append bytes', function()
250      -- this is the actual ga_append, the others are just emulated lua
251      -- versions
252      local garr = new_garray()
253      ga_init(garr, ffi.sizeof('uint8_t'), 1)
254      ga_append(garr, 'h')
255      ga_append(garr, 'e')
256      ga_append(garr, 'l')
257      ga_append(garr, 'l')
258      ga_append(garr, 'o')
259      ga_append(garr, 0)
260      local bytes = ga_data_as_bytes(garr)
261      eq('hello', ffi.string(bytes))
262    end)
263
264    itp('can append integers', function()
265      local garr = new_garray()
266      ga_init(garr, ffi.sizeof('int'), 1)
267      local input = {
268        -20,
269        94,
270        867615,
271        90927,
272        86,
273      }
274      ga_append_ints(garr, unpack(input))
275      local ints = ga_data_as_ints(garr)
276      for i = 0, #input - 1 do
277        eq(input[i + 1], ints[i])
278      end
279    end)
280
281    itp('can append strings to a growing array of strings', function()
282      local garr = new_string_garray()
283      local input = {
284        'some',
285        'str',
286        '\r\n\r●●●●●●,,,',
287        'hmm',
288        'got it',
289      }
290      ga_append_strings(garr, unpack(input))
291      -- check that we can get the same strings out of the array
292      local strings = ga_data_as_strings(garr)
293      for i = 0, #input - 1 do
294        eq(input[i + 1], ffi.string(strings[i]))
295      end
296    end)
297  end)
298
299  describe('ga_concat', function()
300    itp('concatenates the parameter to the growing byte array', function()
301      local garr = new_garray()
302      ga_init(garr, ffi.sizeof('char'), 1)
303      local str = 'ohwell●●'
304      local loop = 5
305      for _ = 1, loop do
306        ga_concat(garr, str)
307      end
308
309      -- ga_concat does NOT append the NUL in the src string to the
310      -- destination, you have to do that manually by calling something like
311      -- ga_append(gar, '\0'). I'ts always used like that in the vim
312      -- codebase. I feel that this is a bit of an unnecesesary
313      -- micro-optimization.
314      ga_append(garr, 0)
315      local result = ffi.string(ga_data_as_bytes(garr))
316      eq(string.rep(str, loop), result)
317    end)
318  end)
319
320  local function test_concat_fn(input, sep)
321    local garr = new_string_garray()
322    ga_append_strings(garr, unpack(input))
323    eq(table.concat(input, sep), ga_concat_strings(garr, sep))
324  end
325
326  describe('ga_concat_strings', function()
327    itp('returns an empty string when concatenating an empty array', function()
328      test_concat_fn({}, ',')
329      test_concat_fn({}, '---')
330    end)
331
332    itp('can concatenate a non-empty array', function()
333      test_concat_fn({
334        'oh',
335        'my',
336        'neovim',
337      }, ',')
338      test_concat_fn({
339        'oh',
340        'my',
341        'neovim',
342      }, '-●●-')
343    end)
344  end)
345
346  describe('ga_remove_duplicate_strings', function()
347    itp('sorts and removes duplicate strings', function()
348      local garr = new_string_garray()
349      local input = {
350        'ccc',
351        'aaa',
352        'bbb',
353        'ddd●●',
354        'aaa',
355        'bbb',
356        'ccc',
357        'ccc',
358        'ddd●●',
359      }
360      local sorted_dedup_input = {
361        'aaa',
362        'bbb',
363        'ccc',
364        'ddd●●',
365      }
366      ga_append_strings(garr, unpack(input))
367      ga_remove_duplicate_strings(garr)
368      eq(#sorted_dedup_input, ga_len(garr))
369      local strings = ga_data_as_strings(garr)
370      for i = 0, #sorted_dedup_input - 1 do
371        eq(sorted_dedup_input[i + 1], ffi.string(strings[i]))
372      end
373    end)
374  end)
375end)