GnuCash 2.4.99
test-commodities.c
00001 /***************************************************************************
00002  *            test-commodities.c
00003  *
00004  *  Mon Aug 22 09:08:32 2005
00005  *  Original authors: Derek Atkins, Linas Vepstas.
00006  *  Copyright  2005  Neil Williams
00007  *  linux@codehelp.co.uk
00008  ****************************************************************************/
00009 /*
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00023  *  02110-1301, USA.
00024  */
00025 
00026 #include "config.h"
00027 #include <glib.h>
00028 
00029 #include "gnc-commodity.h"
00030 #include "qof.h"
00031 #include "test-engine-stuff.h"
00032 #include "test-stuff.h"
00033 
00034 static void
00035 test_commodity(void)
00036 {
00037     gnc_commodity *com;
00038 
00039     {
00040         QofBook *book;
00041 
00042         book = qof_book_new ();
00043         com = gnc_commodity_new(book, NULL, NULL, NULL, NULL, 0);
00044 
00045         gnc_commodity_destroy(com);
00046         qof_book_destroy (book);
00047 
00048         success("commodity new and destroy");
00049     }
00050 
00051     {
00052         char *fullname;
00053         const char *namespace;
00054         char *mnemonic;
00055         char *cusip;
00056         int fraction;
00057         gnc_commodity *com2;
00058         QofBook *book;
00059 
00060         book = qof_book_new ();
00061         fullname = get_random_string();
00062         namespace = get_random_commodity_namespace();
00063         mnemonic = get_random_string();
00064         cusip = get_random_string();
00065         fraction = get_random_int_in_range(0, 10000);
00066 
00067         com = gnc_commodity_new(book, fullname, namespace, mnemonic,
00068                                 cusip, fraction);
00069 
00070         do_test(
00071             com != NULL, "commodity with data new and destroy");
00072 
00073         do_test(
00074             safe_strcmp(fullname, gnc_commodity_get_fullname(com)) == 0,
00075             "fullnames equal test");
00076 
00077         do_test(
00078             safe_strcmp(namespace, gnc_commodity_get_namespace(com)) == 0,
00079             "namespace equal test");
00080 
00081         do_test(
00082             safe_strcmp(mnemonic, gnc_commodity_get_mnemonic(com)) == 0,
00083             "mnemonic equal test");
00084 
00085         do_test(
00086             safe_strcmp(cusip, gnc_commodity_get_cusip(com)) == 0,
00087             "cusip equal test");
00088 
00089         do_test(
00090             gnc_commodity_get_fraction(com) == fraction,
00091             "fraction code equal test");
00092 
00093         fullname = get_random_string();
00094         gnc_commodity_set_fullname(com, fullname);
00095         do_test(
00096             safe_strcmp(fullname, gnc_commodity_get_fullname(com)) == 0,
00097             "reset fullnames equal test");
00098 
00099         namespace = get_random_commodity_namespace();
00100         gnc_commodity_set_namespace(com, namespace);
00101         do_test(
00102             safe_strcmp(namespace, gnc_commodity_get_namespace(com)) == 0,
00103             "reset namespace equal test");
00104 
00105         mnemonic = get_random_string();
00106         gnc_commodity_set_mnemonic(com, mnemonic);
00107         do_test(
00108             safe_strcmp(mnemonic, gnc_commodity_get_mnemonic(com)) == 0,
00109             "reset mnemonic equal test");
00110 
00111         cusip = get_random_string();
00112         gnc_commodity_set_cusip(com, cusip);
00113         do_test(
00114             safe_strcmp(cusip, gnc_commodity_get_cusip(com)) == 0,
00115             "reset cusip equal test");
00116 
00117         fraction = get_random_int_in_range(0, 10000);
00118         gnc_commodity_set_fraction(com, fraction);
00119         do_test(
00120             gnc_commodity_get_fraction(com) == fraction,
00121             "reset fraction code equal test");
00122 
00123         com2 = gnc_commodity_new(book, fullname, namespace, mnemonic,
00124                                  cusip, fraction);
00125         do_test(
00126             gnc_commodity_equiv(com, com2), "commodity equiv");
00127 
00128         qof_book_destroy (book);
00129     }
00130 
00131     {
00132         int i, j, num_total = 0;
00133         gnc_commodity_table *tbl;
00134         gnc_commodity *coms[20];
00135         QofBook *book;
00136 
00137         book = qof_book_new ();
00138         tbl = gnc_commodity_table_new ();
00139 
00140         do_test(gnc_commodity_table_get_size(tbl) == 0,
00141                 "test size for 0 table");
00142 
00143         for (i = 0; i < 20; i++)
00144         {
00145             coms[i] = get_random_commodity(book);
00146 
00147             if (!gnc_commodity_table_lookup(
00148                         tbl, gnc_commodity_get_namespace(coms[i]),
00149                         gnc_commodity_get_mnemonic(coms[i])))
00150                 num_total++;
00151             do_test(
00152                 gnc_commodity_table_insert(tbl, coms[i]) != NULL,
00153                 "insert test");
00154 
00155             do_test_args(
00156                 (int)gnc_commodity_table_get_size(tbl) == num_total,
00157                 "test next size table", __FILE__, __LINE__,
00158                 "should be %d and is %d", num_total,
00159                 gnc_commodity_table_get_size(tbl));
00160 
00161             for (j = 0; j <= i; j++)
00162             {
00163                 gnc_commodity *testcom;
00164 
00165                 do_test(
00166                     (testcom = gnc_commodity_table_lookup(
00167                                    tbl, gnc_commodity_get_namespace(coms[j]),
00168                                    gnc_commodity_get_mnemonic(coms[j]))) != NULL,
00169                     "lookup commodity");
00170                 do_test(
00171                     gnc_commodity_equiv(testcom, coms[j]),
00172                     "lookup commodity and test equiv");
00173             }
00174 
00175             do_test(
00176                 gnc_commodity_table_has_namespace(
00177                     tbl, gnc_commodity_get_namespace(coms[i])),
00178                 "test have namespace");
00179         }
00180     }
00181 
00182 }
00183 
00184 int
00185 main (int argc, char **argv)
00186 {
00187     qof_init();
00188 
00189     qof_book_register ();
00190     gnc_commodity_table_register();
00191 
00192     test_commodity();
00193 
00194     print_test_results();
00195 
00196     qof_close();
00197     return get_rv();
00198 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines