Last active
January 13, 2016 02:01
-
-
Save jeremyf/e900b5498da95471d156 to your computer and use it in GitHub Desktop.
non_implemented_stack.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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