|
GnuCash 2.4.99
|
00001 /*************************************************************************** 00002 * test-group-vs-book.c 00003 * 00004 * Tue Sep 27 19:32:31 2005 00005 * Copyright 2005 Gnucash team 00006 ****************************************************************************/ 00007 /* 00008 * This program is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00021 * 02110-1301, USA. 00022 */ 00023 00024 #include "config.h" 00025 #include <glib.h> 00026 #include "qof.h" 00027 #include "cashobjects.h" 00028 #include "Account.h" 00029 #include "TransLog.h" 00030 #include "gnc-engine.h" 00031 #include "test-engine-stuff.h" 00032 #include "test-stuff.h" 00033 00034 static gboolean 00035 account_tree_has_book (Account *parent, QofBook *book) 00036 { 00037 GList *children, *node; 00038 00039 if (!parent) 00040 return (book == NULL); 00041 00042 if (gnc_account_get_book(parent) != book) 00043 return FALSE; 00044 00045 children = gnc_account_get_children(parent); 00046 for (node = children; node; node = node->next) 00047 { 00048 if (!account_tree_has_book (node->data, book)) 00049 return FALSE; 00050 } 00051 g_list_free(children); 00052 00053 return TRUE; 00054 } 00055 00056 00057 static void 00058 run_test (void) 00059 { 00060 Account *root1; 00061 Account *root2; 00062 Account *account1; 00063 Account *account2; 00064 QofBook *book; 00065 00066 book = qof_book_new (); 00067 if (!book) 00068 { 00069 failure("book not created"); 00070 exit(get_rv()); 00071 } 00072 00073 root1 = get_random_account (book); 00074 if (!root1) 00075 { 00076 failure("root1 not created"); 00077 exit(get_rv()); 00078 } 00079 00080 if (!account_tree_has_book (root1, book)) 00081 { 00082 failure("new root has wrong book"); 00083 exit(get_rv()); 00084 } 00085 00086 /* This test is testing routines that are private 00087 * to the engine. these tests are intended to test 00088 * the engine as a whole, not just the public 00089 * interface. the maintenance of the correct 00090 * book pointers is important for correct 00091 * engine operation. */ 00092 gnc_book_set_root_account (book, root1); 00093 if (!account_tree_has_book (root1, book)) 00094 { 00095 failure("gnc_book_set_root_account didn't take"); 00096 exit(get_rv()); 00097 } 00098 00099 root2 = get_random_account (book); 00100 if (!root2) 00101 { 00102 failure("root2 not created"); 00103 exit(get_rv()); 00104 } 00105 00106 gnc_book_set_root_account (book, root2); 00107 00108 #if 0 00109 /* a group cannot have a 'null' book; this test is nonsense. */ 00110 if (!account_tree_has_book (root1, NULL)) 00111 { 00112 failure("gnc_book_set_root_account didn't clear old"); 00113 exit(get_rv()); 00114 } 00115 #endif 00116 00117 if (!account_tree_has_book (root2, book)) 00118 { 00119 failure("gnc_book_set_root_account didn't take"); 00120 exit(get_rv()); 00121 } 00122 00123 account1 = get_random_account (book); 00124 if (!account1) 00125 { 00126 failure("account1 not created"); 00127 exit(get_rv()); 00128 } 00129 00130 gnc_account_append_child (root2, account1); 00131 if (root2 != gnc_account_get_parent (account1)) 00132 { 00133 failure("group insert account didn't work"); 00134 exit(get_rv()); 00135 } 00136 00137 account2 = get_random_account (book); 00138 if (!account2) 00139 { 00140 failure("account2 not created"); 00141 exit(get_rv()); 00142 } 00143 00144 gnc_account_append_child (account1, account2); 00145 if (!account_tree_has_book (gnc_account_get_parent (account2), book)) 00146 { 00147 failure("account2 has wrong book"); 00148 exit(get_rv()); 00149 } 00150 00151 gnc_account_remove_child (root2, account1); 00152 if (gnc_account_get_parent (account1) != NULL) 00153 { 00154 failure("remove group didn't take"); 00155 exit(get_rv()); 00156 } 00157 } 00158 00159 int 00160 main (int argc, char **argv) 00161 { 00162 gint i; 00163 qof_init(); 00164 if (cashobjects_register()) 00165 { 00166 xaccLogDisable (); 00167 for (i = 0; i < 10; i++) 00168 { 00169 run_test (); 00170 } 00171 success ("group/book stuff seems to work"); 00172 print_test_results(); 00173 } 00174 qof_close(); 00175 return get_rv(); 00176 }
1.7.4