Software Design Principles II - Object-Oriented Programming and Data Structures | CS 2110, Exams of Computer Science

Material Type: Exam; Class: Object-Oriented Programming and Data Structures; Subject: Computer Science; University: Cornell University; Term: Unknown 2008;

Typology: Exams

Pre 2010

Uploaded on 08/30/2009

koofers-user-v4l
koofers-user-v4l 🇺🇸

10 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2110
Software Design Principles II
Juan Altmayer Pizzorno
port25.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Software Design Principles II - Object-Oriented Programming and Data Structures | CS 2110 and more Exams Computer Science in PDF only on Docsity!

CS 2110

Software Design Principles II

Juan Altmayer Pizzorno

port25.com

Overview

Last week:

Design Concepts & Principles

Refactoring

Today: Test-Driven Development

TDD + JUnit by Example

Test Driven Development

We’ll go about in small iterations

1. add a test

2. run all tests and watch the new one fail

3. make a small change

4. run all tests and see them all succeed

5. refactor (as needed)

We’ll use JUnit

JUnit

What do JUnit tests look like?

SmallSet.java package edu.cornell.cs.cs2110; public class SmallSet { ... } SmallSetTest.java package edu.cornell.cs.cs2110; import org.junit.Test; import static org.junit.Assert.;* public class SmallSetTest { @Test public void testFoo () { SmallSet s = new SmallSet(); ... assertTrue (...); } @Test public void testBar() { ... } }

A First Test

We pick a feature and test it:

This doesn’t compile: size() is undefined

But that’s all right: we’ve started designing the

interface by using it

SmallSet

class SmallSet {}

SmallSetTest

class SmallSetTest {

@Test public void testEmptySetSize() {

SmallSet s = new SmallSet();

assertEquals(0, s.size());

Red Bar

We need the test to fail, so we define size()

Running the test

yields a red bar

indicating failure:

We’ve tested the test, and it works!

SmallSet class SmallSet { public int size() { return 42; } }

Adding Items

To implement adding items, we first test for it:

  • add()^ is undefined, so to run the test we define it:

SmallSetTest

class SmallSetTest {

@Test public void testEmptySetSize() ...

@Test public void testAddOne() {

SmallSet s = new SmallSet();

s.add(new Object());

assertEquals(1, s.size());

SmallSet

public int size() ...

public void add(Object o) {}

Adding Items

The test now fails as expected:

It seems obvious we need to count the number of

items:

And we get a green bar:

SmallSet

private int _size = 0;

public int size() {

return 0;

return _size;

public void add(Object o) {

++_size;

Remember that Item?...

We need to remember which items are in the set...

All tests pass, so we can refactor that loop...

SmallSet private int _size = 0; public static final int MAX = 10; private Object _items[] = new Object[MAX]; ... public void add(Object o) { for (int i=0; i < MAX; i++) { if (_items[i] == o) { return; } } _items[_size] = o; ++_size; }

Refactoring

(...loop) which doesn’t “speak to us” as it could...

All tests still pass, so we didn’t break it!

SmallSet (before) public void add(Object o) { for (int i=0; i < MAX; i++) { if (_items[i] == o) { return; } } _items[_size] = o; ++_size; } SmallSet (after) private boolean inSet(Object o) { for (int i=0; i < MAX; i++) { if (_items[i] == o) { return true; } } return false; } public void add(Object o) { if (!inSet(o)) { _items[_size] = o; ++_size; } }

Size Matters

We first have add() check the size,

... re-run the tests, check for green,

define our own exception...

... re-run the tests, check for green,

and...

SmallSet public void add(Object o) { if (!inSet(o) && _size < MAX ) { _items[_size] = o; ++_size; } } SmallSetFullException public class SmallSetFullException extends Error {}

Testing for Exceptions

... finally test for our exception:

The test fails as expected,

so now we fix it...

SmallSetTest @Test public void testAddTooMany() { SmallSet s = new SmallSet(); for (int i=0; i < SmallSet.MAX; i++) { s.add(new Object()); } try { s.add(new Object()); fail(“SmallSetFullException expected”); } catch (SmallSetFullException e) {} }

Review

Started with a “to do” list of tests / features

could have been expanded

as we thought of more tests / features

Added features in small iterations

“a feature without a test doesn’t exist”

(1) add test (2) make it pass (3) refactor

Fixing a Bug

What if after releasing we found a bug?