Example #1
0
int
main (int argc, char** argv)
{
    int i;

    for (i = 0; i < 20; i++)
    {
        Timespec* spec1;
        Timespec spec2;
        gchar* sec_str;
        gchar* nsec_str;

        spec1 = get_random_timespec ();

        sec_str = timespec_sec_to_string (spec1);
        nsec_str = timespec_nsec_to_string (spec1);

        if (!string_to_timespec_secs (sec_str, &spec2))
        {
            failure_args ("string_to_timespec_secs", __FILE__, __LINE__,
                          "string is %s", sec_str);
        }

        else if (!string_to_timespec_nsecs (nsec_str, &spec2))
        {
            failure_args ("string_to_timespec_nsecs", __FILE__, __LINE__,
                          "string is %s", nsec_str);
        }

        else if (spec1->tv_sec != spec2.tv_sec)
        {
            failure_args ("timespec_secs", __FILE__, __LINE__,
                          "not equal ints are %" G_GINT64_FORMAT
                          " and %" G_GINT64_FORMAT "\n",
                          spec1->tv_sec, spec2.tv_sec);
        }

        else if (spec1->tv_nsec != spec2.tv_nsec)
        {
            failure_args ("timespec_nsecs", __FILE__, __LINE__,
                          "not equal ints are %ld and %ld\n",
                          spec1->tv_nsec, spec2.tv_nsec);
        }

        else
        {
            success ("timespec");
        }

        g_free (spec1);
        g_free (sec_str);
        g_free (nsec_str);
    }
    print_test_results ();
    exit (get_rv ());
}
static void
test_db (GNCPriceDB* db)
{
    xmlNodePtr test_node;
    gchar* filename1;
    int fd;
    QofBook* book = qof_instance_get_book (QOF_INSTANCE (db));

    test_node = gnc_pricedb_dom_tree_create (db);

    if (!test_node && db)
    {
        failure_args ("pricedb_xml", __FILE__, __LINE__,
                      "gnc_pricedb_dom_tree_create returned NULL");
        return;
    }

    if (!db)
        return;

    filename1 = g_strdup_printf ("test_file_XXXXXX");

    fd = g_mkstemp (filename1);

    write_dom_node_to_file (test_node, fd);

    close (fd);

    {
        sixtp *parser;
	load_counter lc = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        sixtp_gdv2 data = {book, lc, NULL, NULL, FALSE};

        parser = sixtp_new ();

        if (!sixtp_add_some_sub_parsers
            (parser, TRUE,
             "gnc:pricedb", gnc_pricedb_sixtp_parser_create (),
             NULL, NULL))
        {
            failure_args ("sixtp_add_some_sub_parsers failed",
                          __FILE__, __LINE__, "%d", iter);
        }
        else if (!gnc_xml_parse_file (parser, filename1, test_add_pricedb,
                                      (gpointer)&data,
                                      qof_session_get_book (session)))
        {
            failure_args ("gnc_xml_parse_file returned FALSE",
                          __FILE__, __LINE__, "%d", iter);
        }
    }

    g_unlink (filename1);
    g_free (filename1);
    xmlFreeNode (test_node);
}
static void
test_dom_tree_to_guid(void)
{
    int i;
    for (i = 0; i < 20; i++)
    {
        GncGUID *test_guid1;
        GncGUID *test_guid2;
        xmlNodePtr test_node;

        test_guid1 = get_random_guid();

        if (!(test_node = guid_to_dom_tree("test-guid", test_guid1)))
        {
            failure_args("guid_to_dom_tree", __FILE__, __LINE__,
                         "conversion to dom tree failed");
        }

        test_guid2 = dom_tree_to_guid(test_node);

        do_test(guid_equal(test_guid1, test_guid2),
                "dom_tree_to_guid" );

        xmlFreeNode(test_node);
        g_free(test_guid1);
        g_free(test_guid2);
    }
}
Example #4
0
static int
test_trans_query (Transaction *trans, gpointer data)
{
    QofBook *book = data;
    GList *list;
    QofQuery *q;

    q = make_trans_query (trans, ALL_QT);
    qof_query_set_book (q, book);

    list = xaccQueryGetTransactions (q, QUERY_TXN_MATCH_ANY);
    if (g_list_length (list) != 1)
    {
        failure_args ("test number returned", __FILE__, __LINE__,
                      "number of matching transactions %d not 1",
                      g_list_length (list));
        g_list_free (list);
        return 13;
    }

    if (list->data != trans)
    {
        failure ("matching transaction is wrong");
        g_list_free (list);
        return 13;
    }

    success ("found right transaction");
    qof_query_destroy (q);
    g_list_free (list);

    return 0;
}
Example #5
0
void
failure_call(
    const char *test_title,
    const char *file,
    int line)
{
    failure_args( test_title, file, line, "" );
}
Example #6
0
gboolean
do_test_call(gboolean result, const char* test_title, const char* filename,
             int line )
{
    if (result)
        success_args( test_title, filename, line, "" );
    else
        failure_args( test_title, filename, line, "" );

    return result;
}
static void
test_dom_tree_to_text(void)
{
    int i;

    for (i = 0; i < 20; i++)
    {
        gchar *test_string1;
        gchar *test_string2;
        xmlNodePtr test_node;

        test_node = xmlNewNode(NULL, BAD_CAST "test-node");
        test_string1 = get_random_string();

        xmlNodeAddContent(test_node, BAD_CAST test_string1);

        test_string2 = dom_tree_to_text(test_node);

        if (!test_string2)
        {
            failure_args("dom_tree_to_text", __FILE__, __LINE__,
                         "null return from dom_tree_to_text");
            xmlElemDump(stdout, NULL, test_node);
        }
        else if (g_strcmp0(test_string1, test_string2) == 0)
        {
            success_args("dom_tree_to_text", __FILE__, __LINE__, "with string %s",
                         test_string1);
        }
        else
        {
            failure_args("dom_tree_to_text", __FILE__, __LINE__,
                         "with string %s", test_string1);
        }

        xmlFreeNode(test_node);
        g_free(test_string1);
        if (test_string2) g_free(test_string2);
    }
}
Example #8
0
static void
test_load_file(const char *filename, gxpf_callback cb, sixtp *top_parser,
               QofBook *book)
{
    xmlNodePtr node;

    node = grab_file_doc(filename);

    if (!node)
    {
        failure_args("failure of libxml to parse file", __FILE__, __LINE__,
                     "%s", filename);
        return;
    }

    if (!gnc_xml_parse_file(top_parser, filename, cb, node->children, book))
    {
        failure_args("failure to parse file", __FILE__, __LINE__,
                     "%s", filename);
    }

    xmlFreeNode(node);
}
static void
test_dom_tree_to_timespec(void)
{
    int i;
    for (i = 0; i < 20; i++)
    {
        Timespec *test_spec1;
        Timespec test_spec2;
        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);

        if (!dom_tree_valid_timespec(&test_spec2, (const xmlChar*)"test-spec"))
        {
            failure_args("dom_tree_to_timespec",
                         __FILE__, __LINE__, "NULL return");
            printf("Node looks like:\n");
            xmlElemDump(stdout, NULL, test_node);
            printf("\n");
        }

        else if (timespec_cmp(test_spec1, &test_spec2) == 0)
        {
            success("dom_tree_to_timespec");
        }
        else
        {
            failure("dom_tree_to_timespec");
            printf("Node looks like:\n");
            xmlElemDump(stdout, NULL, test_node);
            printf("\n");
            printf("Secs are %" G_GUINT64_FORMAT " vs %" G_GUINT64_FORMAT " :: ",
                   test_spec1->tv_sec,
                   test_spec2.tv_sec);
            printf("NSecs are %ld vs %ld\n",
                   test_spec1->tv_nsec,
                   test_spec2.tv_nsec);
        }

        g_free(test_spec1);
        xmlFreeNode(test_node);
    }
}
Example #10
0
static void
test_load_file(const char *filename)
{
    GncExampleAccount *gea;

    gea = gnc_read_example_account(filename);

    if (gea != NULL)
    {
        success("example account load");
        gnc_destroy_example_account(gea);
    }
    else
    {
        failure_args("example account load", __FILE__, __LINE__, "for file %s",
                     filename);
    }
}
static void
test_generation (void)
{
    for (iter = 0; iter < 20; iter++)
    {
        GNCPriceDB* db;
        g_message ("iter=%d", iter);
        session = qof_session_new ();
        db = get_random_pricedb (qof_session_get_book (session));
        if (!db)
        {
            failure_args ("gnc_random_price_db returned NULL",
                          __FILE__, __LINE__, "%d", iter);
            return;
        }
        if (gnc_pricedb_get_num_prices (db))
            test_db (db);

        gnc_pricedb_destroy (db);
        qof_session_end (session);
    }
}
static void
test_account (int i, Account* test_act)
{
    xmlNodePtr test_node;
    gchar* filename1;
    gchar* compare_msg;
    int fd;

    test_node = gnc_account_dom_tree_create (test_act, FALSE, TRUE);

    if (!test_node)
    {
        failure_args ("account_xml", __FILE__, __LINE__,
                      "gnc_account_dom_tree_create returned NULL");
        return;
    }

    if ((compare_msg = node_and_account_equal (test_node, test_act)) != NULL)
    {
        failure_args ("account_xml", __FILE__, __LINE__,
                      "node and account were not equal: %s", compare_msg);
        xmlElemDump (stdout, NULL, test_node);
        fprintf (stdout, "\n");
        xmlFreeNode (test_node);
        g_free (compare_msg);
        return;
    }
    else
    {
        success ("account_xml");
    }

    filename1 = g_strdup_printf ("test_file_XXXXXX");

    fd = g_mkstemp (filename1);

    write_dom_node_to_file (test_node, fd);

    close (fd);

    {
        sixtp* parser;
        act_data data;

        data.act = test_act;
        data.value = i;

        parser = gnc_account_sixtp_parser_create ();

        if (!gnc_xml_parse_file (parser, filename1, test_add_account,
                                 &data, sixbook))
        {
            failure_args ("gnc_xml_parse_file returned FALSE",
                          __FILE__, __LINE__, "%d", i);
        }

        /* no handling of circular data structures.  We'll do that later */
        /* sixtp_destroy(parser); */
    }


    g_unlink (filename1);
    g_free (filename1);
    xmlFreeNode (test_node);
}
static void
test_transaction (void)
{
    int i;

    for (i = 0; i < 50; i++)
    {
        Transaction* ran_trn;
        xmlNodePtr test_node;
        gnc_commodity* com, *new_com;
        gchar* filename1;
        int fd;

        /* The next line exists for its side effect of creating the
         * account tree. */
        get_random_account_tree (book);
        ran_trn = get_random_transaction (book);
        new_com = get_random_commodity (book);
        if (!ran_trn)
        {
            failure_args ("transaction_xml", __FILE__, __LINE__,
                          "get_random_transaction returned NULL");
            return;
        }

        {
            /* xaccAccountInsertSplit can reorder the splits. */
            GList* list = g_list_copy (xaccTransGetSplitList (ran_trn));
            GList* node = list;
            for (; node; node = node->next)
            {
                Split* s = static_cast<decltype (s)> (node->data);
                Account* a = xaccMallocAccount (book);

                xaccAccountBeginEdit (a);
                xaccAccountSetCommodity (a, new_com);
                xaccAccountSetCommoditySCU (a, xaccSplitGetAmount (s).denom);
                xaccAccountInsertSplit (a, s);
                xaccAccountCommitEdit (a);
            }
            g_list_free (list);
        }

        com = xaccTransGetCurrency (ran_trn);

        test_node = gnc_transaction_dom_tree_create (ran_trn);
        if (!test_node)
        {
            failure_args ("transaction_xml", __FILE__, __LINE__,
                          "gnc_transaction_dom_tree_create returned NULL");
            really_get_rid_of_transaction (ran_trn);
            continue;
        }
        auto compare_msg = node_and_transaction_equal (test_node, ran_trn);
        if (compare_msg != nullptr)
        {
            failure_args ("transaction_xml", __FILE__, __LINE__,
                          "node and transaction were not equal: %s",
                          compare_msg);
            xmlElemDump (stdout, NULL, test_node);
            printf ("\n");
            fflush (stdout);
            xmlFreeNode (test_node);
            really_get_rid_of_transaction (ran_trn);
            continue;
        }
        else
        {
            success_args ("transaction_xml", __FILE__, __LINE__, "%d", i);
        }

        filename1 = g_strdup_printf ("test_file_XXXXXX");

        fd = g_mkstemp (filename1);

        write_dom_node_to_file (test_node, fd);

        close (fd);

        {
            GList* node = xaccTransGetSplitList (ran_trn);
            for (; node; node = node->next)
            {
                Split* s = static_cast<decltype (s)> (node->data);
                Account* a1 = xaccSplitGetAccount (s);
                Account* a2 = xaccMallocAccount (book);

                xaccAccountBeginEdit (a2);
                xaccAccountSetCommoditySCU (a2, xaccAccountGetCommoditySCU (a1));
                xaccAccountSetGUID (a2, xaccAccountGetGUID (a1));
                xaccAccountCommitEdit (a2);
            }
        }

        {
            sixtp* parser;
            tran_data data;

            const char* msg =
                "[xaccAccountScrubCommodity()] Account \"\" does not have a commodity!";
            const char* logdomain = "gnc.engine.scrub";
            GLogLevelFlags loglevel = static_cast<decltype (loglevel)>
                                      (G_LOG_LEVEL_CRITICAL);
            TestErrorStruct check = { loglevel, const_cast<char*> (logdomain),
                                      const_cast<char*> (msg)
                                    };
            g_log_set_handler (logdomain, loglevel,
                               (GLogFunc)test_checked_handler, &check);
            data.trn = ran_trn;
            data.com = com;
            data.value = i;
            parser = gnc_transaction_sixtp_parser_create ();

            if (!gnc_xml_parse_file (parser, filename1, test_add_transaction,
                                     (gpointer)&data, book))
            {
                failure_args ("gnc_xml_parse_file returned FALSE",
                              __FILE__, __LINE__, "%d", i);
            }
            else
                really_get_rid_of_transaction (data.new_trn);
        }
        /* no handling of circular data structures.  We'll do that later */
        /* sixtp_destroy(parser); */


        g_unlink (filename1);
        g_free (filename1);
        really_get_rid_of_transaction (ran_trn);
        xmlFreeNode (test_node);
    }
}
Example #14
0
static void
test_generation (void)
{
    int i;
    for (i = 0; i < 20; i++)
    {
        gnc_commodity* ran_com;
        xmlNodePtr test_node;
        gchar* filename1;
        int fd;

        ran_com = get_random_commodity (book);

        test_node = gnc_commodity_dom_tree_create (ran_com);
        if (!test_node)
        {
            failure_args ("commodity_xml", __FILE__, __LINE__,
                          "gnc_commodity_dom_tree_create returned NULL");
            gnc_commodity_destroy (ran_com);
            continue;
        }
        auto compare_msg = node_and_commodity_equal (test_node, ran_com);
        if (compare_msg != nullptr)
        {
            failure_args ("commodity_xml", __FILE__, __LINE__,
                          "node and commodity were not equal: %s", compare_msg);
            xmlElemDump (stdout, NULL, test_node);
            xmlFreeNode (test_node);
            gnc_commodity_destroy (ran_com);
            continue;
        }
        else
        {
            success_args ("commodity_xml", __FILE__, __LINE__, "%d", i);
        }

        filename1 = g_strdup_printf ("test_file_XXXXXX");

        fd = g_mkstemp (filename1);

        write_dom_node_to_file (test_node, fd);

        close (fd);

        {
            sixtp* parser;
            com_data data;

            data.com = ran_com;
            data.value = i;

            parser = gnc_commodity_sixtp_parser_create ();

            if (!gnc_xml_parse_file (parser, filename1, test_add_commodity,
                                     (gpointer)&data, book))
            {
                failure_args ("gnc_xml_parse_file returned FALSE",
                              __FILE__, __LINE__, "%d", i);
            }

            /* no handling of circular data structures.  We'll do that later */
            /* sixtp_destroy(parser); */
        }

        g_unlink (filename1);
        g_free (filename1);
        gnc_commodity_destroy (ran_com);
        xmlFreeNode (test_node);
    }
}
static void
test_transaction(void)
{
    int i;

    for (i = 0; i < 50; i++)
    {
        Transaction *ran_trn;
        Account *root;
        xmlNodePtr test_node;
        gnc_commodity *com, *new_com;
        gchar *compare_msg;
        gchar *filename1;
        int fd;

        /* The next line exists for its side effect of creating the
         * account tree. */
        root = get_random_account_tree(book);
        ran_trn = get_random_transaction(book);
        new_com = get_random_commodity( book );
        if (!ran_trn)
        {
            failure_args("transaction_xml", __FILE__, __LINE__,
                         "get_random_transaction returned NULL");
            return;
        }

        {
            /* xaccAccountInsertSplit can reorder the splits. */
            GList * list = g_list_copy(xaccTransGetSplitList (ran_trn));
            GList * node = list;
            for ( ; node; node = node->next)
            {
                Split * s = node->data;
                Account * a = xaccMallocAccount(book);

                xaccAccountBeginEdit (a);
                xaccAccountSetCommodity( a, new_com );
                xaccAccountSetCommoditySCU (a, xaccSplitGetAmount (s).denom);
                xaccAccountInsertSplit (a, s);
                xaccAccountCommitEdit (a);
            }
            g_list_free(list);
        }

        com = xaccTransGetCurrency (ran_trn);

        test_node = gnc_transaction_dom_tree_create(ran_trn);
        if (!test_node)
        {
            failure_args("transaction_xml", __FILE__, __LINE__,
                         "gnc_transaction_dom_tree_create returned NULL");
            really_get_rid_of_transaction(ran_trn);
            continue;
        }

        if ((compare_msg = node_and_transaction_equal(test_node, ran_trn)) !=
                NULL)
        {
            failure_args("transaction_xml", __FILE__, __LINE__,
                         "node and transaction were not equal: %s",
                         compare_msg);
            xmlElemDump(stdout, NULL, test_node);
            printf("\n");
            fflush(stdout);
            xmlFreeNode(test_node);
            really_get_rid_of_transaction(ran_trn);
            continue;
        }
        else
        {
            success_args("transaction_xml", __FILE__, __LINE__, "%d", i );
        }

        filename1 = g_strdup_printf("test_file_XXXXXX");

        fd = g_mkstemp(filename1);

        write_dom_node_to_file(test_node, fd);

        close(fd);

        {
            GList * node = xaccTransGetSplitList (ran_trn);
            for ( ; node; node = node->next)
            {
                Split * s = node->data;
                Account * a1 = xaccSplitGetAccount(s);
                Account * a2 = xaccMallocAccount(book);

                xaccAccountBeginEdit (a2);
                xaccAccountSetCommoditySCU (a2, xaccAccountGetCommoditySCU (a1));
                xaccAccountSetGUID (a2, xaccAccountGetGUID (a1));
                xaccAccountCommitEdit (a2);
            }
        }

        {
            sixtp *parser;
            tran_data data;

            data.trn = ran_trn;
            data.com = com;
            data.value = i;

            g_print(" There will follow a bunch of CRIT scrub errors about the account not having a commodity. There isn't an account in the XML, so of course not. Ignore the errors\n");
            parser = gnc_transaction_sixtp_parser_create();

            if (!gnc_xml_parse_file(parser, filename1, test_add_transaction,
                                    (gpointer)&data, book))
            {
                failure_args("gnc_xml_parse_file returned FALSE",
                             __FILE__, __LINE__, "%d", i);
            }
            else
                really_get_rid_of_transaction (data.new_trn);

            /* no handling of circular data structures.  We'll do that later */
            /* sixtp_destroy(parser); */
        }

        g_unlink(filename1);
        g_free(filename1);
        really_get_rid_of_transaction(ran_trn);
        xmlFreeNode(test_node);
    }
}