[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# Interview: How would you test this function?

Posted on 2006-07-11
Medium Priority
271 Views
During a programming interview, once you finish writing a function, they often ask you to test it. What are some must-know test cases that apply to most functions one can encounter in an interview?

Thanks!
0
Question by:gromul
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 4

Assisted Solution

ID: 17085405
The test case will depend on what your function is supposed to do. Normally, the test consists of ensuring some post-conditions. A very simple example: if you must test a function that negates a number, you can test like this:

assert(Negate(-3) == 3);
assert(Negate(10) == -10);

You can also test invariants too.

See:
http://en.wikipedia.org/wiki/Test_case
http://en.wikipedia.org/wiki/Unit_test
0

LVL 11

Assisted Solution

dbkruger earned 400 total points
ID: 17085556
For integers you should consider things related to problems with word wrapping. For example, on a 16 bit unsigned short, -1 == 65535, so there might be surprises

common values 0, 1, -1, -32768, 32767

for floating point, issues involve division by zero, sqrt of a negative, overflow, underflow. These get more complicated as the intermediate calculations get more involved.
0

LVL 17

Accepted Solution

rstaveley earned 1200 total points
ID: 17089902
Good buzz words for an interview are: purpose, promise and requirement. I've always known these as the basis for "programming by contract", but there may be some other name for this.

Your code of course has a purpose. Based on the stated purpose, it makes promises, which it must fulfill at the very least (e.g. it will make a pizza within 10 minutes). It can exceed these promises (e.g. make the pizza in 8 minutes), but to achieve its promises, it needs to state its requirements (e.g. pre-heated oven, cheese) and not expect anything better than its documented requirements (e.g. if it only says it requires cheese, it isn't OK for it only to work with Mozzarella cheese).

Unit tests demonstrate that a function fulfills its promises with a range inputs that fulfill its requirements. Thorough testing explores the entire range of requirements (e.g. testing the good pizza code with mouse trap cheese and with a pre-heated over that's absurdly hot). Because it is hard to test thoroughly all possibilities for code that makes few requirements, it is worth narrowing its documented requirements for the sake of testing. It can seem like its "the tail wagging the dog", but code is much more valuable when it can be tested.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month13 days, 9 hours left to enroll