From 82c7a287e7a23d3228fc5637957e41f049bed2e9 Mon Sep 17 00:00:00 2001 From: Eric Scrivner Date: Tue, 23 Feb 2010 19:45:02 -0800 Subject: [PATCH] Renamed the factorial test. Minor modifications to lemon. --- ...mon_example_one.cpp => factorial_test.cpp} | 25 ++++++++----------- lemon.h | 12 ++++----- 2 files changed, 17 insertions(+), 20 deletions(-) rename examples/{lemon_example_one.cpp => factorial_test.cpp} (58%) diff --git a/examples/lemon_example_one.cpp b/examples/factorial_test.cpp similarity index 58% rename from examples/lemon_example_one.cpp rename to examples/factorial_test.cpp index f310ba4..a9e7537 100644 --- a/examples/lemon_example_one.cpp +++ b/examples/factorial_test.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // Lemon Example One: A Simple Factorial Function // -// Time-stamp: +// Time-stamp: // // Description: // Provides a set of unit-tests using Lemon for a simple factorial function. @@ -13,7 +13,7 @@ // // Computes the factorial of the given number int factorial(int n) { - if (n < 2) return 1; + if (n <= 0) return 1; int result = 1; while (n > 1) { @@ -26,22 +26,19 @@ int factorial(int n) { int main(int argc, char* argv[]) { // Setup lemon for 5 tests - Lemon lemon(5); + Lemon lemon(4); - // Test 1: The factorial function handles negative numbers as expected - lemon.is(factorial(-5), 1, "(-5)! = 1"); + // Test 1: Factorial of zero is one + lemon.is(factorial(0), 1, "0! = 1"); - // Test 2: 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: Factorial of one is one - lemon.is(factorial(1), 1, "1! = 1"); + // Test 3: (-5)! = 1 + lemon.is(factorial(-5), 1, "(-5)! = 1"); - // Test 4: Factorial of 2 is 2 - lemon.is(factorial(2), 2, "2! = 2"); - - // Test 5: Factorial of 5 is 120 - lemon.is(factorial(5), 120, "5! = 120"); + // Test 4: 5! = 120 + lemon.is(factorial(5), 120, "5! = 120"); // Finish testing and display statistics lemon.end(); diff --git a/lemon.h b/lemon.h index 9414af5..0515f94 100644 --- a/lemon.h +++ b/lemon.h @@ -2,7 +2,7 @@ // Lemon Unit Test Framework // Author: Eric Scrivner // -// Time-stamp: +// Time-stamp: // // Description: // A lightweight, minimal unit-testing framework based on Perl Test::More @@ -29,7 +29,7 @@ class Lemon // This simply lets lemon know how many tests you're planning to run so that // it can properly output the diagnostic information and doesn't have to // count by hand (which can be tricky as one test can have many assertions). - Lemon(unsigned int num_planned_tests) + Lemon (unsigned int num_planned_tests) : num_tests_(0), test_number_(0), num_skipped_(0), @@ -44,7 +44,7 @@ class Lemon // Signifies the end of the testing phase and prints the results. // // Returns true if all unskipped tests passed, false if there were failures. - bool end() { + bool end () { // If any tests were skipped if (num_skipped_ > 0) { // Display information about the skipped tests @@ -72,7 +72,7 @@ class Lemon // message - A string to be written out to the display // // Used to display diagnostic information which is not a unit test. - void diag(const std::string& message) { + void diag (const std::string& message) { std::cout << "# " << message << std::endl; } @@ -85,7 +85,7 @@ class Lemon // // Marks this test as passed if pass is true. The test is marked as // failing otherwise. - bool ok(bool passed, const std::string& test_name) { + bool ok (bool passed, const std::string& test_name) { // Increment the number of tests run num_tests_++; @@ -120,7 +120,7 @@ class Lemon // // Marks this test as passed if the boolean parameter is false. The test is // marked as failing otherwise. - bool not_ok(bool failed, const std::string& test_name) { + bool not_ok (bool failed, const std::string& test_name) { return ok(!failed, test_name); }