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