OSDN Git Service

change example Maney to complex_t
authortsutsumi <>
Sun, 5 Oct 2003 09:55:36 +0000 (09:55 +0000)
committertsutsumi <>
Sun, 5 Oct 2003 09:55:36 +0000 (09:55 +0000)
doc/cookbook.dox

index acc3710..f9a7da6 100644 (file)
@@ -20,22 +20,18 @@ To make a simple test, here is what you do:
      and pass a bool that is true if the test succeeds</li>
 </ol>
 
-For example, to test that the sum of two Moneys with the
-same currency contains a value which is the sum of the
-values of two Moneys, write:
+For example, to test that the sum of two complex number
+which is the sum of the values of two complex numbers,
+write:
 
 @code
-void testSimpleAdd ()
+void test_complex_add ()
 {
-  Money* m120JPY = Money_new (120, "JPY");
-  Money* m140JPY = Money_new (140, "JPY");
-  Money* expected = Money_new (260, "JPY");
-  Money* result = Money_add (m120JPY, m140JPY);
-  CCUNIT_ASSERT (Money_equals (expected, result));
-  Money_delete (m120JPY);
-  Money_delete (m140JPY);
-  Money_delete (expected);
-  Money_delete (result);
+  complex_t c10_1 = { 10.0, 1.0 };
+  complex_t c1_1 = { 1.0, 1.0 };
+  complex_t result;
+  complex_t c11_2 = { 11.0, 2.0 };
+  CCUNIT_ASSERT (complex_equals (&c11_2, complex_add (&result, c10_1, c1_1)));
 }
 @endcode
 
@@ -71,42 +67,41 @@ When you have a common fixture, here is what you do:
 <li>Add test case objects to fixture object</li>
 </ol>
 
-For example, to write several test cases that want to work
-with different combinations of 120 Japanese Yen, 140
-Japanese Yen, and 28 US Dollars, first create a fixture:
+For example, to write several test cases, first create a
+fixture:
 
 @code
-//** TEST CASE: Money test *\/
+//** TEST CASE: complex number test *\/
 
-#include "Money.h"
+#include "complex.h"
 
-static Money* s120JPY;
-static Money* s140JPY;
-static Money* s23USD;
+static complex_t* s10_1;
+static complex_t* s1_1;
+static complex_t* s11_2;
 
-void setUp_moneyTest ()
+void setUp_ComplexTest ()
 {
-  s120JPY = Money_new (120, "JPY");
-  s140JPY = Money_new (140, "JPY");
-  s23USD = Money_new (23, "USD");
+  s10_1 = complex_new (10, 1);
+  s1_1 = complex_new (1, 1);
+  s11_2 = complex_new (11, 2);
 }
  
-void tearDown_moneyTest ()
+void tearDown_ComplexTest ()
 {
-  Money_delete (s120JPY);
-  Money_delete (s140JPY);
-  Money_delete (s23USD);
+  complex_delete (s10_1);
+  complex_delete (s1_1);
+  complex_delete (s11_2);
 }
 
 ...
 
   CCUnitTestFixture* fixture;
-  fixture = ccunit_newTestFixture ("MoneyTest", 
-                                   CCUNIT_NEWTESTFUNC(setUp_MoneyTest),
-                                   CCUNIT_NEWTESTFUNC(tearDown_MoneyTest));
+  fixture = ccunit_newTestFixture ("ComplexTest",
+                                   CCUNIT_NEWTESTFUNC(setUp_ComplexTest),
+                                   CCUNIT_NEWTESTFUNC(tearDown_ComplexTest));
 @endcode
 
-Once you have the Fixture in place, you can write as many
+Once you have the Fixture in place, you can write as complex
 Test Cases as you'd like.
 
 @section test_case Test Case
@@ -114,32 +109,26 @@ Test Cases as you'd like.
 How do you write and invoke an individual test case when you
 have a Fixture?
 
-For example, to test the addition of a Money and a MoneyBag,
+For example, to test the equality of two complex number,
 write:
 
 @code
-void testMoneyMoneyBag ()
+void test_complex_equals ()
 {
-  Money* bag[] = { s260JPY, s28USD };
-  MoneyBag expected = MoneyBag_new (2, bag);
-  Money* subtotal = Money_add (s120JPY, s28USD);
-  Money* total = Money_add (subtotal, s140JPY);
-  CCUNIT_ASSERT (MoneyBag_equals (expected, total));
-  MoneyBag_delete (expected);
-  Money_delete (subtotal);
-  Money_delete (total);
+  CCUNIT_ASSERT_TEST_OBJ (s10_1, complex_equals, s10_1, complex_to_string);
+  CCUNIT_ASSERT_TEST_OBJ (s10_1, !complex_equals, s1_1, complex_to_string);
 }
 
 ...
 
