neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

multiqueue_spec.lua (4139B)


      1 local t = require('test.unit.testutil')
      2 local itp = t.gen_itp(it)
      3 
      4 local child_call_once = t.child_call_once
      5 local cimport = t.cimport
      6 local ffi = t.ffi
      7 local eq = t.eq
      8 
      9 local multiqueue = cimport('./test/unit/fixtures/multiqueue.h')
     10 
     11 describe('multiqueue (multi-level event-queue)', function()
     12  local parent, child1, child2, child3
     13 
     14  local function put(q, str)
     15    multiqueue.ut_multiqueue_put(q, str)
     16  end
     17 
     18  local function get(q)
     19    return ffi.string(multiqueue.ut_multiqueue_get(q))
     20  end
     21 
     22  local function free(q)
     23    multiqueue.multiqueue_free(q)
     24  end
     25 
     26  before_each(function()
     27    child_call_once(function()
     28      parent = multiqueue.multiqueue_new(ffi.NULL, ffi.NULL)
     29      child1 = multiqueue.multiqueue_new_child(parent)
     30      child2 = multiqueue.multiqueue_new_child(parent)
     31      child3 = multiqueue.multiqueue_new_child(parent)
     32      put(child1, 'c1i1')
     33      put(child1, 'c1i2')
     34      put(child2, 'c2i1')
     35      put(child1, 'c1i3')
     36      put(child2, 'c2i2')
     37      put(child2, 'c2i3')
     38      put(child2, 'c2i4')
     39      put(child3, 'c3i1')
     40      put(child3, 'c3i2')
     41    end)
     42  end)
     43 
     44  itp('keeps count of added events', function()
     45    eq(3, multiqueue.multiqueue_size(child1))
     46    eq(4, multiqueue.multiqueue_size(child2))
     47    eq(2, multiqueue.multiqueue_size(child3))
     48  end)
     49 
     50  itp('keeps count of removed events', function()
     51    multiqueue.multiqueue_get(child1)
     52    eq(2, multiqueue.multiqueue_size(child1))
     53    multiqueue.multiqueue_get(child1)
     54    eq(1, multiqueue.multiqueue_size(child1))
     55    multiqueue.multiqueue_get(child1)
     56    eq(0, multiqueue.multiqueue_size(child1))
     57    put(child1, 'c2ixx')
     58    eq(1, multiqueue.multiqueue_size(child1))
     59    multiqueue.multiqueue_get(child1)
     60    eq(0, multiqueue.multiqueue_size(child1))
     61    multiqueue.multiqueue_get(child1)
     62    eq(0, multiqueue.multiqueue_size(child1))
     63  end)
     64 
     65  itp('removing from parent removes from child', function()
     66    eq('c1i1', get(parent))
     67    eq('c1i2', get(parent))
     68    eq('c2i1', get(parent))
     69    eq('c1i3', get(parent))
     70    eq('c2i2', get(parent))
     71    eq('c2i3', get(parent))
     72    eq('c2i4', get(parent))
     73  end)
     74 
     75  itp('removing from child removes from parent', function()
     76    eq('c2i1', get(child2))
     77    eq('c2i2', get(child2))
     78    eq('c1i1', get(child1))
     79    eq('c1i2', get(parent))
     80    eq('c1i3', get(parent))
     81    eq('c2i3', get(parent))
     82    eq('c2i4', get(parent))
     83  end)
     84 
     85  itp('removing from child at the beginning of parent', function()
     86    eq('c1i1', get(child1))
     87    eq('c1i2', get(child1))
     88    eq('c2i1', get(parent))
     89  end)
     90 
     91  itp('removing from parent after get from parent and put to child', function()
     92    eq('c1i1', get(parent))
     93    eq('c1i2', get(parent))
     94    eq('c2i1', get(parent))
     95    eq('c1i3', get(parent))
     96    eq('c2i2', get(parent))
     97    eq('c2i3', get(parent))
     98    eq('c2i4', get(parent))
     99    eq('c3i1', get(parent))
    100    put(child1, 'c1i11')
    101    put(child1, 'c1i22')
    102    eq('c3i2', get(parent))
    103    eq('c1i11', get(parent))
    104    eq('c1i22', get(parent))
    105  end)
    106 
    107  itp('removing from parent after get and put to child', function()
    108    eq('c1i1', get(child1))
    109    eq('c1i2', get(child1))
    110    eq('c2i1', get(child2))
    111    eq('c1i3', get(child1))
    112    eq('c2i2', get(child2))
    113    eq('c2i3', get(child2))
    114    eq('c2i4', get(child2))
    115    eq('c3i1', get(child3))
    116    eq('c3i2', get(parent))
    117    put(child1, 'c1i11')
    118    put(child2, 'c2i11')
    119    put(child1, 'c1i12')
    120    eq('c2i11', get(child2))
    121    eq('c1i11', get(parent))
    122    eq('c1i12', get(parent))
    123  end)
    124 
    125  itp('put after removing from child at the end of parent', function()
    126    eq('c3i1', get(child3))
    127    eq('c3i2', get(child3))
    128    put(child1, 'c1i11')
    129    put(child2, 'c2i11')
    130    eq('c1i1', get(parent))
    131    eq('c1i2', get(parent))
    132    eq('c2i1', get(parent))
    133    eq('c1i3', get(parent))
    134    eq('c2i2', get(parent))
    135    eq('c2i3', get(parent))
    136    eq('c2i4', get(parent))
    137    eq('c1i11', get(parent))
    138    eq('c2i11', get(parent))
    139  end)
    140 
    141  itp('removes from parent queue when child is freed', function()
    142    free(child2)
    143    eq('c1i1', get(parent))
    144    eq('c1i2', get(parent))
    145    eq('c1i3', get(parent))
    146    eq('c3i1', get(child3))
    147    eq('c3i2', get(child3))
    148  end)
    149 end)