GnuCash 2.4.99
test-transaction-reversal.c
00001 /***************************************************************************
00002  *            test-transaction-reversal.c
00003  *
00004  *  Modified to run without Guile: Mon Aug 22 11:19:56 2005
00005  *  Copyright  2005  Neil Williams
00006  *  linux@codehelp.co.uk
00007  ****************************************************************************/
00008 /*
00009  *  This program is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with this program; if not, write to the Free Software
00021  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00022  *  02110-1301, USA.
00023  */
00024 
00025 #include "config.h"
00026 #include <glib.h>
00027 #include <string.h>
00028 #include "cashobjects.h"
00029 #include "Transaction.h"
00030 #include "Account.h"
00031 #include "TransLog.h"
00032 #include "test-engine-stuff.h"
00033 #include "test-stuff.h"
00034 
00035 #define print_gnc_numeric(num) fprintf(stderr, "%s\n", gnc_numeric_to_string(num))
00036 
00037 static void
00038 transaction_set_splits_to_accounts(Transaction *tr, Account *a1, Account *a2)
00039 {
00040     Split *split;
00041 
00042     split  = xaccTransGetSplit(tr, 0);
00043 
00044     xaccAccountInsertSplit(a1, split);
00045 
00046     split = xaccTransGetSplit(tr, 1);
00047     xaccAccountInsertSplit(a2, split);
00048     return;
00049 }
00050 
00051 static void
00052 run_test (void)
00053 {
00054     Account *acc1, *acc2;
00055     Transaction *transaction, *new_trans;
00056     gnc_numeric old, new, result;
00057     QofBook *book;
00058     char *msg;
00059     int i;
00060 
00061     book = qof_book_new();
00062 
00063     acc1 = get_random_account(book);
00064     acc2 = get_random_account(book);
00065 
00066     if (!acc1 || !acc2)
00067     {
00068         failure("accounts not created");
00069         return;
00070     }
00071 
00072     /* Find a transaction that isn't voided */
00073     do
00074     {
00075         gboolean voyd;
00076 
00077         transaction = get_random_transaction (book);
00078         voyd = xaccTransGetVoidStatus (transaction);
00079         if (voyd)
00080         {
00081             xaccTransBeginEdit (transaction);
00082             xaccTransDestroy (transaction);
00083             xaccTransCommitEdit (transaction);
00084             transaction = NULL;
00085         }
00086     }
00087     while (!transaction);
00088     transaction_set_splits_to_accounts(transaction, acc1, acc2);
00089     xaccTransSortSplits(transaction);
00090 
00091     new_trans = xaccTransReverse(transaction);
00092     for (i = 0; i < 2; i++)
00093     {
00094         old = xaccSplitGetAmount(xaccTransGetSplit(transaction, i));
00095         new = xaccSplitGetAmount(xaccTransGetSplit(new_trans, i));
00096         result = gnc_numeric_add(old, new, GNC_DENOM_AUTO, GNC_HOW_DENOM_FIXED);
00097         if (gnc_numeric_eq(old, gnc_numeric_neg(new)))
00098         {
00099             msg = g_strdup_printf("Amount of split %d wrong after reversal\n", i);
00100             failure(msg);
00101         }
00102 
00103         old = xaccSplitGetValue(xaccTransGetSplit(transaction, i));
00104         new = xaccSplitGetValue(xaccTransGetSplit(new_trans, i));
00105         result = gnc_numeric_add(old, new, GNC_DENOM_AUTO, GNC_HOW_DENOM_FIXED);
00106         if (gnc_numeric_eq(old, gnc_numeric_neg(new)))
00107         {
00108             msg = g_strdup_printf("Value of split %d wrong after reversal\n", i);
00109             failure(msg);
00110         }
00111 
00112     }
00113     return;
00114 }
00115 
00116 int
00117 main (int argc, char **argv)
00118 {
00119     qof_init();
00120     if (cashobjects_register())
00121     {
00122         set_success_print (TRUE);
00123         run_test ();
00124         success("transaction voiding seems OK");
00125         print_test_results();
00126     }
00127     qof_close();
00128     return get_rv();
00129 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines