diff options
author | sanine <sanine.not@pm.me> | 2022-02-25 00:02:50 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-02-25 00:02:50 -0600 |
commit | 255cac17ec699d0ee400882d3a8d3bff2b0db61b (patch) | |
tree | 028fedb88edd9a91169f428624803fe71762d0ba /city | |
parent | fc3fdd59a1b8cceb1eb3e64ac41c6621301772ff (diff) |
begin adding city code
Diffstat (limited to 'city')
-rw-r--r-- | city/geometry-test.lua | 24 | ||||
-rw-r--r-- | city/geometry.lua | 28 | ||||
-rw-r--r-- | city/minunit.lua | 29 | ||||
-rw-r--r-- | city/quadtree.lua | 0 | ||||
-rw-r--r-- | city/test.lua | 3 |
5 files changed, 84 insertions, 0 deletions
diff --git a/city/geometry-test.lua b/city/geometry-test.lua new file mode 100644 index 0000000..ab3265d --- /dev/null +++ b/city/geometry-test.lua @@ -0,0 +1,24 @@ +local test = require('minunit').test +local geom = require 'geometry' + +test( + 'create point object', + function() + local pt = geom.point(1, 5.5) + assert(pt.x == 1) + assert(pt.y == 5.5) + local mt = getmetatable(pt) + assert(mt.__index == geom.point) + end +) + +test( + 'point distances', + function() + local a = geom.point(1, 3) + local b = geom.point(4, 7) + assert(a:distance_to(b) == 5) + + assert(a:distance_to(a) == 0) + end +) diff --git a/city/geometry.lua b/city/geometry.lua new file mode 100644 index 0000000..47e6a78 --- /dev/null +++ b/city/geometry.lua @@ -0,0 +1,28 @@ +local geom = {} + +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +-- +-- point +-- +-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +local point = { + __call = function(_, x, y) + local pt = {} + pt.x = x + pt.y = y + setmetatable(pt, {__index=_}) + return pt + end, + + distance_to = function(self, pt) + local d_x = self.x - pt.x + local d_y = self.y - pt.y + return math.sqrt(d_x*d_x + d_y*d_y) + end, +} +setmetatable(point, point) +geom.point = point + + +return geom diff --git a/city/minunit.lua b/city/minunit.lua new file mode 100644 index 0000000..7f735e6 --- /dev/null +++ b/city/minunit.lua @@ -0,0 +1,29 @@ +local tests_run = 0 +local tests_failed = 0 + +local function printf(fmt, ...) + print(string.format(fmt, ...)) +end + +function test(description, testfunc) + tests_run = tests_run + 1 + local ok, err = pcall(testfunc) + if ok then + printf('\t%s: OK', description) + else + printf('\t%s: FAIL\n\t%s\n', description, err) + tests_failed = tests_failed + 1 + end +end + +function suite(name) + local n_tests_old = tests_run + local n_failed_old = tests_failed + printf('suite: %s', name) + require(name) + printf('\tran %d tests, %d failed', + tests_run - n_tests_old, + tests_failed - n_failed_old) +end + +return { test=test, suite=suite } diff --git a/city/quadtree.lua b/city/quadtree.lua new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/city/quadtree.lua diff --git a/city/test.lua b/city/test.lua new file mode 100644 index 0000000..dd4652f --- /dev/null +++ b/city/test.lua @@ -0,0 +1,3 @@ +local run_suite = require('minunit').run_suite + +suite('geometry-test') |