# HG changeset patch # User Thomas Harning Jr # Date 1189223720 14400 # Node ID b7351b503b4b4c1bad1b3dd3312a7a838b80a178 # Parent 139601546bd9eca2db4082a4f9608cbb914882bd event_buffer: drain learned spec notes, get_data learned negative values, many tests written The additional tests added were tests for: * Obtaining partial data using get_data * Reading lines using various line ending mixes/etc * Draining values w/ negative value diff -r 139601546bd9 -r b7351b503b4b CHANGELOG --- a/CHANGELOG Fri Sep 07 23:23:20 2007 -0400 +++ b/CHANGELOG Fri Sep 07 23:55:20 2007 -0400 @@ -6,7 +6,8 @@ + Added event_buffer object * Can 'add' a sequence of strings/event_buffers * Can 'get_data', 'length','drain','close' and create new instances - * Can 'readline' + * Can 'readline', 'read' from file des, 'write' to file des + * Added lunit tests for basic functions (read/write from/to FD) ====== 0.1.2 - 2007-08-18 + Setup system to use new coro management as described in COROUTINE_MANAGEMENT diff -r 139601546bd9 -r b7351b503b4b src/event_buffer.c --- a/src/event_buffer.c Fri Sep 07 23:23:20 2007 -0400 +++ b/src/event_buffer.c Fri Sep 07 23:55:20 2007 -0400 @@ -117,6 +117,9 @@ () - Returns all data in buffer (len) - Returns data up to 'len' bytes long (begin,len) - Returns data beginning at 'begin' up to 'len' bytes long + If begin < 0, wraps at data length + ex: (-1, 1) returns last character + (-2,2) returns last 2 chars [length meaning does not get inverted] */ static int event_buffer_get_data(lua_State* L) { le_buffer* buf = event_buffer_check(L, 1); @@ -135,8 +138,18 @@ break; case 3: default: + /* - 1 to map it to Lua's 1-based indexing + * If begin < 0 add length to cause position wrapping + */ begin = luaL_checkinteger(L, 2); + if(begin < 0) + begin += EVBUFFER_LENGTH(buf->buffer); + else + begin--; len = luaL_checkinteger(L, 3); + /* If length is less than zero, capture entire remaining string */ + + if(len < 0) len = EVBUFFER_LENGTH(buf->buffer); if(begin > EVBUFFER_LENGTH(buf->buffer)) begin = EVBUFFER_LENGTH(buf->buffer); if(begin + len > EVBUFFER_LENGTH(buf->buffer)) @@ -150,7 +163,7 @@ /* LUA: buffer:readline() Returns a line terminated by either '\r\n','\n\r' or '\r' or '\n' Returns nil and leaves data alone if no terminator is found - TODO: Evaluate whether or not the newline is included + Newline is not present in the captured string. */ static int event_buffer_readline(lua_State* L) { le_buffer* buf = event_buffer_check(L, 1); @@ -164,6 +177,8 @@ /* LUA: buffer:drain(amt) Drains 'amt' bytes from the buffer + If amt < 0, drains all data + (Due to auto-casting to unsigned int and automatic capping) */ static int event_buffer_drain(lua_State* L) { le_buffer* buf = event_buffer_check(L, 1); diff -r 139601546bd9 -r b7351b503b4b test/event_buffer-tests.lua --- a/test/event_buffer-tests.lua Fri Sep 07 23:23:20 2007 -0400 +++ b/test/event_buffer-tests.lua Fri Sep 07 23:55:20 2007 -0400 @@ -112,6 +112,52 @@ self.buffer:drain(5) end) testDataEqual("", self.buffer) + self.buffer:add("123456789") + testDataEqual("123456789", self.buffer) + assert_pass([[Should be able to apply negative draining to cause draining `all data` + (see source comments for why)]], function() + self.buffer:drain(-1) + end) + testDataEqual("", self.buffer) +end + +function bufferTests:test_getPartial() + self.buffer:add("123456789") + assert_equal("1234", self.buffer:get_data(4)) + assert_equal("1234", self.buffer:get_data(1,4)) + assert_equal("5678", self.buffer:get_data(5,4)) + assert_equal("5", self.buffer:get_data(5,1)) + assert_equal("56789", self.buffer:get_data(5,100000000), "Data length is capped at max obtainable") + assert_equal("56789", self.buffer:get_data(5,-100), "Negative sizes capture entire remaining string") + assert_equal("9", self.buffer:get_data(-1, 1, "Negative position causes wraparound")) + assert_equal("89", self.buffer:get_data(-2,2, "Negative wraparound does not cause length inversion")) +end + +local lineData = [[1 +2 +3]] +local splitLineData = { + "1","2",nil +} +local mixedLineData = "1\r2\n3\r\n4\n\r5\r\r6\n\n7\r\n\r8\r\n\r9" +local splitMixedLineData = { + "1","2","3","4","5","","6","","7","","8","", nil +} +function bufferTests:test_readline() + self.buffer:add(lineData) + testDataEqual(lineData, self.buffer) + for _, data in ipairs(splitLineData) do + assert_equal(data, self.buffer:readline()) + end + testDataEqual("3", self.buffer, "Failed readline doesn't affect buffer contents") + self.buffer:drain(-1) + testDataEqual("", self.buffer) + self.buffer:add(mixedLineData) + testDataEqual(mixedLineData, self.buffer) + for _, data in ipairs(splitMixedLineData) do + assert_equal(data, self.buffer:readline()) + end + testDataEqual("9", self.buffer) end lunit.run() \ No newline at end of file