GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-account-object.c
Go to the documentation of this file.
1 /***************************************************************************
2  * test-account-object.c
3  *
4  * Copyright (C) 2007 David Hampton <[email protected]>
5  ****************************************************************************/
6 /*
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301, USA.
21  */
28 #include "config.h"
29 #include <unistd.h>
30 #include <glib.h>
31 #include "qof.h"
32 #include "Account.h"
33 #include "cashobjects.h"
34 #include "test-stuff.h"
35 #include "test-engine-stuff.h"
36 #include <qofinstance-p.h>
37 
38 static void
39 run_test (void)
40 {
41  QofSession *sess;
42  QofBook *book;
43  Account *acc;
44  gnc_numeric *start, *end, end2, delta, five;
45 
46  sess = get_random_session ();
47  book = qof_session_get_book (sess);
48  do_test ((NULL != book), "create random data");
49  acc = get_random_account(book);
50 
51  /*****/
52 
53  g_object_get(acc, "start-balance", &start, "end-balance", &end, NULL);
54  end2 = xaccAccountGetBalance(acc);
55  delta = gnc_numeric_sub(*end, *start, GNC_DENOM_AUTO, GNC_HOW_DENOM_FIXED);
56 
57  do_test (gnc_numeric_zero_p(*start), "start balance is zero");
58  do_test (gnc_numeric_zero_p(*end), "end balance is zero");
59  do_test (gnc_numeric_zero_p(delta), "delta is zero");
60  do_test (gnc_numeric_zero_p(end2), "end2 balance is zero");
61 
62  /*****/
63 
64  five = gnc_numeric_create(5, 1);
65  qof_instance_increase_editlevel (acc);
66  g_object_set(acc, "start-balance", &five, NULL);
67  qof_instance_decrease_editlevel (acc);
69  g_object_get(acc, "start-balance", &start, "end-balance", &end, NULL);
70  end2 = xaccAccountGetBalance(acc);
71 
73  do_test (gnc_numeric_zero_p(delta), "end balance matches");
75  do_test (gnc_numeric_zero_p(delta), "end2 balance matches");
76 
77  /*****/
78 
79  qof_session_end (sess);
80 
81 }
82 
83 int
84 main (int argc, char **argv)
85 {
86  qof_init();
87  if (!cashobjects_register())
88  exit(1);
89 
90  /* Any tests that cause an error or warning to be printed
91  * automatically fail! */
92  g_log_set_always_fatal( G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING );
93  /* Set up a reproducible test-case */
94  srand(0);
95 
96  /* Run the tests */
97  run_test ();
98 
99  print_test_results();
100 
101  qof_close();
102  return get_rv();
103 }
gboolean gnc_numeric_zero_p(gnc_numeric a)
QofBook * qof_session_get_book(const QofSession *session)
Account handling public routines.
void xaccAccountRecomputeBalance(Account *acc)
Definition: Account.c:2058
gnc_numeric xaccAccountGetBalance(const Account *acc)
Definition: Account.c:3229
gnc_numeric gnc_numeric_sub(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
void qof_close(void)
Safely close down the Query Object Framework.
void qof_session_end(QofSession *session)
void qof_init(void)
Initialise the Query Object Framework.
#define GNC_DENOM_AUTO
Definition: gnc-numeric.h:246