Skip to content

Instantly share code, notes, and snippets.

@etscrivner
Created February 28, 2010 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etscrivner/317082 to your computer and use it in GitHub Desktop.
Save etscrivner/317082 to your computer and use it in GitHub Desktop.
///////////////////////////////////////////////////////////////////////////////
// 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