[gcalctool] Add temperature conversion



commit 68333595437c809057e13db829adc01d97a0b414
Author: Robert Ancell <robert ancell canonical com>
Date:   Fri Jan 28 16:53:42 2011 +1000

    Add temperature conversion

 src/unit-category.c |   13 +++---
 src/unit-manager.c  |  106 +++++++++++++++++++++++++--------------------------
 src/unit.c          |   88 +++++++++++++++++++++++++++++++++---------
 src/unit.h          |   11 ++++-
 src/unittest.c      |   15 +++++++-
 5 files changed, 152 insertions(+), 81 deletions(-)
---
diff --git a/src/unit-category.c b/src/unit-category.c
index 324e5d1..972ef08 100644
--- a/src/unit-category.c
+++ b/src/unit-category.c
@@ -85,16 +85,17 @@ gboolean
 unit_category_convert(UnitCategory *category, const MPNumber *x, const char *x_units, const char *z_units, MPNumber *z)
 {
     Unit *unit_x, *unit_z;
+    MPNumber t;
 
     unit_x = get_unit(category, x_units);
     unit_z = get_unit(category, z_units);
-    if (unit_x && unit_z && unit_get_value(unit_x) && unit_get_value(unit_z)) {
-        mp_multiply(x, unit_get_value(unit_x), z);
-        mp_divide(z, unit_get_value(unit_z), z);
-        return TRUE;
-    }
+    if (!unit_x || !unit_z)
+        return FALSE;
+  
+    unit_convert_from(unit_x, x, &t);
+    unit_convert_to(unit_z, &t, z);
 
-    return FALSE;
+    return TRUE;
 }
 
 
diff --git a/src/unit-manager.c b/src/unit-manager.c
index 1491ba6..ce11819 100644
--- a/src/unit-manager.c
+++ b/src/unit-manager.c
@@ -27,58 +27,59 @@ unit_manager_get_default(void)
         gchar *name;
         gchar *display_name;
         gchar *format;
-        gchar *value;
+        gchar *from_function;
+        gchar *to_function;
         gchar *symbols;
     } units[] =
     {
         /* FIXME: Approximations of 1/(units in a circle), therefore, 360 deg != 400 grads */
-        {"angle",    "degree",              N_("Degrees"),           NC_("unit-format", "%s degrees"),  "0.002777778",       NC_("unit-symbols", "degree,degrees,deg")},
-        {NULL,       "radian",              N_("Radians"),           NC_("unit-format", "%s radians"),  "0.159154943",       NC_("unit-symbols", "radian,radians,rad")},
-        {NULL,       "gradian",             N_("Gradians"),          NC_("unit-format", "%s gradians"), "0.0025",            NC_("unit-symbols", "gradian,gradians,grad")},
-        {"length",   "parsec",              N_("Parsecs"),           NC_("unit-format", "%s pc"),       "30857000000000000", NC_("unit-symbols", "parsec,parsecs,pc")},
-        {NULL,       "lightyear",           N_("Light Years"),       NC_("unit-format", "%s ly"),       "9460730472580800",  NC_("unit-symbols", "lightyear,lightyears,ly")},
-        {NULL,       "astronomical-unit",   N_("Austronomical Units"), NC_("unit-format", "%s au"),     "149597870691",      NC_("unit-symbols", "au")},
-        {NULL,       "nautical-mile",       N_("Nautical Miles"),    NC_("unit-format", "%s nm"),       "1852000",           NC_("unit-symbols", "nm")},
-        {NULL,       "mile",                N_("Miles"),             NC_("unit-format", "%s mi"),       "1609.344",          NC_("unit-symbols", "mile,miles,mi")},
-        {NULL,       "kilometer",           N_("Kilometers"),        NC_("unit-format", "%s km"),       "1000",              NC_("unit-symbols", "kilometer,kilometers,km,kms")},
-        {NULL,       "cable",               N_("Cables"),            NC_("unit-format", "%s cb"),       "219.456",           NC_("unit-symbols", "cable,cables,cb")},
-        {NULL,       "fathom",              N_("Fathoms"),           NC_("unit-format", "%s ftm"),      "1.8288",            NC_("unit-symbols", "fathom,fathoms,ftm")},
-        {NULL,       "meter",               N_("Meters"),            NC_("unit-format", "%s m"),        "1",                 NC_("unit-symbols", "meter,meters,m")},
-        {NULL,       "yard",                N_("Yards"),             NC_("unit-format", "%s yd"),       "0.9144",            NC_("unit-symbols", "yard,yards,yd")},
-        {NULL,       "foot",                N_("Feet"),              NC_("unit-format", "%s ft"),       "0.3048",            NC_("unit-symbols", "foot,feet,ft")},
-        {NULL,       "inch",                N_("Inches"),            NC_("unit-format", "%s in"),       "0.0254",            NC_("unit-symbols", "inch,inches,in")},
-        {NULL,       "centimeter",          N_("Centimeters"),       NC_("unit-format", "%s cm"),       "0.01",              NC_("unit-symbols", "centimeter,centimeters,cm,cms")},
-        {NULL,       "millimeter",          N_("Millimeters"),       NC_("unit-format", "%s mm"),       "0.001",             NC_("unit-symbols", "millimeter,millimeters,mm")},
-        {NULL,       "micrometer",          N_("Micrometers"),       NC_("unit-format", "%s μm"),       "0.000001",          NC_("unit-symbols", "micrometer,micrometers,um")},
-        {NULL,       "nanometer",           N_("Nanometers"),        NC_("unit-format", "%s nm"),       "0.000000001",       NC_("unit-symbols", "nanometer,nanometers")},
-        {"area",     "hectare",             N_("Hectares"),          NC_("unit-format", "%s ha"),       "10000",             NC_("unit-symbols", "hectare,hectares,ha")},
-        {NULL,       "acre",                N_("Acres"),             NC_("unit-format", "%s acres"),    "4046.8564224",      NC_("unit-symbols", "acre,acres")},
-        {NULL,       "square-meter",        N_("Square Meter"),      NC_("unit-format", "%s m²"),       "1",                 NC_("unit-symbols", "m²")},
-        {NULL,       "square-centimeter",   N_("Square Centimeter"), NC_("unit-format", "%s cm²"),      "0.001",             NC_("unit-symbols", "cm²")},
-        {NULL,       "square-millimeter",   N_("Square Millimeter"), NC_("unit-format", "%s mm²"),      "0.000001",          NC_("unit-symbols", "mm²")},
-        {"volume",   "cubic-meter",         N_("Cubic Meters"),      NC_("unit-format", "%s m³"),       "1000",              NC_("unit-symbols", "m³")},
-        {NULL,       "gallon",              N_("Gallons"),           NC_("unit-format", "%s gal"),      "3.785412",          NC_("unit-symbols", "gallon,gallons,gal")},
-        {NULL,       "litre",               N_("Litres"),            NC_("unit-format", "%s L"),        "1",                 NC_("unit-symbols", "litre,litres,liter,liters,L")},
-        {NULL,       "quart",               N_("Quarts"),            NC_("unit-format", "%s qt"),       "0.9463529",         NC_("unit-symbols", "quart,quarts,qt")},
-        {NULL,       "pint",                N_("Pints"),             NC_("unit-format", "%s pt"),       "0.4731765",         NC_("unit-symbols", "pint,pints,pt")},
-        {NULL,       "millilitre",          N_("Millilitres"),       NC_("unit-format", "%s mL"),       "0.001",             NC_("unit-symbols", "millilitre,millilitres,milliliter,milliliters,mL,cm³")},
-        {NULL,       "microlitre",          N_("Microlitre"),        NC_("unit-format", "%s μL"),       "0.000001",          NC_("unit-symbols", "mm³,μL,uL")},
-        {"weight",   "tonne",               N_("Tonnes"),            NC_("unit-format", "%s T"),        "1000",              NC_("unit-symbols", "tonne,tonnes")},
-        {NULL,       "kilograms",           N_("Kilograms"),         NC_("unit-format", "%s kg"),       "1",                 NC_("unit-symbols", "kilogram,kilograms,kilogramme,kilogrammes,kg,kgs")},
-        {NULL,       "pound",               N_("Pounds"),            NC_("unit-format", "%s lb"),       "0.45359237",        NC_("unit-symbols", "pound,pounds,lb")},
-        {NULL,       "ounce",               N_("Ounces"),            NC_("unit-format", "%s oz"),       "0.02834952",        NC_("unit-symbols", "ounce,ounces,oz")},
-        {NULL,       "gram",                N_("Grams"),             NC_("unit-format", "%s g"),        "0.001",             NC_("unit-symbols", "gram,grams,gramme,grammes,g")},
-        {"duration", "year",                N_("Years"),             NC_("unit-format", "%s years"),    "31557600",          NC_("unit-symbols", "year,years")},
-        {NULL,       "day",                 N_("Days"),              NC_("unit-format", "%s days"),     "86400",             NC_("unit-symbols", "day,days")},
-        {NULL,       "hour",                N_("Hours"),             NC_("unit-format", "%s hours"),    "3600",              NC_("unit-symbols", "hour,hours")},
-        {NULL,       "minute",              N_("Minutes"),           NC_("unit-format", "%s minutes"),  "60",                NC_("unit-symbols", "minute,minutes")},
-        {NULL,       "second",              N_("Seconds"),           NC_("unit-format", "%s s"),        "1",                 NC_("unit-symbols", "second,seconds,s")},
-        {NULL,       "millisecond",         N_("Milliseconds"),      NC_("unit-format", "%s ms"),       "0.001",             NC_("unit-symbols", "millisecond,milliseconds,ms")},
-        {NULL,       "microsecond",         N_("Microseconds"),      NC_("unit-format", "%s μs"),       "0.000001",          NC_("unit-symbols", "microsecond,microseconds,us")},
-        // FIXME: Need offset
-        //{"temperature", "degree-celcius",   N_("Celcius"),           NC_("unit-format", "%sË?C"),        "1",                 "Ë?C"},
-        //{NULL,          "degree-farenheit", N_("Farenheit"),         NC_("unit-format", "%sË?F"),        "",                  "Ë?F"},
-        //{NULL,          "degree-kelvin",    N_("Kelvin"),            NC_("unit-format", "%sË?K"),        "",                  "Ë?K"},
+        {"angle",    "degree",              N_("Degrees"),           NC_("unit-format", "%s degrees"),  "Ï?*x/180",            "180x/Ï?",         NC_("unit-symbols", "degree,degrees,deg")},
+        {NULL,       "radian",              N_("Radians"),           NC_("unit-format", "%s radians"),  "x",                  "x",              NC_("unit-symbols", "radian,radians,rad")},
+        {NULL,       "gradian",             N_("Gradians"),          NC_("unit-format", "%s gradians"), "Ï?*x/200",            "200x/Ï?",         NC_("unit-symbols", "gradian,gradians,grad")},
+        {"length",   "parsec",              N_("Parsecs"),           NC_("unit-format", "%s pc"),       "30857000000000000x", "x/30857000000000000", NC_("unit-symbols", "parsec,parsecs,pc")},
+        {NULL,       "lightyear",           N_("Light Years"),       NC_("unit-format", "%s ly"),       "9460730472580800x",  "x/9460730472580800",  NC_("unit-symbols", "lightyear,lightyears,ly")},
+        {NULL,       "astronomical-unit",   N_("Austronomical Units"), NC_("unit-format", "%s au"),     "149597870691x",      "x/149597870691", NC_("unit-symbols", "au")},
+        {NULL,       "nautical-mile",       N_("Nautical Miles"),    NC_("unit-format", "%s nm"),       "1852000x",           "x/1852000",      NC_("unit-symbols", "nm")},
+        {NULL,       "mile",                N_("Miles"),             NC_("unit-format", "%s mi"),       "1609.344x",          "x/1609.344",     NC_("unit-symbols", "mile,miles,mi")},
+        {NULL,       "kilometer",           N_("Kilometers"),        NC_("unit-format", "%s km"),       "1000x",              "x/1000",         NC_("unit-symbols", "kilometer,kilometers,km,kms")},
+        {NULL,       "cable",               N_("Cables"),            NC_("unit-format", "%s cb"),       "219.456x",           "x/219.456",      NC_("unit-symbols", "cable,cables,cb")},
+        {NULL,       "fathom",              N_("Fathoms"),           NC_("unit-format", "%s ftm"),      "1.8288x",            "x/1.8288",       NC_("unit-symbols", "fathom,fathoms,ftm")},
+        {NULL,       "meter",               N_("Meters"),            NC_("unit-format", "%s m"),        "x",                  "x",              NC_("unit-symbols", "meter,meters,m")},
+        {NULL,       "yard",                N_("Yards"),             NC_("unit-format", "%s yd"),       "0.9144x",            "x/0.9144",       NC_("unit-symbols", "yard,yards,yd")},
+        {NULL,       "foot",                N_("Feet"),              NC_("unit-format", "%s ft"),       "0.3048x",            "x/0.3048",       NC_("unit-symbols", "foot,feet,ft")},
+        {NULL,       "inch",                N_("Inches"),            NC_("unit-format", "%s in"),       "0.0254x",            "x/0.0254",       NC_("unit-symbols", "inch,inches,in")},
+        {NULL,       "centimeter",          N_("Centimeters"),       NC_("unit-format", "%s cm"),       "x/100",              "100x",           NC_("unit-symbols", "centimeter,centimeters,cm,cms")},
+        {NULL,       "millimeter",          N_("Millimeters"),       NC_("unit-format", "%s mm"),       "x/1000",             "1000x",          NC_("unit-symbols", "millimeter,millimeters,mm")},
+        {NULL,       "micrometer",          N_("Micrometers"),       NC_("unit-format", "%s μm"),       "x/1000000",          "1000000x",       NC_("unit-symbols", "micrometer,micrometers,um")},
+        {NULL,       "nanometer",           N_("Nanometers"),        NC_("unit-format", "%s nm"),       "x/1000000000",       "1000000000x",    NC_("unit-symbols", "nanometer,nanometers")},
+        {"area",     "hectare",             N_("Hectares"),          NC_("unit-format", "%s ha"),       "10000x",             "x/10000",        NC_("unit-symbols", "hectare,hectares,ha")},
+        {NULL,       "acre",                N_("Acres"),             NC_("unit-format", "%s acres"),    "4046.8564224x",      "x/4046.8564224", NC_("unit-symbols", "acre,acres")},
+        {NULL,       "square-meter",        N_("Square Meter"),      NC_("unit-format", "%s m²"),       "x",                  "x",              NC_("unit-symbols", "m²")},
+        {NULL,       "square-centimeter",   N_("Square Centimeter"), NC_("unit-format", "%s cm²"),      "0.001x",             "1000x",          NC_("unit-symbols", "cm²")},
+        {NULL,       "square-millimeter",   N_("Square Millimeter"), NC_("unit-format", "%s mm²"),      "0.000001x",          "1000000x",       NC_("unit-symbols", "mm²")},
+        {"volume",   "cubic-meter",         N_("Cubic Meters"),      NC_("unit-format", "%s m³"),       "1000x",              "x/1000",         NC_("unit-symbols", "m³")},
+        {NULL,       "gallon",              N_("Gallons"),           NC_("unit-format", "%s gal"),      "3.785412x",          "x/3.785412",     NC_("unit-symbols", "gallon,gallons,gal")},
+        {NULL,       "litre",               N_("Litres"),            NC_("unit-format", "%s L"),        "x",                  "x",              NC_("unit-symbols", "litre,litres,liter,liters,L")},
+        {NULL,       "quart",               N_("Quarts"),            NC_("unit-format", "%s qt"),       "0.9463529x",         "x/0.9463529",    NC_("unit-symbols", "quart,quarts,qt")},
+        {NULL,       "pint",                N_("Pints"),             NC_("unit-format", "%s pt"),       "0.4731765x",         "x/0.4731765",    NC_("unit-symbols", "pint,pints,pt")},
+        {NULL,       "millilitre",          N_("Millilitres"),       NC_("unit-format", "%s mL"),       "0.001x",             "1000x",          NC_("unit-symbols", "millilitre,millilitres,milliliter,milliliters,mL,cm³")},
+        {NULL,       "microlitre",          N_("Microlitre"),        NC_("unit-format", "%s μL"),       "0.000001x",          "1000000x",       NC_("unit-symbols", "mm³,μL,uL")},
+        {"weight",   "tonne",               N_("Tonnes"),            NC_("unit-format", "%s T"),        "x/1000",             "1000x",          NC_("unit-symbols", "tonne,tonnes")},
+        {NULL,       "kilograms",           N_("Kilograms"),         NC_("unit-format", "%s kg"),       "x",                  "x",              NC_("unit-symbols", "kilogram,kilograms,kilogramme,kilogrammes,kg,kgs")},
+        {NULL,       "pound",               N_("Pounds"),            NC_("unit-format", "%s lb"),       "0.45359237x",        "x/0.45359237",   NC_("unit-symbols", "pound,pounds,lb")},
+        {NULL,       "ounce",               N_("Ounces"),            NC_("unit-format", "%s oz"),       "0.02834952x",        "x/0.02834952",   NC_("unit-symbols", "ounce,ounces,oz")},
+        {NULL,       "gram",                N_("Grams"),             NC_("unit-format", "%s g"),        "0.001x",             "1000x",          NC_("unit-symbols", "gram,grams,gramme,grammes,g")},
+        {"duration", "year",                N_("Years"),             NC_("unit-format", "%s years"),    "31557600x",          "x/31557600",     NC_("unit-symbols", "year,years")},
+        {NULL,       "day",                 N_("Days"),              NC_("unit-format", "%s days"),     "86400x",             "x/86400",        NC_("unit-symbols", "day,days")},
+        {NULL,       "hour",                N_("Hours"),             NC_("unit-format", "%s hours"),    "3600x",              "x/3600",         NC_("unit-symbols", "hour,hours")},
+        {NULL,       "minute",              N_("Minutes"),           NC_("unit-format", "%s minutes"),  "60x",                "x/60",           NC_("unit-symbols", "minute,minutes")},
+        {NULL,       "second",              N_("Seconds"),           NC_("unit-format", "%s s"),        "x",                  "x",              NC_("unit-symbols", "second,seconds,s")},
+        {NULL,       "millisecond",         N_("Milliseconds"),      NC_("unit-format", "%s ms"),       "0.001x",             "1000x",          NC_("unit-symbols", "millisecond,milliseconds,ms")},
+        {NULL,       "microsecond",         N_("Microseconds"),      NC_("unit-format", "%s μs"),       "0.000001x",          "1000000x",       NC_("unit-symbols", "microsecond,microseconds,us")},
+        {"temperature", "degree-celcius",   N_("Celcius"),           NC_("unit-format", "%sË?C"),        "x+273.15",           "x-273.15",       NC_("unit-symbols", "degC,Ë?C")},
+        {NULL,          "degree-farenheit", N_("Farenheit"),         NC_("unit-format", "%sË?F"),        "(x+459.67)*5/9",     "x*9/5-459.67",   NC_("unit-symbols", "degF,Ë?F")},
+        {NULL,          "degree-kelvin",    N_("Kelvin"),            NC_("unit-format", "%sË?K"),        "x",                  "x",              NC_("unit-symbols", "degK,Ë?K")},
+        {NULL,          "degree-rankine",   N_("Rankine"),           NC_("unit-format", "%sË?R"),        "x*5/9",              "x*9/5",          NC_("unit-symbols", "degR,Ë?R")},
         { NULL, NULL, NULL, NULL, NULL, NULL }
     };
 
@@ -93,18 +94,15 @@ unit_manager_get_default(void)
     unit_manager_add_category(default_unit_manager, "volume",   _("Volume"));
     unit_manager_add_category(default_unit_manager, "weight",   _("Weight"));
     unit_manager_add_category(default_unit_manager, "duration", _("Duration"));
-    // FIXME: Need offset
-    //unit_manager_add_category(default_unit_manager, "temperature", _("Temperature"));
+    unit_manager_add_category(default_unit_manager, "temperature", _("Temperature"));
 
     for (i = 0; units[i].name; i++) {
-        MPNumber t;
         if (units[i].category)
             category = unit_manager_get_category(default_unit_manager, units[i].category);
-        mp_set_from_string(units[i].value, 10, &t);
         unit_category_add_unit(category, unit_new(units[i].name,
                                                   _(units[i].display_name),
                                                   g_dpgettext2(NULL, "unit-format", units[i].format),
-                                                  &t,
+                                                  units[i].from_function, units[i].to_function,
                                                   g_dpgettext2(NULL, "unit-symbols", units[i].symbols)));
     }
 
@@ -116,7 +114,7 @@ unit_manager_get_default(void)
         Unit *unit;
 
         format = g_strdup_printf("%s%%s", currency_get_symbol(currency));
-        unit = unit_new(currency_get_name(currency), currency_get_name(currency), format, NULL, currency_get_name(currency));
+        unit = unit_new(currency_get_name(currency), currency_get_name(currency), format, NULL, NULL, currency_get_name(currency));
         g_free(format);
 
         unit_category_add_unit(category, unit);
diff --git a/src/unit.c b/src/unit.c
index 319b7ae..ed12c5e 100644
--- a/src/unit.c
+++ b/src/unit.c
@@ -2,6 +2,7 @@
 
 #include "unit.h"
 #include "mp-serializer.h"
+#include "mp-equation.h"
 #include "currency-manager.h" // FIXME: Move out of here
 
 struct UnitPrivate
@@ -10,8 +11,8 @@ struct UnitPrivate
     gchar *display_name;
     gchar *format;
     GList *symbols;
-    MPNumber value;
-    gboolean has_value;
+    gchar *from_function;
+    gchar *to_function;
     MpSerializer *serializer;
 };
 
@@ -19,7 +20,12 @@ G_DEFINE_TYPE (Unit, unit, G_TYPE_OBJECT);
 
 
 Unit *
-unit_new(const gchar *name, const gchar *display_name, const gchar *format, MPNumber *value, const gchar *symbols)
+unit_new(const gchar *name,
+         const gchar *display_name,
+         const gchar *format,
+         const gchar *from_function,
+         const gchar *to_function,
+         const gchar *symbols)
 {
     Unit *unit = g_object_new(unit_get_type(), NULL);
     gchar **symbol_names;
@@ -28,14 +34,8 @@ unit_new(const gchar *name, const gchar *display_name, const gchar *format, MPNu
     unit->priv->name = g_strdup(name);
     unit->priv->display_name = g_strdup(display_name);
     unit->priv->format = g_strdup(format);
-    if (value)
-    {
-        unit->priv->has_value = TRUE;
-        mp_set_from_mp(value, &unit->priv->value);
-    }
-    else
-        unit->priv->has_value = FALSE;
-
+    unit->priv->from_function = g_strdup(from_function);
+    unit->priv->to_function = g_strdup(to_function);
     symbol_names = g_strsplit(symbols, ",", 0);
     for (i = 0; symbol_names[i]; i++)
         unit->priv->symbols = g_list_append(unit->priv->symbols, g_strdup(symbol_names[i]));
@@ -81,13 +81,65 @@ unit_get_symbols(Unit *unit)
 }
 
 
-const MPNumber *
-unit_get_value(Unit *unit)
+static int
+variable_is_defined(const char *name, void *data)
+{
+    return TRUE;
+}
+
+
+static int
+get_variable(const char *name, MPNumber *z, void *data)
+{
+    MPNumber *x = data;
+    mp_set_from_mp(x, z);
+    return TRUE;
+}
+
+
+static void
+solve_function(const gchar *function, const MPNumber *x, MPNumber *z)
+{
+    MPEquationOptions options;
+    int ret;
+
+    memset(&options, 0, sizeof(options));
+    options.base = 10;
+    options.wordlen = 32;
+    options.variable_is_defined = variable_is_defined;
+    options.get_variable = get_variable;
+    options.callback_data = (void *)x;
+    ret = mp_equation_parse(function, &options, z, NULL);
+    if (ret)
+        g_warning("Failed to convert value: %s", function);
+}
+
+
+void
+unit_convert_from(Unit *unit, const MPNumber *x, MPNumber *z)
 {
-    if (unit->priv->has_value)
-        return &unit->priv->value;
-    else
-        return currency_manager_get_value(currency_manager_get_default(), unit->priv->name); // FIXME: Hack to make currency work
+    if (unit->priv->from_function)
+        solve_function(unit->priv->from_function, x, z);
+    else {
+        // FIXME: Hack to make currency work
+        const MPNumber *r;
+        r = currency_manager_get_value(currency_manager_get_default(), unit->priv->name);
+        mp_divide(x, r, z);
+    }
+}
+
+
+void
+unit_convert_to(Unit *unit, const MPNumber *x, MPNumber *z)
+{
+    if (unit->priv->from_function)
+        solve_function(unit->priv->to_function, x, z);
+    else {
+        // FIXME: Hack to make currency work
+        const MPNumber *r;
+        r = currency_manager_get_value(currency_manager_get_default(), unit->priv->name);
+        mp_multiply(x, r, z);
+    }
 }
 
 
@@ -99,7 +151,7 @@ unit_format(Unit *unit, MPNumber *x)
     number_text = mp_serializer_to_string(unit->priv->serializer, x);
     text = g_strdup_printf(unit->priv->format, number_text);
     g_free(number_text);
-  
+
     return text;
 }
 
diff --git a/src/unit.h b/src/unit.h
index eb646ed..9a1eb0f 100644
--- a/src/unit.h
+++ b/src/unit.h
@@ -23,7 +23,12 @@ typedef struct
 
 GType unit_get_type(void);
 
-Unit *unit_new(const gchar *name, const gchar *display_name, const gchar *format, MPNumber *value, const gchar *symbols);
+Unit *unit_new(const gchar *name,
+               const gchar *display_name,
+               const gchar *format,
+               const gchar *from_function,
+               const gchar *to_function,
+               const gchar *symbols);
 
 const gchar *unit_get_name(Unit *unit);
 
@@ -33,7 +38,9 @@ gboolean unit_matches_symbol(Unit *unit, const gchar *symbol);
 
 const GList *unit_get_symbols(Unit *unit);
 
-const MPNumber *unit_get_value(Unit *unit);
+void unit_convert_from(Unit *unit, const MPNumber *x, MPNumber *z);
+
+void unit_convert_to(Unit *unit, const MPNumber *x, MPNumber *z);
 
 gchar *unit_format(Unit *unit, MPNumber *x);
 
diff --git a/src/unittest.c b/src/unittest.c
index 68c705b..17103ec 100644
--- a/src/unittest.c
+++ b/src/unittest.c
@@ -132,6 +132,10 @@ test_conversions()
     options.wordlen = 32;
     options.angle_units = MP_DEGREES;
     options.convert = do_convert;
+  
+    /* Angle units */
+    //test("Ï? radians in degrees", "180", 0);
+    test("100 gradians in degrees", "90", 0);
 
     /* Length */
     test("1 meter in mm", "1000", 0);  
@@ -147,8 +151,17 @@ test_conversions()
     /* Weight */
     test("1 kg in pounds", "2.204622622", 0);
   
-    /* Time */
+    /* Duration */
     test("1 minute in seconds", "60", 0);
+    test("1s in ms", "1000", 0);
+
+    /* Temperature */
+    //test("100Ë?C in Ë?F", "", 0);
+    //test("0Ë?C in Ë?F", "32", 0);
+    //test("0Ë?K in Ë?C", "â??273.15", 0);
+    test("100degC in degF", "212", 0);
+    test("0degC in degF", "32", 0);
+    test("0degK in degC", "â??273.15", 0);
 }
 
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]