GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-transaction-reversal.c
1 /***************************************************************************
2  * test-transaction-reversal.c
3  *
4  * Modified to run without Guile: Mon Aug 22 11:19:56 2005
5  * Copyright 2005 Neil Williams
7  ****************************************************************************/
8 /*
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301, USA.
23  */
24 
25 #include "config.h"
26 #include <glib.h>
27 #include <string.h>
28 #include "cashobjects.h"
29 #include "Transaction.h"
30 #include "Account.h"
31 #include "TransLog.h"
32 #include "test-engine-stuff.h"
33 #include "test-stuff.h"
34 
35 #define print_gnc_numeric(num) fprintf(stderr, "%s\n", gnc_numeric_to_string(num))
36 
37 static void
38 transaction_set_splits_to_accounts(Transaction *tr, Account *a1, Account *a2)
39 {
40  Split *split;
41 
42  split = xaccTransGetSplit(tr, 0);
43 
44  xaccAccountInsertSplit(a1, split);
45 
46  split = xaccTransGetSplit(tr, 1);
47  xaccAccountInsertSplit(a2, split);
48  return;
49 }
50 
51 static void
52 run_test (void)
53 {
54  Account *acc1, *acc2;
55  Transaction *transaction, *new_trans;
56  gnc_numeric old, new;
57  QofBook *book;
58  char *msg;
59  int i;
60 
61  book = qof_book_new();
62 
63  acc1 = get_random_account(book);
64  acc2 = get_random_account(book);
65 
66  if (!acc1 || !acc2)
67  {
68  failure("accounts not created");
69  return;
70  }
71 
72  /* Find a transaction that isn't voided */
73  do
74  {
75  gboolean voyd;
76 
77  transaction = get_random_transaction (book);
78  voyd = xaccTransGetVoidStatus (transaction);
79  if (voyd)
80  {
81  xaccTransBeginEdit (transaction);
82  xaccTransDestroy (transaction);
83  xaccTransCommitEdit (transaction);
84  transaction = NULL;
85  }
86  }
87  while (!transaction);
88  transaction_set_splits_to_accounts(transaction, acc1, acc2);
89  xaccTransSortSplits(transaction);
90 
91  new_trans = xaccTransReverse(transaction);
92  for (i = 0; i < 2; i++)
93  {
94  old = xaccSplitGetAmount(xaccTransGetSplit(transaction, i));
95  new = xaccSplitGetAmount(xaccTransGetSplit(new_trans, i));
96  if (gnc_numeric_eq(old, gnc_numeric_neg(new)))
97  {
98  msg = g_strdup_printf("Amount of split %d wrong after reversal\n", i);
99  failure(msg);
100  }
101 
102  old = xaccSplitGetValue(xaccTransGetSplit(transaction, i));
103  new = xaccSplitGetValue(xaccTransGetSplit(new_trans, i));
104  if (gnc_numeric_eq(old, gnc_numeric_neg(new)))
105  {
106  msg = g_strdup_printf("Value of split %d wrong after reversal\n", i);
107  failure(msg);
108  }
109 
110  }
111  return;
112 }
113 
114 int
115 main (int argc, char **argv)
116 {
117  qof_init();
118  if (cashobjects_register())
119  {
120  set_success_print (TRUE);
121  run_test ();
122  success("transaction voiding seems OK");
123  print_test_results();
124  }
125  qof_close();
126  return get_rv();
127 }
Split * xaccTransGetSplit(const Transaction *trans, int i)
Definition: Transaction.c:2144
gnc_numeric gnc_numeric_neg(gnc_numeric a)
QofBook * qof_book_new(void)
void xaccTransDestroy(Transaction *trans)
Definition: Transaction.c:1402
Account handling public routines.
void xaccTransCommitEdit(Transaction *trans)
Definition: Transaction.c:1579
void xaccTransBeginEdit(Transaction *trans)
Definition: Transaction.c:1380
gboolean gnc_numeric_eq(gnc_numeric a, gnc_numeric b)
API for the transaction logger.
void qof_close(void)
Safely close down the Query Object Framework.
Transaction * xaccTransReverse(Transaction *orig)
Definition: Transaction.c:2579
gboolean xaccTransGetVoidStatus(const Transaction *trans)
Definition: Transaction.c:2526
Definition: SplitP.h:71
gnc_numeric xaccSplitGetValue(const Split *split)
Definition: Split.c:1993
#define xaccAccountInsertSplit(acc, s)
Definition: Account.h:972
void xaccTransSortSplits(Transaction *trans)
Definition: Transaction.c:564
void qof_init(void)
Initialise the Query Object Framework.
API for Transactions and Splits (journal entries)
gnc_numeric xaccSplitGetAmount(const Split *split)
Definition: Split.c:1987