SlideShare a Scribd company logo
1 of 128
Download to read offline
Structure and
Interpretation
of Test Cases
@KevlinHenney
Programs must be
written for people to
read, and only
incidentally for
machines to execute.
Write Tests for People
Gerard Meszaros
So who should you be writing the
tests for? For the person trying to
understand your code.
Good tests act as documentation
for the code they are testing.
Gerard Meszaros
Very many people say "TDD"
when they really mean, "I have
good unit tests" ("I have GUTs"?).
Alistair Cockburn
The modern programming professional has GUTs
good unit tests" ("I have GUTs
A unit test is a test of behaviour
whose success or failure is wholly
determined by the correctness of
the test and the correctness of
the unit under test.
Kevlin Henney
https://www.theregister.co.uk/2007/07/28/what_are_your_units/
Unit testable
Necessarily not unit testable
Should be unit testable... but isn’t
The programmer in me made unit testing more
about applying and exercising frameworks.
I had essentially reduced my concept of unit
testing to the basic mechanics of exercising
xUnit to verify the behavior of my classes.
My mindset had me thinking far too narrowly
about what it meant to write good unit tests.
Tod Golding
Tapping into Testing Nirvana
about what it meant to write good unit tests
public static bool IsLeapYear(int year) 
[Test]
public void Test() 
[Test]
public void TestIsLeapYear() 
[Test]
public void TestIsLeapYearIsCorrect() 
[Test]
public void Test1() 
[Test]
public void Test2() 
[Test]
public void TestLeapYears() 
[Test]
public void TestNonLeapYears() 
[Test]
public void TestLeapYears()
{
Assert.IsTrue(IsLeapYear(2016));
Assert.IsTrue(IsLeapYear(2000));
}
[Test]
public void TestNonLeapYears() 
[Test]
public void Test2016IsALeapYear() 
[Test]
public void Test2000IsALeapYear() 
[Test]
public void Test2018IsNotALeapYear() 
[Test]
public void Test1900IsNotALeapYear() 
[Test]
public void _2016IsALeapYear() 
[Test]
public void _2000IsALeapYear() 
[Test]
public void _2018IsNotALeapYear() 
[Test]
public void _1900IsNotALeapYear() 
[Test]
public void _2016_is_a_leap_year() 
[Test]
public void _2000_is_a_leap_year() 
[Test]
public void _2018_is_not_a_leap_year() 
[Test]
public void _1900_is_not_a_leap_year() 
_2016_is_a_leap_year
_2000_is_a_leap_year
_2018_is_not_a_leap_year
_1900_is_not_a_leap_year
_2020_is_a_leap_year
_2400_is_a_leap_year
_2019_is_not_a_leap_year
_2100_is_not_a_leap_year
Years_divisible_by_4_are_leap_years
Years_divisible_by_400_are_leap_years
Years_not_divisible_by_4_are_not_leap_years
Years_divisible_by_100_are_not_leap_years
Years_not_divisible_by_4_are_not_leap_years
Years_divisible_by_4_are_leap_years
Years_divisible_by_100_are_not_leap_years
Years_divisible_by_400_are_leap_years
Years_not_divisible_by_4_are_not_leap_years
Years_divisible_by_4_but_not_by_100_are_leap_year
Years_divisible_by_100_but_not_by_400_are_not_lea
Years_divisible_by_400_are_leap_years
Years_not_divisible_by_4_are_not_leap_years
Years_divisible_by_4_but_not_by_100_are_leap_years
Years_divisible_by_100_but_not_by_400_are_not_leap_years
Years_divisible_by_400_are_leap_years
IsLeapYear_YearNotDivisibleBy4_ReturnsFalse
IsLeapYear_YearDivisibleBy4ButNotBy100_ReturnsTrue
IsLeapYear_YearDivisibleBy100ButNotBy400_ReturnsFalse
IsLeapYear_YearDivisibleBy400_ReturnsTrue
Propositions
are vehicles
for stating
how things are
or might be.
Thus only indicative
sentences which it
makes sense to think
of as being true or as
being false are
capable of expressing
propositions.
Years_not_divisible_by_4_are_not_leap_years
Years_divisible_by_4_but_not_by_100_are_leap_years
Years_divisible_by_100_but_not_by_400_are_not_leap_years
Years_divisible_by_400_are_leap_years
public static bool IsLeapYear(int year)
{
return
year % 4 == 0 &&
year % 100 != 0 ||
year % 400 == 0;
}
Years_not_divisible_by_4_are_not_leap_years
Years_divisible_by_4_but_not_by_100_are_leap_years
Years_divisible_by_100_but_not_by_400_are_not_leap_years
Years_divisible_by_400_are_leap_years
public static bool IsLeapYear(int year)
{
return year % 4 == 0;
}
Years_not_divisible_by_4_are_not_leap_years
Years_divisible_by_4_but_not_by_100_are_leap_years
Years_divisible_by_100_but_not_by_400_are_not_leap_years
Years_divisible_by_400_are_leap_years
For tests to drive development they must
do more than just test that code performs
its required functionality: they must clearly
express that required functionality to the
reader.
That is, they must be clear specifications
of the required functionality.
Nat Pryce & Steve Freeman
Are your tests really driving your development?
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year
{
[Test]
public void if_it_is_divisible_by_4_but_not_by_100() 
[Test]
public void if_it_is_divisible_by_400() 
}
[TestFixture]
public class A_year_is_not_a_leap_year
{
[Test]
public void if_it_is_not_divisible_by_4() 
[Test]
public void if_it_is_divisible_by_100_but_not_by_400() 
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year
{
[Test]
public void if_it_is_divisible_by_4_but_not_by_100() 
[Test]
public void if_it_is_divisible_by_400() 
}
[TestFixture]
public class A_year_is_not_a_leap_year
{
[Test]
public void if_it_is_not_divisible_by_4() 
[Test]
public void if_it_is_divisible_by_100_but_not_by_400() 
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year 
[TestFixture]
public class A_year_is_not_a_leap_year
{
[Test]
public void if_it_is_not_divisible_by_4()
{
Assert.IsFalse(IsLeapYear(2018));
}
[Test]
public void if_it_is_divisible_by_100_but_not_by_400()
{
Assert.IsFalse(IsLeapYear(1900));
}
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year 
[TestFixture]
public class A_year_is_not_a_leap_year
{
[Test]
public void if_it_is_not_divisible_by_4()
{
Assert.IsFalse(isLeapYear(2018));
}
[Test]
public void if_it_is_divisible_by_100_but_not_by_400()
{
Assert.IsFalse(isLeapYear(1900));
}
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year 
[TestFixture]
public class A_year_is_not_a_leap_year
{
[Test]
public void if_it_is_not_divisible_by_4()
{
Assert.IsFalse(isLeapYear(42));
}
[Test]
public void if_it_is_divisible_by_100_but_not_by_400()
{
Assert.IsFalse(isLeapYear(100));
}
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year 
[TestFixture]
public class A_year_is_not_a_leap_year
{
[Test]
public void if_it_is_not_divisible_by_4()
{
Assert.IsFalse(IsLeapYear(2018));
Assert.IsFalse(IsLeapYear(2017));
Assert.IsFalse(IsLeapYear(42));
Assert.IsFalse(IsLeapYear(1));
}
[Test]
public void if_it_is_divisible_by_100_but_not_by_400() 
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year 
[TestFixture]
public class A_year_is_not_a_leap_year
{
[TestCase(2018)]
[TestCase(2017)]
[TestCase(42)]
[TestCase(1)]
public void if_it_is_not_divisible_by_4(int year)
{
Assert.IsFalse(IsLeapYear(year));
}
[Test]
public void if_it_is_divisible_by_100_but_not_by_400() 
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year 
[TestFixture]
public class A_year_is_not_a_leap_year
{
[Test]
public void if_it_is_not_divisible_by_4(
[Values(2018, 2017, 42, 1)] int year)
{
Assert.IsFalse(IsLeapYear(year));
}
[Test]
public void if_it_is_divisible_by_100_but_not_by_400() 
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year
{
[Test]
public void if_it_is_divisible_by_4_but_not_by_100(
[Values(2016, 1984, 4)] int year) 
[Test]
public void if_it_is_divisible_by_400(
[Range(400, 2400, 400)] int year) 
}
[TestFixture]
public class A_year_is_not_a_leap_year
{
[Test]
public void if_it_is_not_divisible_by_4(
[Values(2018, 2017, 42, 1)] int year) 
[Test]
public void if_it_is_divisible_by_100_but_not_by_400(
[Values(2100, 1900, 100)] int year) 
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year
{
[Test]
public void if_it_is_divisible_by_4_but_not_by_100(
[Values(2016, 1984, 4)] int year) 
[Test]
public void if_it_is_divisible_by_400(
[Range(400, 2400, 400)] int year) 
}
[TestFixture]
public class A_year_is_not_a_leap_year
{
[Test]
public void if_it_is_not_divisible_by_4(
[Values(2018, 2017, 42, 1)] int year) 
[Test]
public void if_it_is_divisible_by_100_but_not_by_400(
[Values(2100, 1900, 100)] int year) 
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year
{
[Test]
public void is_either_a_leap_year_or_not([Range(1, 10000)] int year) 
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year
{
[Test]
public void is_either_a_leap_year_or_not([Range(1, 10000)] int year) 
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year
{
[Test]
public void is_either_a_leap_year_or_not([Range(1, 10000)] int year)
{
Assert.AreEqual(
year % 4 == 0 && year % 100 != 0 || year % 400 == 0,
IsLeapYear(year));
}
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year
{
[Test]
public void is_either_a_leap_year_or_not([Range(1, 10000)] int year)
{
Assert.AreEqual(LeapYearExpectation(year), IsLeapYear(year));
}
public static bool LeapYearExpectation(int year)
{
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
}
}
public static bool IsLeapYear(int year)
{
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
All happy families are alike;
each unhappy family is
unhappy in its own way.
Leo Tolstoy
Anna Karenina
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year 
[TestFixture]
public class A_year_is_not_a_leap_year 
[TestFixture]
public class A_year_is_not_supported
{
[Test]
public void if_it_is_0()
{
Assert.Throws<ArgumentException>(() => IsLeapYear(0));
}
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year 
[TestFixture]
public class A_year_is_not_a_leap_year 
[TestFixture]
public class A_year_is_not_supported
{
[Test]
public void if_it_is_0()
{
Assert.Catch<ArgumentException>(() => IsLeapYear(0));
}
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year 
[TestFixture]
public class A_year_is_not_a_leap_year 
[TestFixture]
public class A_year_is_not_supported
{
[Test]
public void if_it_is_0()
{
Assert.Catch<ArgumentException>(() => IsLeapYear(0));
}
[Test]
public void if_it_is_negative(
[Values(-1, -4, -100, -400)] int year)
{
Assert.Catch<ArgumentException>(() => IsLeapYear(year));
}
}
}
namespace Leap_year_spec
{
[TestFixture]
public class A_year_is_a_leap_year 
[TestFixture]
public class A_year_is_not_a_leap_year 
[TestFixture]
public class A_year_is_supported
{
[Test]
public void if_it_is_positive(
[Values(1, int.MaxValue)] int year)
{
Assert.DoesNotThrow(() => IsLeapYear(year));
}
}
[TestFixture]
public class A_year_is_not_supported 
}
Stack
{new, push, pop, depth, top}
An abstract data type defines a
class of abstract objects which
is completely characterized by
the operations available on
those objects.
Barbara Liskov
Programming with Abstract Data Types
 T •  Stack
{
new: Stack[T],
push: Stack[T]  T → Stack[T],
pop: Stack[T] ⇸ Stack[T],
depth: Stack[T] → Integer,
top: Stack[T] ⇸ T
}
template<typename T>
class stack
{
public:
stack();
void push(const T & new_top);
void pop();
std::size_t depth() const;
T top() const;
private:
...
};
TEST_CASE(“test stack::stack”) 
TEST_CASE(“test stack::push”) 
TEST_CASE(“test stack::pop”) 
TEST_CASE(“test stack::depth”) 
TEST_CASE(“test stack::top”) 
TEST_CASE(“stack tests”)
{
SECTION(“test constructor”) 
SECTION(“test push”) 
SECTION(“test pop”) 
SECTION(“test depth”) 
SECTION(“test top”) 
}
TEST_CASE(“stack tests”)
{
SECTION(“constructor”) 
SECTION(“push”) 
SECTION(“pop”) 
SECTION(“depth”) 
SECTION(“top”) 
}
template<typename T>
class stack
{
public:
stack();
void push(const T & new_top);
void pop();
std::size_t depth() const;
T top() const;
private:
...
};
template<typename T>
class stack
{
public:
stack() = default;
void push(const T & new_top);
void pop();
std::size_t depth() const;
T top() const;
private:
...
};
TEST_CASE(“stack tests”)
{
SECTION(“constructor”) 
SECTION(“push”) 
SECTION(“pop”) 
SECTION(“depth”) 
SECTION(“top”) 
}
TEST_CASE(“stack tests”)
{
SECTION(“push”) 
SECTION(“pop”) 
SECTION(“depth”) 
SECTION(“top”) 
}
TEST_CASE(“stack tests”)
{
SECTION(“can be constructed”) 
SECTION(“can be pushed”) 
SECTION(“can be popped”) 
SECTION(“has depth”) 
SECTION(“has a top”) 
}
TEST_CASE(“stack tests”)
{
SECTION(“can be constructed”) 
SECTION(“can be pushed”) 
SECTION(“can sometimes be popped”) 
SECTION(“has depth”) 
SECTION(“sometimes has a top”) 
}
https://twitter.com/chrisoldwood/status/918139432447954944
Given
When
Then
an empty stack
an item is pushed
it should not be empty
Given
When
Then
an empty stack
an item is pushed
if it's OK with you, I
think that, perhaps, it
should probably not be
empty, don't you think?
Make definite assertions.
Avoid tame, colourless, hesitating, noncommittal
language.
When a sentence is made stronger, it usually becomes
shorter. Thus brevity is a by-product of vigour.
William Strunk and E B White
The Elements of Style
Given
When
Then
an empty stack
an item is pushed
it must not be empty
Given
When
Then
an empty stack
an item is pushed
it is not empty
Given
When
Then
And
an empty stack
an item is pushed
it has a depth of 1
the top item is the item
that was pushed
GivenAnEmptyStackWhenAnI
temIsPushedThenItHasADep
thOf1AndTheTopItemIsTheI
temThatWasPushed
Given_an_empty_stack_Whe
n_an_item_is_pushed_Then
_it_has_a_depth_of_1_And
_the_top_item_is_the_ite
m_that_was_pushed
Given an empty stack
When an item is pushed
Then it has a depth of 1
And the top item is the
item that was pushed
An empty stack acquires
depth by retaining a
pushed item as its top
Omit needless words.
William Strunk and E B White
The Elements of Style
TEST_CASE(“Stack specification”)
{

SECTION(“An empty stack acquires depth by retaining a pushed item as its top")
{
stack<std::string> stack;
stack.push(“NDC");
REQUIRE(stack.depth() == 1);
REQUIRE(stack.top() == “NDC");
}

}
TEST_CASE(“Stack specification”)
{
SECTION(“A new stack is empty”) 
SECTION(“An empty stack throws when queried for its top item”) 
SECTION(“An empty stack throws when popped”) 
SECTION(“An empty stack acquires depth by retaining a pushed item as its top”) 
SECTION(“A non-empty stack becomes deeper by retaining a pushed item as its top”) 
SECTION(“A non-empty stack on popping reveals tops in reverse order of pushing”) 
}
TEST_CASE(“Stack specification”)
{
SECTION(“A new stack is empty”) 
SECTION(“An empty stack throws when queried for its top item”) 
SECTION(“An empty stack throws when popped”) 
SECTION(“An empty stack acquires depth by retaining a pushed item as its top”) 
SECTION(“A non-empty stack becomes deeper by retaining a pushed item as its top”) 
SECTION(“A non-empty stack on popping reveals tops in reverse order of pushing”) 
}
TEST_CASE(“Stack specification”)
{

SECTION(“An empty stack acquires depth by retaining a pushed item as its top")
{
stack<std::string> stack;
stack.push(“NDC");
REQUIRE(stack.depth() == 1);
REQUIRE(stack.top() == “NDC");
}

}
For each usage scenario, the test(s):
▪ Describe the context, starting point, or
preconditions that must be satisfied
▪ Illustrate how the software is invoked
▪ Describe the expected results or
postconditions to be verified
Gerard Meszaros
TEST_CASE(“Stack specification”)
{

SECTION(“An empty stack acquires depth by retaining a pushed item as its top")
{
// Arrange:
stack<std::string> stack;
// Act:
stack.push(“NDC");
// Assert:
REQUIRE(stack.depth() == 1);
REQUIRE(stack.top() == “NDC");
}

}
TEST_CASE(“Stack specification”)
{

SECTION(“An empty stack acquires depth by retaining a pushed item as its top")
{
// Arrange:
stack<std::string> stack;
// Act:
stack.push(“NDC");
// Assert:
REQUIRE(stack.depth() == 1);
REQUIRE(stack.top() == “NDC");
}

}
TEST_CASE(“Stack specification”)
{

SECTION(“An empty stack acquires depth by retaining a pushed item as its top")
{
// Given:
stack<std::string> stack;
// When:
stack.push(“NDC");
// Then:
REQUIRE(stack.depth() == 1);
REQUIRE(stack.top() == “NDC");
}

}
https://twitter.com/jasongorman/status/572829496342134784
{P} Q {R}
Communications of the ACM, 12(10), October 1969
P {Q} R
If the assertion P is true before
initiation of a program Q, then
the assertion R will be true on
its completion.
Non-EmptyEmpty
depth depth
top
push
pop [depth > 1]
push
pop [depth = 1]
new
Non-EmptyEmpty
depth
top / error
pop / error
depth
top
push
pop [depth > 1]
push
pop [depth = 1]
new
TEST_CASE("Stack specification")
{
SECTION("A new stack")
{
SECTION("is empty") ...
}
SECTION("An empty stack")
{
SECTION("throws when queried for its top item") ...
SECTION("throws when popped") ...
SECTION("acquires depth by retaining a pushed item as its top") ...
}
SECTION("A non empty stack")
{
SECTION("becomes deeper by retaining a pushed item as its top") ...
SECTION("on popping reveals tops in reverse order of pushing") ...
}
}
TEST_CASE("Stack specification")
{
SECTION("A new stack")
{
SECTION("is empty") ...
}
SECTION("An empty stack")
{
SECTION("throws when queried for its top item") ...
SECTION("throws when popped") ...
SECTION("acquires depth by retaining a pushed item as its top") ...
}
SECTION("A non empty stack")
{
SECTION("becomes deeper by retaining a pushed item as its top") ...
SECTION("on popping reveals tops in reverse order of pushing") ...
}
}
TEST_CASE("Stack specification")
{
SECTION("A new stack")
{
...
}
SECTION("An empty stack")
{
...
SECTION("acquires depth by retaining a pushed item as its top")
{
stack<std::string> stack;
stack.push(“NDC");
REQUIRE(stack.depth() == 1);
REQUIRE(stack.top() == “NDC");
}
}
SECTION("A non empty stack")
{
...
}
}
TEST_CASE("Stack specification")
{
SECTION("A new stack")
{
...
}
SECTION("An empty stack")
{
stack<std::string> stack;
...
SECTION("acquires depth by retaining a pushed item as its top")
{
stack.push(“NDC");
REQUIRE(stack.depth() == 1);
REQUIRE(stack.top() == “NDC");
}
}
SECTION("A non empty stack")
{
...
}
}
SCENARIO("Stack specification")
{
...
GIVEN("An empty stack")
{
stack<std::string> stack;
...
WHEN("an item is pushed")
{
stack.push(“NDC");
THEN(“acquires depth by retaining the new item as its top”)
{
REQUIRE(stack.depth() == 1);
REQUIRE(stack.top() == “NDC");
}
}
}
...
}
SCENARIO("Stack specification")
{
...
GIVEN("An empty stack")
{
stack<std::string> stack;
...
WHEN("an item is pushed")
{
stack.push(“NDC");
THEN(“acquires depth”)
{
REQUIRE(stack.depth() == 1);
}
THEN(“retains the new item as its top”)
{
REQUIRE(stack.top() == “NDC");
}
}
}
...
}
SCENARIO("Stack specification")
{
...
GIVEN("An empty stack")
{
stack<std::string> stack;
...
WHEN("an item is pushed")
{
stack.push(“NDC");
THEN(“acquires depth”)
REQUIRE(stack.depth() == 1);
THEN(“retains the new item as its top”)
REQUIRE(stack.top() == “NDC");
}
}
...
}
Given can be used to group
tests for operations with
respect to common
initial state
When can be used to group
tests by operation,
regardless of initial state
or outcome
Then can be used to group
tests by common
outcome, regardless of
operation or initial state
Queue
{new, length, capacity, enqueue, dequeue}
producer consumerspacetime decoupling
N
buffered
bounded
asynchronous
N =
buffered
unbounded
asynchronous
∞
N = 1
buffered
bounded
asynchronous
futurepromise
N = 0
unbuffered
bounded
synchronous
public class Queue<T>
{
...
public Queue(int capacity) ...
public int length() ...
public int capacity() ...
public boolean enqueue(T last) ...
public Optional<T> dequeue() ...
}
Non-EmptyEmpty
length
capacity
dequeue
length
capacity
enqueue
dequeue [length > 1]
enqueue
dequeue [length = 1]
new [capacity > 0]
Non-Full
Empty
length
capacity
dequeue
length
capacity
enqueue
dequeue [length > 1]
enqueue
dequeue [length = 1]
new [capacity > 0]
Full
Non-Empty
[length = capacity]
[length < capacity]
public class Queue_spec 
public class A_new_queue 
public void is_empty() 
public void preserves_positive_bounding_capacity(int capacity) 
public void cannot_be_created_with_non_positive_bounding_capacity(int capacity) 
public class An_empty_queue 
public void dequeues_an_empty_value() 
public void remains_empty_when_null_enqueued() 
public void becomes_non_empty_when_non_null_value_enqueued(String value) 
public class A_non_empty_queue 
public class that_is_not_full 
public void becomes_longer_when_non_null_value_enqueued(String value) 
public void becomes_full_when_enqueued_up_to_capacity() 
public class that_is_full 
public void ignores_further_enqueued_values() 
public void becomes_non_full_when_dequeued() 
public void dequeues_values_in_order_enqueued() 
public void remains_unchanged_when_null_enqueued() 
public class Queue_spec 
public class A_new_queue 
public void is_empty() 
public void preserves_positive_bounding_capacity(int capacity) 
public void cannot_be_created_with_non_positive_bounding_capacity(int capacity) 
public class An_empty_queue 
public void dequeues_an_empty_value() 
public void remains_empty_when_null_enqueued() 
public void becomes_non_empty_when_non_null_value_enqueued(String value) 
public class A_non_empty_queue 
public class that_is_not_full 
public void becomes_longer_when_non_null_value_enqueued(String value) 
public void becomes_full_when_enqueued_up_to_capacity() 
public class that_is_full 
public void ignores_further_enqueued_values() 
public void becomes_non_full_when_dequeued() 
public void dequeues_values_in_order_enqueued() 
public void remains_unchanged_when_null_enqueued() 
public class Queue<T>
{
private Deque<T> items = new LinkedList<>();
private final int capacity;
public Queue(int boundingCapacity)
{
if (boundingCapacity < 1)
throw new IllegalArgumentException();
capacity = boundingCapacity;
}
public int length()
{
return items.size();
}
public int capacity()
{
return capacity;
}
public boolean enqueue(T last)
{
boolean enqueuing = last != null && length() < capacity();
if (enqueuing)
items.addLast(last);
return enqueuing;
}
public Optional<T> dequeue()
{
return Optional.ofNullable(items.pollFirst());
}
}
Structure and Interpretation of Test Cases
Structure and Interpretation of Test Cases

More Related Content

More from Kevlin Henney

More from Kevlin Henney (20)

Structure and Interpretation of Test Cases
Structure and Interpretation of Test CasesStructure and Interpretation of Test Cases
Structure and Interpretation of Test Cases
 
Agility ≠ Speed
Agility ≠ SpeedAgility ≠ Speed
Agility ≠ Speed
 
Refactoring to Immutability
Refactoring to ImmutabilityRefactoring to Immutability
Refactoring to Immutability
 
Old Is the New New
Old Is the New NewOld Is the New New
Old Is the New New
 
Turning Development Outside-In
Turning Development Outside-InTurning Development Outside-In
Turning Development Outside-In
 
Giving Code a Good Name
Giving Code a Good NameGiving Code a Good Name
Giving Code a Good Name
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
 
Thinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation QuadrantThinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation Quadrant
 
Code as Risk
Code as RiskCode as Risk
Code as Risk
 
Software Is Details
Software Is DetailsSoftware Is Details
Software Is Details
 
Game of Sprints
Game of SprintsGame of Sprints
Game of Sprints
 
Good Code
Good CodeGood Code
Good Code
 
The Error of Our Ways
The Error of Our WaysThe Error of Our Ways
The Error of Our Ways
 
Seven Ineffective Coding Habits of Many Programmers
Seven Ineffective Coding Habits of Many ProgrammersSeven Ineffective Coding Habits of Many Programmers
Seven Ineffective Coding Habits of Many Programmers
 
SOLID Deconstruction
SOLID DeconstructionSOLID Deconstruction
SOLID Deconstruction
 
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative PracticeDeclarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
 
Object? You Keep Using that Word
Object? You Keep Using that WordObject? You Keep Using that Word
Object? You Keep Using that Word
 
Driven to Tests
Driven to TestsDriven to Tests
Driven to Tests
 
Learning Curve
Learning CurveLearning Curve
Learning Curve
 
Unequal Equivalence
Unequal EquivalenceUnequal Equivalence
Unequal Equivalence
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Recently uploaded (20)

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 

Structure and Interpretation of Test Cases