Skip to content

Instantly share code, notes, and snippets.

@graue
Last active December 14, 2015 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graue/5000167 to your computer and use it in GitHub Desktop.
Save graue/5000167 to your computer and use it in GitHub Desktop.
Specification (tests!) for not yet implemented Lua HistArray
local HistArray = require "histArray"
-- The HistArray module provides an array that automatically purges
-- old values. It lets you write code like:
--
-- y[n] = c1*x[n] + c2*x[n-1] + c3*x[n-2]
-- - c4*y[n-1] - c5*y[n-2]
--
-- If x and y are HistArrays created with history size 2, all samples from
-- x[n-3] and y[n-3] and below will be purged (accessing them is an error)
-- so that the no longer needed values do not waste any memory.
--
-- FAQ: How does a HistArray know when to purge old values?
-- Answer: For all n, when y[n] is assigned to, y[n - (y.histSize+1)]
-- and below are purged. This allows most recurrence relations
-- (like the above) to be defined in a natural manner.
describe("a history array", function()
it("is initialized with a history size", function()
local y = HistArray.new(2)
assert.are.equal(2, y.histSize)
end)
it("holds as many values as its history size, plus one", function()
local y = HistArray.new(2)
y[1] = 0.1
y[2] = 0.2
y[3] = 0.3
y[4] = 0.4
assert.are.equal(0.4, y[4]) -- current value
assert.are.equal(0.3, y[3]) -- current minus 1
assert.are.equal(0.2, y[2]) -- current minus 2
assert.has_error(function() local _ = y[1] end) -- too old!
end)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment