[Vala] [PATCH] Functions for inspecting values and ranges of numbers



---
 trunk/vala/tests/Makefile.am   |    4 +
 trunk/vala/tests/test-030.out  |    8 ++
 trunk/vala/tests/test-030.vala |   39 ++++++++++
 trunk/vala/tests/test-031.out  |   18 +++++
 trunk/vala/tests/test-031.vala |   91 +++++++++++++++++++++++
 trunk/vala/vapi/glib-2.0.vala  |  154 +++++++++++++++++++++++++++++++++++++++-
 6 files changed, 312 insertions(+), 2 deletions(-)

diff --git a/trunk/vala/tests/Makefile.am b/trunk/vala/tests/Makefile.am
index be001c2..549eaf0 100644
--- a/trunk/vala/tests/Makefile.am
+++ b/trunk/vala/tests/Makefile.am
@@ -32,6 +32,8 @@ TESTS = \
        test-027.vala \
        test-028.vala \
        test-029.vala \
+       test-030.vala \
+       test-031.vala \
        $(NULL)
 
 EXTRA_DIST = \
@@ -94,4 +96,6 @@ EXTRA_DIST = \
        test-027.out \
        test-028.out \
        test-029.out \
+       test-030.out \
+       test-031.out \
        $(NULL)
diff --git a/trunk/vala/tests/test-030.out b/trunk/vala/tests/test-030.out
new file mode 100644
index 0000000..7a16172
--- /dev/null
+++ b/trunk/vala/tests/test-030.out
@@ -0,0 +1,8 @@
+int8: -128...127
+int16: -32768...32767
+int32: -2147483648...2147483647
+int64: -9223372036854775808...9223372036854775807
+uint8: 0...255
+uint16: 0...65535
+uint32: 0...4294967295
+uint64: 0...18446744073709551615
diff --git a/trunk/vala/tests/test-030.vala b/trunk/vala/tests/test-030.vala
new file mode 100644
index 0000000..4adf2b9
--- /dev/null
+++ b/trunk/vala/tests/test-030.vala
@@ -0,0 +1,39 @@
+using GLib;
+
+class Maman.Foo {
+       static void main(string[] args) {
+               stdout.printf(
+                       "int8: %s...%s\n",
+                       int8.MIN_VALUE.to_string(), 
+                       int8.MAX_VALUE.to_string());
+               stdout.printf(
+                       "int16: %s...%s\n",
+                       int16.MIN_VALUE.to_string(),
+                       int16.MAX_VALUE.to_string());
+               stdout.printf(
+                       "int32: %s...%s\n",
+                       int32.MIN_VALUE.to_string(),
+                       int32.MAX_VALUE.to_string());
+               stdout.printf(
+                       "int64: %s...%s\n",
+                       int64.MIN_VALUE.to_string(),
+                       int64.MAX_VALUE.to_string());
+
+               stdout.printf(
+                       "uint8: %s...%s\n",
+                       uint8.MIN_VALUE.to_string(),
+                       uint8.MAX_VALUE.to_string());
+               stdout.printf(
+                       "uint16: %s...%s\n",
+                       uint16.MIN_VALUE.to_string(),
+                       uint16.MAX_VALUE.to_string());
+               stdout.printf(
+                       "uint32: %s...%s\n",
+                       uint32.MIN_VALUE.to_string(),
+                       uint32.MAX_VALUE.to_string());
+               stdout.printf(
+                       "uint64: %s...%s\n",
+                       uint64.MIN_VALUE.to_string(),
+                       uint64.MAX_VALUE.to_string());
+       }
+}
diff --git a/trunk/vala/tests/test-031.out b/trunk/vala/tests/test-031.out
new file mode 100644
index 0000000..9fef60e
--- /dev/null
+++ b/trunk/vala/tests/test-031.out
@@ -0,0 +1,18 @@
+float: range=1.17549e-38...3.40282e+38
+       digits=24(6), exp=-125..128(-37..38)
+       epsilon=1.19209e-07, infinity=inf/-inf, nan=nan
+float(1.19209e-07): nan=false, finite=true, normal=true, infinity=none
+float(0): nan=false, finite=true, normal=false, infinity=none
+float(1): nan=false, finite=true, normal=true, infinity=none
+float(-inf): nan=false, finite=false, normal=false, infinity=negative
+float(inf): nan=false, finite=false, normal=false, infinity=positive
+float(nan): nan=true, finite=false, normal=false, infinity=none
+double: range=2.22507e-308...1.79769e+308
+        digits=53(15), exp=-1021..1024(-307..308)
+        epsilon=2.22045e-16, infinity=inf/-inf, nan=nan
+double(2.22045e-16): nan=false, finite=true, normal=true, infinity=none
+double(0): nan=false, finite=true, normal=false, infinity=none
+double(1): nan=false, finite=true, normal=true, infinity=none
+double(-inf): nan=false, finite=false, normal=false, infinity=negative
+double(inf): nan=false, finite=false, normal=false, infinity=positive
+double(nan): nan=true, finite=false, normal=false, infinity=none
diff --git a/trunk/vala/tests/test-031.vala b/trunk/vala/tests/test-031.vala
new file mode 100644
index 0000000..faeabae
--- /dev/null
+++ b/trunk/vala/tests/test-031.vala
@@ -0,0 +1,91 @@
+using GLib;
+
+class Maman.Foo {
+       const float[] FLOAT_TESTS = { 
+               float.EPSILON, 0.0, 1.0, 
+               float.NEGATIVE_INFINITY,
+               float.POSITIVE_INFINITY,
+               float.NAN
+       };
+
+       const double[] DOUBLE_TESTS = { 
+               double.EPSILON, 0.0, 1.0, 
+               double.NEGATIVE_INFINITY,
+               double.POSITIVE_INFINITY,
+               double.NAN
+       };
+
+       static void main(string[] args) {
+               stdout.printf(
+                       "float: range=%s...%s\n" +
+                       "       digits=%s(%s), exp=%s..%s(%s..%s)\n" +
+                       "       epsilon=%s, infinity=%s/%s, nan=%s\n",
+
+                       float.MIN_VALUE.to_string(), 
+                       float.MAX_VALUE.to_string(),
+
+                       float.MANTISE_DIGITS.to_string(),
+                       float.SECURE_DIGITS.to_string(),
+                       float.MIN_EXPONENT.to_string(), 
+                       float.MAX_EXPONENT.to_string(),
+                       float.MIN_EXPONENT_10.to_string(), 
+                       float.MAX_EXPONENT_10.to_string(),
+
+                       float.EPSILON.to_string(),
+                       float.POSITIVE_INFINITY.to_string(),
+                       float.NEGATIVE_INFINITY.to_string(),
+                       float.NAN.to_string());
+
+               for(int i = 0; i < 6; i++) { // XXX use foreach
+                       float value = FLOAT_TESTS[i];
+                       int infinity = value.get_infinity();
+
+                       stdout.printf(
+                               "float(%g): nan=%s, finite=%s, normal=%s, infinity=%s\n", 
+                               
+                               value,
+                               value.is_nan() ? "true" : "false", 
+                               value.is_finite() ? "true" : "false",
+                               value.is_normal() ? "true" : "false", 
+
+                               infinity > 0 ? "positive" : 
+                               infinity < 0 ? "negative" : "none");
+               }
+
+               stdout.printf(
+                       "double: range=%s...%s\n" +
+                       "        digits=%s(%s), exp=%s..%s(%s..%s)\n" +
+                       "        epsilon=%s, infinity=%s/%s, nan=%s\n",
+
+                       double.MIN_VALUE.to_string(), 
+                       double.MAX_VALUE.to_string(),
+
+                       double.MANTISE_DIGITS.to_string(),
+                       double.SECURE_DIGITS.to_string(),
+                       double.MIN_EXPONENT.to_string(), 
+                       double.MAX_EXPONENT.to_string(),
+                       double.MIN_EXPONENT_10.to_string(), 
+                       double.MAX_EXPONENT_10.to_string(),
+
+                       double.EPSILON.to_string(),
+                       double.POSITIVE_INFINITY.to_string(),
+                       double.NEGATIVE_INFINITY.to_string(),
+                       double.NAN.to_string());
+
+               for(int i = 0; i < 6; i++) { // XXX use foreach
+                       double value = DOUBLE_TESTS[i];
+                       int infinity = value.get_infinity();
+
+                       stdout.printf(
+                               "double(%g): nan=%s, finite=%s, normal=%s, infinity=%s\n", 
+                               
+                               value,
+                               value.is_nan() ? "true" : "false", 
+                               value.is_finite() ? "true" : "false",
+                               value.is_normal() ? "true" : "false", 
+
+                               infinity > 0 ? "positive" : 
+                               infinity < 0 ? "negative" : "none");
+               }
+       }
+}
diff --git a/trunk/vala/vapi/glib-2.0.vala b/trunk/vala/vapi/glib-2.0.vala
index 023ac7d..0a32d0f 100644
--- a/trunk/vala/vapi/glib-2.0.vala
+++ b/trunk/vala/vapi/glib-2.0.vala
@@ -97,29 +97,92 @@ public struct ulong {
        public ref string! to_string (string! format = "%lu");
 }
 
