Queue tests
This commit is contained in:
parent
3651af523d
commit
2765c5b4c9
47
spec/queue_spec.cr
Normal file
47
spec/queue_spec.cr
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
require "spec"
|
||||||
|
require "./../src/queue"
|
||||||
|
require "./../src/command"
|
||||||
|
|
||||||
|
macro define_dummy_classes(count)
|
||||||
|
{% for i in (1...count) %}
|
||||||
|
class Test::DummyCommand{{ i }} < Command
|
||||||
|
def start(world) : Int32
|
||||||
|
end
|
||||||
|
|
||||||
|
def finish(world)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
{% end %}
|
||||||
|
end
|
||||||
|
|
||||||
|
define_dummy_classes(5)
|
||||||
|
|
||||||
|
describe App::Queue do
|
||||||
|
it "should pop nil on empty queue" do
|
||||||
|
queue = App::Queue.new
|
||||||
|
item = queue.pop(50)
|
||||||
|
item.should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should pop command on one element queue" do
|
||||||
|
queue = App::Queue.new
|
||||||
|
queue.push(10, Test::DummyCommand1.new)
|
||||||
|
item = queue.pop(50)
|
||||||
|
item.nil?.should be_false
|
||||||
|
item.as(App::Queue::Item).ts.should eq 10
|
||||||
|
item.as(App::Queue::Item).command.should be_a(Test::DummyCommand1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should pop commands in proper order" do
|
||||||
|
queue = App::Queue.new
|
||||||
|
queue.push(10, Test::DummyCommand1.new)
|
||||||
|
queue.push(50, Test::DummyCommand2.new)
|
||||||
|
queue.push(30, Test::DummyCommand3.new)
|
||||||
|
item1 = queue.pop(100)
|
||||||
|
item1.as(App::Queue::Item).command.should be_a(Test::DummyCommand1)
|
||||||
|
item2 = queue.pop(100)
|
||||||
|
item2.as(App::Queue::Item).command.should be_a(Test::DummyCommand3)
|
||||||
|
item3 = queue.pop(100)
|
||||||
|
item3.as(App::Queue::Item).command.should be_a(Test::DummyCommand2)
|
||||||
|
end
|
||||||
|
end
|
@ -12,12 +12,3 @@ describe Point do
|
|||||||
p2.distance(p1).should eq 10
|
p2.distance(p1).should eq 10
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe App::Queue do
|
|
||||||
q = App::Queue(Int32).new
|
|
||||||
q.push(0, 1)
|
|
||||||
q.push(10, 2)
|
|
||||||
q.push(5, 3)
|
|
||||||
item = q.pop(50)
|
|
||||||
item.nil?.should be_falsey
|
|
||||||
end
|
|
||||||
|
17
src/map.cr
17
src/map.cr
@ -14,22 +14,15 @@ struct Point
|
|||||||
end
|
end
|
||||||
|
|
||||||
abstract class Tile
|
abstract class Tile
|
||||||
|
property cap : Int32 = 0
|
||||||
|
|
||||||
def initialize(@point : Point)
|
def initialize(@point : Point)
|
||||||
@cap = 0
|
|
||||||
@cur = 0
|
@cur = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def point
|
getter point
|
||||||
@point
|
getter cap
|
||||||
end
|
getter cur
|
||||||
|
|
||||||
def cap
|
|
||||||
@cap
|
|
||||||
end
|
|
||||||
|
|
||||||
def cur
|
|
||||||
@cur
|
|
||||||
end
|
|
||||||
|
|
||||||
def withdraw(value)
|
def withdraw(value)
|
||||||
if value >= @cur
|
if value >= @cur
|
||||||
|
14
src/queue.cr
14
src/queue.cr
@ -1,25 +1,25 @@
|
|||||||
class App::Queue(T)
|
class App::Queue
|
||||||
struct Item(T)
|
struct Item
|
||||||
def initialize(@ts : Int32, @value : T)
|
def initialize(@ts : Int32, @command : Command)
|
||||||
end
|
end
|
||||||
|
|
||||||
getter ts
|
getter ts
|
||||||
getter value
|
getter command
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@data = [] of Item(T)
|
@data = [] of Item
|
||||||
end
|
end
|
||||||
|
|
||||||
# Plan finishing of *command* at time *ts*
|
# Plan finishing of *command* at time *ts*
|
||||||
def push(ts : Int32, value : T)
|
def push(ts : Int32, value : Command)
|
||||||
@data.push(Item.new(ts, value))
|
@data.push(Item.new(ts, value))
|
||||||
@data.sort! do |a, b|
|
@data.sort! do |a, b|
|
||||||
b.ts <=> a.ts
|
b.ts <=> a.ts
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def pop(ts : Int32) : Item(T) | Nil
|
def pop(ts : Int32) : Item | Nil
|
||||||
if @data.size == 0
|
if @data.size == 0
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
@ -21,7 +21,7 @@ class World
|
|||||||
@ts = 0
|
@ts = 0
|
||||||
@resources = Resources.new
|
@resources = Resources.new
|
||||||
@map = Map.new
|
@map = Map.new
|
||||||
@queue = App::CommandQueue.new
|
@queue = App::Queue.new
|
||||||
end
|
end
|
||||||
|
|
||||||
private def ts
|
private def ts
|
||||||
@ -49,8 +49,8 @@ class World
|
|||||||
if item.nil?
|
if item.nil?
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
command_ts, command = item[:ts], item[:cmd]
|
command = item.command
|
||||||
@ts = command_ts
|
@ts = item.ts
|
||||||
command.finish(self)
|
command.finish(self)
|
||||||
printf "world : %d : finish `%s`\n", @ts, typeof(command)
|
printf "world : %d : finish `%s`\n", @ts, typeof(command)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user