GnuCash 2.4.99
test-account-object.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *            test-account-object.c
00003  *
00004  *  Copyright (C) 2007 David Hampton <hampton@employees.org>
00005  ****************************************************************************/
00006 /*
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00020  *  02110-1301, USA.
00021  */
00028 #include "config.h"
00029 #include <unistd.h>
00030 #include <glib.h>
00031 #include "qof.h"
00032 #include "Account.h"
00033 #include "cashobjects.h"
00034 #include "test-stuff.h"
00035 #include "test-engine-stuff.h"
00036 
00037 static void
00038 run_test (void)
00039 {
00040     QofSession *sess;
00041     QofBook *book;
00042     Account *acc;
00043     gnc_numeric *start, *end, end2, delta, zero, five;
00044 
00045     sess = get_random_session ();
00046     book = qof_session_get_book (sess);
00047     do_test ((NULL != book), "create random data");
00048     acc = get_random_account(book);
00049 
00050     /*****/
00051 
00052     g_object_get(acc, "start-balance", &start, "end-balance", &end, NULL);
00053     end2 = xaccAccountGetBalance(acc);
00054     delta = gnc_numeric_sub(*end, *start, GNC_DENOM_AUTO, GNC_HOW_DENOM_FIXED);
00055 
00056     do_test (gnc_numeric_zero_p(*start), "start balance is zero");
00057     do_test (gnc_numeric_zero_p(*end), "end balance is zero");
00058     do_test (gnc_numeric_zero_p(delta), "delta is zero");
00059     do_test (gnc_numeric_zero_p(end2), "end2 balance is zero");
00060 
00061     /*****/
00062 
00063     five = gnc_numeric_create(5, 1);
00064     g_object_set(acc, "start-balance", &five, NULL);
00065     xaccAccountRecomputeBalance(acc);
00066     g_object_get(acc, "start-balance", &start, "end-balance", &end, NULL);
00067     end2 = xaccAccountGetBalance(acc);
00068 
00069     delta = gnc_numeric_sub(*end, five, GNC_DENOM_AUTO, GNC_HOW_DENOM_FIXED);
00070     do_test (gnc_numeric_zero_p(delta), "end balance matches");
00071     delta = gnc_numeric_sub(end2, five, GNC_DENOM_AUTO, GNC_HOW_DENOM_FIXED);
00072     do_test (gnc_numeric_zero_p(delta), "end2 balance matches");
00073 
00074     /*****/
00075 
00076     qof_session_end (sess);
00077 
00078 }
00079 
00080 int
00081 main (int argc, char **argv)
00082 {
00083     gint i;
00084 
00085     qof_init();
00086     if (!cashobjects_register())
00087         exit(1);
00088 
00089     /* Any tests that cause an error or warning to be printed
00090      * automatically fail! */
00091     g_log_set_always_fatal( G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING );
00092     /* Set up a reproducible test-case */
00093     srand(0);
00094 
00095     /* Run the tests */
00096     run_test ();
00097 
00098     print_test_results();
00099 
00100     qof_close();
00101     return get_rv();
00102 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines