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
  • Learn & ask questions
Solved

Interview: How would you test this function?

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

861 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