2005-11-02 03:32:36 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* test-dom-converters1.c
|
|
|
|
|
*
|
|
|
|
|
* Fri Oct 7 20:51:06 2005
|
|
|
|
|
* Copyright 2005 Neil Williams
|
|
|
|
|
* linux@codehelp.co.uk
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
/*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-11-17 05:35:02 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
|
* 02110-1301, USA.
|
2005-11-02 03:32:36 +00:00
|
|
|
*/
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
#include "test-stuff.h"
|
|
|
|
|
#include "test-engine-stuff.h"
|
|
|
|
|
#include "test-file-stuff.h"
|
2005-11-02 03:32:36 +00:00
|
|
|
#include "cashobjects.h"
|
2001-08-07 23:29:04 +00:00
|
|
|
#include "gnc-xml-helper.h"
|
2005-11-02 03:32:36 +00:00
|
|
|
#include "gnc-engine.h"
|
2001-08-07 23:29:04 +00:00
|
|
|
#include "sixtp.h"
|
|
|
|
|
#include "sixtp-parsers.h"
|
|
|
|
|
#include "sixtp-utils.h"
|
|
|
|
|
#include "sixtp-dom-parsers.h"
|
|
|
|
|
#include "sixtp-dom-generators.h"
|
|
|
|
|
|
|
|
|
|
#include "gnc-commodity.h"
|
|
|
|
|
|
2012-01-29 01:12:30 +00:00
|
|
|
#define GNC_V2_STRING "gnc-v2"
|
|
|
|
|
const gchar *gnc_v2_xml_version_string = GNC_V2_STRING;
|
|
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
static void
|
|
|
|
|
test_dom_tree_to_commodity_ref(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2009-12-29 20:12:48 +00:00
|
|
|
for (i = 0; i < 20; i++)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
gnc_commodity *test_com1;
|
|
|
|
|
gchar *test_str1;
|
|
|
|
|
gchar *test_str2;
|
|
|
|
|
gnc_commodity *test_com2;
|
|
|
|
|
xmlNodePtr test_node;
|
2005-11-02 03:32:36 +00:00
|
|
|
QofBook *book;
|
|
|
|
|
|
|
|
|
|
book = qof_book_new ();
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
test_str1 = get_random_string();
|
|
|
|
|
test_str2 = get_random_string();
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
test_com1 = gnc_commodity_new(book, NULL, test_str1, test_str2, NULL, 0);
|
2001-08-07 23:29:04 +00:00
|
|
|
test_node = commodity_ref_to_dom_tree("test-com", test_com1);
|
|
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
test_com2 = dom_tree_to_commodity_ref_no_engine(test_node, book);
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
do_test(gnc_commodity_equiv(test_com1, test_com2),
|
2009-12-29 20:12:48 +00:00
|
|
|
"dom_tree_to_commodity_ref_no_engine");
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
xmlFreeNode(test_node);
|
|
|
|
|
gnc_commodity_destroy(test_com1);
|
|
|
|
|
gnc_commodity_destroy(test_com2);
|
|
|
|
|
g_free(test_str1);
|
|
|
|
|
g_free(test_str2);
|
2005-11-02 03:32:36 +00:00
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
qof_book_destroy (book);
|
2001-08-07 23:29:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_dom_tree_to_text(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
for (i = 0; i < 20; i++)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
gchar *test_string1;
|
|
|
|
|
gchar *test_string2;
|
|
|
|
|
xmlNodePtr test_node;
|
|
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
test_node = xmlNewNode(NULL, BAD_CAST "test-node");
|
2001-08-07 23:29:04 +00:00
|
|
|
test_string1 = get_random_string();
|
|
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
xmlNodeAddContent(test_node, BAD_CAST test_string1);
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
test_string2 = dom_tree_to_text(test_node);
|
|
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
if (!test_string2)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
2009-12-29 20:12:48 +00:00
|
|
|
failure_args("dom_tree_to_text", __FILE__, __LINE__,
|
2001-08-07 23:29:04 +00:00
|
|
|
"null return from dom_tree_to_text");
|
|
|
|
|
xmlElemDump(stdout, NULL, test_node);
|
|
|
|
|
}
|
2012-08-07 17:24:55 +00:00
|
|
|
else if (g_strcmp0(test_string1, test_string2) == 0)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
success_args("dom_tree_to_text", __FILE__, __LINE__, "with string %s",
|
|
|
|
|
test_string1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-09-01 15:08:26 +00:00
|
|
|
failure_args("dom_tree_to_text", __FILE__, __LINE__,
|
2001-08-07 23:29:04 +00:00
|
|
|
"with string %s", test_string1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xmlFreeNode(test_node);
|
|
|
|
|
g_free(test_string1);
|
2009-12-29 20:12:48 +00:00
|
|
|
if (test_string2) g_free(test_string2);
|
2001-08-07 23:29:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_dom_tree_to_timespec(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2009-12-29 20:12:48 +00:00
|
|
|
for (i = 0; i < 20; i++)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
Timespec *test_spec1;
|
2003-09-01 15:08:26 +00:00
|
|
|
Timespec test_spec2;
|
2001-08-07 23:29:04 +00:00
|
|
|
xmlNodePtr test_node;
|
|
|
|
|
|
|
|
|
|
test_spec1 = get_random_timespec();
|
|
|
|
|
|
|
|
|
|
test_node = timespec_to_dom_tree("test-spec", test_spec1);
|
|
|
|
|
|
|
|
|
|
test_spec2 = dom_tree_to_timespec(test_node);
|
|
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
if (!dom_tree_valid_timespec(&test_spec2, (const xmlChar*)"test-spec"))
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
failure_args("dom_tree_to_timespec",
|
|
|
|
|
__FILE__, __LINE__, "NULL return");
|
|
|
|
|
printf("Node looks like:\n");
|
|
|
|
|
xmlElemDump(stdout, NULL, test_node);
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
2009-12-29 20:12:48 +00:00
|
|
|
|
|
|
|
|
else if (timespec_cmp(test_spec1, &test_spec2) == 0)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
success("dom_tree_to_timespec");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
failure("dom_tree_to_timespec");
|
|
|
|
|
printf("Node looks like:\n");
|
|
|
|
|
xmlElemDump(stdout, NULL, test_node);
|
|
|
|
|
printf("\n");
|
2006-04-14 16:45:10 +00:00
|
|
|
printf("Secs are %" G_GUINT64_FORMAT " vs %" G_GUINT64_FORMAT " :: ",
|
2001-08-07 23:29:04 +00:00
|
|
|
test_spec1->tv_sec,
|
2003-09-01 15:08:26 +00:00
|
|
|
test_spec2.tv_sec);
|
2001-08-07 23:29:04 +00:00
|
|
|
printf("NSecs are %ld vs %ld\n",
|
|
|
|
|
test_spec1->tv_nsec,
|
2003-09-01 15:08:26 +00:00
|
|
|
test_spec2.tv_nsec);
|
2001-08-07 23:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_free(test_spec1);
|
|
|
|
|
xmlFreeNode(test_node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
|
test_gnc_nums_internal(gnc_numeric to_test)
|
|
|
|
|
{
|
|
|
|
|
gchar *ret = NULL;
|
|
|
|
|
gnc_numeric *to_compare = NULL;
|
|
|
|
|
xmlNodePtr to_gen = NULL;
|
|
|
|
|
|
|
|
|
|
to_gen = gnc_numeric_to_dom_tree("test-num", &to_test);
|
2009-12-29 20:12:48 +00:00
|
|
|
if (!to_gen)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
ret = "no dom tree created";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
to_compare = dom_tree_to_gnc_numeric(to_gen);
|
2009-12-29 20:12:48 +00:00
|
|
|
if (!to_compare)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
ret = "no gnc_numeric parsed";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-12-29 20:12:48 +00:00
|
|
|
if (!gnc_numeric_equal(to_test, *to_compare))
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
ret = "numerics compared different";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
if (to_compare)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
g_free(to_compare);
|
|
|
|
|
}
|
2009-12-29 20:12:48 +00:00
|
|
|
if (to_gen)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
xmlFreeNode(to_gen);
|
|
|
|
|
}
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_dom_tree_to_gnc_numeric(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
for (i = 0; i < 20; i++)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
gchar *message = NULL;
|
|
|
|
|
|
2014-12-03 10:24:37 -08:00
|
|
|
message = test_gnc_nums_internal(get_random_gnc_numeric(GNC_DENOM_AUTO));
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
do_test_args(message == NULL, "dom_tree_to_gnc_numeric",
|
|
|
|
|
__FILE__, __LINE__, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
gchar *message = NULL;
|
|
|
|
|
|
|
|
|
|
message = test_gnc_nums_internal
|
2009-12-29 20:12:48 +00:00
|
|
|
(gnc_numeric_create(18768786810LL, 100000));
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
do_test_args(message == NULL, "gnc_num 18768786810/100000",
|
|
|
|
|
__FILE__, __LINE__, message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_dom_tree_to_guid(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2009-12-29 20:12:48 +00:00
|
|
|
for (i = 0; i < 20; i++)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
2010-03-27 21:01:21 +00:00
|
|
|
GncGUID *test_guid1;
|
|
|
|
|
GncGUID *test_guid2;
|
2001-08-07 23:29:04 +00:00
|
|
|
xmlNodePtr test_node;
|
|
|
|
|
|
|
|
|
|
test_guid1 = get_random_guid();
|
|
|
|
|
|
|
|
|
|
if (!(test_node = guid_to_dom_tree("test-guid", test_guid1)))
|
|
|
|
|
{
|
2009-12-29 20:12:48 +00:00
|
|
|
failure_args("guid_to_dom_tree", __FILE__, __LINE__,
|
|
|
|
|
"conversion to dom tree failed");
|
2001-08-07 23:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test_guid2 = dom_tree_to_guid(test_node);
|
|
|
|
|
|
|
|
|
|
do_test(guid_equal(test_guid1, test_guid2),
|
2009-12-29 20:12:48 +00:00
|
|
|
"dom_tree_to_guid" );
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
xmlFreeNode(test_node);
|
|
|
|
|
g_free(test_guid1);
|
|
|
|
|
g_free(test_guid2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main(int argc, char **argv)
|
|
|
|
|
{
|
2009-12-29 20:12:48 +00:00
|
|
|
qof_init();
|
|
|
|
|
cashobjects_register();
|
2001-08-07 23:29:04 +00:00
|
|
|
test_dom_tree_to_guid();
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
test_dom_tree_to_commodity_ref();
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
test_dom_tree_to_text();
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
test_dom_tree_to_timespec();
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
test_dom_tree_to_gnc_numeric();
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
print_test_results();
|
2009-12-29 20:12:48 +00:00
|
|
|
qof_close();
|
2001-08-07 23:29:04 +00:00
|
|
|
exit(get_rv());
|
|
|
|
|
}
|