Let's say we're asked to test a function which calculates the factorial (n!) of a number n, returning 0 for negative inputs and -1 when the result is out of range:
int factorial(int n)
What about environment? Is there anything about the containing
system (operating system, computer, language, etc.) which might affect
the behaviour of this function? I can think of one possibility, which
is that int
has a limited range: on a 32-bit computer
system, C's int
has a range from -231 to
231-1 (-2,147,483,648 to 2,147,483,647). On 64-bit computers
the range is from -263 to 263-1
(-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807). Those are
big numbers, but factorials are big as well: 13! is too big for a 32-bit
int
, and 21! too big for a 64-bit int
. Java
defines its int
to be 64-bit on all systems (note that the
“specification” above is very brief, and does not
specify what implementation language we're dealing with, or what
“out of range” means). So let's do two things:
int
as an
environment factor, and
int
might be one of several different sizes depending on the platform.
int
)
int
)
And for our environment, we know that chips nowadays usually have an
int
of size 32, 64 or (mainly in embedded devices) 16 bits.
There's no obvious interesting characteristic other than this.
int
:Where max is the
largest factorial we can compute in the relevant environment: 7, 12 or
20 according to the size of int
.
Note that when we get to looking at values in a continuum, boundaries between partitions become very interesting. We want to be sure that behaviour around boundaries is well tested. So we might expand our value classes for n a little:
Here we're now testing exactly on both sides of our boundaries, as well as further away — the downside of this extra care is that we're now looking at seven value classes for n instead of the previous three.
Test case | Size of int |
Value class for n | Expected result |
---|---|---|---|
1 | any | Value class n < -1 | 0 (undefined) |
2 | any | n = -1 | 0 (undefined) |
3 | any | n = 0 | 1 |
4 | any | 0 < n < max (for all environments) | varies (correct answer) |
5 | any | n > max + 1 (for all environments) | -1 (out of range) |
That leaves us trying to cover n = max and n = max + 1. max varies according to the
size of int
, so we do need 2 × 3 = 6 more cases for
these:
Test case | Size of int |
Value class for n | Expected result |
---|---|---|---|
6 | 16-bit | n = max | (correct answer) |
7 | 16-bit | n = max + 1 | -1 (out of range) |
8 | 32-bit | n = max | (correct answer) |
9 | 32-bit | n = max + 1 | -1 (out of range) |
10 | 64-bit | n = max | (correct answer) |
11 | 64-bit | n = max + 1 | -1 (out of range) |
Test case | Size of int |
n | Expected result | Notes |
---|---|---|---|---|
1 | 64-bit | -100 | 0 | Value class n < -1 |
2 | 32-bit | -1 | 0 | Value class n = -1 |
3 | 32-bit | 0 | 1 | Value class n = 0 |
4 | 16-bit | 5 | 120 | Value class 0 < n < max (for all environments) |
5 | 64-bit | 99 | -1 | Value class n > max + 1 (for all environments) |
6 | 16-bit | 7 | 5,040 | Value class n = max, max = 7 |
7 | 16-bit | 8 | -1 | Value class n = max + 1, max = 7 |
8 | 32-bit | 12 | 479,001,600 | Value class n = max, max = 12 |
9 | 32-bit | 13 | -1 | Value class n = max + 1, max = 12 |
10 | 64-bit | 20 | 2,432,902,008,176,640,000 | Value class n = max, max = 20 |
11 | 64-bit | 21 | -1 | Value class n = max + 1, max = 20 |
Version 1.1, 2010/01/29 17:37:05
Informatics Forum, 10 Crichton Street, Edinburgh, EH8 9AB, Scotland, UK
Tel: +44 131 651 5661, Fax: +44 131 651 1426, E-mail: school-office@inf.ed.ac.uk Please contact our webadmin with any comments or corrections. Logging and Cookies Unless explicitly stated otherwise, all material is copyright © The University of Edinburgh |