Add new tests for inputstreams
This commit is contained in:
parent
253ab538ca
commit
c5833e094e
|
@ -1,6 +1,8 @@
|
||||||
local ds = require("DataStructures")
|
local ds = require("DataStructures")
|
||||||
local Dictionary, Stack, WordInfo, Environment = ds.Dictionary, ds.Stack, ds.WordInfo, ds.Environment
|
local Dictionary, Stack, WordInfo, Environment = ds.Dictionary, ds.Stack, ds.WordInfo, ds.Environment
|
||||||
local Pointer = ds.Pointer
|
local Pointer = ds.Pointer
|
||||||
|
|
||||||
|
local InputStream = require("InputStream")
|
||||||
local CoreHelpers = {}
|
local CoreHelpers = {}
|
||||||
|
|
||||||
function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(Environment, ...: any): any..., imm: boolean)
|
function CoreHelpers.defineWord(dict: Dictionary, str: string, func: function(Environment, ...: any): any..., imm: boolean)
|
||||||
|
@ -13,6 +15,37 @@ function CoreHelpers.standardInputRefill(): string
|
||||||
return input
|
return input
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function CoreHelpers._oneReadRefill(str: string): function(): string
|
||||||
|
local alreadyRead: boolean = false
|
||||||
|
return function(): string
|
||||||
|
if not alreadyRead then
|
||||||
|
alreadyRead = true
|
||||||
|
return str
|
||||||
|
else
|
||||||
|
return ""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function CoreHelpers._fileRefill(fname: string): function(): string
|
||||||
|
local f = assert(io.open(fname, "r"))
|
||||||
|
return function(): string
|
||||||
|
local chunk = f:read(2^13)
|
||||||
|
if not chunk then chunk = "" end
|
||||||
|
return chunk
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function CoreHelpers.fileStream(fname: string): InputStream
|
||||||
|
local istream = InputStream:new(CoreHelpers._fileRefill(fname))
|
||||||
|
return istream
|
||||||
|
end
|
||||||
|
|
||||||
|
function CoreHelpers.oneReadInputStream(str: string): InputStream
|
||||||
|
local iStream = InputStream:new(CoreHelpers._oneReadRefill(str))
|
||||||
|
return iStream
|
||||||
|
end
|
||||||
|
|
||||||
function CoreHelpers.areNumbers(a: any, b: any): boolean
|
function CoreHelpers.areNumbers(a: any, b: any): boolean
|
||||||
return a is number and b is number
|
return a is number and b is number
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,7 +25,7 @@ function InputStream:_manageBuffer()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function initial_stream(): string
|
local function initial_stream(): string
|
||||||
return "DUMMY"
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -52,14 +52,21 @@ function InputStream:__setRefill(func: function(): string)
|
||||||
end
|
end
|
||||||
|
|
||||||
--
|
--
|
||||||
function InputStream:curr(): string
|
function InputStream:curr(): string | nil
|
||||||
|
self:_manageBuffer()
|
||||||
|
if #self.str > 0 then
|
||||||
return self.str:sub(self.offset, self.offset)
|
return self.str:sub(self.offset, self.offset)
|
||||||
end
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
function InputStream:next(): string
|
function InputStream:next(): string | nil
|
||||||
self:_manageBuffer()
|
self:_manageBuffer()
|
||||||
local current_char = self.str:sub(self.offset, self.offset)
|
local current_char: string | nil = nil
|
||||||
|
if #self.str > 0 then
|
||||||
|
current_char = self.str:sub(self.offset, self.offset)
|
||||||
self.offset = self.offset + 1
|
self.offset = self.offset + 1
|
||||||
|
end
|
||||||
return current_char
|
return current_char
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
as;dlfkjasl;dfjasl;dfkjasl;dfkjaslfjgbag;aga;gja;gjasl
|
||||||
|
afasl;fjasl;kdfj
|
||||||
|
|
||||||
|
adfl;jkasl;dfasjkl;j;l
|
||||||
|
|
||||||
|
;alsjdfl;kasjd
|
||||||
|
|
||||||
|
|
||||||
|
a;sldkfjals;dfj
|
||||||
|
|
||||||
|
g;aga;fgnagbn;
|
||||||
|
;
|
||||||
|
kaFASDLFM
|
||||||
|
|
||||||
|
;aSKDFa;fJ
|
||||||
|
|
||||||
|
;lkjasdldjkfal
|
||||||
|
|
||||||
|
lasfsalfkmakvml;mzxcv,mz!6*84a;lskdmas,dmfas.,fm
|
||||||
|
|
37
tests.lua
37
tests.lua
|
@ -1,7 +1,7 @@
|
||||||
local istream = require("InputStream")
|
local istream = require("InputStream")
|
||||||
local ds = require("DataStructures")
|
local ds = require("DataStructures")
|
||||||
local Stack, Dictionary, WordInfo, Environment = ds.Stack, ds.Dictionary, ds.WordInfo, ds.Environment
|
local Stack, Dictionary, WordInfo, Environment = ds.Stack, ds.Dictionary, ds.WordInfo, ds.Environment
|
||||||
|
local CoreHelpers = require("CoreHelpers")
|
||||||
|
|
||||||
function pushThree(s)
|
function pushThree(s)
|
||||||
s:push(1)
|
s:push(1)
|
||||||
|
@ -61,9 +61,42 @@ describe("InputStream tests", function()
|
||||||
local inputString = dummyInput()
|
local inputString = dummyInput()
|
||||||
for i = 0, 100 do
|
for i = 0, 100 do
|
||||||
local j = i % #inputString + 1
|
local j = i % #inputString + 1
|
||||||
assert.are.same(inputString:sub(j, j), input:advance())
|
assert.are.same(inputString:sub(j, j), input:next())
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
describe("Tests with ends of input", function()
|
||||||
|
it("handles a single line", function()
|
||||||
|
local inputstr = "TEST TEST TEST"
|
||||||
|
input = CoreHelpers.oneReadInputStream(inputstr)
|
||||||
|
for i = 1, #inputstr do
|
||||||
|
assert.are.same(inputstr:sub(i, i), input:next())
|
||||||
|
end
|
||||||
|
for i = 1, 1000 do
|
||||||
|
assert.are.same(nil, input:next())
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
it("handles multiple lines", function()
|
||||||
|
local inputstr = "TEST\nTEST\nTEST\nTEST\nTEST\nTEST51515151"
|
||||||
|
input = CoreHelpers.oneReadInputStream(inputstr)
|
||||||
|
for i = 1, #inputstr do
|
||||||
|
assert.are.same(inputstr:sub(i, i), input:next())
|
||||||
|
end
|
||||||
|
for i = 1, 1000 do
|
||||||
|
assert.are.same(nil, input:next())
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
it("can do files", function()
|
||||||
|
local fname = "./filetest.txt"
|
||||||
|
input = CoreHelpers.fileStream(fname)
|
||||||
|
local expected_output = io.open(fname, "r"):read("*all")
|
||||||
|
for i = 1, #expected_output do
|
||||||
|
assert.are.same(expected_output:sub(i,i), input:next())
|
||||||
|
end
|
||||||
|
for i = 1, 1000 do
|
||||||
|
assert.are.same(nil, input:next())
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
describe("WordInfo tests", function()
|
describe("WordInfo tests", function()
|
||||||
local Environment = ds.Environment
|
local Environment = ds.Environment
|
||||||
|
|
Loading…
Reference in New Issue