Solved

Interview: How would you test this function?

Posted on 2006-07-11
3
236 Views
Last Modified: 2010-04-01
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
Comment
Question by:gromul
3 Comments
 
LVL 4

Assisted Solution

by:e_tadeu
e_tadeu earned 100 total points
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

by:dbkruger
dbkruger earned 100 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

by:
rstaveley earned 300 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now