Solved

Interview: How would you test this function?

Posted on 2006-07-11
3
241 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

776 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