GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-commodities.c
1 /***************************************************************************
2  * test-commodities.c
3  *
4  * Mon Aug 22 09:08:32 2005
5  * Original authors: Derek Atkins, Linas Vepstas.
6  * Copyright 2005 Neil Williams
8  ****************************************************************************/
9 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  * 02110-1301, USA.
24  */
25 
26 #include "config.h"
27 #include <glib.h>
28 
29 #include "gnc-commodity.h"
30 #include "qof.h"
31 #include "test-engine-stuff.h"
32 #include "test-stuff.h"
33 
34 static void
35 test_commodity(void)
36 {
37  gnc_commodity *com;
38 
39  {
40  QofBook *book;
41 
42  book = qof_book_new ();
43  com = gnc_commodity_new(book, NULL, NULL, NULL, NULL, 0);
44 
46  qof_book_destroy (book);
47 
48  success("commodity new and destroy");
49  }
50 
51  {
52  char *fullname;
53  const char *namespace;
54  char *mnemonic;
55  char *cusip;
56  int fraction;
57  gnc_commodity *com2;
58  QofBook *book;
59 
60  book = qof_book_new ();
61  fullname = get_random_string();
62  namespace = get_random_commodity_namespace();
63  mnemonic = get_random_string();
64  cusip = get_random_string();
65  fraction = get_random_int_in_range(0, 10000);
66 
67  com = gnc_commodity_new(book, fullname, namespace, mnemonic,
68  cusip, fraction);
69 
70  do_test(
71  com != NULL, "commodity with data new and destroy");
72 
73  do_test(
74  g_strcmp0(fullname, gnc_commodity_get_fullname(com)) == 0,
75  "fullnames equal test");
76 
77  do_test(
78  g_strcmp0(namespace, gnc_commodity_get_namespace(com)) == 0,
79  "namespace equal test");
80 
81  do_test(
82  g_strcmp0(mnemonic, gnc_commodity_get_mnemonic(com)) == 0,
83  "mnemonic equal test");
84 
85  do_test(
86  g_strcmp0(cusip, gnc_commodity_get_cusip(com)) == 0,
87  "cusip equal test");
88 
89  do_test(
90  gnc_commodity_get_fraction(com) == fraction,
91  "fraction code equal test");
92 
93  fullname = get_random_string();
94  gnc_commodity_set_fullname(com, fullname);
95  do_test(
96  g_strcmp0(fullname, gnc_commodity_get_fullname(com)) == 0,
97  "reset fullnames equal test");
98 
99  namespace = get_random_commodity_namespace();
100  gnc_commodity_set_namespace(com, namespace);
101  do_test(
102  g_strcmp0(namespace, gnc_commodity_get_namespace(com)) == 0,
103  "reset namespace equal test");
104 
105  mnemonic = get_random_string();
106  gnc_commodity_set_mnemonic(com, mnemonic);
107  do_test(
108  g_strcmp0(mnemonic, gnc_commodity_get_mnemonic(com)) == 0,
109  "reset mnemonic equal test");
110 
111  cusip = get_random_string();
112  gnc_commodity_set_cusip(com, cusip);
113  do_test(
114  g_strcmp0(cusip, gnc_commodity_get_cusip(com)) == 0,
115  "reset cusip equal test");
116 
117  fraction = get_random_int_in_range(0, 10000);
118  gnc_commodity_set_fraction(com, fraction);
119  do_test(
120  gnc_commodity_get_fraction(com) == fraction,
121  "reset fraction code equal test");
122 
123  com2 = gnc_commodity_new(book, fullname, namespace, mnemonic,
124  cusip, fraction);
125  do_test(
126  gnc_commodity_equiv(com, com2), "commodity equiv");
127 
128  qof_book_destroy (book);
129  }
130 
131  {
132  int i, j, num_total = 0;
133  gnc_commodity_table *tbl;
134  gnc_commodity *coms[20];
135  QofBook *book;
136 
137  book = qof_book_new ();
138  tbl = gnc_commodity_table_new ();
139 
140  do_test(gnc_commodity_table_get_size(tbl) == 0,
141  "test size for 0 table");
142 
143  for (i = 0; i < 20; i++)
144  {
145  coms[i] = get_random_commodity(book);
146 
147  if (!gnc_commodity_table_lookup(
148  tbl, gnc_commodity_get_namespace(coms[i]),
149  gnc_commodity_get_mnemonic(coms[i])))
150  num_total++;
151  do_test(
152  gnc_commodity_table_insert(tbl, coms[i]) != NULL,
153  "insert test");
154 
155  do_test_args(
156  (int)gnc_commodity_table_get_size(tbl) == num_total,
157  "test next size table", __FILE__, __LINE__,
158  "should be %d and is %d", num_total,
160 
161  for (j = 0; j <= i; j++)
162  {
163  gnc_commodity *testcom;
164 
165  do_test(
166  (testcom = gnc_commodity_table_lookup(
167  tbl, gnc_commodity_get_namespace(coms[j]),
168  gnc_commodity_get_mnemonic(coms[j]))) != NULL,
169  "lookup commodity");
170  do_test(
171  gnc_commodity_equiv(testcom, coms[j]),
172  "lookup commodity and test equiv");
173  }
174 
175  do_test(
177  tbl, gnc_commodity_get_namespace(coms[i])),
178  "test have namespace");
179  }
180  }
181 
182 }
183 
184 int
185 main (int argc, char **argv)
186 {
187  qof_init();
188 
191 
192  test_commodity();
193 
194  print_test_results();
195 
196  qof_close();
197  return get_rv();
198 }
gnc_commodity * gnc_commodity_table_insert(gnc_commodity_table *table, gnc_commodity *comm)
const char * gnc_commodity_get_cusip(const gnc_commodity *cm)
int gnc_commodity_get_fraction(const gnc_commodity *cm)
gboolean qof_book_register(void)
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
void gnc_commodity_set_fraction(gnc_commodity *cm, int fraction)
QofBook * qof_book_new(void)
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
gboolean gnc_commodity_table_register(void)
void gnc_commodity_set_cusip(gnc_commodity *cm, const char *cusip)
gnc_commodity * gnc_commodity_new(QofBook *book, const char *fullname, const char *name_space, const char *mnemonic, const char *cusip, int fraction)
int gnc_commodity_table_has_namespace(const gnc_commodity_table *table, const char *name_space)
const char * gnc_commodity_get_fullname(const gnc_commodity *cm)
void qof_close(void)
Safely close down the Query Object Framework.
void gnc_commodity_set_fullname(gnc_commodity *cm, const char *fullname)
void gnc_commodity_set_mnemonic(gnc_commodity *cm, const char *mnemonic)
gnc_commodity_table * gnc_commodity_table_new(void)
void gnc_commodity_set_namespace(gnc_commodity *cm, const char *name_space)
guint gnc_commodity_table_get_size(const gnc_commodity_table *tbl)
void qof_init(void)
Initialise the Query Object Framework.
Commodity handling public routines.
void gnc_commodity_destroy(gnc_commodity *cm)
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)
void qof_book_destroy(QofBook *book)