+[CCode (cname = "gint8", cheader_filename = "glib.h")]
+[IntegerType (rank = 16)]
+public struct int8 {
+       [CCode (cname = "G_MININT8")]
+       public static int8 MIN_VALUE;
+       [CCode (cname = "G_MAXINT8")]
+       public static int8 MAX_VALUE;
+
+       [InstanceLast ()]
+       [CCode (cname = "g_strdup_printf")]
+       public ref string! to_string (string! format = "%i");
+}
+
+[CCode (cname = "guint8", cheader_filename = "glib.h")]
+[IntegerType (rank = 17)]
+public struct uint8 {
+       [CCode (cname = "0")]
+       public static uint8 MIN_VALUE;
+       [CCode (cname = "G_MAXUINT8")]
+       public static uint8 MAX_VALUE;
+
+       [InstanceLast ()]
+       [CCode (cname = "g_strdup_printf")]
+       public ref string! to_string (string! format = "%u");
+}
+
 [CCode (cname = "gint16", cheader_filename = "glib.h")]
 [IntegerType (rank = 5)]
 public struct int16 {
+       [CCode (cname = "G_MININT16")]
+       public static int16 MIN_VALUE;
+       [CCode (cname = "G_MAXINT16")]
+       public static int16 MAX_VALUE;
+
+       [InstanceLast ()]
+       [CCode (cname = "g_strdup_printf")]
+       public ref string! to_string (string! format = "%i");
 }
 
 [CCode (cname = "guint16", cheader_filename = "glib.h")]
 [IntegerType (rank = 6)]
 public struct uint16 {
+       [CCode (cname = "0")]
+       public static uint16 MIN_VALUE;
+       [CCode (cname = "G_MAXUINT16")]
+       public static uint16 MAX_VALUE;
+
+       [InstanceLast ()]
+       [CCode (cname = "g_strdup_printf")]
+       public ref string! to_string (string! format = "%u");
 }
 
 [CCode (cname = "gint32", cheader_filename = "glib.h")]
 [IntegerType (rank = 9)]
 public struct int32 {
+       [CCode (cname = "G_MININT32")]
+       public static int32 MIN_VALUE;
+       [CCode (cname = "G_MAXINT32")]
+       public static int32 MAX_VALUE;
+
+       [InstanceLast ()]
+       [CCode (cname = "g_strdup_printf")]
+       public ref string! to_string (string! format = "%li");
 }
 
 [CCode (cname = "guint32", cheader_filename = "glib.h")]
 [IntegerType (rank = 10)]
 public struct uint32 {
+       [CCode (cname = "0L")]
+       public static uint32 MIN_VALUE;
+       [CCode (cname = "G_MAXUINT32")]
+       public static uint32 MAX_VALUE;
+
+       [InstanceLast ()]
+       [CCode (cname = "g_strdup_printf")]
+       public ref string! to_string (string! format = "%lu");
 }
 
 [CCode (cname = "gint64", cheader_filename = "glib.h", type_id = "G_TYPE_INT64", marshaller_type_name = 
"INT64", get_value_function = "g_value_get_int64", set_value_function = "g_value_set_int64")]
 [IntegerType (rank = 14)]
 public struct int64 {
+       [CCode (cname = "G_MININT64")]
+       public static int64 MIN_VALUE;
+       [CCode (cname = "G_MAXINT64")]
+       public static int64 MAX_VALUE;
+
        [InstanceLast ()]
        [CCode (cname = "g_strdup_printf")]
        public ref string! to_string (string! format = "%lli");
@@ -128,19 +191,106 @@ public struct int64 {
 [CCode (cname = "guint64", cheader_filename = "glib.h", type_id = "G_TYPE_UINT64", marshaller_type_name = 
"UINT64", get_value_function = "g_value_get_uint64", set_value_function = "g_value_set_uint64")]
 [IntegerType (rank = 15)]
 public struct uint64 {
+       [CCode (cname = "0LL")]
+       public static uint64 MIN_VALUE;
+       [CCode (cname = "G_MAXUINT64")]
+       public static uint64 MAX_VALUE;
+
        [InstanceLast ()]
        [CCode (cname = "g_strdup_printf")]
        public ref string! to_string (string! format = "%llu");
 }
 
-[CCode (cname = "float", cheader_filename = "glib.h", type_id = "G_TYPE_FLOAT", marshaller_type_name = 
"FLOAT", get_value_function = "g_value_get_float", set_value_function = "g_value_set_float")]
+[CCode (cname = "float", cheader_filename = "glib.h,float.h,math.h", type_id = "G_TYPE_FLOAT", 
marshaller_type_name = "FLOAT", get_value_function = "g_value_get_float", set_value_function = 
"g_value_set_float")]
 [FloatingType (rank = 1)]
 public struct float {
+       [CCode (cname = "FLT_MANT_DIG")]
+       public static int MANTISE_DIGITS;
+       [CCode (cname = "FLT_DIG")]
+       public static int SECURE_DIGITS;
+
+       [CCode (cname = "FLT_MIN_EXP")]
+       public static int MIN_EXPONENT;
+       [CCode (cname = "FLT_MAX_EXP")]
+       public static int MAX_EXPONENT;
+
+       [CCode (cname = "FLT_MIN_10_EXP")]
+       public static int MIN_EXPONENT_10;
+       [CCode (cname = "FLT_MAX_10_EXP")]
+       public static int MAX_EXPONENT_10;
+
+       [CCode (cname = "FLT_EPSILON")]
+       public static float EPSILON;
+       [CCode (cname = "FLT_MIN")]
+       public static float MIN_VALUE;
+       [CCode (cname = "FLT_MAX")]
+       public static float MAX_VALUE;
+
+       [CCode (cname = "((float)NAN)")]
+       public static float NAN;
+       [CCode (cname = "(+((float)INFINITY))")]
+       public static float POSITIVE_INFINITY;
+       [CCode (cname = "(-((float)INFINITY))")]
+       public static float NEGATIVE_INFINITY;
+
+       [CCode (cname = "isnan")]
+       public bool is_nan ();
+       [CCode (cname = "isfinite")]
+       public bool is_finite ();
+       [CCode (cname = "isnormal")]
+       public bool is_normal ();
+       [CCode (cname = "isinf")]
+       public int get_infinity ();
+
+       [InstanceLast ()]
+       [CCode (cname = "g_strdup_printf")]
+       public ref string! to_string (string! format = "%g");
 }
 
-[CCode (cname = "double", cheader_filename = "glib.h", type_id = "G_TYPE_DOUBLE", marshaller_type_name = 
"DOUBLE", get_value_function = "g_value_get_double", set_value_function = "g_value_set_double")]
+[CCode (cname = "double", cheader_filename = "glib.h,float.h,math.h", type_id = "G_TYPE_DOUBLE", 
marshaller_type_name = "DOUBLE", get_value_function = "g_value_get_double", set_value_function = 
"g_value_set_double")]
 [FloatingType (rank = 2)]
 public struct double {
+       [CCode (cname = "DBL_MANT_DIG")]
+       public static int MANTISE_DIGITS;
+       [CCode (cname = "DBL_DIG")]
+       public static int SECURE_DIGITS;
+
+       [CCode (cname = "DBL_MIN_EXP")]
+       public static int MIN_EXPONENT;
+       [CCode (cname = "DBL_MAX_EXP")]
+       public static int MAX_EXPONENT;
+
+       [CCode (cname = "DBL_MIN_10_EXP")]
+       public static int MIN_EXPONENT_10;
+       [CCode (cname = "DBL_MAX_10_EXP")]
+       public static int MAX_EXPONENT_10;
+
+       [CCode (cname = "DBL_EPSILON")]
+       public static double EPSILON;
+       [CCode (cname = "DBL_MIN")]
+       public static double MIN_VALUE;
+       [CCode (cname = "DBL_MAX")]
+       public static double MAX_VALUE;
+
+       [CCode (cname = "((double)NAN)")]
+       public static float NAN;
+       [CCode (cname = "(+((double)INFINITY))")]
+       public static float POSITIVE_INFINITY;
+       [CCode (cname = "(-((double)INFINITY))")]
+       public static float NEGATIVE_INFINITY;
+
+       [CCode (cname = "isnan")]
+       public bool is_nan ();
+       [CCode (cname = "isfinite")]
+       public bool is_finite ();
+       [CCode (cname = "isnormal")]
+       public bool is_normal ();
+       [CCode (cname = "isinf")]
+       public int get_infinity ();
+
+       [InstanceLast ()]
+       [CCode (cname = "g_strdup_printf")]
+       public ref string! to_string (string! format = "%g");
 }
 
 [CCode (cname = "gunichar", cheader_filename = "glib.h", get_value_function = "g_value_get_int", 
set_value_function = "g_value_set_int")]
-- 
1.4.4.2




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