|
GnuCash 2.4.99
|
00001 /*************************************************************************** 00002 * test-dom-converters1.c 00003 * 00004 * Fri Oct 7 20:51:06 2005 00005 * Copyright 2005 Neil Williams 00006 * linux@codehelp.co.uk 00007 ****************************************************************************/ 00008 /* 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00022 * 02110-1301, USA. 00023 */ 00024 00025 #include "config.h" 00026 00027 #include <stdlib.h> 00028 #include <string.h> 00029 00030 #include <glib.h> 00031 00032 #include "test-stuff.h" 00033 #include "test-engine-stuff.h" 00034 #include "test-file-stuff.h" 00035 #include "cashobjects.h" 00036 #include "gnc-xml-helper.h" 00037 #include "gnc-engine.h" 00038 #include "sixtp.h" 00039 #include "sixtp-parsers.h" 00040 #include "sixtp-utils.h" 00041 #include "sixtp-dom-parsers.h" 00042 #include "sixtp-dom-generators.h" 00043 00044 #include "gnc-commodity.h" 00045 00046 #define GNC_V2_STRING "gnc-v2" 00047 const gchar *gnc_v2_xml_version_string = GNC_V2_STRING; 00048 00049 static void 00050 test_dom_tree_to_commodity_ref(void) 00051 { 00052 int i; 00053 for (i = 0; i < 20; i++) 00054 { 00055 gnc_commodity *test_com1; 00056 gchar *test_str1; 00057 gchar *test_str2; 00058 gnc_commodity *test_com2; 00059 xmlNodePtr test_node; 00060 QofBook *book; 00061 00062 book = qof_book_new (); 00063 00064 test_str1 = get_random_string(); 00065 test_str2 = get_random_string(); 00066 00067 test_com1 = gnc_commodity_new(book, NULL, test_str1, test_str2, NULL, 0); 00068 test_node = commodity_ref_to_dom_tree("test-com", test_com1); 00069 00070 test_com2 = dom_tree_to_commodity_ref_no_engine(test_node, book); 00071 00072 do_test(gnc_commodity_equiv(test_com1, test_com2), 00073 "dom_tree_to_commodity_ref_no_engine"); 00074 00075 xmlFreeNode(test_node); 00076 gnc_commodity_destroy(test_com1); 00077 gnc_commodity_destroy(test_com2); 00078 g_free(test_str1); 00079 g_free(test_str2); 00080 00081 qof_book_destroy (book); 00082 } 00083 } 00084 00085 static void 00086 test_dom_tree_to_text(void) 00087 { 00088 int i; 00089 00090 for (i = 0; i < 20; i++) 00091 { 00092 gchar *test_string1; 00093 gchar *test_string2; 00094 xmlNodePtr test_node; 00095 00096 test_node = xmlNewNode(NULL, BAD_CAST "test-node"); 00097 test_string1 = get_random_string(); 00098 00099 xmlNodeAddContent(test_node, BAD_CAST test_string1); 00100 00101 test_string2 = dom_tree_to_text(test_node); 00102 00103 if (!test_string2) 00104 { 00105 failure_args("dom_tree_to_text", __FILE__, __LINE__, 00106 "null return from dom_tree_to_text"); 00107 xmlElemDump(stdout, NULL, test_node); 00108 } 00109 else if (safe_strcmp(test_string1, test_string2) == 0) 00110 { 00111 success_args("dom_tree_to_text", __FILE__, __LINE__, "with string %s", 00112 test_string1); 00113 } 00114 else 00115 { 00116 failure_args("dom_tree_to_text", __FILE__, __LINE__, 00117 "with string %s", test_string1); 00118 } 00119 00120 xmlFreeNode(test_node); 00121 g_free(test_string1); 00122 if (test_string2) g_free(test_string2); 00123 } 00124 } 00125 00126 00127 static void 00128 test_dom_tree_to_timespec(void) 00129 { 00130 int i; 00131 for (i = 0; i < 20; i++) 00132 { 00133 Timespec *test_spec1; 00134 Timespec test_spec2; 00135 xmlNodePtr test_node; 00136 00137 test_spec1 = get_random_timespec(); 00138 00139 test_node = timespec_to_dom_tree("test-spec", test_spec1); 00140 00141 test_spec2 = dom_tree_to_timespec(test_node); 00142 00143 if (!dom_tree_valid_timespec(&test_spec2, (const xmlChar*)"test-spec")) 00144 { 00145 failure_args("dom_tree_to_timespec", 00146 __FILE__, __LINE__, "NULL return"); 00147 printf("Node looks like:\n"); 00148 xmlElemDump(stdout, NULL, test_node); 00149 printf("\n"); 00150 } 00151 00152 else if (timespec_cmp(test_spec1, &test_spec2) == 0) 00153 { 00154 success("dom_tree_to_timespec"); 00155 } 00156 else 00157 { 00158 failure("dom_tree_to_timespec"); 00159 printf("Node looks like:\n"); 00160 xmlElemDump(stdout, NULL, test_node); 00161 printf("\n"); 00162 printf("Secs are %" G_GUINT64_FORMAT " vs %" G_GUINT64_FORMAT " :: ", 00163 test_spec1->tv_sec, 00164 test_spec2.tv_sec); 00165 printf("NSecs are %ld vs %ld\n", 00166 test_spec1->tv_nsec, 00167 test_spec2.tv_nsec); 00168 } 00169 00170 g_free(test_spec1); 00171 xmlFreeNode(test_node); 00172 } 00173 } 00174 00175 static gchar * 00176 test_gnc_nums_internal(gnc_numeric to_test) 00177 { 00178 gchar *ret = NULL; 00179 gnc_numeric *to_compare = NULL; 00180 xmlNodePtr to_gen = NULL; 00181 00182 to_gen = gnc_numeric_to_dom_tree("test-num", &to_test); 00183 if (!to_gen) 00184 { 00185 ret = "no dom tree created"; 00186 } 00187 else 00188 { 00189 to_compare = dom_tree_to_gnc_numeric(to_gen); 00190 if (!to_compare) 00191 { 00192 ret = "no gnc_numeric parsed"; 00193 } 00194 else 00195 { 00196 if (!gnc_numeric_equal(to_test, *to_compare)) 00197 { 00198 ret = "numerics compared different"; 00199 } 00200 } 00201 } 00202 00203 if (to_compare) 00204 { 00205 g_free(to_compare); 00206 } 00207 if (to_gen) 00208 { 00209 xmlFreeNode(to_gen); 00210 } 00211 00212 return ret; 00213 } 00214 00215 static void 00216 test_dom_tree_to_gnc_numeric(void) 00217 { 00218 int i; 00219 00220 for (i = 0; i < 20; i++) 00221 { 00222 gchar *message = NULL; 00223 00224 message = test_gnc_nums_internal(get_random_gnc_numeric()); 00225 00226 do_test_args(message == NULL, "dom_tree_to_gnc_numeric", 00227 __FILE__, __LINE__, message); 00228 } 00229 00230 { 00231 gchar *message = NULL; 00232 00233 message = test_gnc_nums_internal 00234 (gnc_numeric_create(18768786810LL, 100000)); 00235 00236 do_test_args(message == NULL, "gnc_num 18768786810/100000", 00237 __FILE__, __LINE__, message); 00238 } 00239 } 00240 00241 00242 static void 00243 test_dom_tree_to_guid(void) 00244 { 00245 int i; 00246 for (i = 0; i < 20; i++) 00247 { 00248 GncGUID *test_guid1; 00249 GncGUID *test_guid2; 00250 xmlNodePtr test_node; 00251 00252 test_guid1 = get_random_guid(); 00253 00254 if (!(test_node = guid_to_dom_tree("test-guid", test_guid1))) 00255 { 00256 failure_args("guid_to_dom_tree", __FILE__, __LINE__, 00257 "conversion to dom tree failed"); 00258 } 00259 00260 test_guid2 = dom_tree_to_guid(test_node); 00261 00262 do_test(guid_equal(test_guid1, test_guid2), 00263 "dom_tree_to_guid" ); 00264 00265 xmlFreeNode(test_node); 00266 g_free(test_guid1); 00267 g_free(test_guid2); 00268 } 00269 } 00270 00271 int 00272 main(int argc, char **argv) 00273 { 00274 qof_init(); 00275 cashobjects_register(); 00276 test_dom_tree_to_guid(); 00277 fflush(stdout); 00278 test_dom_tree_to_commodity_ref(); 00279 fflush(stdout); 00280 test_dom_tree_to_text(); 00281 fflush(stdout); 00282 test_dom_tree_to_timespec(); 00283 fflush(stdout); 00284 test_dom_tree_to_gnc_numeric(); 00285 fflush(stdout); 00286 print_test_results(); 00287 qof_close(); 00288 exit(get_rv()); 00289 }
1.7.4