-  ccunit_addNewTestCase (fixture,
-                         "testSimpleAdd",
-                         "simple add test",
-                         testSympleAdd);
   ccunit_addNewTestCase (fixture, 
-                         "testMoneyMoneyBag",
-                         "money bag test",
-                         testMoneyMoneyBag);
+                         "test_complex_equals",
+                         "complex equals test",
+                         test_complex_equals);
+  ccunit_addNewTestCase (fixture,
+                         "test_complex_add",
+                         "complex add test",
+                         test_complex_add);
 @endcode  
 
 One may create and run objects for each test case like this:
@@ -172,14 +161,16 @@ To create a suite of two or more tests, you do the following:
 CCUnitTestSuite* suite;
 CCUnitTestFixture* fixture;
 CCUnitTestResult* result;
-suite = ccunit_newTestSuite ("Money test suite");
-fixture = ccunit_newTestFixture ("Money Tests", 
-                                 CCUNIT_NEWTESTFUNC(setUp_moneyTest),
-                                 CCUNIT_NEWTESTFUNC(tearDown_moneyTest));
-ccunit_addNewTestCase (fixture,
-                       "testSimpleAdd", "simple add test", testSympleAdd);
-ccunit_addNewTestCase (fixture, 
-                       "testMoneyMoneyBag", "money bag test", testMoneyMoneyBag);
+suite = ccunit_newTestSuite ("Complex test suite");
+fixture = ccunit_newTestFixture ("Complex Tests", 
+                                 CCUNIT_NEWTESTFUNC(setUp_complex_test),
+                                 CCUNIT_NEWTESTFUNC(tearDown_complex_test));
+ccunit_addNewTestCase (fixture, "test_complex_equals", "complex equals test",
+                       test_complex_equals);
+ccunit_addNewTestCase (fixture, "test_complex_add", "complex add test",
+                       test_complex_add);
+ccunit_addNewTestCase (fixture, "test_complex_sub", "complex sub test",
+                       test_complex_sub);
 ccunit_addTestFixture (suite, fixtuer);
 result = ccunit_runTestSuite (suite, NULL);
 @endcode         
@@ -197,8 +188,8 @@ contains both:
 CCUnitTestSuite* suite;
 CCUnitTestResult* result;
 suite = ccunit_newTestSuite ("suite");
-ccunit_addTestSuite (suite, moneyTestSuite);
-ccunit_addTestSuite (suite, complexNumberTestSuite);
+ccunit_addTestSuite (suite, complex_add_sub_suite ());
+ccunit_addTestSuite (suite, complex_mul_div_suite ());
 result = ccunit_runTestSuite(suite, NULL);
 @endcode
 
@@ -213,13 +204,13 @@ its results.  You make your suite accessible to a @link
 CreatingTestSuite ccunit_makeSuite @endlink tool that generate a
 creating test suite code.
 
-For example, to make a MoneyTest suite available to a 
+For example, to make a ComplexTest suite available to a 
 @link CreatingTestSuite ccunit_makeSuite @endlink, 
 excute the following tool to 
-MoneyTest.c:
+ComplexTest.c:
 
 @code
-$ ccunit_makeSuite -f MoneyTest_suite -o makeMoneyTestSuite.c MoneyTest.c
+$ ccunit_makeSuite -f complex_suite -o suiteComplex.c ComplexTest.c
 @endcode
 
 @anchor test_runner_code
@@ -236,16 +227,15 @@ ccunit_runTestRunner (CCUnitTestRunner*, CCUnitTestSuite *) @endlink
 in the <code>main()</code> function:
 
 @code
-extern CCUnitTestSuite* MoneyTest_suite(const char* name);
+extern CCUnitTestSuite* complex_suite(const char* name);
 
 int main( int argc, char **argv)
 {
   CCUnitTestRunner* runner;
   CCUnitTestSuite* suite;
   runner = ccunit_newTestRunner (stdout);
-  suite = MoneyTest_suite ("MathTest Suite");
-  ccunit_runTestRunner (runner, suite);
-  return 0;
+  suite = complex_suite ("complex test suite");
+  return ccunit_runTestRunner (runner, suite);
 }
 @endcode
 
@@ -269,7 +259,7 @@ prone task. A @ref CreatingTestSuite set of functions and
 command have been created to automatically implements the
 <code>suite()</code> method.
 
-The following code is a rewrite of MoneyTest using those command:
+The following code is a rewrite of ComplexTest using those command:
 
 @code
 #include <cppunit/CCUnitAssert.h>
@@ -280,7 +270,7 @@ name to the javaDoc style comment, which consist of a
 C-style comment block starting with two <tt>*</tt>'s:
 
 @code
-//** test case: Money test *\/
+//** test case: complex number test *\/
 @endcode
 
 The suite created by the <code>ccunit_suite()</code>
@@ -290,46 +280,51 @@ case of the fixture with prefix <code>test</code>,
 <code>setUp</code>, <code>tearDown</code>:
 
 @code
-static Money* s120JPY;
-static Money* s140JPY;
-static Money* s260JPY;
-static Money* s23USD;
+#include <complex.h>
 
