Lab 04 - Test Cases
Thanks for my Lab Tutor Zhang Puyu
Last updated
Thanks for my Lab Tutor Zhang Puyu
Last updated
Slides:
It is impossible to enumerate all test cases.
It is possible to divide the test cases into a few groups.
We can choose to test for all possible classes of inputs/outputs.
Test for the boundary cases according to the constraints given.
Should cover all conditional (i.e. for all conditional branch, there exists at least one test case which can enter that branch)
Should contain at least one case for 0 iteration and at least one case for k iterations where .
Should contain boundary conditions.
Design the test cases for the program below:
We don't enter the conditonal first. So, that means there is no 9 in n
We enter the conditional, which means there is at leasat a 9 in n
9 in the last digit.
9 in the middle.
Have multiple 9.
Testing Tips:
Test each function seperately (print out the value to be returned and compare it with your expectation!
Decompose your program into shorter and simpler functions: simpler code is easier to test!
Optimization methods:
The above implies that we can count factors in pairs.
When to use for
or while
?
`for` is used under the circumstances where we iterate through consecutive numbers. `while` loops are used under the circumstances that we don't iterate through consecutive numbers, which means that we can define a more general condition to "loop through".
Using an array, we can speed up the algorithm. The algorithm we are using is called Sieve of Eratosthenes.
Take 2 (which happens to be the smallest prime), and cross out all multiples of 2 in the list.
Take the un-crossed integer (which is 3 in this case), and cross out all muptiples of 3.
We won't enter the loop while n != 0
, that means and now our output is 0, which is correct.
We enter the loop, which means .
since anything bigger than for sure cannot divide !
If divides , then surely divides as well. See Proof of 2.
Note that if divides and , then
So, the supremum (the least upper bound) is .
We should then deal with the special case where is a perfect square (a perfet square has an odd numbero factors while any other integer has an even number of factors)
If divides , then we can write , where . Manipulate this equation, we know . So, now our problem becomes does divide right? This answer is obvious yes right, since we already know that . Proof!
How to find all primes less than or equal to .
We iterate every integer from to (even if we exclude all even numbers, there are still steps. And checking if each integer is prime takes another steps in the worst case, so in total there are about steps -- Slow!
Consider all integers from to
Iterate through the list while repeating the above process. We claim that: each time we come across an un-crossed integer , it will be a prime.
Stop when we encounter the first integer that is bigger than . We claim that from this integer onwards, all un-crossed integers are prime!
Number of operations in the worst case: upper-bounded by (fewer than ).