For each of the following problems you should start by writing several JUnit tes
ID: 3672991 • Letter: F
Question
For each of the following problems you should start by writing several JUnit tests that will verify correct behavior. Once you have written at least four distinct tests for each of the problems, begin to implement solutions to each of the problems. Be sure to run your unit tests often. They are not necessarily in order of difficulty.
Define a method named accept that has a single String parameter and which returns a HashMap. Supposing the parameter is named inputFilePath, the method must: a. read the contents of a file (identified by inputFilePath) one char at a time, b. segment the input into words, and c. keep track of word counts in a java.util.HashMap. A word is defined as a contiguous sequence of characters that does not contain word separator characters, where word separator characters are: ' ' (space), ' ' (tab), ' ' (newline), ',' (comma) and '.' (period). 0 1 3 P D S W 2 Think carefully about this definition, and how it applies to an initial character sequence not preceded by a word separator, and also a final character sequence not followed by a word separator. You must use only CharacterFromFileReader1 to read characters from the input file. Use it as an Iterator, keeping in mind the autoboxing and unboxing features of the Java language. In order to keep your code readable, break your code into several methods. Define meaningful private helper methods that you call from the required public method.
Explanation / Answer
In computer programming, unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use. Intuitively, one can view a unit as the smallest testable part of an application. In procedural programming, a unit could be an entire module, but it is more commonly an individual function or procedure. In object-oriented programming, a unit is often an entire interface, such as a class, but could be an individual method. Unit tests are short code fragments created by programmers or occasionally by white box testers during the development process. It forms the basis for component testing.
Ideally, each test case is independent from the others. Substitutes such as method stubs, mock objects,fakes, and test harnesses can be used to assist testing a module in isolation. Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended.
When software is developed using a test-driven approach, the combination of writing the unit test to specify the interface plus the refactoring activities performed after the test is passing, may take the place of formal design. Each unit test can be seen as a design element specifying classes, methods, and observable behaviour. The following Java example will help illustrate this point.
Here is a set of test cases that specify a number of elements of the implementation. First, that there must be an interface called Adder, and an implementing class with a zero-argument constructor called AdderImpl. It goes on to assert that the Adder interface should have a method called add, with two integer parameters, which returns another integer. It also specifies the behaviour of this method for a small range of values over a number of test methods.
PROGRAM :
package fibsandlies;
import java.util.HashMap;
public class FibCalculator extends Fibonacci implements Calculator {
private static Map<Integer, Integer> memoized = new HashMap<Integer, Integer>();
public static void main(String[] args) {
memoized.put(1, 1);
memoized.put(2, 1);
System.out.println(fibonacci(12));
}
public static int fibonacci(int fibIndex) {
if (memoized.containsKey(fibIndex)) {
return memoized.get(fibIndex);
} else {
int answer = fibonacci(fibIndex - 1) + fibonacci(fibIndex - 2);
memoized.put(fibIndex, answer);
return answer;
}
}
}
public class TestAdder {
public void testSumPositiveNumbersOneAndOne() {
Adder adder = new AdderImpl();
assert(adder.add(1, 1) == 2);
}
public void testSumPositiveNumbersOneAndTwo() {
Adder adder = new AdderImpl();
assert(adder.add(1, 2) == 3);
}
public void testSumPositiveNumbersTwoAndTwo() {
Adder adder = new AdderImpl();
assert(adder.add(2, 2) == 4);
}
public void testSumZeroNeutral() {
Adder adder = new AdderImpl();
assert(adder.add(0, 0) == 0);
}
public void testSumNegativeNumbers() {
Adder adder = new AdderImpl();
assert(adder.add(-1, -2) == -3);
}
public void testSumPositiveAndNegative() {
Adder adder = new AdderImpl();
assert(adder.add(-1, 1) == 0);
}
public void testSumLargeNumbers() {
Adder adder = new AdderImpl();
assert(adder.add(1234, 988) == 2222);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.