Skip to content

Instantly share code, notes, and snippets.

@jeremyf

jeremyf/stack.rb

Last active Jan 13, 2016
Embed
What would you like to do?
non_implemented_stack.rb
class LinkedListNode
attr_accessor :value, :points_to_node
def initialize(value, points_to_node=nil)
@value = value
@points_to_node = points_to_node
end
end
class Stack
attr_reader :data
def initialize
@data = nil
end
def pop
# Reverse what you did for push
end
def push(value)
# do something using a LinkedListNode
@data = LinkedListNode.new(value, @data)
end
end
# -------- You could put what's below in a separate file, but why bother
require 'minitest/autorun'
class TestStack < MiniTest::Unit::TestCase
def test_pop_without_push_returns_nil
stack = Stack.new
assert_equal(nil, stack.pop)
end
def test_push_then_pop_returns_what_was_just_pushed
stack = Stack.new
stack.push(3)
assert_equal(3, stack.pop)
end
def test_push_a_few_then_pop_a_few
stack = Stack.new
stack.push(3)
stack.push(7)
stack.push(9)
puts stack.data.inspect
assert_equal(9, stack.pop)
assert_equal(7, stack.pop)
assert_equal(3, stack.pop)
assert_equal(nil, stack.pop)
end
def test_push_a_few_heterogenous_items_then_pop_a_few
object = Object.new
stack = Stack.new
stack.push(3)
stack.push(object)
stack.push(9)
assert_equal(9, stack.pop)
assert_equal(object, stack.pop)
assert_equal(3, stack.pop)
assert_equal(nil, stack.pop)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.