-void setUp_MathTest ()
+static complex_t* s10_1;
+static complex_t* s1_1;
+static complex_t* s11_2;
+
+void setUp_complex_test ()
+{
+  s10_1 = complex_new (10, 1);
+  s1_1 = complex_new (1, 1);
+  s11_2 = complex_new (11, 2);
+}
+void tearDown_complex_test ()
 {
-  s120JPY = Money_new (120, "JPY");
-  s140JPY = Money_new (140, "JPY");
-  s260JPY = Money_new (260, "JPY");
-  s23USD = Money_new (23, "USD");
+  complex_delete (s10_1);
+  complex_delete (s1_1);
+  complex_delete (s11_2);
 }
 
-void tearDown_Mathtest ()
+//** test equals *\/
+void test_complex_equals ()
 {
-  Money_delete (s120JPY);
-  Money_delete (s140JPY);
-  Money_delete (s260JPY);
-  Money_delete (s23USD);
+  CCUNIT_ASSERT_TEST_OBJ (s10_1, complex_equals, s10_1, complex_to_string);
+  CCUNIT_ASSERT_TEST_OBJ (s10_1, !complex_equals, s1_1, complex_to_string);
 }
 
-//** simple add test *\/
-void test_simpleAdd ()
+//** test add *\/
+void test_complex_add ()
 {
-  Money* result = Money_add (s120JPY, s140JPY);
-  CCUNIT_ASSERT (Money_equals (s260JPY, result));
-  Money_delete (result);
+  complex_t c10_1 = { 10.0, 1.0 };
+  complex_t c1_1 = { 1.0, 1.0 };
+  complex_t result;
+  complex_t c11_2 = { 11.0, 2.0 };
+  CCUNIT_ASSERT (complex_equals (&c11_2, complex_add (&result, &c10_1, &c1_1)));
 }
 
-//** money bug test *\/
-void test_moneyBag()
+//** test sub *\/
+void test_complex_sub ()
 {
-  Money* bag[] = { s260JPY, s28USD };
-  MoneyBag expected = MoneyBag_new (2, bag);
-  Money* subtotal = Money_add (s120JPY, s28USD);
-  Money* total = Money_add (subtotal, s140JPY);
-  CCUNIT_ASSERT (MoneyBag_equals (expected, total));
-  MoneyBag_delete (expected);
-  Money_delete (subtotal);
-  Money_delete (total);
+  complex_t c9_0 = { 9, 0 };
+  complex_t result;
+  CCUNIT_ASSERT_TEST_OBJ (&c9_0, complex_equals,
+                         complex_sub (&result, s10_1, s1_1),
+                         complex_to_string);
 }
 @endcode
 
@@ -343,37 +338,53 @@ To generate creating suite function code, run
 <code>ccunit_makeSuite</code> tool.
 
 @code
-$ ccunit_makeSuite MoneyTest.c
+$ ccunit_makeSuite testComplex.c
 
 #include <ccunit/CCUnitTestSuite.h>
 #include <ccunit/CCUnitTestFixture.h>
 #include <ccunit/CCUnitTestCase.h>
 
-//* test fixture: Money test *\/
-//* setUp_MathTest *\/
-extern void setUp_MathTest ();
-//* tearDown_Mathtest *\/
-extern void tearDown_Mathtest ();
-//* simple add test *\/
-extern void test_simpleAdd ();
-//* money bug test *\/
-extern void test_moneyBag ();
+//* test fixture: complex number test *\/
+//* setUp_complex_test *\/
+extern void setUp_complex_test ();
+//* tearDown_complex_test *\/
+extern void tearDown_complex_test ();
+//* test_complex_equals *\/
+extern void test_complex_equals ();
+//* test_complex_add *\/
+extern void test_complex_add ();
+//* test_complex_sub *\/
+extern void test_complex_sub ();
+
 
 static CCUnitTestFixtureDfn fx_001 = {
   { ccunitTypeFixture },
-  "Money test",
+  "complex number test",
   {
-    "setUp_MathTest", "setUp_MathTest", setUp_MathTest,
+    "setUp_complex_test",
+    "setUp_complex_test",
+    setUp_complex_test
   },
   {
-    "tearDown_Mathtest", "tearDown_Mathtest", tearDown_Mathtest,
+    "tearDown_complex_test",
+    "tearDown_complex_test",
+    tearDown_complex_test
   },
   {
     {
-      "test_simpleAdd", "simple add test", test_simpleAdd,
+      "test_complex_equals",
+      "test equals",
+      test_complex_equals
+    },
+    {
+      "test_complex_add",
+      "test add",
+      test_complex_add
     },
     {
-      "test_moneyBag", "money bug test", test_moneyBag,
+      "test_complex_sub",
+      "test sub",
+      test_complex_sub
     },
     {
       NULL, NULL, NULL
@@ -385,12 +396,12 @@ static CCUnitTestSuiteDfn suite_001 = {
   { ccunitTypeSuite },
   "",
   {
-    &fx_001.test,
+    &suite_002.test,
     NULL,
   },
 };
 
-CCUnitTestSuite* ccunit_suite (const char* name)
+CCUnitTestSuite* complex_suite (const char* name)
 {
   if (!suite_001.name[0])
     suite_001.name = name;