Assignment1.md 2023/4/5 18:07
1 / 5
IN3329 Software Testing Assignment1 Guideline
Due Date: 24:00 09th Apr
Marking Scheme for Assignment
1.Code Correctness 2.Coding Style(well-formatted, well commented) 3.Explanation
Please use markdown to write your report, it will save you tons of time.
DO NOT PLAGRISM!
Specification
A well submitted answer should contain the following content:
1. The partition strategies, or a testing plan, better be in the comment before the testing code.
2. Code
3. Explanation of the testing result(program result).
Sample Answer would be like:
Code with testing strategies(Typescript form):
describe("multiplication", function() {
/*
* Testing strategy
*
* cover the cartesian product of these partitions:
* partition on a: positive, negative, 0
* partition on b: positive, negative, 0
* partition on a: 1, !=1
* partition on b: 1, !=1
* partition on a: small (fits in a TypeScript number), or large (doesn't fit)
* partition on b: small, large
*
* cover the subdomains of these partitions:
* partition on signs of a and b:
* both positive
* both negative
* different signs
* one or both are 0
*/
it("covers a = 1, b != 1, a and b have same sign", function() {
assert.strictEqual(BigInt(1) * BigInt(33), BigInt(33));
});
it("covers a is positive, b is negative, "
+ "a fits in a number, b fits in a number, "
+ "a and b have different signs", function() {
assert.strictEqual(BigInt(73) * BigInt(-2), BigInt(-146));
});
Assignment1.md 2023/4/5 18:07
2 / 5
...
});
Explanation:
The strategy covers almost all situations only in 6 test cases, all the testing cases have been passed.(if all
passed)
there is a fail because...(if one fails)
1. Design testing strategies and write unit tests for the following 2 methods:(5 marks)
public static String operationString(String a){
String b = a;
b = a+"c";
// System.out.println(a+b);
return a+b;
}
public static StringBuilder operationStringBuilder(StringBuilder a){
StringBuilder b = a;
b = a.append("c");
// System.out.println(a.append(b));
return a.append(b);
}
Code with testing strategies:
Explanation:
2. Design test strategies for the following code:(5 marks)
public static int sumAbsolute(List list) {
// let's reuse sum(), because DRY, so first we take absolute values
for (int i = 0; i < list.size(); ++i) {
list.set(i, Math.abs(list.get(i)));
}
return sum(list);
}
/**
* @return the sum of the numbers in the list
*/
public static int sum(List list) {
int sum = 0;
for (int x : list) {
sum += x;
}
return sum;
}
Assignment1.md 2023/4/5 18:07
3 / 5
Code with testing strategies:
Explanation:
3. Please write an algorithm for Hailstone Sequence:(10 marks)
Set an initial n as 3, then print the Hailstone Sequence
Receive an integer as the input
Print the whole hailstone array.
the expected function should be like this:
output: 3,10,5,16,8,4,2,1
Code with comment:
4.(I) Please Encapsulate the Hailstone calculation process into a method:(10 marks)
The Hailstone Method should function like this
Input: an integer n > 0
Return: A corresponding Hailstone Sequence with any length.
Code with testing strategies:
Explanation:
4.(II) Write a test for the Hailstone Method:(10 marks)
Code with testing strategies:
Explanation:
5. Answer some questions about Exception (20 marks)
5.1 Will the following code throw an exception or error when you use it?(5 marks)
try {
} finally {
Assignment1.md 2023/4/5 18:07
4 / 5
}
Answer:
5.2 If you use the following catch block, what kinds of exception will it catch? What is wrong with
using the following block?(5 marks):
catch (Exception e) {
}
Answer:
5.3 Try to find the bugs inside the following code and explain why it is wron, then make a judgement
on whether it can be compiled(10 marks):
try {
} catch (Exception e) {
} catch (ArithmeticException a) {
}
Answer:
6. Exercise: Modify the following method, make it throw suitable exception like
FileNotFoundException....etc(10 marks)
public static void cat(File file) {
RandomAccessFile input = null;
String line = null;
try {
input = new RandomAccessFile(file, "r");
while ((line = input.readLine()) != null) {
System.out.println(line);
}
return;
} finally {
if (input != null) {
input.close();
}
}
}
7.Please follow the Mock routine to design a Mock-based Test.(10 marks)
Object discription: We want to take an order object and fill it from a warehouse object. The order is very
simple, with only one product and a quantity. The warehouse holds inventories of different products. When
Assignment1.md 2023/4/5 18:07
5 / 5
we ask an order to fill itself from a warehouse there are two possible responses. If there's enough product in
the warehouse to fill the order, the order becomes filled and the warehouse's amount of the product is
reduced by the appropriate amount. If there isn't enough product in the warehouse then the order isn't
filled and nothing happens in the warehouse.
The stub code is like the following:
Note, Coding in Mock-based library(Mockito, JMock, EasyMock) is not required, a well designed
Mock logic is evaluated(that is, you can get 10 marks if your design is great.)
public class OrderStateTester extends TestCase {
private static String TALISKER = "Talisker";
private static String HIGHLAND_PARK = "Highland Park";
private Warehouse warehouse = new WarehouseImpl();
protected void setUp() throws Exception {
warehouse.add(TALISKER, 50);
warehouse.add(HIGHLAND_PARK, 25);
}
public void testOrderIsFilledIfEnoughInWarehouse() {
Order order = new Order(TALISKER, 50);
order.fill(warehouse);
assertTrue(order.isFilled());
assertEquals(0, warehouse.getInventory(TALISKER));
}
public void testOrderDoesNotRemoveIfNotEnough() {
Order order = new Order(TALISKER, 51);
order.fill(warehouse);
assertFalse(order.isFilled());
assertEquals(50, warehouse.getInventory(TALISKER));
}
Explanation:
8.Write a Multiply method which can(20 marks)
calculate integers from -2^Integer.MAX_VALUE(exclusive) to +2^Integer.MAX_VALUE. and then
design a suitable test strategy and implement it in JUnit.**
Code with testing strategies: