?
Solved

Interview: How would you test this function?

Posted on 2006-07-11
3
Medium Priority
?
265 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
[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
  • Learn & ask questions
3 Comments
 
LVL 4

Assisted Solution

by:e_tadeu
e_tadeu earned 400 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 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

by:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

752 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