Created
February 28, 2010 00:33
-
-
Save etscrivner/317082 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/////////////////////////////////////////////////////////////////////////////// | |
// factorial_test.cpp - A simple factorial example | |
// | |
// Description: | |
// Provides a set of unit-tests using Lemon for a simple factorial function. | |
/////////////////////////////////////////////////////////////////////////////// | |
#include "lemon.h" | |
/////////////////////////////////////////////////////////////////////////////// | |
// Function: factorial | |
// | |
// Computes the factorial of the given number | |
int factorial(int n) { | |
if (n <= 0) return 1; | |
int result = 1; | |
while (n > 1) { | |
result *= n; | |
--n; | |
} | |
return result; | |
} | |
int main(int argc, char* argv[]) { | |
// Setup lemon for 5 tests | |
lemon::test<> lemon(4); | |
// Test 1: Factorial of zero is one | |
lemon.is(factorial(0), 1, "0! = 1"); | |
// Test 2: 3! = 3 * 2 * 1 | |
lemon.is(factorial(3), 3 * 2 * 1, "3! = 3 * 2 * 1"); | |
// Test 3: (-5)! = 1 | |
lemon.is(factorial(-5), 1, "(-5)! = 1"); | |
// Test 4: 5! = 120 | |
lemon.is(factorial(5), 120, "5! = 120"); | |
// End testing | |
lemon.done(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment