From af876ebbd621a63a1a0636fb0ce74da5c2aca480 Mon Sep 17 00:00:00 2001 From: Perberos Date: Thu, 24 Nov 2011 23:33:08 -0300 Subject: allman style format --- src/currency.c | 414 +++++++------- src/currency.h | 82 ++- src/financial.h | 22 +- src/math-buttons.h | 28 +- src/math-display.h | 18 +- src/math-equation.h | 134 +++-- src/math-preferences.h | 14 +- src/math-variables.h | 20 +- src/math-window.c | 696 +++++++++++------------ src/math-window.h | 26 +- src/mp-equation-private.h | 44 +- src/mp-equation.h | 71 ++- src/mp-private.h | 10 +- src/mp.h | 26 +- src/unittest.c | 1336 +++++++++++++++++++++++---------------------- 15 files changed, 1469 insertions(+), 1472 deletions(-) (limited to 'src') diff --git a/src/currency.c b/src/currency.c index 4029763..2add81b 100644 --- a/src/currency.c +++ b/src/currency.c @@ -11,234 +11,266 @@ #include "mp.h" typedef struct { - char *short_name; - MPNumber value; + char* short_name; + MPNumber value; } currency; -static currency *currencies = NULL; +static currency* currencies = NULL; static int currency_count = 0; static gboolean downloading_rates = FALSE; static gboolean loaded_rates = FALSE; -static char* -get_rate_filepath() +static char* get_rate_filepath() { - return g_build_filename(g_get_user_cache_dir (), - "mate-calc", - "eurofxref-daily.xml", - NULL); + return g_build_filename(g_get_user_cache_dir(), "mate-calc", "eurofxref-daily.xml", NULL); } -static int -currency_get_index(const char *short_name) +static int currency_get_index(const char *short_name) { - int i; - for (i = 0; i < currency_count; i++) { - if (!strcmp(short_name, currencies[i].short_name)) { - if (mp_is_negative(¤cies[i].value) || - mp_is_zero(¤cies[i].value)) { - return -1; - } else { - return i; - } - } - } - return -1; + int i; + + for (i = 0; i < currency_count; i++) + { + if (!strcmp(short_name, currencies[i].short_name)) + { + if (mp_is_negative(¤cies[i].value) || mp_is_zero(¤cies[i].value)) + { + return -1; + } + else + { + return i; + } + } + } + + return -1; } /* A file needs to be redownloaded if it doesn't exist, or every 7 days. * When an error occur, it probably won't hurt to try to download again. */ -static int -currency_rates_needs_update() +static int currency_rates_needs_update() { - gchar *filename = get_rate_filepath (); - struct stat buf; - if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) { - g_free(filename); - return 1; - } - - if (g_stat(filename, &buf) == -1) { - g_free(filename); - return 1; - } - g_free(filename); - - if (difftime(time(NULL), buf.st_mtime) > (60 * 60 * 24 * 7)) { - return 1; - } - - return 0; -} + gchar* filename = get_rate_filepath(); + struct stat buf; + if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) + { + g_free(filename); + return 1; + } -static void -download_cb(GObject *object, GAsyncResult *result, gpointer user_data) -{ - GError *error = NULL; - - if (g_file_copy_finish(G_FILE(object), result, &error)) - g_debug("Rates updated"); - else - g_warning("Couldn't download currency file: %s", error->message); - g_clear_error(&error); - downloading_rates = FALSE; -} + if (g_stat(filename, &buf) == -1) + { + g_free(filename); + return 1; + } + g_free(filename); -static void -currency_download_rates() -{ - gchar *filename, *directory; - GFile *source, *dest; + if (difftime(time(NULL), buf.st_mtime) > (60 * 60 * 24 * 7)) + { + return 1; + } + + return 0; +} - downloading_rates = TRUE; - g_debug("Downloading rates..."); - filename = get_rate_filepath(); - directory = g_path_get_dirname(filename); - g_mkdir_with_parents(directory, 0755); - g_free(directory); +static void download_cb(GObject* object, GAsyncResult* result, gpointer user_data) +{ + GError* error = NULL; + + if (g_file_copy_finish(G_FILE(object), result, &error)) + { + g_debug("Rates updated"); + } + else + { + g_warning("Couldn't download currency file: %s", error->message); + } + + g_clear_error(&error); + downloading_rates = FALSE; +} - source = g_file_new_for_uri ("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"); - dest = g_file_new_for_path (filename); - g_free(filename); - g_file_copy_async (source, dest, G_FILE_COPY_OVERWRITE, G_PRIORITY_DEFAULT, NULL, NULL, NULL, download_cb, NULL); - g_object_unref(source); - g_object_unref(dest); +static void currency_download_rates() +{ + gchar* filename; + gchar* directory; + GFile* source; + GFile* dest; + + downloading_rates = TRUE; + g_debug("Downloading rates..."); + + filename = get_rate_filepath(); + directory = g_path_get_dirname(filename); + g_mkdir_with_parents(directory, 0755); + g_free(directory); + + /* HACK: It is safe? */ + source = g_file_new_for_uri("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"); + dest = g_file_new_for_path(filename); + g_free(filename); + + g_file_copy_async(source, dest, G_FILE_COPY_OVERWRITE, G_PRIORITY_DEFAULT, NULL, NULL, NULL, download_cb, NULL); + g_object_unref(source); + g_object_unref(dest); } -static void -set_rate (xmlNodePtr node, currency *cur) +static void set_rate(xmlNodePtr node, currency* cur) { - xmlAttrPtr attribute; - for (attribute = node->properties; attribute; attribute = attribute->next) { - if (strcmp((char *)attribute->name, "currency") == 0) { - cur->short_name = (char *)xmlNodeGetContent((xmlNodePtr) attribute); - } else if (strcmp ((char *)attribute->name, "rate") == 0) { - char *val = (char *)xmlNodeGetContent ((xmlNodePtr) attribute); - mp_set_from_string(val, 10, &(cur->value)); - xmlFree (val); - } - } + xmlAttrPtr attribute; + + for (attribute = node->properties; attribute; attribute = attribute->next) + { + if (strcmp((char*) attribute->name, "currency") == 0) + { + cur->short_name = (char*) xmlNodeGetContent((xmlNodePtr) attribute); + } + else if (strcmp ((char*) attribute->name, "rate") == 0) + { + char* val = (char*) xmlNodeGetContent((xmlNodePtr) attribute); + mp_set_from_string(val, 10, &(cur->value)); + xmlFree(val); + } + } } -static void -currency_load_rates() +static void currency_load_rates() { - char *filename = get_rate_filepath(); - xmlDocPtr document; - xmlXPathContextPtr xpath_ctx; - xmlXPathObjectPtr xpath_obj; - int i, len; - - g_return_if_fail(g_file_test(filename, G_FILE_TEST_IS_REGULAR)); - - xmlInitParser(); - document = xmlReadFile(filename, NULL, 0); - g_free (filename); - if (document == NULL) { - fprintf(stderr, "Couldn't parse data file\n"); - return; - } - - xpath_ctx = xmlXPathNewContext(document); - if (xpath_ctx == NULL) { - xmlFreeDoc(document); - fprintf(stderr, "Couldn't create XPath context\n"); - return; - } - - xmlXPathRegisterNs(xpath_ctx, - BAD_CAST("xref"), - BAD_CAST("http://www.ecb.int/vocabulary/2002-08-01/eurofxref")); - xpath_obj = xmlXPathEvalExpression(BAD_CAST("//xref:Cube[@currency][@rate]"), - xpath_ctx); - - if (xpath_obj == NULL) { - xmlXPathFreeContext(xpath_ctx); - xmlFreeDoc(document); - fprintf(stderr, "Couldn't create XPath object\n"); - return; - } - - len = (xpath_obj->nodesetval) ? xpath_obj->nodesetval->nodeNr : 0; - currency_count = len + 1; - currencies = g_slice_alloc0(sizeof(currency) * currency_count); - for (i = 0; i < len; i++) { - if (xpath_obj->nodesetval->nodeTab[i]->type == XML_ELEMENT_NODE) { - set_rate(xpath_obj->nodesetval->nodeTab[i], ¤cies[i]); - } - - // Avoid accessing removed elements - if (xpath_obj->nodesetval->nodeTab[i]->type != XML_NAMESPACE_DECL) - xpath_obj->nodesetval->nodeTab[i] = NULL; - } - - currencies[len].short_name = g_strdup("EUR"); - MPNumber foo; - mp_set_from_integer(1, &foo); - currencies[len].value = foo; - - xmlXPathFreeObject(xpath_obj); - xmlXPathFreeContext(xpath_ctx); - xmlFreeDoc(document); - xmlCleanupParser(); - - g_debug("Rates loaded"); - loaded_rates = TRUE; + char* filename = get_rate_filepath(); + xmlDocPtr document; + xmlXPathContextPtr xpath_ctx; + xmlXPathObjectPtr xpath_obj; + int i; + int len; + + g_return_if_fail(g_file_test(filename, G_FILE_TEST_IS_REGULAR)); + + xmlInitParser(); + document = xmlReadFile(filename, NULL, 0); + g_free (filename); + + if (document == NULL) + { + fprintf(stderr, "Couldn't parse data file\n"); + return; + } + + xpath_ctx = xmlXPathNewContext(document); + + if (xpath_ctx == NULL) + { + xmlFreeDoc(document); + fprintf(stderr, "Couldn't create XPath context\n"); + return; + } + + xmlXPathRegisterNs(xpath_ctx, BAD_CAST("xref"), BAD_CAST("http://www.ecb.int/vocabulary/2002-08-01/eurofxref")); + xpath_obj = xmlXPathEvalExpression(BAD_CAST("//xref:Cube[@currency][@rate]"), xpath_ctx); + + if (xpath_obj == NULL) + { + xmlXPathFreeContext(xpath_ctx); + xmlFreeDoc(document); + fprintf(stderr, "Couldn't create XPath object\n"); + return; + } + + len = (xpath_obj->nodesetval) ? xpath_obj->nodesetval->nodeNr : 0; + currency_count = len + 1; + currencies = g_slice_alloc0(sizeof(currency) * currency_count); + + for (i = 0; i < len; i++) + { + if (xpath_obj->nodesetval->nodeTab[i]->type == XML_ELEMENT_NODE) + { + set_rate(xpath_obj->nodesetval->nodeTab[i], ¤cies[i]); + } + + // Avoid accessing removed elements + if (xpath_obj->nodesetval->nodeTab[i]->type != XML_NAMESPACE_DECL) + { + xpath_obj->nodesetval->nodeTab[i] = NULL; + } + } + + currencies[len].short_name = g_strdup("EUR"); + MPNumber foo; + mp_set_from_integer(1, &foo); + currencies[len].value = foo; + + xmlXPathFreeObject(xpath_obj); + xmlXPathFreeContext(xpath_ctx); + xmlFreeDoc(document); + xmlCleanupParser(); + + g_debug("Rates loaded"); + loaded_rates = TRUE; } -gboolean -currency_convert(const MPNumber *from_amount, - const char *source_currency, const char *target_currency, - MPNumber *to_amount) +gboolean currency_convert(const MPNumber* from_amount, const char* source_currency, const char* target_currency, MPNumber* to_amount) { - int from_index, to_index; - - if (downloading_rates) - return FALSE; - - /* Update currency if necessary */ - if (currency_rates_needs_update()) { - currency_download_rates(); - return FALSE; - } - if (!loaded_rates) - currency_load_rates(); - - from_index = currency_get_index(source_currency); - to_index = currency_get_index(target_currency); - if (from_index < 0 || to_index < 0) - return FALSE; - - if (mp_is_zero(¤cies[from_index].value) || - mp_is_zero(¤cies[to_index].value)) { - mp_set_from_integer(0, to_amount); - return FALSE; - } - - mp_divide(from_amount, ¤cies[from_index].value, to_amount); - mp_multiply(to_amount, ¤cies[to_index].value, to_amount); - - return TRUE; + int from_index, to_index; + + if (downloading_rates) + { + return FALSE; + } + + /* Update currency if necessary */ + if (currency_rates_needs_update()) + { + currency_download_rates(); + return FALSE; + } + + if (!loaded_rates) + { + currency_load_rates(); + } + + from_index = currency_get_index(source_currency); + to_index = currency_get_index(target_currency); + + if (from_index < 0 || to_index < 0) + { + return FALSE; + } + + if (mp_is_zero(¤cies[from_index].value) || mp_is_zero(¤cies[to_index].value)) + { + mp_set_from_integer(0, to_amount); + return FALSE; + } + + mp_divide(from_amount, ¤cies[from_index].value, to_amount); + mp_multiply(to_amount, ¤cies[to_index].value, to_amount); + + return TRUE; } -void -currency_free_resources() +void currency_free_resources() { - int i; - - for (i = 0; i < currency_count; i++) { - if (currencies[i].short_name != NULL) - xmlFree(currencies[i].short_name); - } - g_slice_free1(currency_count * sizeof(currency), currencies); - currencies = NULL; - currency_count = 0; + int i; + + for (i = 0; i < currency_count; i++) + { + if (currencies[i].short_name != NULL) + { + xmlFree(currencies[i].short_name); + } + } + + g_slice_free1(currency_count * sizeof(currency), currencies); + + currencies = NULL; + currency_count = 0; } diff --git a/src/currency.h b/src/currency.h index 02cbfd8..f29239e 100644 --- a/src/currency.h +++ b/src/currency.h @@ -6,9 +6,9 @@ #include "mp.h" struct currency_name { - char *short_name; - char *symbol; - char *long_name; + char* short_name; + char* symbol; + char* long_name; }; /* @@ -16,50 +16,48 @@ struct currency_name { * with euro added. */ static const struct currency_name currency_names[] = { - {"AUD", "$", N_("Australian dollar")}, - {"BGN", "лв", N_("Bulgarian lev")}, - {"BRL", "R$", N_("Brazilian real")}, - {"CAD", "$", N_("Canadian dollar")}, - {"CHF", "Fr", N_("Swiss franc")}, - {"CNY", "元", N_("Chinese yuan renminbi")}, - {"CZK", "Kč", N_("Czech koruna")}, - {"DKK", "kr", N_("Danish krone")}, - {"EEK", "KR", N_("Estonian kroon")}, - {"EUR", "€", N_("Euro")}, - {"GBP", "£", N_("Pound sterling")}, - {"HKD", "$", N_("Hong Kong dollar")}, - {"HRK", "kn", N_("Croatian kuna")}, - {"HUF", "Ft", N_("Hungarian forint")}, - {"IDR", "Rp", N_("Indonesian rupiah")}, - {"INR", "Rs", N_("Indian rupee")}, - {"ISK", "kr", N_("Icelandic krona")}, - {"JPY", "¥", N_("Japanese yen")}, - {"KRW", "₩", N_("South Korean won")}, - {"LTL", "Lt", N_("Lithuanian litas")}, - {"LVL", "Ls", N_("Latvian lats")}, - {"MXN", "$", N_("Mexican peso")}, - {"MYR", "RM", N_("Malaysian ringgit")}, - {"NOK", "kr", N_("Norwegian krone")}, - {"NZD", "$", N_("New Zealand dollar")}, - {"PHP", "₱", N_("Philippine peso")}, - {"PLN", "zł", N_("Polish zloty")}, - {"RON", "L", N_("New Romanian leu")}, - {"RUB", "руб.", N_("Russian rouble")}, - {"SEK", "kr", N_("Swedish krona")}, - {"SGD", "$", N_("Singapore dollar")}, - {"THB", "฿", N_("Thai baht")}, - {"TRY", "TL", N_("New Turkish lira")}, - {"USD", "$", N_("US dollar")}, - {"ZAR", "R", N_("South African rand")}, - {NULL, NULL} + {"AUD", "$", N_("Australian dollar")}, + {"BGN", "лв", N_("Bulgarian lev")}, + {"BRL", "R$", N_("Brazilian real")}, + {"CAD", "$", N_("Canadian dollar")}, + {"CHF", "Fr", N_("Swiss franc")}, + {"CNY", "元", N_("Chinese yuan renminbi")}, + {"CZK", "Kč", N_("Czech koruna")}, + {"DKK", "kr", N_("Danish krone")}, + {"EEK", "KR", N_("Estonian kroon")}, + {"EUR", "€", N_("Euro")}, + {"GBP", "£", N_("Pound sterling")}, + {"HKD", "$", N_("Hong Kong dollar")}, + {"HRK", "kn", N_("Croatian kuna")}, + {"HUF", "Ft", N_("Hungarian forint")}, + {"IDR", "Rp", N_("Indonesian rupiah")}, + {"INR", "Rs", N_("Indian rupee")}, + {"ISK", "kr", N_("Icelandic krona")}, + {"JPY", "¥", N_("Japanese yen")}, + {"KRW", "₩", N_("South Korean won")}, + {"LTL", "Lt", N_("Lithuanian litas")}, + {"LVL", "Ls", N_("Latvian lats")}, + {"MXN", "$", N_("Mexican peso")}, + {"MYR", "RM", N_("Malaysian ringgit")}, + {"NOK", "kr", N_("Norwegian krone")}, + {"NZD", "$", N_("New Zealand dollar")}, + {"PHP", "₱", N_("Philippine peso")}, + {"PLN", "zł", N_("Polish zloty")}, + {"RON", "L", N_("New Romanian leu")}, + {"RUB", "руб.", N_("Russian rouble")}, + {"SEK", "kr", N_("Swedish krona")}, + {"SGD", "$", N_("Singapore dollar")}, + {"THB", "฿", N_("Thai baht")}, + {"TRY", "TL", N_("New Turkish lira")}, + {"USD", "$", N_("US dollar")}, + {"ZAR", "R", N_("South African rand")}, + {NULL, NULL} }; // FIXME: Should indicate when rates are updated to UI /* Converts an amount of money from one currency to another */ -gboolean currency_convert(const MPNumber *from_amount, - const char *source_currency, const char *target_currency, - MPNumber *to_amount); +gboolean currency_convert(const MPNumber* from_amount, const char* source_currency, const char *target_currency, MPNumber* to_amount); /* Frees up all allocated resources */ void currency_free_resources(void); diff --git a/src/financial.h b/src/financial.h index 6aa8f3c..9a6cbe9 100644 --- a/src/financial.h +++ b/src/financial.h @@ -23,19 +23,19 @@ #include "mp.h" #include "math-equation.h" -void do_finc_expression(MathEquation *equation, int function, MPNumber *arg1, MPNumber *arg2, MPNumber *arg3, MPNumber *arg4); +void do_finc_expression(MathEquation* equation, int function, MPNumber* arg1, MPNumber* arg2, MPNumber* arg3, MPNumber* arg4); enum finc_dialogs { - FINC_CTRM_DIALOG, - FINC_DDB_DIALOG, - FINC_FV_DIALOG, - FINC_GPM_DIALOG, - FINC_PMT_DIALOG, - FINC_PV_DIALOG, - FINC_RATE_DIALOG, - FINC_SLN_DIALOG, - FINC_SYD_DIALOG, - FINC_TERM_DIALOG + FINC_CTRM_DIALOG, + FINC_DDB_DIALOG, + FINC_FV_DIALOG, + FINC_GPM_DIALOG, + FINC_PMT_DIALOG, + FINC_PV_DIALOG, + FINC_RATE_DIALOG, + FINC_SLN_DIALOG, + FINC_SYD_DIALOG, + FINC_TERM_DIALOG }; #endif /* FINANCIAL_H */ diff --git a/src/math-buttons.h b/src/math-buttons.h index 7e85e33..711db6f 100644 --- a/src/math-buttons.h +++ b/src/math-buttons.h @@ -29,34 +29,32 @@ G_BEGIN_DECLS typedef struct MathButtonsPrivate MathButtonsPrivate; -typedef struct -{ +typedef struct { GtkVBox parent_instance; - MathButtonsPrivate *priv; + MathButtonsPrivate* priv; } MathButtons; -typedef struct -{ - GtkVBoxClass parent_class; +typedef struct { + GtkVBoxClass parent_class; } MathButtonsClass; typedef enum { - BASIC, - ADVANCED, - FINANCIAL, - PROGRAMMING + BASIC, + ADVANCED, + FINANCIAL, + PROGRAMMING } ButtonMode; GType math_buttons_get_type(void); -MathButtons *math_buttons_new(MathEquation *equation); +MathButtons* math_buttons_new(MathEquation* equation); -void math_buttons_set_mode(MathButtons *buttons, ButtonMode mode); +void math_buttons_set_mode(MathButtons* buttons, ButtonMode mode); -ButtonMode math_buttons_get_mode(MathButtons *buttons); +ButtonMode math_buttons_get_mode(MathButtons* buttons); -void math_buttons_set_programming_base(MathButtons *buttons, gint base); +void math_buttons_set_programming_base(MathButtons* buttons, gint base); -gint math_buttons_get_programming_base(MathButtons *buttons); +gint math_buttons_get_programming_base(MathButtons* buttons); #endif /* MATH_BUTTONS_H */ diff --git a/src/math-display.h b/src/math-display.h index f933d37..7d8718a 100644 --- a/src/math-display.h +++ b/src/math-display.h @@ -30,23 +30,21 @@ G_BEGIN_DECLS typedef struct MathDisplayPrivate MathDisplayPrivate; -typedef struct -{ - GtkVBox parent_instance; - MathDisplayPrivate *priv; +typedef struct { + GtkVBox parent_instance; + MathDisplayPrivate* priv; } MathDisplay; -typedef struct -{ - GtkVBoxClass parent_class; +typedef struct { + GtkVBoxClass parent_class; } MathDisplayClass; GType math_display_get_type(void); -MathDisplay *math_display_new(void); +MathDisplay* math_display_new(void); -MathDisplay *math_display_new_with_equation(MathEquation *equation); +MathDisplay* math_display_new_with_equation(MathEquation* equation); -MathEquation *math_display_get_equation(MathDisplay *display); +MathEquation* math_display_get_equation(MathDisplay* display); #endif /* MATH_DISPLAY_H */ diff --git a/src/math-equation.h b/src/math-equation.h index e93137d..164fec5 100644 --- a/src/math-equation.h +++ b/src/math-equation.h @@ -31,103 +31,101 @@ G_BEGIN_DECLS typedef struct MathEquationPrivate MathEquationPrivate; -typedef struct -{ - GtkTextBuffer parent_instance; - MathEquationPrivate *priv; +typedef struct { + GtkTextBuffer parent_instance; + MathEquationPrivate* priv; } MathEquation; -typedef struct -{ - GtkTextBufferClass parent_class; +typedef struct { + GtkTextBufferClass parent_class; } MathEquationClass; /* Number display mode. */ typedef enum { - FIX, - SCI, - ENG + FIX, + SCI, + ENG } DisplayFormat; typedef enum { - NORMAL, - SUPERSCRIPT, - SUBSCRIPT + NORMAL, + SUPERSCRIPT, + SUBSCRIPT } NumberMode; GType math_equation_get_type(void); -MathEquation *math_equation_new(void); +MathEquation* math_equation_new(void); -MathVariables *math_equation_get_variables(MathEquation *equation); +MathVariables* math_equation_get_variables(MathEquation* equation); -const gchar *math_equation_get_digit_text(MathEquation *equation, guint digit); -const gchar *math_equation_get_numeric_point_text(MathEquation *equation); -const gchar *math_equation_get_thousands_separator_text(MathEquation *equation); +const gchar* math_equation_get_digit_text(MathEquation* equation, guint digit); +const gchar* math_equation_get_numeric_point_text(MathEquation* equation); +const gchar* math_equation_get_thousands_separator_text(MathEquation* equation); -void math_equation_set_status(MathEquation *equation, const gchar *status); -const gchar *math_equation_get_status(MathEquation *equation); +void math_equation_set_status(MathEquation* equation, const gchar* status); +const gchar* math_equation_get_status(MathEquation* equation); -gboolean math_equation_is_empty(MathEquation *equation); -gboolean math_equation_is_result(MathEquation *equation); -gchar *math_equation_get_display(MathEquation *equation); -gchar *math_equation_get_equation(MathEquation *equation); -gboolean math_equation_get_number(MathEquation *equation, MPNumber *z); +gboolean math_equation_is_empty(MathEquation* equation); +gboolean math_equation_is_result(MathEquation* equation); +gchar* math_equation_get_display(MathEquation* equation); +gchar* math_equation_get_equation(MathEquation* equation); +gboolean math_equation_get_number(MathEquation* equation, MPNumber* z); -void math_equation_set_number_mode(MathEquation *equation, NumberMode mode); -NumberMode math_equation_get_number_mode(MathEquation *equation); +void math_equation_set_number_mode(MathEquation* equation, NumberMode mode); +NumberMode math_equation_get_number_mode(MathEquation* equation); -void math_equation_set_accuracy(MathEquation *equation, gint accuracy); -gint math_equation_get_accuracy(MathEquation *equation); +void math_equation_set_accuracy(MathEquation* equation, gint accuracy); +gint math_equation_get_accuracy(MathEquation* equation); -void math_equation_set_show_thousands_separators(MathEquation *equation, gboolean visible); -gboolean math_equation_get_show_thousands_separators(MathEquation *equation); +void math_equation_set_show_thousands_separators(MathEquation* equation, gboolean visible); +gboolean math_equation_get_show_thousands_separators(MathEquation* equation); -void math_equation_set_show_trailing_zeroes(MathEquation *equation, gboolean visible); -gboolean math_equation_get_show_trailing_zeroes(MathEquation *equation); +void math_equation_set_show_trailing_zeroes(MathEquation* equation, gboolean visible); +gboolean math_equation_get_show_trailing_zeroes(MathEquation* equation); -void math_equation_set_number_format(MathEquation *equation, DisplayFormat format); -DisplayFormat math_equation_get_number_format(MathEquation *equation); +void math_equation_set_number_format(MathEquation* equation, DisplayFormat format); +DisplayFormat math_equation_get_number_format(MathEquation* equation); -void math_equation_set_base(MathEquation *equation, gint base); -gint math_equation_get_base(MathEquation *equation); +void math_equation_set_base(MathEquation* equation, gint base); +gint math_equation_get_base(MathEquation* equation); -void math_equation_set_word_size(MathEquation *equation, gint word_size); -gint math_equation_get_word_size(MathEquation *equation); +void math_equation_set_word_size(MathEquation* equation, gint word_size); +gint math_equation_get_word_size(MathEquation* equation); -void math_equation_set_angle_units(MathEquation *equation, MPAngleUnit angle_unit); -MPAngleUnit math_equation_get_angle_units(MathEquation *equation); +void math_equation_set_angle_units(MathEquation* equation, MPAngleUnit angle_unit); +MPAngleUnit math_equation_get_angle_units(MathEquation* equation); -void math_equation_set_source_currency(MathEquation *equation, const gchar *currency); -const gchar *math_equation_get_source_currency(MathEquation *equation); +void math_equation_set_source_currency(MathEquation* equation, const gchar* currency); +const gchar* math_equation_get_source_currency(MathEquation* equation); -void math_equation_set_target_currency(MathEquation *equation, const gchar *currency); -const gchar *math_equation_get_target_currency(MathEquation *equation); +void math_equation_set_target_currency(MathEquation* equation, const gchar* currency); +const gchar* math_equation_get_target_currency(MathEquation* equation); -const MPNumber *math_equation_get_answer(MathEquation *equation); +const MPNumber* math_equation_get_answer(MathEquation* equation); -void math_equation_copy(MathEquation *equation); -void math_equation_paste(MathEquation *equation); -void math_equation_undo(MathEquation *equation); -void math_equation_redo(MathEquation *equation); -void math_equation_store(MathEquation *equation, const gchar *name); -void math_equation_recall(MathEquation *equation, const gchar *name); -void math_equation_set(MathEquation *equation, const gchar *text); -void math_equation_set_number(MathEquation *equation, const MPNumber *x); -void math_equation_insert(MathEquation *equation, const gchar *text); -void math_equation_insert_digit(MathEquation *equation, guint digit); -void math_equation_insert_numeric_point(MathEquation *equation); -void math_equation_insert_number(MathEquation *equation, const MPNumber *x); -void math_equation_insert_subtract(MathEquation *equation); -void math_equation_insert_exponent(MathEquation *equation); -void math_equation_solve(MathEquation *equation); -void math_equation_factorize(MathEquation *equation); -void math_equation_delete(MathEquation *equation); -void math_equation_backspace(MathEquation *equation); -void math_equation_clear(MathEquation *equation); -void math_equation_shift(MathEquation *equation, gint count); -void math_equation_toggle_bit(MathEquation *equation, guint bit); +void math_equation_copy(MathEquation* equation); +void math_equation_paste(MathEquation* equation); +void math_equation_undo(MathEquation* equation); +void math_equation_redo(MathEquation* equation); +void math_equation_store(MathEquation* equation, const gchar* name); +void math_equation_recall(MathEquation* equation, const gchar* name); +void math_equation_set(MathEquation* equation, const gchar* text); +void math_equation_set_number(MathEquation* equation, const MPNumber* x); +void math_equation_insert(MathEquation* equation, const gchar* text); +void math_equation_insert_digit(MathEquation* equation, guint digit); +void math_equation_insert_numeric_point(MathEquation* equation); +void math_equation_insert_number(MathEquation* equation, const MPNumber* x); +void math_equation_insert_subtract(MathEquation* equation); +void math_equation_insert_exponent(MathEquation* equation); +void math_equation_solve(MathEquation* equation); +void math_equation_factorize(MathEquation* equation); +void math_equation_delete(MathEquation* equation); +void math_equation_backspace(MathEquation* equation); +void math_equation_clear(MathEquation* equation); +void math_equation_shift(MathEquation* equation, gint count); +void math_equation_toggle_bit(MathEquation* equation, guint bit); //FIXME: Obsolete -void display_make_number(MathEquation *equation, char *target, int target_len, const MPNumber *x); +void display_make_number(MathEquation* equation, char* target, int target_len, const MPNumber* x); #endif /* MATH_EQUATION_H */ diff --git a/src/math-preferences.h b/src/math-preferences.h index 487a68e..e7767f6 100644 --- a/src/math-preferences.h +++ b/src/math-preferences.h @@ -29,19 +29,17 @@ G_BEGIN_DECLS typedef struct MathPreferencesDialogPrivate MathPreferencesDialogPrivate; -typedef struct -{ - GtkDialog parent_instance; - MathPreferencesDialogPrivate *priv; +typedef struct { + GtkDialog parent_instance; + MathPreferencesDialogPrivate* priv; } MathPreferencesDialog; -typedef struct -{ - GtkDialogClass parent_class; +typedef struct { + GtkDialogClass parent_class; } MathPreferencesDialogClass; GType math_preferences_get_type(void); -MathPreferencesDialog *math_preferences_dialog_new(MathEquation *equation); +MathPreferencesDialog* math_preferences_dialog_new(MathEquation* equation); #endif /* MATH_PREFERENCES_H */ diff --git a/src/math-variables.h b/src/math-variables.h index b2317b0..dab20a0 100644 --- a/src/math-variables.h +++ b/src/math-variables.h @@ -28,25 +28,23 @@ G_BEGIN_DECLS typedef struct MathVariablesPrivate MathVariablesPrivate; -typedef struct -{ - GObject parent_instance; - MathVariablesPrivate *priv; +typedef struct { + GObject parent_instance; + MathVariablesPrivate* priv; } MathVariables; -typedef struct -{ - GObjectClass parent_class; +typedef struct { + GObjectClass parent_class; } MathVariablesClass; GType math_variables_get_type(void); -MathVariables *math_variables_new(void); +MathVariables* math_variables_new(void); -gchar **math_variables_get_names(MathVariables *variables); +gchar** math_variables_get_names(MathVariables* variables); -void math_variables_set_value(MathVariables *variables, const char *name, const MPNumber *value); +void math_variables_set_value(MathVariables* variables, const char* name, const MPNumber* value); -MPNumber *math_variables_get_value(MathVariables *variables, const char *name); +MPNumber* math_variables_get_value(MathVariables* variables, const char* name); #endif /* MATH_VARIABLES_H */ diff --git a/src/math-window.c b/src/math-window.c index 2fc010e..d7f0912 100644 --- a/src/math-window.c +++ b/src/math-window.c @@ -29,499 +29,463 @@ enum { PROP_EQUATION }; -struct MathWindowPrivate -{ - GtkWidget *menu_bar; - MathEquation *equation; - MathDisplay *display; - MathButtons *buttons; - MathPreferencesDialog *preferences_dialog; - gboolean right_aligned; - GtkWidget *mode_basic_menu_item, *mode_advanced_menu_item, *mode_financial_menu_item, *mode_programming_menu_item; +struct MathWindowPrivate { + GtkWidget* menu_bar; + MathEquation* equation; + MathDisplay* display; + MathButtons* buttons; + MathPreferencesDialog* preferences_dialog; + gboolean right_aligned; + GtkWidget* mode_basic_menu_item; + GtkWidget* mode_advanced_menu_item; + GtkWidget* mode_financial_menu_item; + GtkWidget* mode_programming_menu_item; }; -G_DEFINE_TYPE (MathWindow, math_window, GTK_TYPE_WINDOW); +G_DEFINE_TYPE(MathWindow, math_window, GTK_TYPE_WINDOW); enum { - QUIT, - LAST_SIGNAL + QUIT, + LAST_SIGNAL }; -static guint signals[LAST_SIGNAL] = { 0, }; + +static guint signals[LAST_SIGNAL] = {0, }; -MathWindow * -math_window_new(MathEquation *equation) +MathWindow* math_window_new(MathEquation* equation) { - return g_object_new (math_window_get_type(), "equation", equation, NULL); + return g_object_new(math_window_get_type(), "equation", equation, NULL); } -GtkWidget * -math_window_get_menu_bar(MathWindow *window) +GtkWidget* math_window_get_menu_bar(MathWindow* window) { - return window->priv->menu_bar; + return window->priv->menu_bar; } -MathEquation *math_window_get_equation(MathWindow *window) +MathEquation* math_window_get_equation(MathWindow* window) { - return window->priv->equation; + return window->priv->equation; } -MathDisplay * -math_window_get_display(MathWindow *window) +MathDisplay* math_window_get_display(MathWindow* window) { - return window->priv->display; + return window->priv->display; } -MathButtons * -math_window_get_buttons(MathWindow *window) +MathButtons* math_window_get_buttons(MathWindow* window) { - return window->priv->buttons; + return window->priv->buttons; } -void -math_window_critical_error(MathWindow *window, const gchar *title, const gchar *contents) +void math_window_critical_error(MathWindow* window, const gchar* title, const gchar* contents) { - GtkWidget *dialog; + GtkWidget* dialog; - dialog = gtk_message_dialog_new(NULL, 0, - GTK_MESSAGE_ERROR, - GTK_BUTTONS_NONE, - "%s", title); - gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), - "%s", contents); - gtk_dialog_add_buttons(GTK_DIALOG(dialog), GTK_STOCK_QUIT, GTK_RESPONSE_ACCEPT, NULL); + dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_NONE, "%s", title); + gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s", contents); + gtk_dialog_add_buttons(GTK_DIALOG(dialog), GTK_STOCK_QUIT, GTK_RESPONSE_ACCEPT, NULL); - gtk_dialog_run(GTK_DIALOG(dialog)); + gtk_dialog_run(GTK_DIALOG(dialog)); - g_signal_emit(window, signals[QUIT], 0); + g_signal_emit(window, signals[QUIT], 0); } -static void -copy_cb(GtkWidget *widget, MathWindow *window) +static void copy_cb(GtkWidget* widget, MathWindow* window) { - math_equation_copy (window->priv->equation); + math_equation_copy(window->priv->equation); } -static void -paste_cb(GtkWidget *widget, MathWindow *window) +static void paste_cb(GtkWidget* widget, MathWindow* window) { - math_equation_paste (window->priv->equation); + math_equation_paste(window->priv->equation); } -static void -undo_cb(GtkWidget *widget, MathWindow *window) +static void undo_cb(GtkWidget* widget, MathWindow* window) { - math_equation_undo(window->priv->equation); + math_equation_undo(window->priv->equation); } -static void -redo_cb(GtkWidget *widget, MathWindow *window) +static void redo_cb(GtkWidget* widget, MathWindow* window) { - math_equation_redo(window->priv->equation); + math_equation_redo(window->priv->equation); } -static void -mode_changed_cb(GtkWidget *menu, MathWindow *window) +static void mode_changed_cb(GtkWidget* menu, MathWindow* window) { - int mode; + int mode; - if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu))) - return; + if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu))) + { + return; + } - mode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menu), "calcmode")); - math_buttons_set_mode(window->priv->buttons, mode); + mode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menu), "calcmode")); + math_buttons_set_mode(window->priv->buttons, mode); } -static void -show_preferences_cb(GtkMenuItem *menu, MathWindow *window) +static void show_preferences_cb(GtkMenuItem* menu, MathWindow* window) { - if (!window->priv->preferences_dialog) { - window->priv->preferences_dialog = math_preferences_dialog_new(window->priv->equation); - gtk_window_set_transient_for(GTK_WINDOW(window->priv->preferences_dialog), GTK_WINDOW(window)); - } - gtk_window_present(GTK_WINDOW(window->priv->preferences_dialog)); + if (!window->priv->preferences_dialog) + { + window->priv->preferences_dialog = math_preferences_dialog_new(window->priv->equation); + gtk_window_set_transient_for(GTK_WINDOW(window->priv->preferences_dialog), GTK_WINDOW(window)); + } + + gtk_window_present(GTK_WINDOW(window->priv->preferences_dialog)); } -static void -help_cb(GtkWidget *widget, MathWindow *window) +static void help_cb(GtkWidget* widget, MathWindow* window) { - GdkScreen *screen; - GError *error = NULL; - - screen = gtk_widget_get_screen (GTK_WIDGET (window)); - gtk_show_uri (screen, "ghelp:mate-calc", gtk_get_current_event_time (), &error); - - if (error != NULL) - { - GtkWidget *d; - /* Translators: Error message displayed when unable to launch help browser */ - const char *message = _("Unable to open help file"); - - d = gtk_message_dialog_new (GTK_WINDOW (window), - GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, - GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, - "%s", message); - gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (d), - "%s", error->message); - g_signal_connect (d, "response", G_CALLBACK (gtk_widget_destroy), NULL); - gtk_window_present (GTK_WINDOW (d)); - - g_error_free (error); - } + GdkScreen* screen; + GError* error = NULL; + + screen = gtk_widget_get_screen(GTK_WIDGET(window)); + gtk_show_uri(screen, "ghelp:mate-calc", gtk_get_current_event_time(), &error); + + if (error != NULL) + { + GtkWidget* d; + /* Translators: Error message displayed when unable to launch help browser */ + const char* message = _("Unable to open help file"); + + d = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", message); + gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(d), "%s", error->message); + g_signal_connect(d, "response", G_CALLBACK(gtk_widget_destroy), NULL); + gtk_window_present(GTK_WINDOW (d)); + + g_error_free(error); + } } -static void -about_cb(GtkWidget *widget, MathWindow *window) +static void about_cb(GtkWidget* widget, MathWindow* window) { - const gchar *authors[] = { - "Rich Burridge ", - "Robert Ancell ", - "Klaus Niederkrüger ", - NULL - }; - const gchar *documenters[] = { - "Sun Microsystems", - NULL - }; - - /* The translator credits. Please translate this with your name(s). */ - const gchar *translator_credits = _("translator-credits"); - - /* The license this software is under (GPL2+) */ - char *license = _("mate-calc is free software; you can redistribute it and/or modify\n" - "it under the terms of the GNU General Public License as published by\n" - "the Free Software Foundation; either version 2 of the License, or\n" - "(at your option) any later version.\n" - "\n" - "mate-calc is distributed in the hope that it will be useful,\n" - "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" - "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" - "GNU General Public License for more details.\n" - "\n" - "You should have received a copy of the GNU General Public License\n" - "along with mate-calc; if not, write to the Free Software Foundation, Inc.,\n" - "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA"); - - gtk_show_about_dialog(GTK_WINDOW(window), - "name", - /* Program name in the about dialog */ - _("mate-calc"), - "version", VERSION, - "copyright", - /* Copyright notice in the about dialog */ - _("\xc2\xa9 1986–2010 The mate-calc authors"), - "license", license, - "comments", - /* Short description in the about dialog */ - _("Calculator with financial and scientific modes."), - "authors", authors, - "documenters", documenters, - "translator_credits", translator_credits, - "logo-icon-name", "accessories-calculator", - NULL); + char* authors[] = { + "Rich Burridge ", + "Robert Ancell ", + "Klaus Niederkrüger ", + NULL + }; + + char* documenters[] = { + "Sun Microsystems", + NULL + }; + + /* The translator credits. Please translate this with your name(s). */ + char* translator_credits = _("translator-credits"); + + char copyright[] = \ + "Copyright \xc2\xa9 1986–2010 The mate-calc authors"; + + /* The license this software is under (GPL2+) */ + char* license = _("mate-calc is free software; you can redistribute it and/or modify\n" + "it under the terms of the GNU General Public License as published by\n" + "the Free Software Foundation; either version 2 of the License, or\n" + "(at your option) any later version.\n" + "\n" + "mate-calc is distributed in the hope that it will be useful,\n" + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + "GNU General Public License for more details.\n" + "\n" + "You should have received a copy of the GNU General Public License\n" + "along with mate-calc; if not, write to the Free Software Foundation, Inc.,\n" + "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA"); + + gtk_show_about_dialog(GTK_WINDOW(window), + "name", _("mate-calc"), + "version", VERSION, + "copyright", copyright, + "license", license, + "comments", _("Calculator with financial and scientific modes."), + "authors", authors, + "documenters", documenters, + "translator_credits", translator_credits, + "logo-icon-name", "accessories-calculator", + NULL); } -static void -quit_cb(GtkWidget *widget, MathWindow *window) +static void quit_cb(GtkWidget* widget, MathWindow* window) { - g_signal_emit(window, signals[QUIT], 0); + g_signal_emit(window, signals[QUIT], 0); } -static gboolean -key_press_cb(MathWindow *window, GdkEventKey *event) +static gboolean key_press_cb(MathWindow* window, GdkEventKey* event) { - gboolean result; - g_signal_emit_by_name(window->priv->display, "key-press-event", event, &result); - return result; + gboolean result; + + g_signal_emit_by_name(window->priv->display, "key-press-event", event, &result); + + return result; } -static void -delete_cb(MathWindow *window, GdkEvent *event) +static void delete_cb(MathWindow* window, GdkEvent* event) { - g_signal_emit(window, signals[QUIT], 0); + g_signal_emit(window, signals[QUIT], 0); } -static void -scroll_changed_cb(GtkAdjustment *adjustment, MathWindow *window) +static void scroll_changed_cb(GtkAdjustment* adjustment, MathWindow* window) { - if (window->priv->right_aligned) - gtk_adjustment_set_value(adjustment, gtk_adjustment_get_upper(adjustment) - gtk_adjustment_get_page_size(adjustment)); + if (window->priv->right_aligned) + { + gtk_adjustment_set_value(adjustment, gtk_adjustment_get_upper(adjustment) - gtk_adjustment_get_page_size(adjustment)); + } } -static void -scroll_value_changed_cb(GtkAdjustment *adjustment, MathWindow *window) +static void scroll_value_changed_cb(GtkAdjustment* adjustment, MathWindow* window) { - if (gtk_adjustment_get_value(adjustment) == gtk_adjustment_get_upper(adjustment) - gtk_adjustment_get_page_size(adjustment)) - window->priv->right_aligned = TRUE; - else - window->priv->right_aligned = FALSE; + if (gtk_adjustment_get_value(adjustment) == gtk_adjustment_get_upper(adjustment) - gtk_adjustment_get_page_size(adjustment)) + { + window->priv->right_aligned = TRUE; + } + else + { + window->priv->right_aligned = FALSE; + } } -static void -button_mode_changed_cb(MathButtons *buttons, GParamSpec *spec, MathWindow *window) +static void button_mode_changed_cb(MathButtons* buttons, GParamSpec* spec, MathWindow* window) { - GtkWidget *menu; - - switch(math_buttons_get_mode(buttons)) - { - default: - case BASIC: - menu = window->priv->mode_basic_menu_item; - //FIXME: Should it revert to decimal mode? math_equation_set_number_format(window->priv->equation, DEC); - break; - - case ADVANCED: - menu = window->priv->mode_advanced_menu_item; - break; - - case FINANCIAL: - menu = window->priv->mode_financial_menu_item; - break; - - case PROGRAMMING: - menu = window->priv->mode_programming_menu_item; - break; - } - gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), TRUE); + GtkWidget* menu; + + switch(math_buttons_get_mode(buttons)) + { + default: + case BASIC: + menu = window->priv->mode_basic_menu_item; + //FIXME: Should it revert to decimal mode? math_equation_set_number_format(window->priv->equation, DEC); + break; + + case ADVANCED: + menu = window->priv->mode_advanced_menu_item; + break; + + case FINANCIAL: + menu = window->priv->mode_financial_menu_item; + break; + + case PROGRAMMING: + menu = window->priv->mode_programming_menu_item; + break; + } + + gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), TRUE); } -static GtkWidget * -add_menu(GtkWidget *menu_bar, const gchar *name) +static GtkWidget* add_menu(GtkWidget* menu_bar, const gchar* name) { - GtkWidget *menu_item, *menu; + GtkWidget* menu_item; + GtkWidget* menu; - menu_item = gtk_menu_item_new_with_mnemonic(name); - gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), menu_item); - gtk_widget_show(menu_item); - menu = gtk_menu_new(); - gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), menu); + menu_item = gtk_menu_item_new_with_mnemonic(name); + gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), menu_item); + gtk_widget_show(menu_item); + menu = gtk_menu_new(); + gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), menu); - return menu; + return menu; } -static GtkWidget * -add_menu_item(GtkWidget *menu, GtkWidget *menu_item, GCallback callback, gpointer callback_data) +static GtkWidget* add_menu_item(GtkWidget* menu, GtkWidget* menu_item, GCallback callback, gpointer callback_data) { - gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item); - gtk_widget_show(menu_item); - if (callback) - g_signal_connect(G_OBJECT(menu_item), "activate", callback, callback_data); - return menu_item; + gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item); + gtk_widget_show(menu_item); + + if (callback) + { + g_signal_connect(G_OBJECT(menu_item), "activate", callback, callback_data); + } + + return menu_item; } -static GtkWidget * -radio_menu_item_new(GSList **group, const gchar *name) +static GtkWidget* radio_menu_item_new(GSList** group, const gchar* name) { - GtkWidget *menu_item; - menu_item = gtk_radio_menu_item_new_with_mnemonic(*group, name); + GtkWidget* menu_item = gtk_radio_menu_item_new_with_mnemonic(*group, name); + *group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(menu_item)); + return menu_item; } -static void -create_menu(MathWindow *window) +static void create_menu(MathWindow* window) { - GtkAccelGroup *accel_group; - GtkWidget *menu, *menu_item; - GSList *group = NULL; - - accel_group = gtk_accel_group_new(); - gtk_window_add_accel_group(GTK_WINDOW(window), accel_group); - - /* Calculator menu */ - #define CALCULATOR_MENU_LABEL _("_Calculator") - /* Mode menu */ - #define MODE_MENU_LABEL _("_Mode") - /* Help menu label */ - #define HELP_MENU_LABEL _("_Help") - /* Basic menu label */ - #define MODE_BASIC_LABEL _("_Basic") - /* Advanced menu label */ - #define MODE_ADVANCED_LABEL _("_Advanced") - /* Financial menu label */ - #define MODE_FINANCIAL_LABEL _("_Financial") - /* Programming menu label */ - #define MODE_PROGRAMMING_LABEL _("_Programming") - /* Help>Contents menu label */ - #define HELP_CONTENTS_LABEL _("_Contents") - - menu = add_menu(window->priv->menu_bar, CALCULATOR_MENU_LABEL); - add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY, accel_group), G_CALLBACK(copy_cb), window); - add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE, accel_group), G_CALLBACK(paste_cb), window); - menu_item = add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_UNDO, accel_group), G_CALLBACK(undo_cb), window); - gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_z, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); - menu_item = add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_REDO, accel_group), G_CALLBACK(redo_cb), window); - gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_z, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE); - add_menu_item(menu, gtk_separator_menu_item_new(), NULL, NULL); - add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, accel_group), G_CALLBACK(show_preferences_cb), window); - add_menu_item(menu, gtk_separator_menu_item_new(), NULL, NULL); - menu_item = add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, accel_group), G_CALLBACK(quit_cb), window); - gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_w, GDK_CONTROL_MASK, 0); - - menu = add_menu(window->priv->menu_bar, MODE_MENU_LABEL); - window->priv->mode_basic_menu_item = add_menu_item(menu, radio_menu_item_new(&group, MODE_BASIC_LABEL), G_CALLBACK(mode_changed_cb), window); - g_object_set_data(G_OBJECT(window->priv->mode_basic_menu_item), "calcmode", GINT_TO_POINTER(BASIC)); - window->priv->mode_advanced_menu_item = add_menu_item(menu, radio_menu_item_new(&group, MODE_ADVANCED_LABEL), G_CALLBACK(mode_changed_cb), window); - g_object_set_data(G_OBJECT(window->priv->mode_advanced_menu_item), "calcmode", GINT_TO_POINTER(ADVANCED)); - window->priv->mode_financial_menu_item = add_menu_item(menu, radio_menu_item_new(&group, MODE_FINANCIAL_LABEL), G_CALLBACK(mode_changed_cb), window); - g_object_set_data(G_OBJECT(window->priv->mode_financial_menu_item), "calcmode", GINT_TO_POINTER(FINANCIAL)); - window->priv->mode_programming_menu_item = add_menu_item(menu, radio_menu_item_new(&group, MODE_PROGRAMMING_LABEL), G_CALLBACK(mode_changed_cb), window); - g_object_set_data(G_OBJECT(window->priv->mode_programming_menu_item), "calcmode", GINT_TO_POINTER(PROGRAMMING)); - - menu = add_menu(window->priv->menu_bar, HELP_MENU_LABEL); - menu_item = add_menu_item(menu, gtk_menu_item_new_with_mnemonic(HELP_CONTENTS_LABEL), G_CALLBACK(help_cb), window); - gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_F1, 0, GTK_ACCEL_VISIBLE); - add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_ABOUT, accel_group), G_CALLBACK(about_cb), window); + GtkAccelGroup* accel_group; + GtkWidget* menu; + GtkWidget* menu_item; + GSList* group = NULL; + + accel_group = gtk_accel_group_new(); + gtk_window_add_accel_group(GTK_WINDOW(window), accel_group); + + /* Calculator menu */ + #define CALCULATOR_MENU_LABEL _("_Calculator") + /* Mode menu */ + #define MODE_MENU_LABEL _("_Mode") + /* Help menu label */ + #define HELP_MENU_LABEL _("_Help") + /* Basic menu label */ + #define MODE_BASIC_LABEL _("_Basic") + /* Advanced menu label */ + #define MODE_ADVANCED_LABEL _("_Advanced") + /* Financial menu label */ + #define MODE_FINANCIAL_LABEL _("_Financial") + /* Programming menu label */ + #define MODE_PROGRAMMING_LABEL _("_Programming") + /* Help>Contents menu label */ + #define HELP_CONTENTS_LABEL _("_Contents") + + menu = add_menu(window->priv->menu_bar, CALCULATOR_MENU_LABEL); + add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY, accel_group), G_CALLBACK(copy_cb), window); + add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE, accel_group), G_CALLBACK(paste_cb), window); + menu_item = add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_UNDO, accel_group), G_CALLBACK(undo_cb), window); + gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_z, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); + menu_item = add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_REDO, accel_group), G_CALLBACK(redo_cb), window); + gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_z, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE); + add_menu_item(menu, gtk_separator_menu_item_new(), NULL, NULL); + add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, accel_group), G_CALLBACK(show_preferences_cb), window); + add_menu_item(menu, gtk_separator_menu_item_new(), NULL, NULL); + menu_item = add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, accel_group), G_CALLBACK(quit_cb), window); + gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_w, GDK_CONTROL_MASK, 0); + + menu = add_menu(window->priv->menu_bar, MODE_MENU_LABEL); + window->priv->mode_basic_menu_item = add_menu_item(menu, radio_menu_item_new(&group, MODE_BASIC_LABEL), G_CALLBACK(mode_changed_cb), window); + g_object_set_data(G_OBJECT(window->priv->mode_basic_menu_item), "calcmode", GINT_TO_POINTER(BASIC)); + window->priv->mode_advanced_menu_item = add_menu_item(menu, radio_menu_item_new(&group, MODE_ADVANCED_LABEL), G_CALLBACK(mode_changed_cb), window); + g_object_set_data(G_OBJECT(window->priv->mode_advanced_menu_item), "calcmode", GINT_TO_POINTER(ADVANCED)); + window->priv->mode_financial_menu_item = add_menu_item(menu, radio_menu_item_new(&group, MODE_FINANCIAL_LABEL), G_CALLBACK(mode_changed_cb), window); + g_object_set_data(G_OBJECT(window->priv->mode_financial_menu_item), "calcmode", GINT_TO_POINTER(FINANCIAL)); + window->priv->mode_programming_menu_item = add_menu_item(menu, radio_menu_item_new(&group, MODE_PROGRAMMING_LABEL), G_CALLBACK(mode_changed_cb), window); + g_object_set_data(G_OBJECT(window->priv->mode_programming_menu_item), "calcmode", GINT_TO_POINTER(PROGRAMMING)); + + menu = add_menu(window->priv->menu_bar, HELP_MENU_LABEL); + menu_item = add_menu_item(menu, gtk_menu_item_new_with_mnemonic(HELP_CONTENTS_LABEL), G_CALLBACK(help_cb), window); + gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_F1, 0, GTK_ACCEL_VISIBLE); + add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_ABOUT, accel_group), G_CALLBACK(about_cb), window); } -static void -create_gui(MathWindow *window) +static void create_gui(MathWindow* window) { - GtkWidget *main_vbox, *vbox; - GtkWidget *scrolled_window; - - main_vbox = gtk_vbox_new(FALSE, 0); - gtk_container_add(GTK_CONTAINER(window), main_vbox); - gtk_widget_show(main_vbox); - - window->priv->menu_bar = gtk_menu_bar_new(); - gtk_box_pack_start(GTK_BOX(main_vbox), window->priv->menu_bar, TRUE, TRUE, 0); - gtk_widget_show(window->priv->menu_bar); - - create_menu(window); - - vbox = gtk_vbox_new(FALSE, 6); - gtk_container_set_border_width(GTK_CONTAINER(vbox), 6); - gtk_box_pack_start(GTK_BOX(main_vbox), vbox, TRUE, TRUE, 0); - gtk_widget_show(vbox); - - scrolled_window = gtk_scrolled_window_new(NULL, NULL); - gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_NEVER); - gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN); - gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(scrolled_window), TRUE, TRUE, 0); - g_signal_connect(gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(scrolled_window)), "changed", G_CALLBACK(scroll_changed_cb), window); - g_signal_connect(gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(scrolled_window)), "value-changed", G_CALLBACK(scroll_value_changed_cb), window); - window->priv->right_aligned = TRUE; - gtk_widget_show(scrolled_window); - - window->priv->display = math_display_new_with_equation(window->priv->equation); - gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window), GTK_WIDGET(window->priv->display)); - gtk_widget_show(GTK_WIDGET(window->priv->display)); - - window->priv->buttons = math_buttons_new(window->priv->equation); - g_signal_connect(window->priv->buttons, "notify::mode", G_CALLBACK(button_mode_changed_cb), window); - button_mode_changed_cb(window->priv->buttons, NULL, window); - gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(window->priv->buttons), TRUE, TRUE, 0); - gtk_widget_show(GTK_WIDGET(window->priv->buttons)); + GtkWidget* main_vbox; + GtkWidget* vbox; + GtkWidget* scrolled_window; + + main_vbox = gtk_vbox_new(FALSE, 0); + gtk_container_add(GTK_CONTAINER(window), main_vbox); + gtk_widget_show(main_vbox); + + window->priv->menu_bar = gtk_menu_bar_new(); + gtk_box_pack_start(GTK_BOX(main_vbox), window->priv->menu_bar, TRUE, TRUE, 0); + gtk_widget_show(window->priv->menu_bar); + + create_menu(window); + + vbox = gtk_vbox_new(FALSE, 6); + gtk_container_set_border_width(GTK_CONTAINER(vbox), 6); + gtk_box_pack_start(GTK_BOX(main_vbox), vbox, TRUE, TRUE, 0); + gtk_widget_show(vbox); + + scrolled_window = gtk_scrolled_window_new(NULL, NULL); + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_NEVER); + gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN); + gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(scrolled_window), TRUE, TRUE, 0); + g_signal_connect(gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(scrolled_window)), "changed", G_CALLBACK(scroll_changed_cb), window); + g_signal_connect(gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(scrolled_window)), "value-changed", G_CALLBACK(scroll_value_changed_cb), window); + window->priv->right_aligned = TRUE; + gtk_widget_show(scrolled_window); + + window->priv->display = math_display_new_with_equation(window->priv->equation); + gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window), GTK_WIDGET(window->priv->display)); + gtk_widget_show(GTK_WIDGET(window->priv->display)); + + window->priv->buttons = math_buttons_new(window->priv->equation); + g_signal_connect(window->priv->buttons, "notify::mode", G_CALLBACK(button_mode_changed_cb), window); + button_mode_changed_cb(window->priv->buttons, NULL, window); + gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(window->priv->buttons), TRUE, TRUE, 0); + gtk_widget_show(GTK_WIDGET(window->priv->buttons)); } -static void -math_window_set_property(GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) +static void math_window_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec) { - MathWindow *self; - - self = MATH_WINDOW (object); - - switch (prop_id) { - case PROP_EQUATION: - self->priv->equation = g_value_get_object (value); - create_gui(self); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } + MathWindow* self = MATH_WINDOW(object); + + switch (prop_id) + { + case PROP_EQUATION: + self->priv->equation = g_value_get_object(value); + create_gui(self); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } } -static void -math_window_get_property(GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) +static void math_window_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec) { - MathWindow *self; - - self = MATH_WINDOW (object); - - switch (prop_id) { - case PROP_EQUATION: - g_value_set_object (value, self->priv->equation); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } + MathWindow* self = MATH_WINDOW(object); + + switch (prop_id) + { + case PROP_EQUATION: + g_value_set_object (value, self->priv->equation); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } } -static void -math_window_class_init (MathWindowClass *klass) +static void math_window_class_init(MathWindowClass* klass) { - GObjectClass *object_class = G_OBJECT_CLASS (klass); - - object_class->get_property = math_window_get_property; - object_class->set_property = math_window_set_property; - - g_type_class_add_private (klass, sizeof (MathWindowPrivate)); - - g_object_class_install_property(object_class, - PROP_EQUATION, - g_param_spec_object("equation", - "equation", - "Equation being calculated", - math_equation_get_type(), - G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - signals[QUIT] = - g_signal_new ("quit", - G_TYPE_FROM_CLASS (klass), - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (MathWindowClass, quit), - NULL, NULL, - g_cclosure_marshal_VOID__VOID, - G_TYPE_NONE, 0); + GObjectClass* object_class = G_OBJECT_CLASS(klass); + + object_class->get_property = math_window_get_property; + object_class->set_property = math_window_set_property; + + g_type_class_add_private(klass, sizeof(MathWindowPrivate)); + + g_object_class_install_property(object_class, PROP_EQUATION, g_param_spec_object("equation", "equation", "Equation being calculated", math_equation_get_type(), G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); + + signals[QUIT] = g_signal_new("quit", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET(MathWindowClass, quit), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); } -static void -math_window_init(MathWindow *window) +static void math_window_init(MathWindow* window) { - window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, math_window_get_type(), MathWindowPrivate); - gtk_window_set_title(GTK_WINDOW(window), - /* Title of main window */ - _("Calculator")); - gtk_window_set_icon_name(GTK_WINDOW(window), "accessories-calculator"); - gtk_window_set_role(GTK_WINDOW(window), "mate-calc"); - gtk_window_set_resizable(GTK_WINDOW(window), FALSE); - g_signal_connect_after(G_OBJECT(window), "key-press-event", G_CALLBACK(key_press_cb), NULL); - g_signal_connect(G_OBJECT(window), "delete-event", G_CALLBACK(delete_cb), NULL); + window->priv = G_TYPE_INSTANCE_GET_PRIVATE(window, math_window_get_type(), MathWindowPrivate); + + gtk_window_set_title(GTK_WINDOW(window), _("Calculator")); /* Title of main window */ + gtk_window_set_icon_name(GTK_WINDOW(window), "accessories-calculator"); + gtk_window_set_role(GTK_WINDOW(window), "mate-calc"); + gtk_window_set_resizable(GTK_WINDOW(window), FALSE); + g_signal_connect_after(G_OBJECT(window), "key-press-event", G_CALLBACK(key_press_cb), NULL); + g_signal_connect(G_OBJECT(window), "delete-event", G_CALLBACK(delete_cb), NULL); } diff --git a/src/math-window.h b/src/math-window.h index 70c81bd..cd180f6 100644 --- a/src/math-window.h +++ b/src/math-window.h @@ -31,31 +31,29 @@ G_BEGIN_DECLS typedef struct MathWindowPrivate MathWindowPrivate; -typedef struct -{ - GtkWindow parent_instance; - MathWindowPrivate *priv; +typedef struct { + GtkWindow parent_instance; + MathWindowPrivate* priv; } MathWindow; -typedef struct -{ - GtkWindowClass parent_class; +typedef struct { + GtkWindowClass parent_class; - void (*quit)(MathWindow *window); + void (*quit) (MathWindow* window); } MathWindowClass; GType math_window_get_type(void); -MathWindow *math_window_new(MathEquation *equation); +MathWindow* math_window_new(MathEquation* equation); -GtkWidget *math_window_get_menu_bar(MathWindow *window); +GtkWidget* math_window_get_menu_bar(MathWindow* window); -MathEquation *math_window_get_equation(MathWindow *window); +MathEquation* math_window_get_equation(MathWindow* window); -MathDisplay *math_window_get_display(MathWindow *window); +MathDisplay* math_window_get_display(MathWindow* window); -MathButtons *math_window_get_buttons(MathWindow *window); +MathButtons* math_window_get_buttons(MathWindow* window); -void math_window_critical_error(MathWindow *window, const gchar *title, const gchar *contents); +void math_window_critical_error(MathWindow* window, const gchar* title, const gchar* contents); #endif /* MATH_WINDOW_H */ diff --git a/src/mp-equation-private.h b/src/mp-equation-private.h index 7ef96c2..319e916 100644 --- a/src/mp-equation-private.h +++ b/src/mp-equation-private.h @@ -26,39 +26,39 @@ typedef struct MPEquationParserState MPEquationParserState; /* State for parser */ struct MPEquationParserState { - /* User provided options */ - MPEquationOptions *options; + /* User provided options */ + MPEquationOptions* options; - /* Function to check if a variable is defined */ - int (*variable_is_defined)(MPEquationParserState *state, const char *name); + /* Function to check if a variable is defined */ + int (*variable_is_defined)(MPEquationParserState* state, const char* name); - /* Function to get variable values */ - int (*get_variable)(MPEquationParserState *state, const char *name, MPNumber *z); + /* Function to get variable values */ + int (*get_variable)(MPEquationParserState* state, const char* name, MPNumber* z); - /* Function to set variable values */ - void (*set_variable)(MPEquationParserState *state, const char *name, const MPNumber *x); + /* Function to set variable values */ + void (*set_variable)(MPEquationParserState* state, const char* name, const MPNumber *x); - /* Function to check if a function is defined */ - int (*function_is_defined)(MPEquationParserState *state, const char *name); + /* Function to check if a function is defined */ + int (*function_is_defined)(MPEquationParserState* state, const char* name); - /* Function to solve functions */ - int (*get_function)(MPEquationParserState *state, const char *name, const MPNumber *x, MPNumber *z); + /* Function to solve functions */ + int (*get_function)(MPEquationParserState* state, const char* name, const MPNumber* x, MPNumber* z); - /* Function to convert units */ - int (*convert)(MPEquationParserState *state, const MPNumber *x, const char *x_units, const char *z_units, MPNumber *z); + /* Function to convert units */ + int (*convert)(MPEquationParserState* state, const MPNumber* x, const char* x_units, const char* z_units, MPNumber* z); - // FIXME: get_operator?? + // FIXME: get_operator?? - /* Error returned from parser */ - int error; + /* Error returned from parser */ + int error; - /* Name of token where error occured */ - char *error_token; + /* Name of token where error occured */ + char* error_token; - /* Value returned from parser */ - MPNumber ret; + /* Value returned from parser */ + MPNumber ret; }; -int _mp_equation_error(void *yylloc, MPEquationParserState *state, char *text); +int _mp_equation_error(void* yylloc, MPEquationParserState* state, char* text); #endif diff --git a/src/mp-equation.h b/src/mp-equation.h index 60e6690..c5b86fa 100644 --- a/src/mp-equation.h +++ b/src/mp-equation.h @@ -22,56 +22,55 @@ #include "mp.h" -typedef enum -{ - PARSER_ERR_NONE = 0, - PARSER_ERR_INVALID, - PARSER_ERR_OVERFLOW, - PARSER_ERR_UNKNOWN_VARIABLE, - PARSER_ERR_UNKNOWN_FUNCTION, - PARSER_ERR_UNKNOWN_CONVERSION, - PARSER_ERR_MP +typedef enum { + PARSER_ERR_NONE = 0, + PARSER_ERR_INVALID, + PARSER_ERR_OVERFLOW, + PARSER_ERR_UNKNOWN_VARIABLE, + PARSER_ERR_UNKNOWN_FUNCTION, + PARSER_ERR_UNKNOWN_CONVERSION, + PARSER_ERR_MP } MPErrorCode; /* Options for parser */ typedef struct { - /* Default number base */ - int base; + /* Default number base */ + int base; - /* The wordlength for binary operations in bits (e.g. 8, 16, 32) */ - int wordlen; + /* The wordlength for binary operations in bits (e.g. 8, 16, 32) */ + int wordlen; - /* Units for angles (e.g. radians, degrees) */ - MPAngleUnit angle_units; + /* Units for angles (e.g. radians, degrees) */ + MPAngleUnit angle_units; - // FIXME: - // int enable_builtins; + // FIXME: + // int enable_builtins; - /* Data to pass to callbacks */ - void *callback_data; - - /* Function to check if a variable is defined */ - int (*variable_is_defined)(const char *name, void *data); + /* Data to pass to callbacks */ + void *callback_data; - /* Function to get variable values */ - int (*get_variable)(const char *name, MPNumber *z, void *data); + /* Function to check if a variable is defined */ + int (*variable_is_defined)(const char* name, void* data); - /* Function to set variable values */ - void (*set_variable)(const char *name, const MPNumber *x, void *data); + /* Function to get variable values */ + int (*get_variable)(const char* name, MPNumber* z, void* data); - /* Function to check if a function is defined */ - int (*function_is_defined)(const char *name, void *data); + /* Function to set variable values */ + void (*set_variable)(const char* name, const MPNumber *x, void* data); - /* Function to solve functions */ - int (*get_function)(const char *name, const MPNumber *x, MPNumber *z, void *data); + /* Function to check if a function is defined */ + int (*function_is_defined)(const char* name, void* data); - /* Function to convert units */ - int (*convert)(const MPNumber *x, const char *x_units, const char *z_units, MPNumber *z, void *data); + /* Function to solve functions */ + int (*get_function)(const char* name, const MPNumber* x, MPNumber* z, void* data); + + /* Function to convert units */ + int (*convert)(const MPNumber* x, const char* x_units, const char* z_units, MPNumber* z, void* data); } MPEquationOptions; -MPErrorCode mp_equation_parse(const char *expression, MPEquationOptions *options, MPNumber *result, char **error_token); -const char *mp_error_code_to_string(MPErrorCode error_code); +MPErrorCode mp_equation_parse(const char* expression, MPEquationOptions* options, MPNumber* result, char** error_token); +const char* mp_error_code_to_string(MPErrorCode error_code); -int sub_atoi(const char *data); -int super_atoi(const char *data); +int sub_atoi(const char* data); +int super_atoi(const char* data); #endif diff --git a/src/mp-private.h b/src/mp-private.h index 94b1530..5e30bc3 100644 --- a/src/mp-private.h +++ b/src/mp-private.h @@ -38,10 +38,10 @@ //} #define MP_T 100 -void mperr(const char *format, ...) __attribute__((format(printf, 1, 2))); -void mp_gcd(int64_t *, int64_t *); -void mp_normalize(MPNumber *); -void convert_to_radians(const MPNumber *x, MPAngleUnit unit, MPNumber *z); -void convert_from_radians(const MPNumber *x, MPAngleUnit unit, MPNumber *z); +void mperr(const char* format, ...) __attribute__((format(printf, 1, 2))); +void mp_gcd(int64_t*, int64_t*); +void mp_normalize(MPNumber*); +void convert_to_radians(const MPNumber* x, MPAngleUnit unit, MPNumber* z); +void convert_from_radians(const MPNumber* x, MPAngleUnit unit, MPNumber* z); #endif /* MP_INTERNAL_H */ diff --git a/src/mp.h b/src/mp.h index d71111d..d7f226f 100644 --- a/src/mp.h +++ b/src/mp.h @@ -53,28 +53,26 @@ * * x = sign * (MP_BASE^(exponent-1) + MP_BASE^(exponent-2) + ...) */ -typedef struct -{ - /* Sign (+1, -1) or 0 for the value zero */ - int sign, im_sign; +typedef struct { + /* Sign (+1, -1) or 0 for the value zero */ + int sign, im_sign; - /* Exponent (to base MP_BASE) */ - int exponent, im_exponent; + /* Exponent (to base MP_BASE) */ + int exponent, im_exponent; - /* Normalized fraction */ - int fraction[MP_SIZE], im_fraction[MP_SIZE]; + /* Normalized fraction */ + int fraction[MP_SIZE], im_fraction[MP_SIZE]; } MPNumber; -typedef enum -{ - MP_RADIANS, - MP_DEGREES, - MP_GRADIANS +typedef enum { + MP_RADIANS, + MP_DEGREES, + MP_GRADIANS } MPAngleUnit; /* Returns error string or NULL if no error */ // FIXME: Global variable -const char *mp_get_error(void); +const char* mp_get_error(void); /* Clear any current error */ void mp_clear_error(void); diff --git a/src/unittest.c b/src/unittest.c index 791391b..e0ecdd7 100644 --- a/src/unittest.c +++ b/src/unittest.c @@ -30,732 +30,750 @@ static int fails = 0; /* If we're not using GNU C, elide __attribute__ */ #ifndef __GNUC__ -# define __attribute__(x) /*NOTHING*/ + #define __attribute__(x) /*NOTHING*/ #endif -static void pass(const char *format, ...) __attribute__((format(printf, 1, 2))); -static void fail(const char *format, ...) __attribute__((format(printf, 1, 2))); +static void pass(const char* format, ...) __attribute__((format(printf, 1, 2))); +static void fail(const char* format, ...) __attribute__((format(printf, 1, 2))); -static void pass(const char *format, ...) +static void pass(const char* format, ...) { - va_list args; + va_list args; - printf(" PASS: "); - va_start(args, format); - vprintf(format, args); - va_end(args); - printf("\n"); + printf(" PASS: "); + va_start(args, format); + vprintf(format, args); + va_end(args); + printf("\n"); } -static void fail(const char *format, ...) +static void fail(const char* format, ...) { - va_list args; - - printf("*FAIL: "); - va_start(args, format); - vprintf(format, args); - va_end(args); - printf("\n"); - fails++; + va_list args; + + printf("*FAIL: "); + va_start(args, format); + vprintf(format, args); + va_end(args); + printf("\n"); + fails++; } -static const char * -error_code_to_string(MPErrorCode error) +static const char* error_code_to_string(MPErrorCode error) { - static char error_string[1024]; + static char error_string[1024]; - if (error != PARSER_ERR_MP) - return mp_error_code_to_string(error); + if (error != PARSER_ERR_MP) + { + return mp_error_code_to_string(error); + } - snprintf(error_string, 1024, "PARSER_ERR_MP(\"%s\")", mp_get_error()); - return error_string; + snprintf(error_string, 1024, "PARSER_ERR_MP(\"%s\")", mp_get_error()); + return error_string; } -static void -test(char *expression, char *expected, int expected_error) +static void test(char* expression, char* expected, int expected_error) { - MPErrorCode error; - MPNumber result; - char result_str[1024] = ""; - - error = mp_equation_parse(expression, &options, &result, NULL); - - if(error == 0) { - mp_cast_to_string(&result, options.base, options.base, 9, 1, result_str, 1024); - if(expected_error != 0) - fail("'%s' -> %s, expected error %s", expression, result_str, error_code_to_string(expected_error)); - else if(strcmp(result_str, expected) != 0) - fail("'%s' -> '%s', expected '%s'", expression, result_str, expected); - else - pass("'%s' -> '%s'", expression, result_str); - } - else { - if(error == expected_error) - pass("'%s' -> error %s", expression, error_code_to_string(error)); - else - fail("'%s' -> error %s, expected error %s", expression, - error_code_to_string(error), error_code_to_string(expected_error)); - } + MPErrorCode error; + MPNumber result; + char result_str[1024] = ""; + + error = mp_equation_parse(expression, &options, &result, NULL); + + if (error == 0) + { + mp_cast_to_string(&result, options.base, options.base, 9, 1, result_str, 1024); + + if (expected_error != 0) + { + fail("'%s' -> %s, expected error %s", expression, result_str, error_code_to_string(expected_error)); + } + else if(strcmp(result_str, expected) != 0) + { + fail("'%s' -> '%s', expected '%s'", expression, result_str, expected); + } + else + { + pass("'%s' -> '%s'", expression, result_str); + } + } + else + { + if (error == expected_error) + { + pass("'%s' -> error %s", expression, error_code_to_string(error)); + } + else + { + fail("'%s' -> error %s, expected error %s", expression, error_code_to_string(error), error_code_to_string(expected_error)); + } + } } -static void -test_conversions() +static void test_conversions() { - memset(&options, 0, sizeof(options)); - options.base = 10; - options.wordlen = 32; - options.angle_units = MP_DEGREES; - - /* Length */ - test("1 meter in mm", "1000", 0); - test("1m in mm", "1000", 0); - test("1 inch in cm", "2.54", 0); - - /* Area */ - test("1m² in mm²", "1000000", 0); - - /* Volume */ - test("1m³ in mm³", "1000000000", 0); - - /* Weight */ - test("1 kg in pounds", "2.204622622", 0); - - /* Time */ - test("1 minute in seconds", "60", 0); + memset(&options, 0, sizeof(options)); + options.base = 10; + options.wordlen = 32; + options.angle_units = MP_DEGREES; + + /* Length */ + test("1 meter in mm", "1000", 0); + test("1m in mm", "1000", 0); + test("1 inch in cm", "2.54", 0); + + /* Area */ + test("1m² in mm²", "1000000", 0); + + /* Volume */ + test("1m³ in mm³", "1000000000", 0); + + /* Weight */ + test("1 kg in pounds", "2.204622622", 0); + + /* Time */ + test("1 minute in seconds", "60", 0); } -static int -variable_is_defined(const char *name, void *data) +static int variable_is_defined(const char* name, void* data) { return strcmp (name, "x") == 0 || strcmp (name, "y") == 0; } -static int -get_variable(const char *name, MPNumber *z, void *data) +static int get_variable(const char* name, MPNumber* z, void* data) { - if (strcmp (name, "x") == 0) { - mp_set_from_integer (2, z); - return 1; - } - if (strcmp (name, "y") == 0) { - mp_set_from_integer (3, z); - return 1; - } - return 0; + if (strcmp (name, "x") == 0) + { + mp_set_from_integer (2, z); + return 1; + } + + if (strcmp (name, "y") == 0) + { + mp_set_from_integer (3, z); + return 1; + } + + return 0; } -static void -set_variable(const char *name, const MPNumber *x, void *data) +static void set_variable(const char* name, const MPNumber* x, void* data) { + /* nothing */ } -static void -test_equations() +static void test_equations() { - memset(&options, 0, sizeof(options)); - options.base = 10; - options.wordlen = 32; - options.angle_units = MP_DEGREES; - options.variable_is_defined = variable_is_defined; - options.get_variable = get_variable; - options.set_variable = set_variable; - - options.base = 2; - test("2₁₀", "10", 0); - - options.base = 8; - test("16434824₁₀", "76543210", 0); - - options.base = 16; - test("FF", "FF", 0); - test("18364758544493064720₁₀", "FEDCBA9876543210", 0); - - options.base = 10; - test("0₂", "0", 0); test("0₈", "0", 0); test("0", "0", 0); test("0₁₆", "0", 0); - test("1₂", "1", 0); test("1₈", "1", 0); test("1", "1", 0); test("1₁₆", "1", 0); - test("2₂", "", PARSER_ERR_INVALID); test("2₈", "2", 0); test("2", "2", 0); test("2₁₆", "2", 0); - test("3₂", "", PARSER_ERR_INVALID); test("3₈", "3", 0); test("3", "3", 0); test("3₁₆", "3", 0); - test("4₂", "", PARSER_ERR_INVALID); test("4₈", "4", 0); test("4", "4", 0); test("4₁₆", "4", 0); - test("5₂", "", PARSER_ERR_INVALID); test("5₈", "5", 0); test("5", "5", 0); test("5₁₆", "5", 0); - test("6₂", "", PARSER_ERR_INVALID); test("6₈", "6", 0); test("6", "6", 0); test("6₁₆", "6", 0); - test("7₂", "", PARSER_ERR_INVALID); test("7₈", "7", 0); test("7", "7", 0); test("7₁₆", "7", 0); - test("8₂", "", PARSER_ERR_INVALID); test("8₈", "", PARSER_ERR_INVALID); test("8", "8", 0); test("8₁₆", "8", 0); - test("9₂", "", PARSER_ERR_INVALID); test("9₈", "", PARSER_ERR_INVALID); test("9", "9", 0); test("9₁₆", "9", 0); - test("A₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("A₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("A", "", PARSER_ERR_UNKNOWN_VARIABLE); test("A₁₆", "10", 0); - test("B₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("B₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("B", "", PARSER_ERR_UNKNOWN_VARIABLE); test("B₁₆", "11", 0); - test("C₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("C₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("C", "", PARSER_ERR_UNKNOWN_VARIABLE); test("C₁₆", "12", 0); - test("D₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("D₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("D", "", PARSER_ERR_UNKNOWN_VARIABLE); test("D₁₆", "13", 0); - test("E₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("E₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("E", "", PARSER_ERR_UNKNOWN_VARIABLE); test("E₁₆", "14", 0); - test("F₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("F₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("F", "", PARSER_ERR_UNKNOWN_VARIABLE); test("F₁₆", "15", 0); - test("a₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("a₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("a", "", PARSER_ERR_UNKNOWN_VARIABLE); test("a₁₆", "10", 0); - test("b₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("b₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("b", "", PARSER_ERR_UNKNOWN_VARIABLE); test("b₁₆", "11", 0); - test("c₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("c₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("c", "", PARSER_ERR_UNKNOWN_VARIABLE); test("c₁₆", "12", 0); - test("d₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("d₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("d", "", PARSER_ERR_UNKNOWN_VARIABLE); test("d₁₆", "13", 0); - test("e₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("e₈", "", PARSER_ERR_UNKNOWN_VARIABLE); /* e is a built-in variable */ test("e₁₆", "14", 0); - test("f₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("f₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("f", "", PARSER_ERR_UNKNOWN_VARIABLE); test("f₁₆", "15", 0); - - test("+1", "1", 0); - test("−1", "−1", 0); - test("+ 1", "1", 0); // FIXME: Should this be allowed? - test("− 1", "−1", 0); // FIXME: Should this be allowed? - test("++1", "1", PARSER_ERR_INVALID); - test("−−1", "1", 0); - test("255", "255", 0); - test("256", "256", 0); - test("½", "0.5", 0); - test("1½", "1.5", 0); - test("0°", "0", 0); - test("1°", "1", 0); - test("0°30'", "0.5", 0); - //test("0°0.1'", "1", 0); // FIXME: Not yet supported - test("0°0'1\"", "0.000277778", 0); - test("0°0'0.1\"", "0.000027778", 0); - test("1.00", "1", 0); - test("1.01", "1.01", 0); - - test("١٢٣٤٥٦٧٨٩٠", "1234567890", 0); - test("۱۲۳۴۵۶۷۸۹۰", "1234567890", 0); - -/* - //test("2A", "2000000000000000", 0); - test("2T", "2000000000000", 0); - test("2G", "2000000000", 0); - test("2M", "2000000", 0); - test("2k", "2000", 0); - test("2c", "0.02", 0); - test("2d", "0.2", 0); - test("2c", "0.02", 0); - test("2m", "0.002", 0); - test("2u", "0.000002", 0); - test("2µ", "0.000002", 0); - test("2n", "0.000000002", 0); - //test("2p", "0.000000000002", 0); // FIXME: Need to print out significant figures, not decimal places - //test("2f", "0.000000000000002", 0); // FIXME: Need to print out significant figures, not decimal places - //test("2A3", "2300000000000000", 0); - test("2T3", "2300000000000", 0); - test("2G3", "2300000000", 0); - test("2M3", "2300000", 0); - test("2k3", "2300", 0); - test("2c3", "0.023", 0); - test("2d3", "0.23", 0); - test("2c3", "0.023", 0); - test("2m3", "0.0023", 0); - test("2u3", "0.0000023", 0); - test("2µ3", "0.0000023", 0); - //test("2n3", "0.0000000023", 0); // FIXME: Need to print out significant figures, not decimal places - //test("2p3", "0.0000000000023", 0); // FIXME: Need to print out significant figures, not decimal places - //test("2f3", "0.0000000000000023", 0); // FIXME: Need to print out significant figures, not decimal places + memset(&options, 0, sizeof(options)); + options.base = 10; + options.wordlen = 32; + options.angle_units = MP_DEGREES; + options.variable_is_defined = variable_is_defined; + options.get_variable = get_variable; + options.set_variable = set_variable; + + options.base = 2; + test("2₁₀", "10", 0); + + options.base = 8; + test("16434824₁₀", "76543210", 0); + + options.base = 16; + test("FF", "FF", 0); + test("18364758544493064720₁₀", "FEDCBA9876543210", 0); + + options.base = 10; + test("0₂", "0", 0); test("0₈", "0", 0); test("0", "0", 0); test("0₁₆", "0", 0); + test("1₂", "1", 0); test("1₈", "1", 0); test("1", "1", 0); test("1₁₆", "1", 0); + test("2₂", "", PARSER_ERR_INVALID); test("2₈", "2", 0); test("2", "2", 0); test("2₁₆", "2", 0); + test("3₂", "", PARSER_ERR_INVALID); test("3₈", "3", 0); test("3", "3", 0); test("3₁₆", "3", 0); + test("4₂", "", PARSER_ERR_INVALID); test("4₈", "4", 0); test("4", "4", 0); test("4₁₆", "4", 0); + test("5₂", "", PARSER_ERR_INVALID); test("5₈", "5", 0); test("5", "5", 0); test("5₁₆", "5", 0); + test("6₂", "", PARSER_ERR_INVALID); test("6₈", "6", 0); test("6", "6", 0); test("6₁₆", "6", 0); + test("7₂", "", PARSER_ERR_INVALID); test("7₈", "7", 0); test("7", "7", 0); test("7₁₆", "7", 0); + test("8₂", "", PARSER_ERR_INVALID); test("8₈", "", PARSER_ERR_INVALID); test("8", "8", 0); test("8₁₆", "8", 0); + test("9₂", "", PARSER_ERR_INVALID); test("9₈", "", PARSER_ERR_INVALID); test("9", "9", 0); test("9₁₆", "9", 0); + test("A₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("A₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("A", "", PARSER_ERR_UNKNOWN_VARIABLE); test("A₁₆", "10", 0); + test("B₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("B₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("B", "", PARSER_ERR_UNKNOWN_VARIABLE); test("B₁₆", "11", 0); + test("C₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("C₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("C", "", PARSER_ERR_UNKNOWN_VARIABLE); test("C₁₆", "12", 0); + test("D₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("D₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("D", "", PARSER_ERR_UNKNOWN_VARIABLE); test("D₁₆", "13", 0); + test("E₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("E₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("E", "", PARSER_ERR_UNKNOWN_VARIABLE); test("E₁₆", "14", 0); + test("F₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("F₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("F", "", PARSER_ERR_UNKNOWN_VARIABLE); test("F₁₆", "15", 0); + test("a₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("a₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("a", "", PARSER_ERR_UNKNOWN_VARIABLE); test("a₁₆", "10", 0); + test("b₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("b₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("b", "", PARSER_ERR_UNKNOWN_VARIABLE); test("b₁₆", "11", 0); + test("c₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("c₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("c", "", PARSER_ERR_UNKNOWN_VARIABLE); test("c₁₆", "12", 0); + test("d₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("d₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("d", "", PARSER_ERR_UNKNOWN_VARIABLE); test("d₁₆", "13", 0); + test("e₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("e₈", "", PARSER_ERR_UNKNOWN_VARIABLE); /* e is a built-in variable */ test("e₁₆", "14", 0); + test("f₂", "", PARSER_ERR_UNKNOWN_VARIABLE); test("f₈", "", PARSER_ERR_UNKNOWN_VARIABLE); test("f", "", PARSER_ERR_UNKNOWN_VARIABLE); test("f₁₆", "15", 0); + + test("+1", "1", 0); + test("−1", "−1", 0); + test("+ 1", "1", 0); // FIXME: Should this be allowed? + test("− 1", "−1", 0); // FIXME: Should this be allowed? + test("++1", "1", PARSER_ERR_INVALID); + test("−−1", "1", 0); + test("255", "255", 0); + test("256", "256", 0); + test("½", "0.5", 0); + test("1½", "1.5", 0); + test("0°", "0", 0); + test("1°", "1", 0); + test("0°30'", "0.5", 0); + //test("0°0.1'", "1", 0); // FIXME: Not yet supported + test("0°0'1\"", "0.000277778", 0); + test("0°0'0.1\"", "0.000027778", 0); + test("1.00", "1", 0); + test("1.01", "1.01", 0); + + test("١٢٣٤٥٦٧٨٩٠", "1234567890", 0); + test("۱۲۳۴۵۶۷۸۹۰", "1234567890", 0); + +/* + //test("2A", "2000000000000000", 0); + test("2T", "2000000000000", 0); + test("2G", "2000000000", 0); + test("2M", "2000000", 0); + test("2k", "2000", 0); + test("2c", "0.02", 0); + test("2d", "0.2", 0); + test("2c", "0.02", 0); + test("2m", "0.002", 0); + test("2u", "0.000002", 0); + test("2µ", "0.000002", 0); + test("2n", "0.000000002", 0); + //test("2p", "0.000000000002", 0); // FIXME: Need to print out significant figures, not decimal places + //test("2f", "0.000000000000002", 0); // FIXME: Need to print out significant figures, not decimal places + //test("2A3", "2300000000000000", 0); + test("2T3", "2300000000000", 0); + test("2G3", "2300000000", 0); + test("2M3", "2300000", 0); + test("2k3", "2300", 0); + test("2c3", "0.023", 0); + test("2d3", "0.23", 0); + test("2c3", "0.023", 0); + test("2m3", "0.0023", 0); + test("2u3", "0.0000023", 0); + test("2µ3", "0.0000023", 0); + //test("2n3", "0.0000000023", 0); // FIXME: Need to print out significant figures, not decimal places + //test("2p3", "0.0000000000023", 0); // FIXME: Need to print out significant figures, not decimal places + //test("2f3", "0.0000000000000023", 0); // FIXME: Need to print out significant figures, not decimal places */ - test("2×10^3", "2000", 0); - test("2×10^−3", "0.002", 0); - - test("x", "2", 0); - test("y", "3", 0); - test("z", "", PARSER_ERR_UNKNOWN_VARIABLE); - test("2y", "6", 0); - test("y2", "", PARSER_ERR_INVALID); - test("y 2", "", PARSER_ERR_INVALID); - test("2z", "", PARSER_ERR_UNKNOWN_VARIABLE); - test("z2", "", PARSER_ERR_UNKNOWN_VARIABLE); - test("z 2", "", PARSER_ERR_UNKNOWN_VARIABLE); - test("z(2)", "", PARSER_ERR_UNKNOWN_VARIABLE); - test("y²", "9", 0); - test("2y²", "18", 0); - test("x×y", "6", 0); - test("xy", "6", 0); - test("yx", "6", 0); - test("2xy", "12", 0); - test("x²y", "12", 0); - test("xy²", "18", 0); - test("(xy)²", "36", 0); - test("2x²y", "24", 0); - test("2xy²", "36", 0); - test("2x²y²", "72", 0); - test("x²yx²y", "144", 0); - test("x³+2x²−5", "11", 0); - test("2(x+3y)", "22", 0); - test("x(x+3y)", "22", 0); - test("(x+3y)(2x-4y)", "−88", 0); - test("2x²+2xy−12y²", "−88", 0); - - test("π", "3.141592654", 0); - test("e", "2.718281828", 0); - - test("z=99", "99", 0); - test("longname=99", "99", 0); - //test("e=99", "", PARSER_ERR_BUILTIN_VARIABLE); - - test("0+0", "0", 0); - test("1+1", "2", 0); - test("1+4", "5", 0); - test("4+1", "5", 0); - test("40000+0.001", "40000.001", 0); - test("0.001+40000", "40000.001", 0); - test("2-3", "−1", 0); - test("2−3", "−1", 0); - test("3−2", "1", 0); - test("40000−0.001", "39999.999", 0); - test("0.001−40000", "−39999.999", 0); - test("2*3", "6", 0); - test("2×3", "6", 0); - test("−2×3", "−6", 0); - test("2×−3", "−6", 0); - test("−2×−3", "6", 0); - test("6/3", "2", 0); - test("6÷3", "2", 0); - test("1÷2", "0.5", 0); - test("−6÷3", "−2", 0); - test("6÷−3", "−2", 0); - test("−6÷−3", "2", 0); - test("(−3)÷(−6)", "0.5", 0); - test("2÷2", "1", 0); - test("1203÷1", "1203", 0); - test("−0÷32352.689", "0", 0); - test("1÷4", "0.25", 0); - test("1÷3", "0.333333333", 0); - test("2÷3", "0.666666667", 0); - test("1÷0", "", PARSER_ERR_MP); - test("0÷0", "", PARSER_ERR_MP); - - /* Precision */ - test("1000000000000000−1000000000000000", "0", 0); - test("1000000000000000÷1000000000000000", "1", 0); - test("1000000000000000×0.000000000000001", "1", 0); - - /* Order of operations */ - test("1−0.9−0.1", "0", 0); - test("1+2×3", "7", 0); - test("1+(2×3)", "7", 0); - test("(1+2)×3", "9", 0); - test("(1+2×3)", "7", 0); - - /* Percentage */ - test("100%", "1", 0); - test("1%", "0.01", 0); - test("100+1%", "101", 0); - test("100−1%", "99", 0); - test("100×1%", "1", 0); - test("100÷1%", "10000", 0); - - /* Factorial */ - test("0!", "1", 0); - test("1!", "1", 0); - test("5!", "120", 0); - test("69!", "171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000", 0); - test("0.1!", "", PARSER_ERR_MP); - test("−1!", "−1", 0); - test("(−1)!", "", PARSER_ERR_MP); - test("−(1!)", "−1", 0); - - /* Powers */ - test("2²", "4", 0); - test("2³", "8", 0); - test("2¹⁰", "1024", 0); - test("(1+2)²", "9", 0); - test("(x)²", "4", 0); - test("|1−3|²", "4", 0); - test("|x|²", "4", 0); - test("0^0", "1", 0); - test("2^0", "1", 0); - test("2^1", "2", 0); - test("2^2", "4", 0); - test("2⁻¹", "0.5", 0); - //test("2⁻", "", PARSER_ERR_MP); // FIXME: Maybe an error in bison? - test("2^−1", "0.5", 0); - test("2^(−1)", "0.5", 0); - test("x⁻¹", "0.5", 0); - test("−10^2", "−100", 0); - test("(−10)^2", "100", 0); - test("−(10^2)", "−100", 0); - test("2^100", "1267650600228229401496703205376", 0); - test("4^3^2", "262144", 0); - test("4^(3^2)", "262144", 0); - test("(4^3)^2", "4096", 0); - test("√4", "2", 0); - test("√4−2", "0", 0); - test("∛8", "2", 0); - test("∜16", "2", 0); - test("₃√8", "2", 0); - test("₁₀√1024", "2", 0); - test("√(2+2)", "2", 0); - test("2√4", "4", 0); - test("2×√4", "4", 0); - test("Sqrt(4)", "2", 0); - test("Sqrt(2)", "1.414213562", 0); - test("4^0.5", "2", 0); - test("2^0.5", "1.414213562", 0); - test("₃√−8", "−2", 0); - test("(−8)^(1÷3)", "−2", 0); - - test("0 mod 7", "0", 0); - test("6 mod 7", "6", 0); - test("7 mod 7", "0", 0); - test("8 mod 7", "1", 0); - test("−1 mod 7", "6", 0); - - test("sgn 0", "0", 0); - test("sgn 3", "1", 0); - test("sgn −3", "−1", 0); - test("⌊3⌋", "3", 0); - test("⌈3⌉", "3", 0); - test("[3]", "3", 0); - test("⌊−3⌋", "−3", 0); - test("⌈−3⌉", "−3", 0); - test("[−3]", "−3", 0); - test("⌊3.2⌋", "3", 0); - test("⌈3.2⌉", "4", 0); - test("[3.2]", "3", 0); - test("⌊−3.2⌋", "−4", 0); - test("⌈−3.2⌉", "−3", 0); - test("[−3.2]", "−3", 0); - test("⌊3.5⌋", "3", 0); - test("⌈3.5⌉", "4", 0); - test("[3.5]", "4", 0); - test("⌊−3.5⌋", "−4", 0); - test("⌈−3.5⌉", "−3", 0); - test("[−3.5]", "−4", 0); - test("⌊3.7⌋", "3", 0); - test("⌈3.7⌉", "4", 0); - test("[3.7]", "4", 0); - test("⌊−3.7⌋", "−4", 0); - test("⌈−3.7⌉", "−3", 0); - test("[−3.7]", "−4", 0); - test("{3.2}", "0.2", 0); - test("{−3.2}", "0.8", 0); - - test("|1|", "1", 0); - test("|−1|", "1", 0); - test("|3−5|", "2", 0); - test("|x|", "2", 0); - test("abs 1", "1", 0); - test("abs (−1)", "1", 0); - - test("log 0", "", PARSER_ERR_MP); - test("log 1", "0", 0); - test("log 2", "0.301029996", 0); - test("log 10", "1", 0); - test("log₁₀ 10", "1", 0); - test("log₂ 2", "1", 0); - test("2 log 2", "0.602059991", 0); - - test("ln 0", "", PARSER_ERR_MP); - test("ln 1", "0", 0); - test("ln 2", "0.693147181", 0); - test("ln e", "1", 0); - test("2 ln 2", "1.386294361", 0); - - options.angle_units = MP_DEGREES; - test("sin 0", "0", 0); - test("sin 45 − 1÷√2", "0", 0); - test("sin 20 + sin(−20)", "0", 0); - test("sin 90", "1", 0); - test("sin 180", "0", 0); - test("2 sin 90", "2", 0); - test("sin²45", "0.5", 0); - - test("cos 0", "1", 0); - test("cos 45 − 1÷√2", "0", 0); - test("cos 20 − cos (−20)", "0", 0); - test("cos 90", "0", 0); - test("cos 180", "−1", 0); - test("2 cos 0", "2", 0); - test("cos²45", "0.5", 0); - - test("tan 0", "0", 0); - test("tan 10 − sin 10÷cos 10", "0", 0); - test("tan 90", "", PARSER_ERR_MP); - test("tan 10", "0.176326981", 0); - test("tan²10", "0.031091204", 0); - - test("cos⁻¹ 0", "90", 0); - test("cos⁻¹ 1", "0", 0); - test("cos⁻¹ (−1)", "180", 0); - test("cos⁻¹ (1÷√2)", "45", 0); - - test("sin⁻¹ 0", "0", 0); - test("sin⁻¹ 1", "90", 0); - test("sin⁻¹ (−1)", "−90", 0); - test("sin⁻¹ (1÷√2)", "45", 0); - - test("cosh 0", "1", 0); - test("cosh 10 − (e^10 + e^−10)÷2", "0", 0); - - test("sinh 0", "0", 0); - test("sinh 10 − (e^10 − e^−10)÷2", "0", 0); - test("sinh (−10) + sinh 10", "0", 0); - - test("cosh² (−5) − sinh² (−5)", "1", 0); - test("tanh 0", "0", 0); - test("tanh 10 − sinh 10 ÷ cosh 10", "0", 0); - - test("atanh 0", "0", 0); - test("atanh (1÷10) − 0.5 ln(11÷9)", "0", 0); - - options.angle_units = MP_DEGREES; - test("sin 90", "1", 0); - - options.angle_units = MP_RADIANS; - test("sin (π÷2)", "1", 0); // FIXME: Shouldn't need brackets - - options.angle_units = MP_GRADIANS; - test("sin 100", "1", 0); - - /* Complex numbers */ - options.angle_units = MP_DEGREES; - test("i", "i", 0); - test("−i", "−i", 0); - test("2i", "2i", 0); - test("1+i", "1+i", 0); - test("i+1", "1+i", 0); - test("1−i", "1−i", 0); - test("i−1", "−1+i", 0); - test("i×i", "−1", 0); - test("i÷i", "1", 0); - test("1÷i", "−i", 0); - test("|i|", "1", 0); - test("|3+4i|", "5", 0); - test("arg 0", "", PARSER_ERR_MP); - test("arg 1", "0", 0); - test("arg (1+i)", "45", 0); - test("arg i", "90", 0); - test("arg (−1+i)", "135", 0); - test("arg −1", "180", 0); - test("arg (1+−i)", "−45", 0); - test("arg −i", "−90", 0); - test("arg (−1−i)", "−135", 0); - test("i⁻¹", "−i", 0); - test("√−1", "i", 0); - test("(−1)^0.5", "i", 0); - test("√−4", "2i", 0); - test("e^iπ", "−1", 0); - test("log (−10) − (1 + πi÷ln(10))", "0", 0); - test("ln (−e) − (1 + πi)", "0", 0); - test("sin(iπ÷4) − i×sinh(π÷4)", "0", 0); - test("cos(iπ÷4) − cosh(π÷4)", "0", 0); - - /* Boolean */ - test("0 and 0", "0", 0); - test("1 and 0", "0", 0); - test("0 and 1", "0", 0); - test("1 and 1", "1", 0); - test("3 and 5", "1", 0); - - test("0 or 0", "0", 0); - test("1 or 0", "1", 0); - test("0 or 1", "1", 0); - test("1 or 1", "1", 0); - test("3 or 5", "7", 0); - - test("0 xor 0", "0", 0); - test("1 xor 0", "1", 0); - test("0 xor 1", "1", 0); - test("1 xor 1", "0", 0); - test("3 xor 5", "6", 0); - - options.base = 16; - test("ones 1", "FFFFFFFE", 0); - test("ones 7FFFFFFF", "80000000", 0); - test("twos 1", "FFFFFFFF", 0); - test("twos 7FFFFFFF", "80000001", 0); - test("~7A₁₆", "FFFFFF85", 0); - - options.base = 2; - options.wordlen = 4; - test("1100∧1010", "1000", 0); - test("1100∨1010", "1110", 0); - test("1100⊻1010", "110", 0); - test("1100⊕1010", "110", 0); - //test("1100⊼1010", "0111", 0); - //test("1100⊽1010", "0001", 0); - //options.wordlen = 2; - //test("¬01₂", "10₂", 0); - //test("¬¬10₂", "10₂", 0); + test("2×10^3", "2000", 0); + test("2×10^−3", "0.002", 0); + + test("x", "2", 0); + test("y", "3", 0); + test("z", "", PARSER_ERR_UNKNOWN_VARIABLE); + test("2y", "6", 0); + test("y2", "", PARSER_ERR_INVALID); + test("y 2", "", PARSER_ERR_INVALID); + test("2z", "", PARSER_ERR_UNKNOWN_VARIABLE); + test("z2", "", PARSER_ERR_UNKNOWN_VARIABLE); + test("z 2", "", PARSER_ERR_UNKNOWN_VARIABLE); + test("z(2)", "", PARSER_ERR_UNKNOWN_VARIABLE); + test("y²", "9", 0); + test("2y²", "18", 0); + test("x×y", "6", 0); + test("xy", "6", 0); + test("yx", "6", 0); + test("2xy", "12", 0); + test("x²y", "12", 0); + test("xy²", "18", 0); + test("(xy)²", "36", 0); + test("2x²y", "24", 0); + test("2xy²", "36", 0); + test("2x²y²", "72", 0); + test("x²yx²y", "144", 0); + test("x³+2x²−5", "11", 0); + test("2(x+3y)", "22", 0); + test("x(x+3y)", "22", 0); + test("(x+3y)(2x-4y)", "−88", 0); + test("2x²+2xy−12y²", "−88", 0); + + test("π", "3.141592654", 0); + test("e", "2.718281828", 0); + + test("z=99", "99", 0); + test("longname=99", "99", 0); + //test("e=99", "", PARSER_ERR_BUILTIN_VARIABLE); + + test("0+0", "0", 0); + test("1+1", "2", 0); + test("1+4", "5", 0); + test("4+1", "5", 0); + test("40000+0.001", "40000.001", 0); + test("0.001+40000", "40000.001", 0); + test("2-3", "−1", 0); + test("2−3", "−1", 0); + test("3−2", "1", 0); + test("40000−0.001", "39999.999", 0); + test("0.001−40000", "−39999.999", 0); + test("2*3", "6", 0); + test("2×3", "6", 0); + test("−2×3", "−6", 0); + test("2×−3", "−6", 0); + test("−2×−3", "6", 0); + test("6/3", "2", 0); + test("6÷3", "2", 0); + test("1÷2", "0.5", 0); + test("−6÷3", "−2", 0); + test("6÷−3", "−2", 0); + test("−6÷−3", "2", 0); + test("(−3)÷(−6)", "0.5", 0); + test("2÷2", "1", 0); + test("1203÷1", "1203", 0); + test("−0÷32352.689", "0", 0); + test("1÷4", "0.25", 0); + test("1÷3", "0.333333333", 0); + test("2÷3", "0.666666667", 0); + test("1÷0", "", PARSER_ERR_MP); + test("0÷0", "", PARSER_ERR_MP); + + /* Precision */ + test("1000000000000000−1000000000000000", "0", 0); + test("1000000000000000÷1000000000000000", "1", 0); + test("1000000000000000×0.000000000000001", "1", 0); + + /* Order of operations */ + test("1−0.9−0.1", "0", 0); + test("1+2×3", "7", 0); + test("1+(2×3)", "7", 0); + test("(1+2)×3", "9", 0); + test("(1+2×3)", "7", 0); + + /* Percentage */ + test("100%", "1", 0); + test("1%", "0.01", 0); + test("100+1%", "101", 0); + test("100−1%", "99", 0); + test("100×1%", "1", 0); + test("100÷1%", "10000", 0); + + /* Factorial */ + test("0!", "1", 0); + test("1!", "1", 0); + test("5!", "120", 0); + test("69!", "171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000", 0); + test("0.1!", "", PARSER_ERR_MP); + test("−1!", "−1", 0); + test("(−1)!", "", PARSER_ERR_MP); + test("−(1!)", "−1", 0); + + /* Powers */ + test("2²", "4", 0); + test("2³", "8", 0); + test("2¹⁰", "1024", 0); + test("(1+2)²", "9", 0); + test("(x)²", "4", 0); + test("|1−3|²", "4", 0); + test("|x|²", "4", 0); + test("0^0", "1", 0); + test("2^0", "1", 0); + test("2^1", "2", 0); + test("2^2", "4", 0); + test("2⁻¹", "0.5", 0); + //test("2⁻", "", PARSER_ERR_MP); // FIXME: Maybe an error in bison? + test("2^−1", "0.5", 0); + test("2^(−1)", "0.5", 0); + test("x⁻¹", "0.5", 0); + test("−10^2", "−100", 0); + test("(−10)^2", "100", 0); + test("−(10^2)", "−100", 0); + test("2^100", "1267650600228229401496703205376", 0); + test("4^3^2", "262144", 0); + test("4^(3^2)", "262144", 0); + test("(4^3)^2", "4096", 0); + test("√4", "2", 0); + test("√4−2", "0", 0); + test("∛8", "2", 0); + test("∜16", "2", 0); + test("₃√8", "2", 0); + test("₁₀√1024", "2", 0); + test("√(2+2)", "2", 0); + test("2√4", "4", 0); + test("2×√4", "4", 0); + test("Sqrt(4)", "2", 0); + test("Sqrt(2)", "1.414213562", 0); + test("4^0.5", "2", 0); + test("2^0.5", "1.414213562", 0); + test("₃√−8", "−2", 0); + test("(−8)^(1÷3)", "−2", 0); + + test("0 mod 7", "0", 0); + test("6 mod 7", "6", 0); + test("7 mod 7", "0", 0); + test("8 mod 7", "1", 0); + test("−1 mod 7", "6", 0); + + test("sgn 0", "0", 0); + test("sgn 3", "1", 0); + test("sgn −3", "−1", 0); + test("⌊3⌋", "3", 0); + test("⌈3⌉", "3", 0); + test("[3]", "3", 0); + test("⌊−3⌋", "−3", 0); + test("⌈−3⌉", "−3", 0); + test("[−3]", "−3", 0); + test("⌊3.2⌋", "3", 0); + test("⌈3.2⌉", "4", 0); + test("[3.2]", "3", 0); + test("⌊−3.2⌋", "−4", 0); + test("⌈−3.2⌉", "−3", 0); + test("[−3.2]", "−3", 0); + test("⌊3.5⌋", "3", 0); + test("⌈3.5⌉", "4", 0); + test("[3.5]", "4", 0); + test("⌊−3.5⌋", "−4", 0); + test("⌈−3.5⌉", "−3", 0); + test("[−3.5]", "−4", 0); + test("⌊3.7⌋", "3", 0); + test("⌈3.7⌉", "4", 0); + test("[3.7]", "4", 0); + test("⌊−3.7⌋", "−4", 0); + test("⌈−3.7⌉", "−3", 0); + test("[−3.7]", "−4", 0); + test("{3.2}", "0.2", 0); + test("{−3.2}", "0.8", 0); + + test("|1|", "1", 0); + test("|−1|", "1", 0); + test("|3−5|", "2", 0); + test("|x|", "2", 0); + test("abs 1", "1", 0); + test("abs (−1)", "1", 0); + + test("log 0", "", PARSER_ERR_MP); + test("log 1", "0", 0); + test("log 2", "0.301029996", 0); + test("log 10", "1", 0); + test("log₁₀ 10", "1", 0); + test("log₂ 2", "1", 0); + test("2 log 2", "0.602059991", 0); + + test("ln 0", "", PARSER_ERR_MP); + test("ln 1", "0", 0); + test("ln 2", "0.693147181", 0); + test("ln e", "1", 0); + test("2 ln 2", "1.386294361", 0); + + options.angle_units = MP_DEGREES; + test("sin 0", "0", 0); + test("sin 45 − 1÷√2", "0", 0); + test("sin 20 + sin(−20)", "0", 0); + test("sin 90", "1", 0); + test("sin 180", "0", 0); + test("2 sin 90", "2", 0); + test("sin²45", "0.5", 0); + + test("cos 0", "1", 0); + test("cos 45 − 1÷√2", "0", 0); + test("cos 20 − cos (−20)", "0", 0); + test("cos 90", "0", 0); + test("cos 180", "−1", 0); + test("2 cos 0", "2", 0); + test("cos²45", "0.5", 0); + + test("tan 0", "0", 0); + test("tan 10 − sin 10÷cos 10", "0", 0); + test("tan 90", "", PARSER_ERR_MP); + test("tan 10", "0.176326981", 0); + test("tan²10", "0.031091204", 0); + + test("cos⁻¹ 0", "90", 0); + test("cos⁻¹ 1", "0", 0); + test("cos⁻¹ (−1)", "180", 0); + test("cos⁻¹ (1÷√2)", "45", 0); + + test("sin⁻¹ 0", "0", 0); + test("sin⁻¹ 1", "90", 0); + test("sin⁻¹ (−1)", "−90", 0); + test("sin⁻¹ (1÷√2)", "45", 0); + + test("cosh 0", "1", 0); + test("cosh 10 − (e^10 + e^−10)÷2", "0", 0); + + test("sinh 0", "0", 0); + test("sinh 10 − (e^10 − e^−10)÷2", "0", 0); + test("sinh (−10) + sinh 10", "0", 0); + + test("cosh² (−5) − sinh² (−5)", "1", 0); + test("tanh 0", "0", 0); + test("tanh 10 − sinh 10 ÷ cosh 10", "0", 0); + + test("atanh 0", "0", 0); + test("atanh (1÷10) − 0.5 ln(11÷9)", "0", 0); + + options.angle_units = MP_DEGREES; + test("sin 90", "1", 0); + + options.angle_units = MP_RADIANS; + test("sin (π÷2)", "1", 0); // FIXME: Shouldn't need brackets + + options.angle_units = MP_GRADIANS; + test("sin 100", "1", 0); + + /* Complex numbers */ + options.angle_units = MP_DEGREES; + test("i", "i", 0); + test("−i", "−i", 0); + test("2i", "2i", 0); + test("1+i", "1+i", 0); + test("i+1", "1+i", 0); + test("1−i", "1−i", 0); + test("i−1", "−1+i", 0); + test("i×i", "−1", 0); + test("i÷i", "1", 0); + test("1÷i", "−i", 0); + test("|i|", "1", 0); + test("|3+4i|", "5", 0); + test("arg 0", "", PARSER_ERR_MP); + test("arg 1", "0", 0); + test("arg (1+i)", "45", 0); + test("arg i", "90", 0); + test("arg (−1+i)", "135", 0); + test("arg −1", "180", 0); + test("arg (1+−i)", "−45", 0); + test("arg −i", "−90", 0); + test("arg (−1−i)", "−135", 0); + test("i⁻¹", "−i", 0); + test("√−1", "i", 0); + test("(−1)^0.5", "i", 0); + test("√−4", "2i", 0); + test("e^iπ", "−1", 0); + test("log (−10) − (1 + πi÷ln(10))", "0", 0); + test("ln (−e) − (1 + πi)", "0", 0); + test("sin(iπ÷4) − i×sinh(π÷4)", "0", 0); + test("cos(iπ÷4) − cosh(π÷4)", "0", 0); + + /* Boolean */ + test("0 and 0", "0", 0); + test("1 and 0", "0", 0); + test("0 and 1", "0", 0); + test("1 and 1", "1", 0); + test("3 and 5", "1", 0); + + test("0 or 0", "0", 0); + test("1 or 0", "1", 0); + test("0 or 1", "1", 0); + test("1 or 1", "1", 0); + test("3 or 5", "7", 0); + + test("0 xor 0", "0", 0); + test("1 xor 0", "1", 0); + test("0 xor 1", "1", 0); + test("1 xor 1", "0", 0); + test("3 xor 5", "6", 0); + + options.base = 16; + test("ones 1", "FFFFFFFE", 0); + test("ones 7FFFFFFF", "80000000", 0); + test("twos 1", "FFFFFFFF", 0); + test("twos 7FFFFFFF", "80000001", 0); + test("~7A₁₆", "FFFFFF85", 0); + + options.base = 2; + options.wordlen = 4; + test("1100∧1010", "1000", 0); + test("1100∨1010", "1110", 0); + test("1100⊻1010", "110", 0); + test("1100⊕1010", "110", 0); + //test("1100⊼1010", "0111", 0); + //test("1100⊽1010", "0001", 0); + //options.wordlen = 2; + //test("¬01₂", "10₂", 0); + //test("¬¬10₂", "10₂", 0); } -static void -print_number(MPNumber *x) +static void print_number(MPNumber* x) { - int i, j; - - printf("sign=%d exponent=%d fraction=%d", x->sign, x->exponent, x->fraction[0]); - for (i = 1; i < MP_SIZE; i++) { - for (j = i; j < MP_SIZE && x->fraction[j] == 0; j++); - if (j == MP_SIZE) { - printf(",..."); - break; - } - printf(",%d", x->fraction[i]); - } + int i, j; + + printf("sign=%d exponent=%d fraction=%d", x->sign, x->exponent, x->fraction[0]); + + for (i = 1; i < MP_SIZE; i++) + { + for (j = i; j < MP_SIZE && x->fraction[j] == 0; j++) + { + /* nothing*/ + } + + if (j == MP_SIZE) + { + printf(",..."); + break; + } + + printf(",%d", x->fraction[i]); + } } -static void -test_string(const char *number) +static void test_string(const char* number) { - MPNumber t; + MPNumber t; - mp_set_from_string(number, 10, &t); + mp_set_from_string(number, 10, &t); - printf("MPNumber(%s) -> {", number); - print_number(&t); - printf("}\n"); + printf("MPNumber(%s) -> {", number); + print_number(&t); + printf("}\n"); } -static void -test_integer(int number) +static void test_integer(int number) { - MPNumber t; + MPNumber t; - mp_set_from_integer(number, &t); + mp_set_from_integer(number, &t); - printf("MPNumber(%d) -> {", number); - print_number(&t); - printf("}\n"); + printf("MPNumber(%d) -> {", number); + print_number(&t); + printf("}\n"); } #include "mp-private.h" -static void -test_numbers() +static void test_numbers() { - printf("base=%d\n", MP_BASE); - test_integer(0); - test_integer(1); - test_integer(-1); - test_integer(2); - test_integer(9999); - test_integer(10000); - test_integer(10001); - test_integer(2147483647); - - test_string("0"); - test_string("1"); - test_string("-1"); - test_string("16383"); - test_string("16384"); - test_string("16385"); - test_string("268435456"); - - test_string("0.1"); - test_string("0.5"); - test_string("0.25"); - test_string("0.125"); - test_string("0.0625"); - test_string("0.00006103515625"); - test_string("0.000030517578125"); - - test_string("1.00006103515625"); - test_string("16384.00006103515625"); + printf("base=%d\n", MP_BASE); + + test_integer(0); + test_integer(1); + test_integer(-1); + test_integer(2); + test_integer(9999); + test_integer(10000); + test_integer(10001); + test_integer(2147483647); + + test_string("0"); + test_string("1"); + test_string("-1"); + test_string("16383"); + test_string("16384"); + test_string("16385"); + test_string("268435456"); + + test_string("0.1"); + test_string("0.5"); + test_string("0.25"); + test_string("0.125"); + test_string("0.0625"); + test_string("0.00006103515625"); + test_string("0.000030517578125"); + + test_string("1.00006103515625"); + test_string("16384.00006103515625"); } -static void -try(const char *string, bool result, bool expected) +static void try(const char* string, bool result, bool expected) { - if ((result && !expected) || (!result && expected)) - fail("%s -> %s, expected %s", string, expected ? "true" : "false", result ? "true" : "false"); - else - pass("%s -> %s", string, result ? "true" : "false"); + if ((result && !expected) || (!result && expected)) + { + fail("%s -> %s, expected %s", string, expected ? "true" : "false", result ? "true" : "false"); + } + else + { + pass("%s -> %s", string, result ? "true" : "false"); + } } -static void -test_mp() +static void test_mp() { - MPNumber zero, one, minus_one; - - mp_set_from_integer(0, &zero); - mp_set_from_integer(1, &one); - mp_set_from_integer(-1, &minus_one); - - try("mp_is_zero(-1)", mp_is_zero(&minus_one), false); - try("mp_is_zero(0)", mp_is_zero(&zero), true); - try("mp_is_zero(1)", mp_is_zero(&one), false); - - try("mp_is_negative(-1)", mp_is_negative(&minus_one), true); - try("mp_is_negative(0)", mp_is_negative(&zero), false); - try("mp_is_negative(1)", mp_is_negative(&one), false); - - try("mp_is_integer(-1)", mp_is_integer(&minus_one), true); - try("mp_is_integer(0)", mp_is_integer(&zero), true); - try("mp_is_integer(1)", mp_is_integer(&one), true); - - try("mp_is_positive_integer(-1)", mp_is_positive_integer(&minus_one), false); - try("mp_is_positive_integer(0)", mp_is_positive_integer(&zero), true); - try("mp_is_positive_integer(1)", mp_is_positive_integer(&one), true); - - try("mp_is_natural(-1)", mp_is_natural(&minus_one), false); - try("mp_is_natural(0)", mp_is_natural(&zero), false); - try("mp_is_natural(1)", mp_is_natural(&one), true); - - try("mp_is_complex(-1)", mp_is_complex(&minus_one), false); - try("mp_is_complex(0)", mp_is_complex(&zero), false); - try("mp_is_complex(1)", mp_is_complex(&one), false); - - try("mp_is_equal(-1, -1)", mp_is_equal(&minus_one, &minus_one), true); - try("mp_is_equal(-1, 0)", mp_is_equal(&minus_one, &zero), false); - try("mp_is_equal(-1, 1)", mp_is_equal(&minus_one, &one), false); - try("mp_is_equal(0, -1)", mp_is_equal(&zero, &minus_one), false); - try("mp_is_equal(0, 0)", mp_is_equal(&zero, &zero), true); - try("mp_is_equal(0, 1)", mp_is_equal(&zero, &one), false); - try("mp_is_equal(1, -1)", mp_is_equal(&one, &minus_one), false); - try("mp_is_equal(1, 0)", mp_is_equal(&one, &zero), false); - try("mp_is_equal(1, 1)", mp_is_equal(&one, &one), true); - - try("mp_is_greater_than(0, -1)", mp_is_greater_than (&zero, &minus_one), true); - try("mp_is_greater_than(0, 0)", mp_is_greater_than (&zero, &zero), false); - try("mp_is_greater_than(0, 1)", mp_is_greater_than (&zero, &one), false); - try("mp_is_greater_than(1, -1)", mp_is_greater_than (&one, &minus_one), true); - try("mp_is_greater_than(1, 0)", mp_is_greater_than (&one, &zero), true); - try("mp_is_greater_than(1, 1)", mp_is_greater_than (&one, &one), false); - try("mp_is_greater_than(-1, -1)", mp_is_greater_than (&minus_one, &minus_one), false); - try("mp_is_greater_than(-1, 0)", mp_is_greater_than (&minus_one, &zero), false); - try("mp_is_greater_than(-1, 1)", mp_is_greater_than (&minus_one, &one), false); - - try("mp_is_greater_equal(0, -1)", mp_is_greater_equal (&zero, &minus_one), true); - try("mp_is_greater_equal(0, 0)", mp_is_greater_equal (&zero, &zero), true); - try("mp_is_greater_equal(0, 1)", mp_is_greater_equal (&zero, &one), false); - try("mp_is_greater_equal(1, -1)", mp_is_greater_equal (&one, &minus_one), true); - try("mp_is_greater_equal(1, 0)", mp_is_greater_equal (&one, &zero), true); - try("mp_is_greater_equal(1, 1)", mp_is_greater_equal (&one, &one), true); - try("mp_is_greater_equal(-1, -1)", mp_is_greater_equal (&minus_one, &minus_one), true); - try("mp_is_greater_equal(-1, 0)", mp_is_greater_equal (&minus_one, &zero), false); - try("mp_is_greater_equal(-1, 1)", mp_is_greater_equal (&minus_one, &one), false); - - try("mp_is_less_than(0, -1)", mp_is_less_than (&zero, &minus_one), false); - try("mp_is_less_than(0, 0)", mp_is_less_than (&zero, &zero), false); - try("mp_is_less_than(0, 1)", mp_is_less_than (&zero, &one), true); - try("mp_is_less_than(1, -1)", mp_is_less_than (&one, &minus_one), false); - try("mp_is_less_than(1, 0)", mp_is_less_than (&one, &zero), false); - try("mp_is_less_than(1, 1)", mp_is_less_than (&one, &one), false); - try("mp_is_less_than(-1, -1)", mp_is_less_than (&minus_one, &minus_one), false); - try("mp_is_less_than(-1, 0)", mp_is_less_than (&minus_one, &zero), true); - try("mp_is_less_than(-1, 1)", mp_is_less_than (&minus_one, &one), true); - - try("mp_is_less_equal(0, -1)", mp_is_less_equal (&zero, &minus_one), false); - try("mp_is_less_equal(0, 0)", mp_is_less_equal (&zero, &zero), true); - try("mp_is_less_equal(0, 1)", mp_is_less_equal (&zero, &one), true); - try("mp_is_less_equal(1, -1)", mp_is_less_equal (&one, &minus_one), false); - try("mp_is_less_equal(1, 0)", mp_is_less_equal (&one, &zero), false); - try("mp_is_less_equal(1, 1)", mp_is_less_equal (&one, &one), true); - try("mp_is_less_equal(-1, -1)", mp_is_less_equal (&minus_one, &minus_one), true); - try("mp_is_less_equal(-1, 0)", mp_is_less_equal (&minus_one, &zero), true); - try("mp_is_less_equal(-1, 1)", mp_is_less_equal (&minus_one, &one), true); + MPNumber zero, one, minus_one; + + mp_set_from_integer(0, &zero); + mp_set_from_integer(1, &one); + mp_set_from_integer(-1, &minus_one); + + try("mp_is_zero(-1)", mp_is_zero(&minus_one), false); + try("mp_is_zero(0)", mp_is_zero(&zero), true); + try("mp_is_zero(1)", mp_is_zero(&one), false); + + try("mp_is_negative(-1)", mp_is_negative(&minus_one), true); + try("mp_is_negative(0)", mp_is_negative(&zero), false); + try("mp_is_negative(1)", mp_is_negative(&one), false); + + try("mp_is_integer(-1)", mp_is_integer(&minus_one), true); + try("mp_is_integer(0)", mp_is_integer(&zero), true); + try("mp_is_integer(1)", mp_is_integer(&one), true); + + try("mp_is_positive_integer(-1)", mp_is_positive_integer(&minus_one), false); + try("mp_is_positive_integer(0)", mp_is_positive_integer(&zero), true); + try("mp_is_positive_integer(1)", mp_is_positive_integer(&one), true); + + try("mp_is_natural(-1)", mp_is_natural(&minus_one), false); + try("mp_is_natural(0)", mp_is_natural(&zero), false); + try("mp_is_natural(1)", mp_is_natural(&one), true); + + try("mp_is_complex(-1)", mp_is_complex(&minus_one), false); + try("mp_is_complex(0)", mp_is_complex(&zero), false); + try("mp_is_complex(1)", mp_is_complex(&one), false); + + try("mp_is_equal(-1, -1)", mp_is_equal(&minus_one, &minus_one), true); + try("mp_is_equal(-1, 0)", mp_is_equal(&minus_one, &zero), false); + try("mp_is_equal(-1, 1)", mp_is_equal(&minus_one, &one), false); + try("mp_is_equal(0, -1)", mp_is_equal(&zero, &minus_one), false); + try("mp_is_equal(0, 0)", mp_is_equal(&zero, &zero), true); + try("mp_is_equal(0, 1)", mp_is_equal(&zero, &one), false); + try("mp_is_equal(1, -1)", mp_is_equal(&one, &minus_one), false); + try("mp_is_equal(1, 0)", mp_is_equal(&one, &zero), false); + try("mp_is_equal(1, 1)", mp_is_equal(&one, &one), true); + + try("mp_is_greater_than(0, -1)", mp_is_greater_than (&zero, &minus_one), true); + try("mp_is_greater_than(0, 0)", mp_is_greater_than (&zero, &zero), false); + try("mp_is_greater_than(0, 1)", mp_is_greater_than (&zero, &one), false); + try("mp_is_greater_than(1, -1)", mp_is_greater_than (&one, &minus_one), true); + try("mp_is_greater_than(1, 0)", mp_is_greater_than (&one, &zero), true); + try("mp_is_greater_than(1, 1)", mp_is_greater_than (&one, &one), false); + try("mp_is_greater_than(-1, -1)", mp_is_greater_than (&minus_one, &minus_one), false); + try("mp_is_greater_than(-1, 0)", mp_is_greater_than (&minus_one, &zero), false); + try("mp_is_greater_than(-1, 1)", mp_is_greater_than (&minus_one, &one), false); + + try("mp_is_greater_equal(0, -1)", mp_is_greater_equal (&zero, &minus_one), true); + try("mp_is_greater_equal(0, 0)", mp_is_greater_equal (&zero, &zero), true); + try("mp_is_greater_equal(0, 1)", mp_is_greater_equal (&zero, &one), false); + try("mp_is_greater_equal(1, -1)", mp_is_greater_equal (&one, &minus_one), true); + try("mp_is_greater_equal(1, 0)", mp_is_greater_equal (&one, &zero), true); + try("mp_is_greater_equal(1, 1)", mp_is_greater_equal (&one, &one), true); + try("mp_is_greater_equal(-1, -1)", mp_is_greater_equal (&minus_one, &minus_one), true); + try("mp_is_greater_equal(-1, 0)", mp_is_greater_equal (&minus_one, &zero), false); + try("mp_is_greater_equal(-1, 1)", mp_is_greater_equal (&minus_one, &one), false); + + try("mp_is_less_than(0, -1)", mp_is_less_than (&zero, &minus_one), false); + try("mp_is_less_than(0, 0)", mp_is_less_than (&zero, &zero), false); + try("mp_is_less_than(0, 1)", mp_is_less_than (&zero, &one), true); + try("mp_is_less_than(1, -1)", mp_is_less_than (&one, &minus_one), false); + try("mp_is_less_than(1, 0)", mp_is_less_than (&one, &zero), false); + try("mp_is_less_than(1, 1)", mp_is_less_than (&one, &one), false); + try("mp_is_less_than(-1, -1)", mp_is_less_than (&minus_one, &minus_one), false); + try("mp_is_less_than(-1, 0)", mp_is_less_than (&minus_one, &zero), true); + try("mp_is_less_than(-1, 1)", mp_is_less_than (&minus_one, &one), true); + + try("mp_is_less_equal(0, -1)", mp_is_less_equal (&zero, &minus_one), false); + try("mp_is_less_equal(0, 0)", mp_is_less_equal (&zero, &zero), true); + try("mp_is_less_equal(0, 1)", mp_is_less_equal (&zero, &one), true); + try("mp_is_less_equal(1, -1)", mp_is_less_equal (&one, &minus_one), false); + try("mp_is_less_equal(1, 0)", mp_is_less_equal (&one, &zero), false); + try("mp_is_less_equal(1, 1)", mp_is_less_equal (&one, &one), true); + try("mp_is_less_equal(-1, -1)", mp_is_less_equal (&minus_one, &minus_one), true); + try("mp_is_less_equal(-1, 0)", mp_is_less_equal (&minus_one, &zero), true); + try("mp_is_less_equal(-1, 1)", mp_is_less_equal (&minus_one, &one), true); } -void -unittest() -{ - test_mp(); - test_numbers(); - test_conversions(); - test_equations(); - exit(fails > 0 ? 1 : 0); +void unittest() +{ + test_mp(); + test_numbers(); + test_conversions(); + test_equations(); + exit(fails > 0 ? 1 : 0); } -- cgit v1.2.1