GnuCash 2.4.99
test-print-parse-amount.c
00001 #include "config.h"
00002 #include <glib.h>
00003 #include <stdlib.h>
00004 #include <glib/gprintf.h>
00005 
00006 #include "gnc-ui-util.h"
00007 #include "gnc-numeric.h"
00008 #include "test-engine-stuff.h"
00009 #include "test-stuff.h"
00010 #include <unittest-support.h>
00011 
00012 static void
00013 test_num_print_info (gnc_numeric n, GNCPrintAmountInfo print_info, int line)
00014 {
00015     gnc_numeric n_parsed = gnc_numeric_zero();
00016     const char *s;
00017     gboolean ok, print_ok;
00018 
00019     gchar *msg = "[PrintAmountInternal()] Bad numeric from rounding: GNC_ERROR_OVERFLOW.";
00020     gchar *log_domain = "gnc.gui";
00021     guint loglevel = G_LOG_LEVEL_WARNING, hdlr;
00022     TestErrorStruct check = { loglevel, log_domain, msg };
00023 
00024     /* Throws overflows during rounding step in xaccPrintAmount when the "fraction" is high. See bug 665707. */
00025     hdlr = g_log_set_handler (log_domain, loglevel,
00026                               (GLogFunc)test_checked_handler, &check);
00027     s = xaccPrintAmount (n, print_info);
00028     print_ok = (s && s[0] != '\0');
00029     if (!print_ok)
00030         return;
00031 
00032     ok = xaccParseAmount (s, print_info.monetary, &n_parsed, NULL);
00033     g_log_remove_handler (log_domain, hdlr);
00034 
00035 
00036     do_test_args (ok, "parsing failure", __FILE__, __LINE__,
00037                   "num: %s, string %s (line %d)", gnc_numeric_to_string (n), s, line);
00038 
00039     ok = gnc_numeric_equal (n, n_parsed);
00040     do_test_args (ok, "not equal", __FILE__, __LINE__,
00041                   "start: %s, string %s, finish: %s (line %d)",
00042                   gnc_numeric_to_string (n), s,
00043                   gnc_numeric_to_string (n_parsed), line);
00044 
00045 }
00046 
00047 static void
00048 test_num (gnc_numeric n)
00049 {
00050     GNCPrintAmountInfo print_info;
00051     int fraction;
00052     int i;
00053 
00054     print_info.commodity = NULL;
00055     print_info.min_decimal_places = 0;
00056     print_info.use_locale = 1;
00057     print_info.use_symbol = 0;
00058 
00059     for (i = 1, fraction = 10; i < 9; i++, fraction *= 10)
00060     {
00061         gnc_numeric n1;
00062 
00063         print_info.use_separators = 1;
00064         print_info.monetary = 1;
00065         print_info.max_decimal_places = i;
00066         print_info.force_fit = 0;
00067         print_info.round = 0;
00068 
00069         n1 = gnc_numeric_convert (n, fraction, GNC_HOW_RND_ROUND_HALF_UP);
00070         if (gnc_numeric_check(n1))
00071         {
00072             do_test_args((gnc_numeric_check(n1) == GNC_ERROR_OVERFLOW),
00073                          "BAD NUMERIC CONVERSION", __FILE__, __LINE__,
00074                          "num: %s, fraction: %d", gnc_numeric_to_string(n), fraction);
00075             continue;
00076         }
00077 
00078         test_num_print_info (n1, print_info, __LINE__);
00079 
00080         print_info.monetary = 0;
00081         test_num_print_info (n1, print_info, __LINE__);
00082 
00083         print_info.use_separators = 0;
00084         test_num_print_info (n1, print_info, __LINE__);
00085 
00086         print_info.round = 1;
00087         test_num_print_info (n1, print_info, __LINE__);
00088 
00089         print_info.round = 0;
00090         print_info.force_fit = 1;
00091         test_num_print_info (n1, print_info, __LINE__);
00092 
00093         print_info.round = 1;
00094         test_num_print_info (n1, print_info, __LINE__);
00095     }
00096 }
00097 
00098 #define IS_VALID_NUM(n,m)                                               \
00099     if (gnc_numeric_check(n)) {                                         \
00100         do_test_args(gnc_numeric_check(n) == GNC_ERROR_OVERFLOW,        \
00101                      "BAD NUMERIC", __FILE__, __LINE__,                 \
00102                      "num: %s (from %s)",                               \
00103                      gnc_numeric_to_string(n),                          \
00104                      gnc_numeric_to_string(m));                         \
00105         continue;                                                       \
00106     } else { m = n; }
00107 
00108 static void
00109 run_tests (void)
00110 {
00111     int i;
00112 
00113     for (i = 0; i < 50; i++)
00114     {
00115         gnc_numeric n;
00116         gnc_numeric n1;
00117 
00118         n = get_random_gnc_numeric ();
00119         IS_VALID_NUM(n, n);
00120         test_num (n);
00121 
00122         n1 = gnc_numeric_mul (n, n, n.denom, GNC_HOW_RND_ROUND_HALF_UP);
00123         IS_VALID_NUM(n1, n);
00124         test_num (n);
00125 
00126         n1 = gnc_numeric_mul (n, n, n.denom, GNC_HOW_RND_ROUND_HALF_UP);
00127         IS_VALID_NUM(n1, n);
00128         test_num (n);
00129     }
00130 }
00131 
00132 int
00133 main (int argc, char **argv)
00134 {
00135     run_tests ();
00136     print_test_results ();
00137     exit (get_rv ());
00138 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines