summaryrefslogtreecommitdiffhomepage
path: root/mrbgems
diff options
context:
space:
mode:
authormattn <[email protected]>2013-03-01 14:42:42 +0900
committermattn <[email protected]>2013-03-01 14:42:42 +0900
commit261efd8e9dc3bdaf4438797a9cdb34f7792a5639 (patch)
tree2a3b8b19d045089479fbfcece1e1318f17c32223 /mrbgems
parent4c4cf0a4ce738fde01a047194b52a5389b400e89 (diff)
parent138ecf4723078cf8ef4342fb3995db23003eff01 (diff)
downloadmruby-261efd8e9dc3bdaf4438797a9cdb34f7792a5639.tar.gz
mruby-261efd8e9dc3bdaf4438797a9cdb34f7792a5639.zip
Merge branch 'master' into pluggable_struct
Diffstat (limited to 'mrbgems')
-rw-r--r--mrbgems/mruby-math/mrbgem.rake4
-rw-r--r--mrbgems/mruby-math/src/math.c691
-rw-r--r--mrbgems/mruby-math/test/math.rb136
-rw-r--r--mrbgems/mruby-struct/mrbgem.rake4
-rw-r--r--mrbgems/mruby-struct/src/struct.c785
-rw-r--r--mrbgems/mruby-struct/test/struct.rb77
-rw-r--r--mrbgems/mruby-time/mrbgem.rake4
-rw-r--r--mrbgems/mruby-time/src/time.c755
-rw-r--r--mrbgems/mruby-time/test/time.rb201
9 files changed, 2657 insertions, 0 deletions
diff --git a/mrbgems/mruby-math/mrbgem.rake b/mrbgems/mruby-math/mrbgem.rake
new file mode 100644
index 000000000..4b0fa40fd
--- /dev/null
+++ b/mrbgems/mruby-math/mrbgem.rake
@@ -0,0 +1,4 @@
+MRuby::Gem::Specification.new('mruby-math') do |spec|
+ spec.license = 'MIT'
+ spec.authors = 'mruby developers'
+end
diff --git a/mrbgems/mruby-math/src/math.c b/mrbgems/mruby-math/src/math.c
new file mode 100644
index 000000000..9955d9862
--- /dev/null
+++ b/mrbgems/mruby-math/src/math.c
@@ -0,0 +1,691 @@
+/*
+** math.c - Math module
+**
+** See Copyright Notice in mruby.h
+*/
+
+#include "mruby.h"
+#include "mruby/array.h"
+
+#include <math.h>
+
+#define domain_error(msg) \
+ mrb_raise(mrb, E_RANGE_ERROR, "Numerical argument is out of domain - " #msg)
+
+/* math functions not provided under Microsoft Visual C++ */
+#ifdef _MSC_VER
+
+#define MATH_TOLERANCE 1E-12
+
+#define asinh(x) log(x + sqrt(pow(x,2.0) + 1))
+#define acosh(x) log(x + sqrt(pow(x,2.0) - 1))
+#define atanh(x) (log(1+x) - log(1-x))/2.0
+#define cbrt(x) pow(x,1.0/3.0)
+
+/* Declaration of complementary Error function */
+double
+erfc(double x);
+
+/*
+** Implementations of error functions
+** credits to http://www.digitalmars.com/archives/cplusplus/3634.html
+*/
+
+/* Implementation of Error function */
+double
+erf(double x)
+{
+ static const double two_sqrtpi = 1.128379167095512574;
+ double sum = x;
+ double term = x;
+ double xsqr = x*x;
+ int j= 1;
+ if (fabs(x) > 2.2) {
+ return 1.0 - erfc(x);
+ }
+ do {
+ term *= xsqr/j;
+ sum -= term/(2*j+1);
+ ++j;
+ term *= xsqr/j;
+ sum += term/(2*j+1);
+ ++j;
+ } while (fabs(term/sum) > MATH_TOLERANCE);
+ return two_sqrtpi*sum;
+}
+
+/* Implementation of complementary Error function */
+double
+erfc(double x)
+{
+ static const double one_sqrtpi= 0.564189583547756287;
+ double a = 1;
+ double b = x;
+ double c = x;
+ double d = x*x+0.5;
+ double q1;
+ double q2 = b/d;
+ double n = 1.0;
+ double t;
+ if (fabs(x) < 2.2) {
+ return 1.0 - erf(x);
+ }
+ if (x < 0.0) { /*signbit(x)*/
+ return 2.0 - erfc(-x);
+ }
+ do {
+ t = a*n+b*x;
+ a = b;
+ b = t;
+ t = c*n+d*x;
+ c = d;
+ d = t;
+ n += 0.5;
+ q1 = q2;
+ q2 = b/d;
+ } while (fabs(q1-q2)/q2 > MATH_TOLERANCE);
+ return one_sqrtpi*exp(-x*x)*q2;
+}
+
+#endif
+
+/*
+ TRIGONOMETRIC FUNCTIONS
+*/
+
+/*
+ * call-seq:
+ * Math.sin(x) -> float
+ *
+ * Computes the sine of <i>x</i> (expressed in radians). Returns
+ * -1..1.
+ */
+static mrb_value
+math_sin(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = sin(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.cos(x) -> float
+ *
+ * Computes the cosine of <i>x</i> (expressed in radians). Returns
+ * -1..1.
+ */
+static mrb_value
+math_cos(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = cos(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.tan(x) -> float
+ *
+ * Returns the tangent of <i>x</i> (expressed in radians).
+ */
+static mrb_value
+math_tan(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = tan(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ INVERSE TRIGONOMETRIC FUNCTIONS
+*/
+
+/*
+ * call-seq:
+ * Math.asin(x) -> float
+ *
+ * Computes the arc sine of <i>x</i>. Returns -{PI/2} .. {PI/2}.
+ */
+static mrb_value
+math_asin(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = asin(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.acos(x) -> float
+ *
+ * Computes the arc cosine of <i>x</i>. Returns 0..PI.
+ */
+static mrb_value
+math_acos(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = acos(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.atan(x) -> float
+ *
+ * Computes the arc tangent of <i>x</i>. Returns -{PI/2} .. {PI/2}.
+ */
+static mrb_value
+math_atan(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = atan(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.atan2(y, x) -> float
+ *
+ * Computes the arc tangent given <i>y</i> and <i>x</i>. Returns
+ * -PI..PI.
+ *
+ * Math.atan2(-0.0, -1.0) #=> -3.141592653589793
+ * Math.atan2(-1.0, -1.0) #=> -2.356194490192345
+ * Math.atan2(-1.0, 0.0) #=> -1.5707963267948966
+ * Math.atan2(-1.0, 1.0) #=> -0.7853981633974483
+ * Math.atan2(-0.0, 1.0) #=> -0.0
+ * Math.atan2(0.0, 1.0) #=> 0.0
+ * Math.atan2(1.0, 1.0) #=> 0.7853981633974483
+ * Math.atan2(1.0, 0.0) #=> 1.5707963267948966
+ * Math.atan2(1.0, -1.0) #=> 2.356194490192345
+ * Math.atan2(0.0, -1.0) #=> 3.141592653589793
+ *
+ */
+static mrb_value
+math_atan2(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x, y;
+
+ mrb_get_args(mrb, "ff", &x, &y);
+ x = atan2(x, y);
+
+ return mrb_float_value(x);
+}
+
+
+
+/*
+ HYPERBOLIC TRIG FUNCTIONS
+*/
+/*
+ * call-seq:
+ * Math.sinh(x) -> float
+ *
+ * Computes the hyperbolic sine of <i>x</i> (expressed in
+ * radians).
+ */
+static mrb_value
+math_sinh(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = sinh(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.cosh(x) -> float
+ *
+ * Computes the hyperbolic cosine of <i>x</i> (expressed in radians).
+ */
+static mrb_value
+math_cosh(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = cosh(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.tanh() -> float
+ *
+ * Computes the hyperbolic tangent of <i>x</i> (expressed in
+ * radians).
+ */
+static mrb_value
+math_tanh(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = tanh(x);
+
+ return mrb_float_value(x);
+}
+
+
+/*
+ INVERSE HYPERBOLIC TRIG FUNCTIONS
+*/
+
+/*
+ * call-seq:
+ * Math.asinh(x) -> float
+ *
+ * Computes the inverse hyperbolic sine of <i>x</i>.
+ */
+static mrb_value
+math_asinh(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+
+ x = asinh(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.acosh(x) -> float
+ *
+ * Computes the inverse hyperbolic cosine of <i>x</i>.
+ */
+static mrb_value
+math_acosh(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = acosh(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.atanh(x) -> float
+ *
+ * Computes the inverse hyperbolic tangent of <i>x</i>.
+ */
+static mrb_value
+math_atanh(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = atanh(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ EXPONENTIALS AND LOGARITHMS
+*/
+#if defined __CYGWIN__
+# include <cygwin/version.h>
+# if CYGWIN_VERSION_DLL_MAJOR < 1005
+# define nan(x) nan()
+# endif
+# define log(x) ((x) < 0.0 ? nan("") : log(x))
+# define log10(x) ((x) < 0.0 ? nan("") : log10(x))
+#endif
+
+#ifndef log2
+#ifndef HAVE_LOG2
+double
+log2(double x)
+{
+ return log10(x)/log10(2.0);
+}
+#else
+extern double log2(double);
+#endif
+#endif
+
+/*
+ * call-seq:
+ * Math.exp(x) -> float
+ *
+ * Returns e**x.
+ *
+ * Math.exp(0) #=> 1.0
+ * Math.exp(1) #=> 2.718281828459045
+ * Math.exp(1.5) #=> 4.4816890703380645
+ *
+ */
+static mrb_value
+math_exp(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = exp(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.log(numeric) -> float
+ * Math.log(num,base) -> float
+ *
+ * Returns the natural logarithm of <i>numeric</i>.
+ * If additional second argument is given, it will be the base
+ * of logarithm.
+ *
+ * Math.log(1) #=> 0.0
+ * Math.log(Math::E) #=> 1.0
+ * Math.log(Math::E**3) #=> 3.0
+ * Math.log(12,3) #=> 2.2618595071429146
+ *
+ */
+static mrb_value
+math_log(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x, base;
+ int argc;
+
+ argc = mrb_get_args(mrb, "f|f", &x, &base);
+ x = log(x);
+ if (argc == 2) {
+ x /= log(base);
+ }
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.log2(numeric) -> float
+ *
+ * Returns the base 2 logarithm of <i>numeric</i>.
+ *
+ * Math.log2(1) #=> 0.0
+ * Math.log2(2) #=> 1.0
+ * Math.log2(32768) #=> 15.0
+ * Math.log2(65536) #=> 16.0
+ *
+ */
+static mrb_value
+math_log2(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = log2(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.log10(numeric) -> float
+ *
+ * Returns the base 10 logarithm of <i>numeric</i>.
+ *
+ * Math.log10(1) #=> 0.0
+ * Math.log10(10) #=> 1.0
+ * Math.log10(10**100) #=> 100.0
+ *
+ */
+static mrb_value
+math_log10(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = log10(x);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.sqrt(numeric) -> float
+ *
+ * Returns the square root of <i>numeric</i>.
+ *
+ */
+static mrb_value
+math_sqrt(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = sqrt(x);
+
+ return mrb_float_value(x);
+}
+
+
+/*
+ * call-seq:
+ * Math.cbrt(numeric) -> float
+ *
+ * Returns the cube root of <i>numeric</i>.
+ *
+ * -9.upto(9) {|x|
+ * p [x, Math.cbrt(x), Math.cbrt(x)**3]
+ * }
+ * #=>
+ * [-9, -2.0800838230519, -9.0]
+ * [-8, -2.0, -8.0]
+ * [-7, -1.91293118277239, -7.0]
+ * [-6, -1.81712059283214, -6.0]
+ * [-5, -1.7099759466767, -5.0]
+ * [-4, -1.5874010519682, -4.0]
+ * [-3, -1.44224957030741, -3.0]
+ * [-2, -1.25992104989487, -2.0]
+ * [-1, -1.0, -1.0]
+ * [0, 0.0, 0.0]
+ * [1, 1.0, 1.0]
+ * [2, 1.25992104989487, 2.0]
+ * [3, 1.44224957030741, 3.0]
+ * [4, 1.5874010519682, 4.0]
+ * [5, 1.7099759466767, 5.0]
+ * [6, 1.81712059283214, 6.0]
+ * [7, 1.91293118277239, 7.0]
+ * [8, 2.0, 8.0]
+ * [9, 2.0800838230519, 9.0]
+ *
+ */
+static mrb_value
+math_cbrt(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = cbrt(x);
+
+ return mrb_float_value(x);
+}
+
+
+/*
+ * call-seq:
+ * Math.frexp(numeric) -> [ fraction, exponent ]
+ *
+ * Returns a two-element array containing the normalized fraction (a
+ * <code>Float</code>) and exponent (a <code>Fixnum</code>) of
+ * <i>numeric</i>.
+ *
+ * fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11]
+ * fraction * 2**exponent #=> 1234.0
+ */
+static mrb_value
+math_frexp(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+ int exp;
+
+ mrb_get_args(mrb, "f", &x);
+ x = frexp(x, &exp);
+
+ return mrb_assoc_new(mrb, mrb_float_value(x), mrb_fixnum_value(exp));
+}
+
+/*
+ * call-seq:
+ * Math.ldexp(flt, int) -> float
+ *
+ * Returns the value of <i>flt</i>*(2**<i>int</i>).
+ *
+ * fraction, exponent = Math.frexp(1234)
+ * Math.ldexp(fraction, exponent) #=> 1234.0
+ */
+static mrb_value
+math_ldexp(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+ mrb_int i;
+
+ mrb_get_args(mrb, "fi", &x, &i);
+ x = ldexp(x, i);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.hypot(x, y) -> float
+ *
+ * Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle
+ * with sides <i>x</i> and <i>y</i>.
+ *
+ * Math.hypot(3, 4) #=> 5.0
+ */
+static mrb_value
+math_hypot(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x, y;
+
+ mrb_get_args(mrb, "ff", &x, &y);
+ x = hypot(x, y);
+
+ return mrb_float_value(x);
+}
+
+/*
+ * call-seq:
+ * Math.erf(x) -> float
+ *
+ * Calculates the error function of x.
+ */
+static mrb_value
+math_erf(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = erf(x);
+
+ return mrb_float_value(x);
+}
+
+
+/*
+ * call-seq:
+ * Math.erfc(x) -> float
+ *
+ * Calculates the complementary error function of x.
+ */
+static mrb_value
+math_erfc(mrb_state *mrb, mrb_value obj)
+{
+ mrb_float x;
+
+ mrb_get_args(mrb, "f", &x);
+ x = erfc(x);
+
+ return mrb_float_value(x);
+}
+
+/* ------------------------------------------------------------------------*/
+void
+mrb_mruby_math_gem_init(mrb_state* mrb)
+{
+ struct RClass *mrb_math;
+ mrb_math = mrb_define_module(mrb, "Math");
+
+#ifdef M_PI
+ mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(M_PI));
+#else
+ mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(atan(1.0)*4.0));
+#endif
+
+#ifdef M_E
+ mrb_define_const(mrb, mrb_math, "E", mrb_float_value(M_E));
+#else
+ mrb_define_const(mrb, mrb_math, "E", mrb_float_value(exp(1.0)));
+#endif
+
+#ifdef MRB_USE_FLOAT
+ mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(1e-5));
+#else
+ mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(1e-12));
+#endif
+
+ mrb_define_module_function(mrb, mrb_math, "sin", math_sin, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "cos", math_cos, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "tan", math_tan, ARGS_REQ(1));
+
+ mrb_define_module_function(mrb, mrb_math, "asin", math_asin, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "acos", math_acos, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "atan", math_atan, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "atan2", math_atan2, ARGS_REQ(2));
+
+ mrb_define_module_function(mrb, mrb_math, "sinh", math_sinh, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "cosh", math_cosh, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "tanh", math_tanh, ARGS_REQ(1));
+
+ mrb_define_module_function(mrb, mrb_math, "asinh", math_asinh, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "acosh", math_acosh, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "atanh", math_atanh, ARGS_REQ(1));
+
+ mrb_define_module_function(mrb, mrb_math, "exp", math_exp, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "log", math_log, ARGS_REQ(1)|ARGS_OPT(1));
+ mrb_define_module_function(mrb, mrb_math, "log2", math_log2, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "log10", math_log10, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "sqrt", math_sqrt, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "cbrt", math_cbrt, ARGS_REQ(1));
+
+ mrb_define_module_function(mrb, mrb_math, "frexp", math_frexp, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "ldexp", math_ldexp, ARGS_REQ(2));
+
+ mrb_define_module_function(mrb, mrb_math, "hypot", math_hypot, ARGS_REQ(2));
+
+ mrb_define_module_function(mrb, mrb_math, "erf", math_erf, ARGS_REQ(1));
+ mrb_define_module_function(mrb, mrb_math, "erfc", math_erfc, ARGS_REQ(1));
+}
+
+void
+mrb_mruby_math_gem_final(mrb_state* mrb)
+{
+}
diff --git a/mrbgems/mruby-math/test/math.rb b/mrbgems/mruby-math/test/math.rb
new file mode 100644
index 000000000..1cc3a20b0
--- /dev/null
+++ b/mrbgems/mruby-math/test/math.rb
@@ -0,0 +1,136 @@
+##
+# Math Test
+
+##
+# Performs fuzzy check for equality on methods returning floats
+# on the basis of the Math::TOLERANCE constant.
+def check_float(a, b)
+ tolerance = Math::TOLERANCE
+ a = a.to_f
+ b = b.to_f
+ if a.finite? and b.finite?
+ (a-b).abs < tolerance
+ else
+ true
+ end
+end
+
+assert('Math.sin 0') do
+ check_float(Math.sin(0), 0)
+end
+
+assert('Math.sin PI/2') do
+ check_float(Math.sin(Math::PI / 2), 1)
+end
+
+assert('Fundamental trig identities') do
+ result = true
+ N = 13
+ N.times do |i|
+ a = Math::PI / N * i
+ ca = Math::PI / 2 - a
+ s = Math.sin(a)
+ c = Math.cos(a)
+ t = Math.tan(a)
+ result &= check_float(s, Math.cos(ca))
+ result &= check_float(t, 1 / Math.tan(ca))
+ result &= check_float(s ** 2 + c ** 2, 1)
+ result &= check_float(t ** 2 + 1, (1/c) ** 2)
+ result &= check_float((1/t) ** 2 + 1, (1/s) ** 2)
+ end
+ result
+end
+
+assert('Math.erf 0') do
+ check_float(Math.erf(0), 0)
+end
+
+assert('Math.exp 0') do
+ check_float(Math.exp(0), 1.0)
+end
+
+assert('Math.exp 1') do
+ check_float(Math.exp(1), 2.718281828459045)
+end
+
+assert('Math.exp 1.5') do
+ check_float(Math.exp(1.5), 4.4816890703380645)
+end
+
+assert('Math.log 1') do
+ check_float(Math.log(1), 0)
+end
+
+assert('Math.log E') do
+ check_float(Math.log(Math::E), 1.0)
+end
+
+assert('Math.log E**3') do
+ check_float(Math.log(Math::E**3), 3.0)
+end
+
+assert('Math.log2 1') do
+ check_float(Math.log2(1), 0.0)
+end
+
+assert('Math.log2 2') do
+ check_float(Math.log2(2), 1.0)
+end
+
+assert('Math.log10 1') do
+ check_float(Math.log10(1), 0.0)
+end
+
+assert('Math.log10 10') do
+ check_float(Math.log10(10), 1.0)
+end
+
+assert('Math.log10 10**100') do
+ check_float(Math.log10(10**100), 100.0)
+end
+
+assert('Math.sqrt') do
+ num = [0.0, 1.0, 2.0, 3.0, 4.0]
+ sqr = [0, 1, 4, 9, 16]
+ result = true
+ sqr.each_with_index do |v,i|
+ result &= check_float(Math.sqrt(v), num[i])
+ end
+ result
+end
+
+assert('Math.cbrt') do
+ num = [-2.0, -1.0, 0.0, 1.0, 2.0]
+ cub = [-8, -1, 0, 1, 8]
+ result = true
+ cub.each_with_index do |v,i|
+ result &= check_float(Math.cbrt(v), num[i])
+ end
+ result
+end
+
+assert('Math.hypot') do
+ check_float(Math.hypot(3, 4), 5.0)
+end
+
+assert('Math.frexp 1234') do
+ n = 1234
+ fraction, exponent = Math.frexp(n)
+ check_float(Math.ldexp(fraction, exponent), n)
+end
+
+assert('Math.erf 1') do
+ check_float(Math.erf(1), 0.842700792949715)
+end
+
+assert('Math.erfc 1') do
+ check_float(Math.erfc(1), 0.157299207050285)
+end
+
+assert('Math.erf -1') do
+ check_float(Math.erf(-1), -0.8427007929497148)
+end
+
+assert('Math.erfc -1') do
+ check_float(Math.erfc(-1), 1.8427007929497148)
+end
diff --git a/mrbgems/mruby-struct/mrbgem.rake b/mrbgems/mruby-struct/mrbgem.rake
new file mode 100644
index 000000000..476e990da
--- /dev/null
+++ b/mrbgems/mruby-struct/mrbgem.rake
@@ -0,0 +1,4 @@
+MRuby::Gem::Specification.new('mruby-struct') do |spec|
+ spec.license = 'MIT'
+ spec.authors = 'mruby developers'
+end
diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c
new file mode 100644
index 000000000..131702e9c
--- /dev/null
+++ b/mrbgems/mruby-struct/src/struct.c
@@ -0,0 +1,785 @@
+/*
+** struct.c - Struct class
+**
+** See Copyright Notice in mruby.h
+*/
+
+#include <string.h>
+#include <stdarg.h>
+#include "mruby.h"
+#include "mruby/array.h"
+#include "mruby/string.h"
+#include "mruby/class.h"
+#include "mruby/variable.h"
+
+struct RStruct {
+ struct RBasic basic;
+ long len;
+ mrb_value *ptr;
+};
+
+#define RSTRUCT(st) ((struct RStruct*)((st).value.p))
+#define RSTRUCT_LEN(st) ((int)(RSTRUCT(st)->len))
+#define RSTRUCT_PTR(st) (RSTRUCT(st)->ptr)
+
+static struct RClass *
+struct_class(mrb_state *mrb)
+{
+ return mrb_class_get(mrb, "Struct");
+}
+
+static inline mrb_value
+struct_ivar_get(mrb_state *mrb, mrb_value c, mrb_sym id)
+{
+ struct RClass* kclass;
+ struct RClass* sclass = struct_class(mrb);
+
+ mrb_value ans;
+ for (;;) {
+ ans = mrb_iv_get(mrb, c, id);
+ if (!mrb_nil_p(ans)) return ans;
+ kclass = RCLASS_SUPER(c);
+ if (kclass == 0 || kclass == sclass)
+ return mrb_nil_value();
+ c = mrb_obj_value(kclass);
+ }
+}
+
+mrb_value
+mrb_struct_iv_get(mrb_state *mrb, mrb_value c, const char *name)
+{
+ return struct_ivar_get(mrb, c, mrb_intern(mrb, name));
+}
+
+mrb_value
+mrb_struct_s_members(mrb_state *mrb, mrb_value klass)
+{
+ mrb_value members = struct_ivar_get(mrb, klass, mrb_intern(mrb, "__members__"));
+
+ if (mrb_nil_p(members)) {
+ mrb_raise(mrb, E_TYPE_ERROR, "uninitialized struct");
+ }
+ if (!mrb_array_p(members)) {
+ mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct");
+ }
+ return members;
+}
+
+mrb_value
+mrb_struct_members(mrb_state *mrb, mrb_value s)
+{
+ mrb_value members = mrb_struct_s_members(mrb, mrb_obj_value(mrb_obj_class(mrb, s)));
+ if (mrb_type(s) == MRB_TT_STRUCT) {
+ if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) {
+ mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)",
+ RARRAY_LEN(members), RSTRUCT_LEN(s));
+ }
+ }
+ return members;
+}
+
+static mrb_value
+mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass)
+{
+ mrb_value members, ary;
+ mrb_value *p, *pend;
+
+ members = mrb_struct_s_members(mrb, klass);
+ ary = mrb_ary_new_capa(mrb, RARRAY_LEN(members));
+ p = RARRAY_PTR(members); pend = p + RARRAY_LEN(members);
+ while (p < pend) {
+ mrb_ary_push(mrb, ary, *p);
+ p++;
+ }
+
+ return ary;
+}
+
+static inline void
+struct_copy(mrb_value *dst, const mrb_value *src, size_t size)
+{
+ size_t i;
+
+ for (i = 0; i < size; i++) {
+ dst[i] = src[i];
+ }
+}
+
+/* 15.2.18.4.6 */
+/*
+ * call-seq:
+ * struct.members -> array
+ *
+ * Returns an array of strings representing the names of the instance
+ * variables.
+ *
+ * Customer = Struct.new(:name, :address, :zip)
+ * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
+ * joe.members #=> [:name, :address, :zip]
+ */
+
+static mrb_value
+mrb_struct_members_m(mrb_state *mrb, mrb_value obj)
+{
+ return mrb_struct_s_members_m(mrb, mrb_obj_value(mrb_obj_class(mrb, obj)));
+}
+
+mrb_value
+mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id)
+{
+ mrb_value members, slot, *ptr, *ptr_members;
+ long i, len;
+
+ ptr = RSTRUCT_PTR(obj);
+ members = mrb_struct_members(mrb, obj);
+ ptr_members = RARRAY_PTR(members);
+ slot = mrb_symbol_value(id);
+ len = RARRAY_LEN(members);
+ for (i=0; i<len; i++) {
+ if (mrb_obj_equal(mrb, ptr_members[i], slot)) {
+ return ptr[i];
+ }
+ }
+ mrb_name_error(mrb, id, "%s is not struct member", mrb_sym2name(mrb, id));
+ return mrb_nil_value(); /* not reached */
+}
+
+static mrb_value
+mrb_struct_ref(mrb_state *mrb, mrb_value obj)
+{
+ return mrb_struct_getmember(mrb, obj, mrb->ci->mid);
+}
+
+static mrb_value mrb_struct_ref0(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[0];}
+static mrb_value mrb_struct_ref1(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[1];}
+static mrb_value mrb_struct_ref2(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[2];}
+static mrb_value mrb_struct_ref3(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[3];}
+static mrb_value mrb_struct_ref4(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[4];}
+static mrb_value mrb_struct_ref5(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[5];}
+static mrb_value mrb_struct_ref6(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[6];}
+static mrb_value mrb_struct_ref7(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[7];}
+static mrb_value mrb_struct_ref8(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[8];}
+static mrb_value mrb_struct_ref9(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[9];}
+
+#define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
+#define N_REF_FUNC numberof(ref_func)
+
+static mrb_value (*const ref_func[])(mrb_state*, mrb_value) = {
+ mrb_struct_ref0,
+ mrb_struct_ref1,
+ mrb_struct_ref2,
+ mrb_struct_ref3,
+ mrb_struct_ref4,
+ mrb_struct_ref5,
+ mrb_struct_ref6,
+ mrb_struct_ref7,
+ mrb_struct_ref8,
+ mrb_struct_ref9,
+};
+
+mrb_sym
+mrb_id_attrset(mrb_state *mrb, mrb_sym id)
+{
+ const char *name;
+ char *buf;
+ int len;
+ mrb_sym mid;
+
+ name = mrb_sym2name_len(mrb, id, &len);
+ buf = (char *)mrb_malloc(mrb, len+2);
+ memcpy(buf, name, len);
+ buf[len] = '=';
+ buf[len+1] = '\0';
+
+ mid = mrb_intern2(mrb, buf, len+1);
+ mrb_free(mrb, buf);
+ return mid;
+}
+
+static mrb_value
+mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val)
+{
+ const char *name;
+ int i, len;
+ mrb_sym mid;
+ mrb_value members, slot, *ptr, *ptr_members;
+
+ /* get base id */
+ name = mrb_sym2name_len(mrb, mrb->ci->mid, &len);
+ mid = mrb_intern2(mrb, name, len-1); /* omit last "=" */
+
+ members = mrb_struct_members(mrb, obj);
+ ptr_members = RARRAY_PTR(members);
+ len = RARRAY_LEN(members);
+ ptr = RSTRUCT_PTR(obj);
+ for (i=0; i<len; i++) {
+ slot = ptr_members[i];
+ if (mrb_symbol(slot) == mid) {
+ return ptr[i] = val;
+ }
+ }
+
+ mrb_name_error(mrb, mid, "`%s' is not a struct member",
+ mrb_sym2name(mrb, mid));
+ return mrb_nil_value(); /* not reached */
+}
+
+static mrb_value
+mrb_struct_set_m(mrb_state *mrb, mrb_value obj)
+{
+ mrb_value val;
+
+ mrb_get_args(mrb, "o", &val);
+ return mrb_struct_set(mrb, obj, val);
+}
+
+#define is_notop_id(id) (id)//((id)>tLAST_TOKEN)
+#define is_local_id(id) (is_notop_id(id))//&&((id)&ID_SCOPE_MASK)==ID_LOCAL)
+int
+mrb_is_local_id(mrb_sym id)
+{
+ return is_local_id(id);
+}
+
+#define is_const_id(id) (is_notop_id(id))//&&((id)&ID_SCOPE_MASK)==ID_CONST)
+int
+mrb_is_const_id(mrb_sym id)
+{
+ return is_const_id(id);
+}
+
+static mrb_value
+make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * klass)
+{
+ mrb_value nstr, *ptr_members;
+ mrb_sym id;
+ long i, len;
+ struct RClass *c;
+
+ if (mrb_nil_p(name)) {
+ c = mrb_class_new(mrb, klass);
+ }
+ else {
+ /* old style: should we warn? */
+ name = mrb_str_to_str(mrb, name);
+ id = mrb_to_id(mrb, name);
+ if (!mrb_is_const_id(id)) {
+ mrb_name_error(mrb, id, "identifier %s needs to be constant", mrb_string_value_ptr(mrb, name));
+ }
+ if (mrb_const_defined_at(mrb, klass, id)) {
+ mrb_warn("redefining constant Struct::%s", mrb_string_value_ptr(mrb, name));
+ //?rb_mod_remove_const(klass, mrb_sym2name(mrb, id));
+ }
+ c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass);
+ }
+ MRB_SET_INSTANCE_TT(c, MRB_TT_STRUCT);
+ nstr = mrb_obj_value(c);
+ mrb_iv_set(mrb, nstr, mrb_intern(mrb, "__members__"), members);
+
+ mrb_define_class_method(mrb, c, "new", mrb_instance_new, ARGS_ANY());
+ mrb_define_class_method(mrb, c, "[]", mrb_instance_new, ARGS_ANY());
+ mrb_define_class_method(mrb, c, "members", mrb_struct_s_members_m, ARGS_NONE());
+ //RSTRUCT(nstr)->basic.c->super = c->c;
+ ptr_members = RARRAY_PTR(members);
+ len = RARRAY_LEN(members);
+ for (i=0; i< len; i++) {
+ mrb_sym id = mrb_symbol(ptr_members[i]);
+ if (mrb_is_local_id(id) || mrb_is_const_id(id)) {
+ if (i < N_REF_FUNC) {
+ mrb_define_method_id(mrb, c, id, ref_func[i], ARGS_NONE());
+ }
+ else {
+ mrb_define_method_id(mrb, c, id, mrb_struct_ref, ARGS_NONE());
+ }
+ mrb_define_method_id(mrb, c, mrb_id_attrset(mrb, id), mrb_struct_set_m, ARGS_REQ(1));
+ }
+ }
+
+ return nstr;
+}
+
+mrb_value
+mrb_struct_define(mrb_state *mrb, const char *name, ...)
+{
+ va_list ar;
+ mrb_value nm, ary;
+ char *mem;
+
+ if (!name) nm = mrb_nil_value();
+ else nm = mrb_str_new2(mrb, name);
+ ary = mrb_ary_new(mrb);
+
+ va_start(ar, name);
+ while ((mem = va_arg(ar, char*)) != 0) {
+ mrb_sym slot = mrb_intern(mrb, mem);
+ mrb_ary_push(mrb, ary, mrb_symbol_value(slot));
+ }
+ va_end(ar);
+
+ return make_struct(mrb, nm, ary, struct_class(mrb));
+}
+
+/* 15.2.18.3.1 */
+/*
+ * call-seq:
+ * Struct.new( [aString] [, aSym]+> ) -> StructClass
+ * StructClass.new(arg, ...) -> obj
+ * StructClass[arg, ...] -> obj
+ *
+ * Creates a new class, named by <i>aString</i>, containing accessor
+ * methods for the given symbols. If the name <i>aString</i> is
+ * omitted, an anonymous structure class will be created. Otherwise,
+ * the name of this struct will appear as a constant in class
+ * <code>Struct</code>, so it must be unique for all
+ * <code>Struct</code>s in the system and should start with a capital
+ * letter. Assigning a structure class to a constant effectively gives
+ * the class the name of the constant.
+ *
+ * <code>Struct::new</code> returns a new <code>Class</code> object,
+ * which can then be used to create specific instances of the new
+ * structure. The number of actual parameters must be
+ * less than or equal to the number of attributes defined for this
+ * class; unset parameters default to <code>nil</code>. Passing too many
+ * parameters will raise an <code>ArgumentError</code>.
+ *
+ * The remaining methods listed in this section (class and instance)
+ * are defined for this generated class.
+ *
+ * # Create a structure with a name in Struct
+ * Struct.new("Customer", :name, :address) #=> Struct::Customer
+ * Struct::Customer.new("Dave", "123 Main") #=> #<struct Struct::Customer name="Dave", address="123 Main">
+ *
+ * # Create a structure named by its constant
+ * Customer = Struct.new(:name, :address) #=> Customer
+ * Customer.new("Dave", "123 Main") #=> #<struct Customer name="Dave", address="123 Main">
+ */
+static mrb_value
+mrb_struct_s_def(mrb_state *mrb, mrb_value klass)
+{
+ mrb_value name, rest;
+ mrb_value *pargv;
+ int argcnt;
+ long i;
+ mrb_value b, st;
+ mrb_sym id;
+ mrb_value *argv;
+ int argc;
+
+ name = mrb_nil_value();
+ rest = mrb_nil_value();
+ mrb_get_args(mrb, "*&", &argv, &argc, &b);
+ if (argc == 0) { /* special case to avoid crash */
+ rest = mrb_ary_new(mrb);
+ }
+ else {
+ if (argc > 0) name = argv[0];
+ if (argc > 1) rest = argv[1];
+ if (mrb_array_p(rest)) {
+ if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
+ /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
+ mrb_ary_unshift(mrb, rest, name);
+ name = mrb_nil_value();
+ }
+ }
+ else {
+ pargv = &argv[1];
+ argcnt = argc-1;
+ if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
+ /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
+ name = mrb_nil_value();
+ pargv = &argv[0];
+ argcnt++;
+ }
+ rest = mrb_ary_new_from_values(mrb, argcnt, pargv);
+ }
+ for (i=0; i<RARRAY_LEN(rest); i++) {
+ id = mrb_to_id(mrb, RARRAY_PTR(rest)[i]);
+ RARRAY_PTR(rest)[i] = mrb_symbol_value(id);
+ }
+ }
+ st = make_struct(mrb, name, rest, struct_class(mrb));
+ if (!mrb_nil_p(b)) {
+ mrb_funcall(mrb, b, "call", 1, &st);
+ }
+
+ return st;
+}
+
+static int
+num_members(mrb_state *mrb, struct RClass *klass)
+{
+ mrb_value members;
+ members = struct_ivar_get(mrb, mrb_obj_value(klass), mrb_intern(mrb, "__members__"));
+ if (!mrb_array_p(members)) {
+ mrb_raise(mrb, E_TYPE_ERROR, "broken members");
+ }
+ return RARRAY_LEN(members);
+}
+
+/* 15.2.18.4.8 */
+/*
+ */
+static mrb_value
+mrb_struct_initialize_withArg(mrb_state *mrb, int argc, mrb_value *argv, mrb_value self)
+{
+ struct RClass *klass = mrb_obj_class(mrb, self);
+ int n;
+ struct RStruct *st;
+
+ n = num_members(mrb, klass);
+ if (n < argc) {
+ mrb_raise(mrb, E_ARGUMENT_ERROR, "struct size differs");
+ }
+ st = RSTRUCT(self);
+ st->ptr = (mrb_value *)mrb_calloc(mrb, sizeof(mrb_value), n);
+ st->len = n;
+ struct_copy(st->ptr, argv, argc);
+
+ return self;
+}
+
+static mrb_value
+mrb_struct_initialize_m(mrb_state *mrb, /*int argc, mrb_value *argv,*/ mrb_value self)
+{
+ mrb_value *argv;
+ int argc;
+
+ mrb_get_args(mrb, "*", &argv, &argc);
+ return mrb_struct_initialize_withArg(mrb, argc, argv, self);
+}
+
+mrb_value
+mrb_struct_initialize(mrb_state *mrb, mrb_value self, mrb_value values)
+{
+ return mrb_struct_initialize_withArg(mrb, RARRAY_LEN(values), RARRAY_PTR(values), self);
+}
+
+static mrb_value
+inspect_struct(mrb_state *mrb, mrb_value s, int recur)
+{
+ const char *cn = mrb_class_name(mrb, mrb_obj_class(mrb, s));
+ mrb_value members, str = mrb_str_new(mrb, "#<struct ", 9);
+ mrb_value *ptr, *ptr_members;
+ long i, len;
+
+ if (cn) {
+ mrb_str_append(mrb, str, mrb_str_new_cstr(mrb, cn));
+ }
+ if (recur) {
+ return mrb_str_cat2(mrb, str, ":...>");
+ }
+
+ members = mrb_struct_members(mrb, s);
+ ptr_members = RARRAY_PTR(members);
+ ptr = RSTRUCT_PTR(s);
+ len = RSTRUCT_LEN(s);
+ for (i=0; i<len; i++) {
+ mrb_value slot;
+ mrb_sym id;
+
+ if (i > 0) {
+ mrb_str_cat2(mrb, str, ", ");
+ }
+ else if (cn) {
+ mrb_str_cat2(mrb, str, " ");
+ }
+ slot = ptr_members[i];
+ id = mrb_symbol(slot);
+ if (mrb_is_local_id(id) || mrb_is_const_id(id)) {
+ const char *name;
+ int len;
+
+ name = mrb_sym2name_len(mrb, id, &len);
+ mrb_str_append(mrb, str, mrb_str_new(mrb, name, len));
+ }
+ else {
+ mrb_str_append(mrb, str, mrb_inspect(mrb, slot));
+ }
+ mrb_str_cat2(mrb, str, "=");
+ mrb_str_append(mrb, str, mrb_inspect(mrb, ptr[i]));
+ }
+ mrb_str_cat2(mrb, str, ">");
+
+ return str;
+}
+
+/*
+ * call-seq:
+ * struct.to_s -> string
+ * struct.inspect -> string
+ *
+ * Describe the contents of this struct in a string.
+ */
+static mrb_value
+mrb_struct_inspect(mrb_state *mrb, mrb_value s)
+{
+ return inspect_struct(mrb, s, 0);
+}
+
+/* 15.2.18.4.9 */
+/* :nodoc: */
+mrb_value
+mrb_struct_init_copy(mrb_state *mrb, mrb_value copy)
+{
+ mrb_value s;
+
+ mrb_get_args(mrb, "o", &s);
+
+ if (mrb_obj_equal(mrb, copy, s)) return copy;
+ if (!mrb_obj_is_instance_of(mrb, s, mrb_obj_class(mrb, copy))) {
+ mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
+ }
+ if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) {
+ mrb_raise(mrb, E_TYPE_ERROR, "struct size mismatch");
+ }
+ struct_copy(RSTRUCT_PTR(copy), RSTRUCT_PTR(s), RSTRUCT_LEN(copy));
+
+ return copy;
+}
+
+static mrb_value
+mrb_struct_aref_id(mrb_state *mrb, mrb_value s, mrb_sym id)
+{
+ mrb_value *ptr, members, *ptr_members;
+ long i, len;
+
+ ptr = RSTRUCT_PTR(s);
+ members = mrb_struct_members(mrb, s);
+ ptr_members = RARRAY_PTR(members);
+ len = RARRAY_LEN(members);
+ for (i=0; i<len; i++) {
+ if (mrb_symbol(ptr_members[i]) == id) {
+ return ptr[i];
+ }
+ }
+ mrb_name_error(mrb, id, "no member '%s' in struct", mrb_sym2name(mrb, id));
+ return mrb_nil_value(); /* not reached */
+}
+
+/* 15.2.18.4.2 */
+/*
+ * call-seq:
+ * struct[symbol] -> anObject
+ * struct[fixnum] -> anObject
+ *
+ * Attribute Reference---Returns the value of the instance variable
+ * named by <i>symbol</i>, or indexed (0..length-1) by
+ * <i>fixnum</i>. Will raise <code>NameError</code> if the named
+ * variable does not exist, or <code>IndexError</code> if the index is
+ * out of range.
+ *
+ * Customer = Struct.new(:name, :address, :zip)
+ * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
+ *
+ * joe["name"] #=> "Joe Smith"
+ * joe[:name] #=> "Joe Smith"
+ * joe[0] #=> "Joe Smith"
+ */
+mrb_value
+mrb_struct_aref_n(mrb_state *mrb, mrb_value s, mrb_value idx)
+{
+ long i;
+
+ if (mrb_string_p(idx) || mrb_symbol_p(idx)) {
+ return mrb_struct_aref_id(mrb, s, mrb_to_id(mrb, idx));
+ }
+
+ i = mrb_fixnum(idx);
+ if (i < 0) i = RSTRUCT_LEN(s) + i;
+ if (i < 0)
+ mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)",
+ i, RSTRUCT_LEN(s));
+ if (RSTRUCT_LEN(s) <= i)
+ mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)",
+ i, RSTRUCT_LEN(s));
+ return RSTRUCT_PTR(s)[i];
+}
+
+mrb_value
+mrb_struct_aref(mrb_state *mrb, mrb_value s)
+{
+ mrb_value idx;
+
+ mrb_get_args(mrb, "o", &idx);
+ return mrb_struct_aref_n(mrb, s, idx);
+}
+
+static mrb_value
+mrb_struct_aset_id(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val)
+{
+ mrb_value members, *ptr, *ptr_members;
+ long i, len;
+
+ members = mrb_struct_members(mrb, s);
+ len = RARRAY_LEN(members);
+ if (RSTRUCT_LEN(s) != len) {
+ mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)",
+ len, RSTRUCT_LEN(s));
+ }
+ ptr = RSTRUCT_PTR(s);
+ ptr_members = RARRAY_PTR(members);
+ for (i=0; i<len; i++) {
+ if (mrb_symbol(ptr_members[i]) == id) {
+ ptr[i] = val;
+ return val;
+ }
+ }
+ mrb_name_error(mrb, id, "no member '%s' in struct", mrb_sym2name(mrb, id));
+ return val; /* not reach */
+}
+
+/* 15.2.18.4.3 */
+/*
+ * call-seq:
+ * struct[symbol] = obj -> obj
+ * struct[fixnum] = obj -> obj
+ *
+ * Attribute Assignment---Assigns to the instance variable named by
+ * <i>symbol</i> or <i>fixnum</i> the value <i>obj</i> and
+ * returns it. Will raise a <code>NameError</code> if the named
+ * variable does not exist, or an <code>IndexError</code> if the index
+ * is out of range.
+ *
+ * Customer = Struct.new(:name, :address, :zip)
+ * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
+ *
+ * joe["name"] = "Luke"
+ * joe[:zip] = "90210"
+ *
+ * joe.name #=> "Luke"
+ * joe.zip #=> "90210"
+ */
+
+mrb_value
+mrb_struct_aset(mrb_state *mrb, mrb_value s)
+{
+ long i;
+ mrb_value idx;
+ mrb_value val;
+
+ mrb_get_args(mrb, "oo", &idx, &val);
+
+ if (mrb_string_p(idx) || mrb_symbol_p(idx)) {
+ return mrb_struct_aset_id(mrb, s, mrb_to_id(mrb, idx), val);
+ }
+
+ i = mrb_fixnum(idx);
+ if (i < 0) i = RSTRUCT_LEN(s) + i;
+ if (i < 0) {
+ mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)",
+ i, RSTRUCT_LEN(s));
+ }
+ if (RSTRUCT_LEN(s) <= i) {
+ mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)",
+ i, RSTRUCT_LEN(s));
+ }
+ return RSTRUCT_PTR(s)[i] = val;
+}
+
+/* 15.2.18.4.1 */
+/*
+ * call-seq:
+ * struct == other_struct -> true or false
+ *
+ * Equality---Returns <code>true</code> if <i>other_struct</i> is
+ * equal to this one: they must be of the same class as generated by
+ * <code>Struct::new</code>, and the values of all instance variables
+ * must be equal (according to <code>Object#==</code>).
+ *
+ * Customer = Struct.new(:name, :address, :zip)
+ * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
+ * joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
+ * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345)
+ * joe == joejr #=> true
+ * joe == jane #=> false
+ */
+
+static mrb_value
+mrb_struct_equal(mrb_state *mrb, mrb_value s)
+{
+ mrb_value s2;
+ mrb_value *ptr, *ptr2;
+ long i, len;
+
+ mrb_get_args(mrb, "o", &s2);
+ if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value();
+ if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value();
+ if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) return mrb_false_value();
+ if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
+ mrb_bug("inconsistent struct"); /* should never happen */
+ }
+ ptr = RSTRUCT_PTR(s);
+ ptr2 = RSTRUCT_PTR(s2);
+ len = RSTRUCT_LEN(s);
+ for (i=0; i<len; i++) {
+ if (!mrb_equal(mrb, ptr[i], ptr2[i])) return mrb_false_value();
+ }
+ return mrb_true_value();
+}
+
+/* 15.2.18.4.12(x) */
+/*
+ * code-seq:
+ * struct.eql?(other) -> true or false
+ *
+ * Two structures are equal if they are the same object, or if all their
+ * fields are equal (using <code>eql?</code>).
+ */
+static mrb_value
+mrb_struct_eql(mrb_state *mrb, mrb_value s)
+{
+ mrb_value s2;
+ mrb_value *ptr, *ptr2;
+ long i, len;
+
+ mrb_get_args(mrb, "o", &s2);
+ if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value();
+ if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value();
+ if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) return mrb_false_value();
+ if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
+ mrb_bug("inconsistent struct"); /* should never happen */
+ }
+
+ ptr = RSTRUCT_PTR(s);
+ ptr2 = RSTRUCT_PTR(s2);
+ len = RSTRUCT_LEN(s);
+ for (i=0; i<len; i++) {
+ if (!mrb_eql(mrb, ptr[i], ptr2[i])) return mrb_false_value();
+ }
+ return mrb_true_value();
+}
+
+/*
+ * A <code>Struct</code> is a convenient way to bundle a number of
+ * attributes together, using accessor methods, without having to write
+ * an explicit class.
+ *
+ * The <code>Struct</code> class is a generator of specific classes,
+ * each one of which is defined to hold a set of variables and their
+ * accessors. In these examples, we'll call the generated class
+ * ``<i>Customer</i>Class,'' and we'll show an example instance of that
+ * class as ``<i>Customer</i>Inst.''
+ *
+ * In the descriptions that follow, the parameter <i>symbol</i> refers
+ * to a symbol, which is either a quoted string or a
+ * <code>Symbol</code> (such as <code>:name</code>).
+ */
+void
+mrb_init_struct(mrb_state *mrb)
+{
+ struct RClass *st;
+ st = mrb_define_class(mrb, "Struct", mrb->object_class);
+
+ mrb_define_class_method(mrb, st, "new", mrb_struct_s_def, ARGS_ANY()); /* 15.2.18.3.1 */
+
+ mrb_define_method(mrb, st, "==", mrb_struct_equal, ARGS_REQ(1)); /* 15.2.18.4.1 */
+ mrb_define_method(mrb, st, "[]", mrb_struct_aref, ARGS_REQ(1)); /* 15.2.18.4.2 */
+ mrb_define_method(mrb, st, "[]=", mrb_struct_aset, ARGS_REQ(2)); /* 15.2.18.4.3 */
+ mrb_define_method(mrb, st, "members", mrb_struct_members_m, ARGS_NONE()); /* 15.2.18.4.6 */
+ mrb_define_method(mrb, st, "initialize", mrb_struct_initialize_m,ARGS_ANY()); /* 15.2.18.4.8 */
+ mrb_define_method(mrb, st, "initialize_copy", mrb_struct_init_copy, ARGS_REQ(1)); /* 15.2.18.4.9 */
+ mrb_define_method(mrb, st, "inspect", mrb_struct_inspect, ARGS_NONE()); /* 15.2.18.4.10(x) */
+ mrb_define_alias(mrb, st, "to_s", "inspect"); /* 15.2.18.4.11(x) */
+ mrb_define_method(mrb, st, "eql?", mrb_struct_eql, ARGS_REQ(1)); /* 15.2.18.4.12(x) */
+
+}
diff --git a/mrbgems/mruby-struct/test/struct.rb b/mrbgems/mruby-struct/test/struct.rb
new file mode 100644
index 000000000..d79b30c0e
--- /dev/null
+++ b/mrbgems/mruby-struct/test/struct.rb
@@ -0,0 +1,77 @@
+##
+# Struct ISO Test
+
+if Object.const_defined?(:Struct)
+ assert('Struct', '15.2.18') do
+ Struct.class == Class
+ end
+
+ assert('Struct superclass', '15.2.18.2') do
+ Struct.superclass == Object
+ end
+
+ assert('Struct.new', '15.2.18.3.1') do
+ c = Struct.new(:m1, :m2)
+ c.superclass == Struct and
+ c.members == [:m1,:m2]
+ end
+
+ # Check crash bug with Struc.new and no params.
+ assert('Struct.new', '15.2.18.3.1') do
+ c = Struct.new()
+ c.superclass == Struct and c.members == []
+ end
+
+ assert('Struct#==', '15.2.18.4.1') do
+ c = Struct.new(:m1, :m2)
+ cc1 = c.new(1,2)
+ cc2 = c.new(1,2)
+ cc1 == cc2
+ end
+
+ assert('Struct#[]', '15.2.18.4.2') do
+ c = Struct.new(:m1, :m2)
+ cc = c.new(1,2)
+ cc[:m1] == 1 and cc["m2"] == 2
+ end
+
+ assert('Struct#[]=', '15.2.18.4.3') do
+ c = Struct.new(:m1, :m2)
+ cc = c.new(1,2)
+ cc[:m1] = 3
+ cc[:m1] == 3
+ end
+
+ assert('Struct#each', '15.2.18.4.4') do
+ c = Struct.new(:m1, :m2)
+ cc = c.new(1,2)
+ a = []
+ cc.each{|x|
+ a << x
+ }
+ a[0] == 1 and a[1] == 2
+ end
+
+ assert('Struct#each_pair', '15.2.18.4.5') do
+ c = Struct.new(:m1, :m2)
+ cc = c.new(1,2)
+ a = []
+ cc.each_pair{|k,v|
+ a << [k,v]
+ }
+ a[0] == [:m1, 1] and a[1] == [:m2, 2]
+ end
+
+ assert('Struct#members', '15.2.18.4.6') do
+ c = Struct.new(:m1, :m2)
+ cc = c.new(1,2)
+ cc.members == [:m1,:m2]
+ end
+
+ assert('Struct#select', '15.2.18.4.7') do
+ c = Struct.new(:m1, :m2)
+ cc = c.new(1,2)
+ cc.select{|v| v % 2 == 0} == [2]
+ end
+end
+
diff --git a/mrbgems/mruby-time/mrbgem.rake b/mrbgems/mruby-time/mrbgem.rake
new file mode 100644
index 000000000..0f0b4899d
--- /dev/null
+++ b/mrbgems/mruby-time/mrbgem.rake
@@ -0,0 +1,4 @@
+MRuby::Gem::Specification.new('mruby-time') do |spec|
+ spec.license = 'MIT'
+ spec.authors = 'mruby developers'
+end
diff --git a/mrbgems/mruby-time/src/time.c b/mrbgems/mruby-time/src/time.c
new file mode 100644
index 000000000..ed40c5279
--- /dev/null
+++ b/mrbgems/mruby-time/src/time.c
@@ -0,0 +1,755 @@
+/*
+** time.c - Time class
+**
+** See Copyright Notice in mruby.h
+*/
+
+
+#include "mruby.h"
+#include <string.h>
+#include <stdio.h>
+#include <time.h>
+#include "mruby/class.h"
+#include "mruby/data.h"
+
+/** Time class configuration */
+
+/* gettimeofday(2) */
+/* C99 does not have gettimeofday that is required to retrieve microseconds */
+/* uncomment following macro on platforms without gettimeofday(2) */
+/* #define NO_GETTIMEOFDAY */
+
+/* gmtime(3) */
+/* C99 does not have reentrant gmtime_r() so it might cause troubles under */
+/* multi-threading environment. undef following macro on platforms that */
+/* does not have gmtime_r() and localtime_r(). */
+/* #define NO_GMTIME_R */
+
+#ifdef _WIN32
+#if _MSC_VER
+/* Win32 platform do not provide gmtime_r/localtime_r; emulate them using gmtime_s/localtime_s */
+#define gmtime_r(tp, tm) ((gmtime_s((tm), (tp)) == 0) ? (tm) : NULL)
+#define localtime_r(tp, tm) ((localtime_s((tm), (tp)) == 0) ? (tm) : NULL)
+#else
+#define NO_GMTIME_R
+#endif
+#endif
+
+/* timegm(3) */
+/* mktime() creates tm structure for localtime; timegm() is for UTF time */
+/* define following macro to use probably faster timegm() on the platform */
+/* #define USE_SYSTEM_TIMEGM */
+
+/** end of Time class configuration */
+
+#ifndef NO_GETTIMEOFDAY
+#include <sys/time.h>
+#endif
+#ifdef NO_GMTIME_R
+#define gmtime_r(t,r) gmtime(t)
+#define localtime_r(t,r) (tzset(),localtime(t))
+#endif
+
+#ifndef USE_SYSTEM_TIMEGM
+#define timegm my_timgm
+
+static unsigned int
+is_leapyear(unsigned int y)
+{
+ return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
+}
+
+static time_t
+timegm(struct tm *tm)
+{
+ static const unsigned int ndays[2][12] = {
+ {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
+ {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
+ };
+ time_t r = 0;
+ int i;
+ unsigned int *nday = (unsigned int*) ndays[is_leapyear(tm->tm_year+1900)];
+
+ for (i = 70; i < tm->tm_year; ++i)
+ r += is_leapyear(i+1900) ? 366*24*60*60 : 365*24*60*60;
+ for (i = 0; i < tm->tm_mon; ++i)
+ r += nday[i] * 24 * 60 * 60;
+ r += (tm->tm_mday - 1) * 24 * 60 * 60;
+ r += tm->tm_hour * 60 * 60;
+ r += tm->tm_min * 60;
+ r += tm->tm_sec;
+ return r;
+}
+#endif
+
+/* Since we are limited to using ISO C89, this implementation is based
+* on time_t. That means the resolution of time is only precise to the
+* second level. Also, there are only 2 timezones, namely UTC and LOCAL.
+*/
+
+#ifndef mrb_bool_value
+#define mrb_bool_value(val) ((val) ? mrb_true_value() : mrb_false_value())
+#endif
+
+
+enum mrb_timezone {
+ MRB_TIMEZONE_NONE = 0,
+ MRB_TIMEZONE_UTC = 1,
+ MRB_TIMEZONE_LOCAL = 2,
+ MRB_TIMEZONE_LAST = 3
+};
+
+static const char *timezone_names[] = {
+ "none",
+ "UTC",
+ "LOCAL",
+ NULL
+};
+
+static const char *mon_names[] = {
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
+};
+
+static const char *wday_names[] = {
+ "Sun", "Mon", "Tus", "Wed", "Thu", "Fri", "Sat",
+};
+
+struct mrb_time {
+ time_t sec;
+ time_t usec;
+ enum mrb_timezone timezone;
+ struct tm datetime;
+};
+
+static void
+mrb_time_free(mrb_state *mrb, void *ptr)
+{
+ mrb_free(mrb, ptr);
+}
+
+static struct mrb_data_type mrb_time_type = { "Time", mrb_time_free };
+
+/** Updates the datetime of a mrb_time based on it's timezone and
+seconds setting. Returns self on cussess, NULL of failure. */
+static struct mrb_time*
+mrb_time_update_datetime(struct mrb_time *self)
+{
+ struct tm *aid;
+
+ if (self->timezone == MRB_TIMEZONE_UTC) {
+ aid = gmtime_r(&self->sec, &self->datetime);
+ }
+ else {
+ aid = localtime_r(&self->sec, &self->datetime);
+ }
+ if (!aid) return NULL;
+#ifdef NO_GMTIME_R
+ self->datetime = *aid; // copy data
+#endif
+
+ return self;
+}
+
+static mrb_value
+mrb_time_wrap(mrb_state *mrb, struct RClass *tc, struct mrb_time *tm)
+{
+ return mrb_obj_value(Data_Wrap_Struct(mrb, tc, &mrb_time_type, tm));
+}
+
+
+/* Allocates a mrb_time object and initializes it. */
+static struct mrb_time*
+mrb_time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time));
+ tm->sec = (time_t)sec;
+ tm->usec = (sec - tm->sec) * 1.0e6 + usec;
+ while (tm->usec < 0) {
+ tm->sec--;
+ tm->usec += 1.0e6;
+ }
+ while (tm->usec > 1.0e6) {
+ tm->sec++;
+ tm->usec -= 1.0e6;
+ }
+ tm->timezone = timezone;
+ mrb_time_update_datetime(tm);
+
+ return tm;
+}
+
+static mrb_value
+mrb_time_make(mrb_state *mrb, struct RClass *c, double sec, double usec, enum mrb_timezone timezone)
+{
+ return mrb_time_wrap(mrb, c, mrb_time_alloc(mrb, sec, usec, timezone));
+}
+
+static struct mrb_time*
+current_mrb_time(mrb_state *mrb)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
+#ifdef NO_GETTIMEOFDAY
+ {
+ static time_t last_sec = 0, last_usec = 0;
+
+ tm->sec = time(NULL);
+ if (tm->sec != last_sec) {
+ last_sec = tm->sec;
+ last_usec = 0;
+ }
+ else {
+ /* add 1 usec to differentiate two times */
+ last_usec += 1;
+ }
+ tm->usec = last_usec;
+ }
+#else
+ {
+ struct timeval tv;
+
+ gettimeofday(&tv, NULL);
+ tm->sec = tv.tv_sec;
+ tm->usec = tv.tv_usec;
+ }
+#endif
+ tm->timezone = MRB_TIMEZONE_LOCAL;
+ mrb_time_update_datetime(tm);
+
+ return tm;
+}
+
+/* Allocates a new Time object with given millis value. */
+static mrb_value
+mrb_time_now(mrb_state *mrb, mrb_value self)
+{
+ return mrb_time_wrap(mrb, mrb_class_ptr(self), current_mrb_time(mrb));
+}
+
+/* 15.2.19.6.1 */
+/* Creates an instance of time at the given time in seconds, etc. */
+static mrb_value
+mrb_time_at(mrb_state *mrb, mrb_value self)
+{
+ mrb_float f, f2 = 0;
+
+ mrb_get_args(mrb, "f|f", &f, &f2);
+ return mrb_time_make(mrb, mrb_class_ptr(self), f, f2, MRB_TIMEZONE_LOCAL);
+}
+
+static struct mrb_time*
+time_mktime(mrb_state *mrb, mrb_int ayear, mrb_int amonth, mrb_int aday,
+ mrb_int ahour, mrb_int amin, mrb_int asec, mrb_int ausec,
+ enum mrb_timezone timezone)
+{
+ time_t nowsecs;
+ struct tm nowtime = { 0 };
+
+ nowtime.tm_year = (int)ayear - 1900;
+ nowtime.tm_mon = (int)amonth - 1;
+ nowtime.tm_mday = (int)aday;
+ nowtime.tm_hour = (int)ahour;
+ nowtime.tm_min = (int)amin;
+ nowtime.tm_sec = (int)asec;
+ nowtime.tm_isdst = -1;
+ if (timezone == MRB_TIMEZONE_UTC) {
+ nowsecs = timegm(&nowtime);
+ }
+ else {
+ nowsecs = mktime(&nowtime);
+ }
+ if (nowsecs < 0) {
+ mrb_raise(mrb, E_ARGUMENT_ERROR, "Not a valid time.");
+ }
+
+ return mrb_time_alloc(mrb, nowsecs, ausec, timezone);
+}
+
+/* 15.2.19.6.2 */
+/* Creates an instance of time at the given time in UTC. */
+static mrb_value
+mrb_time_gm(mrb_state *mrb, mrb_value self)
+{
+ mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, amin = 0, asec = 0, ausec = 0;
+
+ mrb_get_args(mrb, "i|iiiiii",
+ &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
+ return mrb_time_wrap(mrb, mrb_class_ptr(self),
+ time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_UTC));
+}
+
+
+/* 15.2.19.6.3 */
+/* Creates an instance of time at the given time in local time zone. */
+static mrb_value
+mrb_time_local(mrb_state *mrb, mrb_value self)
+{
+ mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, amin = 0, asec = 0, ausec = 0;
+
+ mrb_get_args(mrb, "i|iiiiii",
+ &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
+ return mrb_time_wrap(mrb, mrb_class_ptr(self),
+ time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL));
+}
+
+
+static mrb_value
+mrb_time_eq(mrb_state *mrb, mrb_value self)
+{
+ mrb_value other;
+ struct mrb_time *tm1, *tm2;
+
+ mrb_get_args(mrb, "o", &other);
+ tm1 = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ tm2 = (struct mrb_time *)mrb_get_datatype(mrb, other, &mrb_time_type);
+ if (!tm1 || !tm2) return mrb_false_value();
+ if (tm1->sec == tm2->sec && tm1->usec == tm2->usec) {
+ return mrb_true_value();
+ }
+ return mrb_false_value();
+}
+
+static mrb_value
+mrb_time_cmp(mrb_state *mrb, mrb_value self)
+{
+ mrb_value other;
+ struct mrb_time *tm1, *tm2;
+
+ mrb_get_args(mrb, "o", &other);
+ tm1 = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ tm2 = (struct mrb_time *)mrb_get_datatype(mrb, other, &mrb_time_type);
+ if (!tm1 || !tm2) return mrb_nil_value();
+ if (tm1->sec > tm2->sec) {
+ return mrb_fixnum_value(1);
+ }
+ else if (tm1->sec < tm2->sec) {
+ return mrb_fixnum_value(-1);
+ }
+ /* tm1->sec == tm2->sec */
+ if (tm1->usec > tm2->usec) {
+ return mrb_fixnum_value(1);
+ }
+ else if (tm1->usec < tm2->usec) {
+ return mrb_fixnum_value(-1);
+ }
+ return mrb_fixnum_value(0);
+}
+
+static mrb_value
+mrb_time_plus(mrb_state *mrb, mrb_value self)
+{
+ mrb_float f;
+ struct mrb_time *tm;
+
+ mrb_get_args(mrb, "f", &f);
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec+f, tm->usec, tm->timezone);
+}
+
+static mrb_value
+mrb_time_minus(mrb_state *mrb, mrb_value self)
+{
+ mrb_float f;
+ mrb_value other;
+ struct mrb_time *tm, *tm2;
+
+ mrb_get_args(mrb, "o", &other);
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+
+ tm2 = (struct mrb_time *)mrb_get_datatype(mrb, other, &mrb_time_type);
+ if (tm2) {
+ f = (mrb_float)(tm->sec - tm2->sec)
+ + (mrb_float)(tm->usec - tm2->usec) / 1.0e6;
+ return mrb_float_value(f);
+ }
+ else {
+ mrb_get_args(mrb, "f", &f);
+ return mrb_time_make(mrb, mrb_obj_class(mrb, self), (double)tm->sec-f, tm->usec, tm->timezone);
+ }
+}
+
+/* 15.2.19.7.30 */
+/* Returns week day number of time. */
+static mrb_value
+mrb_time_wday(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_fixnum_value(tm->datetime.tm_wday);
+}
+
+/* 15.2.19.7.31 */
+/* Returns year day number of time. */
+static mrb_value
+mrb_time_yday(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_check_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_fixnum_value(tm->datetime.tm_yday + 1);
+}
+
+/* 15.2.19.7.32 */
+/* Returns year of time. */
+static mrb_value
+mrb_time_year(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_fixnum_value(tm->datetime.tm_year + 1900);
+}
+
+/* 15.2.19.7.33 */
+/* Returns name of time's timezone. */
+static mrb_value
+mrb_time_zone(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ if (tm->timezone <= MRB_TIMEZONE_NONE) return mrb_nil_value();
+ if (tm->timezone >= MRB_TIMEZONE_LAST) return mrb_nil_value();
+ return mrb_str_new_cstr(mrb, timezone_names[tm->timezone]);
+}
+
+/* 15.2.19.7.4 */
+/* Returns a string that describes the time. */
+static mrb_value
+mrb_time_asctime(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+ struct tm *d;
+ char buf[256];
+ int len;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ d = &tm->datetime;
+ len = snprintf(buf, sizeof(buf), "%s %s %02d %02d:%02d:%02d %s%d",
+ wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday,
+ d->tm_hour, d->tm_min, d->tm_sec,
+ tm->timezone == MRB_TIMEZONE_UTC ? "UTC " : "",
+ d->tm_year + 1900);
+ return mrb_str_new(mrb, buf, len);
+}
+
+/* 15.2.19.7.6 */
+/* Returns the day in the month of the time. */
+static mrb_value
+mrb_time_day(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_fixnum_value(tm->datetime.tm_mday);
+}
+
+
+/* 15.2.19.7.7 */
+/* Returns true if daylight saving was applied for this time. */
+static mrb_value
+mrb_time_dstp(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_bool_value(tm->datetime.tm_isdst);
+}
+
+/* 15.2.19.7.8 */
+/* 15.2.19.7.10 */
+/* Returns the Time object of the UTC(GMT) timezone. */
+static mrb_value
+mrb_time_getutc(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm, *tm2;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return self;
+ tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
+ *tm2 = *tm;
+ tm2->timezone = MRB_TIMEZONE_UTC;
+ mrb_time_update_datetime(tm2);
+ return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
+}
+
+/* 15.2.19.7.9 */
+/* Returns the Time object of the LOCAL timezone. */
+static mrb_value
+mrb_time_getlocal(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm, *tm2;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return self;
+ tm2 = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
+ *tm2 = *tm;
+ tm2->timezone = MRB_TIMEZONE_LOCAL;
+ mrb_time_update_datetime(tm2);
+ return mrb_time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
+}
+
+/* 15.2.19.7.15 */
+/* Returns hour of time. */
+static mrb_value
+mrb_time_hour(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_fixnum_value(tm->datetime.tm_hour);
+}
+
+/* 15.2.19.7.16 */
+/* Initializes a time by setting the amount of milliseconds since the epoch.*/
+static mrb_value
+mrb_time_initialize(mrb_state *mrb, mrb_value self)
+{
+ mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0,
+ amin = 0, asec = 0, ausec = 0;
+ int n;
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (tm) {
+ mrb_time_free(mrb, tm);
+ }
+
+ n = mrb_get_args(mrb, "|iiiiiii",
+ &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec);
+ if (n == 0) {
+ tm = current_mrb_time(mrb);
+ }
+ else {
+ tm = time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL);
+ }
+ DATA_PTR(self) = tm;
+ DATA_TYPE(self) = &mrb_time_type;
+ return self;
+}
+
+/* 15.2.19.7.17(x) */
+/* Initializes a copy of this time object. */
+static mrb_value
+mrb_time_initialize_copy(mrb_state *mrb, mrb_value copy)
+{
+ mrb_value src;
+
+ mrb_get_args(mrb, "o", &src);
+ if (mrb_obj_equal(mrb, copy, src)) return copy;
+ if (!mrb_obj_is_instance_of(mrb, src, mrb_obj_class(mrb, copy))) {
+ mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
+ }
+ if (!DATA_PTR(copy)) {
+ DATA_PTR(copy) = mrb_malloc(mrb, sizeof(struct mrb_time));
+ DATA_TYPE(copy) = &mrb_time_type;
+ }
+ *(struct mrb_time *)DATA_PTR(copy) = *(struct mrb_time *)DATA_PTR(src);
+ return copy;
+}
+
+/* 15.2.19.7.18 */
+/* Sets the timezone attribute of the Time object to LOCAL. */
+static mrb_value
+mrb_time_localtime(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return self;
+ tm->timezone = MRB_TIMEZONE_LOCAL;
+ mrb_time_update_datetime(tm);
+ return self;
+}
+
+/* 15.2.19.7.19 */
+/* Returns day of month of time. */
+static mrb_value
+mrb_time_mday(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_fixnum_value(tm->datetime.tm_mday);
+}
+
+/* 15.2.19.7.20 */
+/* Returns minutes of time. */
+static mrb_value
+mrb_time_min(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_fixnum_value(tm->datetime.tm_min);
+}
+
+/* 15.2.19.7.21 and 15.2.19.7.22 */
+/* Returns month of time. */
+static mrb_value
+mrb_time_mon(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_fixnum_value(tm->datetime.tm_mon + 1);
+}
+
+/* 15.2.19.7.23 */
+/* Returns seconds in minute of time. */
+static mrb_value
+mrb_time_sec(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_fixnum_value(tm->datetime.tm_sec);
+}
+
+
+/* 15.2.19.7.24 */
+/* Returns a Float with the time since the epoch in seconds. */
+static mrb_value
+mrb_time_to_f(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_float_value((mrb_float)tm->sec + (mrb_float)tm->usec/1.0e6);
+}
+
+/* 15.2.19.7.25 */
+/* Returns a Fixnum with the time since the epoch in seconds. */
+static mrb_value
+mrb_time_to_i(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_fixnum_value(tm->sec);
+}
+
+/* 15.2.19.7.26 */
+/* Returns a Float with the time since the epoch in microseconds. */
+static mrb_value
+mrb_time_usec(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_fixnum_value(tm->usec);
+}
+
+/* 15.2.19.7.27 */
+/* Sets the timzeone attribute of the Time object to UTC. */
+static mrb_value
+mrb_time_utc(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (tm) {
+ tm->timezone = MRB_TIMEZONE_UTC;
+ mrb_time_update_datetime(tm);
+ }
+ return self;
+}
+
+/* 15.2.19.7.28 */
+/* Returns true if this time is in the UTC timze zone false if not. */
+static mrb_value
+mrb_time_utcp(mrb_state *mrb, mrb_value self)
+{
+ struct mrb_time *tm;
+ tm = (struct mrb_time *)mrb_get_datatype(mrb, self, &mrb_time_type);
+ if (!tm) return mrb_nil_value();
+ return mrb_bool_value(tm->timezone == MRB_TIMEZONE_UTC);
+}
+
+
+
+void
+mrb_mruby_time_gem_init(mrb_state* mrb)
+{
+ struct RClass *tc;
+ /* ISO 15.2.19.2 */
+ tc = mrb_define_class(mrb, "Time", mrb->object_class);
+ MRB_SET_INSTANCE_TT(tc, MRB_TT_DATA);
+ mrb_include_module(mrb, tc, mrb_class_get(mrb, "Comparable"));
+ mrb_define_class_method(mrb, tc, "at", mrb_time_at, ARGS_ANY()); /* 15.2.19.6.1 */
+ mrb_define_class_method(mrb, tc, "gm", mrb_time_gm, ARGS_REQ(1)|ARGS_OPT(6)); /* 15.2.19.6.2 */
+ mrb_define_class_method(mrb, tc, "local", mrb_time_local, ARGS_REQ(1)|ARGS_OPT(6)); /* 15.2.19.6.3 */
+ mrb_define_class_method(mrb, tc, "mktime", mrb_time_local, ARGS_REQ(1)|ARGS_OPT(6));/* 15.2.19.6.4 */
+ mrb_define_class_method(mrb, tc, "now", mrb_time_now, ARGS_NONE()); /* 15.2.19.6.5 */
+ mrb_define_class_method(mrb, tc, "utc", mrb_time_gm, ARGS_REQ(1)|ARGS_OPT(6)); /* 15.2.19.6.6 */
+
+ mrb_define_method(mrb, tc, "==" , mrb_time_eq , ARGS_REQ(1));
+ mrb_define_method(mrb, tc, "<=>" , mrb_time_cmp , ARGS_REQ(1)); /* 15.2.19.7.1 */
+ mrb_define_method(mrb, tc, "+" , mrb_time_plus , ARGS_REQ(1)); /* 15.2.19.7.2 */
+ mrb_define_method(mrb, tc, "-" , mrb_time_minus , ARGS_REQ(1)); /* 15.2.19.7.3 */
+ mrb_define_method(mrb, tc, "to_s" , mrb_time_asctime, ARGS_NONE());
+ mrb_define_method(mrb, tc, "inspect", mrb_time_asctime, ARGS_NONE());
+ mrb_define_method(mrb, tc, "asctime", mrb_time_asctime, ARGS_NONE()); /* 15.2.19.7.4 */
+ mrb_define_method(mrb, tc, "ctime" , mrb_time_asctime, ARGS_NONE()); /* 15.2.19.7.5 */
+ mrb_define_method(mrb, tc, "day" , mrb_time_day , ARGS_NONE()); /* 15.2.19.7.6 */
+ mrb_define_method(mrb, tc, "dst?" , mrb_time_dstp , ARGS_NONE()); /* 15.2.19.7.7 */
+ mrb_define_method(mrb, tc, "getgm" , mrb_time_getutc , ARGS_NONE()); /* 15.2.19.7.8 */
+ mrb_define_method(mrb, tc, "getlocal",mrb_time_getlocal,ARGS_NONE()); /* 15.2.19.7.9 */
+ mrb_define_method(mrb, tc, "getutc" , mrb_time_getutc , ARGS_NONE()); /* 15.2.19.7.10 */
+ mrb_define_method(mrb, tc, "gmt?" , mrb_time_utcp , ARGS_NONE()); /* 15.2.19.7.11 */
+ mrb_define_method(mrb, tc, "gmtime" , mrb_time_utc , ARGS_NONE()); /* 15.2.19.7.13 */
+ mrb_define_method(mrb, tc, "hour" , mrb_time_hour, ARGS_NONE()); /* 15.2.19.7.15 */
+ mrb_define_method(mrb, tc, "localtime", mrb_time_localtime, ARGS_NONE()); /* 15.2.19.7.18 */
+ mrb_define_method(mrb, tc, "mday" , mrb_time_mday, ARGS_NONE()); /* 15.2.19.7.19 */
+ mrb_define_method(mrb, tc, "min" , mrb_time_min, ARGS_NONE()); /* 15.2.19.7.20 */
+
+ mrb_define_method(mrb, tc, "mon" , mrb_time_mon, ARGS_NONE()); /* 15.2.19.7.21 */
+ mrb_define_method(mrb, tc, "month", mrb_time_mon, ARGS_NONE()); /* 15.2.19.7.22 */
+
+ mrb_define_method(mrb, tc, "sec" , mrb_time_sec, ARGS_NONE()); /* 15.2.19.7.23 */
+ mrb_define_method(mrb, tc, "to_i", mrb_time_to_i, ARGS_NONE()); /* 15.2.19.7.25 */
+ mrb_define_method(mrb, tc, "to_f", mrb_time_to_f, ARGS_NONE()); /* 15.2.19.7.24 */
+ mrb_define_method(mrb, tc, "usec", mrb_time_usec, ARGS_NONE()); /* 15.2.19.7.26 */
+ mrb_define_method(mrb, tc, "utc" , mrb_time_utc, ARGS_NONE()); /* 15.2.19.7.27 */
+ mrb_define_method(mrb, tc, "utc?", mrb_time_utcp, ARGS_NONE()); /* 15.2.19.7.28 */
+ mrb_define_method(mrb, tc, "wday", mrb_time_wday, ARGS_NONE()); /* 15.2.19.7.30 */
+ mrb_define_method(mrb, tc, "yday", mrb_time_yday, ARGS_NONE()); /* 15.2.19.7.31 */
+ mrb_define_method(mrb, tc, "year", mrb_time_year, ARGS_NONE()); /* 15.2.19.7.32 */
+ mrb_define_method(mrb, tc, "zone", mrb_time_zone, ARGS_NONE()); /* 15.2.19.7.33 */
+
+ mrb_define_method(mrb, tc, "initialize", mrb_time_initialize, ARGS_REQ(1)); /* 15.2.19.7.16 */
+ mrb_define_method(mrb, tc, "initialize_copy", mrb_time_initialize_copy, ARGS_REQ(1)); /* 15.2.19.7.17 */
+
+ /*
+ methods not available:
+ gmt_offset(15.2.19.7.12)
+ gmtoff(15.2.19.7.14)
+ utc_offset(15.2.19.7.29)
+ */
+}
+
+void
+mrb_mruby_time_gem_final(mrb_state* mrb)
+{
+}
diff --git a/mrbgems/mruby-time/test/time.rb b/mrbgems/mruby-time/test/time.rb
new file mode 100644
index 000000000..f92459d5e
--- /dev/null
+++ b/mrbgems/mruby-time/test/time.rb
@@ -0,0 +1,201 @@
+##
+# Time ISO Test
+
+if Object.const_defined?(:Time)
+ assert('Time.new', '15.2.3.3.3') do
+ Time.new.class == Time
+ end
+
+ assert('Time', '15.2.19') do
+ Time.class == Class
+ end
+
+ assert('Time superclass', '15.2.19.2') do
+ Time.superclass == Object
+ end
+
+ assert('Time.at', '15.2.19.6.1') do
+ Time.at(1300000000.0)
+ end
+
+ assert('Time.gm', '15.2.19.6.2') do
+ Time.gm(2012, 12, 23)
+ end
+
+ assert('Time.local', '15.2.19.6.3') do
+ Time.local(2012, 12, 23)
+ end
+
+ assert('Time.mktime', '15.2.19.6.4') do
+ Time.mktime(2012, 12, 23)
+ end
+
+ assert('Time.now', '15.2.19.6.5') do
+ Time.now.class == Time
+ end
+
+ assert('Time.utc', '15.2.19.6.6') do
+ Time.utc(2012, 12, 23)
+ end
+
+ assert('Time#+', '15.2.19.7.1') do
+ t1 = Time.at(1300000000.0)
+ t2 = t1.+(60)
+
+ t2.utc.asctime == "Sun Mar 13 07:07:40 UTC 2011"
+ end
+
+ assert('Time#-', '15.2.19.7.2') do
+ t1 = Time.at(1300000000.0)
+ t2 = t1.-(60)
+
+ t2.utc.asctime == "Sun Mar 13 07:05:40 UTC 2011"
+ end
+
+ assert('Time#<=>', '15.2.19.7.3') do
+ t1 = Time.at(1300000000.0)
+ t2 = Time.at(1400000000.0)
+ t3 = Time.at(1500000000.0)
+
+ t2.<=>(t1) == 1 and
+ t2.<=>(t2) == 0 and
+ t2.<=>(t3) == -1 and
+ t2.<=>(nil) == nil
+ end
+
+ assert('Time#asctime', '15.2.19.7.4') do
+ Time.at(1300000000.0).utc.asctime == "Sun Mar 13 07:06:40 UTC 2011"
+ end
+
+ assert('Time#ctime', '15.2.19.7.5') do
+ Time.at(1300000000.0).utc.ctime == "Sun Mar 13 07:06:40 UTC 2011"
+ end
+
+ assert('Time#day', '15.2.19.7.6') do
+ Time.gm(2012, 12, 23).day == 23
+ end
+
+ assert('Time#dst?', '15.2.19.7.7') do
+ not Time.gm(2012, 12, 23).utc.dst?
+ end
+
+ assert('Time#getgm', '15.2.19.7.8') do
+ Time.at(1300000000.0).getgm.asctime == "Sun Mar 13 07:06:40 UTC 2011"
+ end
+
+ assert('Time#getlocal', '15.2.19.7.9') do
+ t1 = Time.at(1300000000.0)
+ t2 = Time.at(1300000000.0)
+ t3 = t1.getlocal
+
+ t1 == t3 and t3 == t2.getlocal
+ end
+
+ assert('Time#getutc', '15.2.19.7.10') do
+ Time.at(1300000000.0).getutc.asctime == "Sun Mar 13 07:06:40 UTC 2011"
+ end
+
+ assert('Time#gmt?', '15.2.19.7.11') do
+ Time.at(1300000000.0).utc.gmt?
+ end
+
+ # ATM not implemented
+ # assert('Time#gmt_offset', '15.2.19.7.12') do
+
+ assert('Time#gmtime', '15.2.19.7.13') do
+ Time.at(1300000000.0).gmtime
+ end
+
+ # ATM not implemented
+ # assert('Time#gmtoff', '15.2.19.7.14') do
+
+ assert('Time#hour', '15.2.19.7.15') do
+ Time.gm(2012, 12, 23, 7, 6).hour == 7
+ end
+
+ # ATM doesn't really work
+ # assert('Time#initialize', '15.2.19.7.16') do
+
+ assert('Time#initialize_copy', '15.2.19.7.17') do
+ time_tmp_2 = Time.at(7.0e6)
+ time_tmp_2.clone == time_tmp_2
+ end
+
+ assert('Time#localtime', '15.2.19.7.18') do
+ t1 = Time.at(1300000000.0)
+ t2 = Time.at(1300000000.0)
+
+ t1.localtime
+ t1 == t2.getlocal
+ end
+
+ assert('Time#mday', '15.2.19.7.19') do
+ Time.gm(2012, 12, 23).mday == 23
+ end
+
+ assert('Time#min', '15.2.19.7.20') do
+ Time.gm(2012, 12, 23, 7, 6).min == 6
+ end
+
+ assert('Time#mon', '15.2.19.7.21') do
+ Time.gm(2012, 12, 23).mon == 12
+ end
+
+ assert('Time#month', '15.2.19.7.22') do
+ Time.gm(2012, 12, 23).month == 12
+ end
+
+ assert('Times#sec', '15.2.19.7.23') do
+ Time.gm(2012, 12, 23, 7, 6, 40).sec == 40
+ end
+
+ assert('Time#to_f', '15.2.19.7.24') do
+ Time.at(1300000000.0).to_f == 1300000000.0
+ end
+
+ assert('Time#to_i', '15.2.19.7.25') do
+ Time.at(1300000000.0).to_i == 1300000000
+ end
+
+ assert('Time#usec', '15.2.19.7.26') do
+ Time.at(1300000000.0).usec == 0
+ end
+
+ assert('Time#utc', '15.2.19.7.27') do
+ Time.at(1300000000.0).utc
+ end
+
+ assert('Time#utc?', '15.2.19.7.28') do
+ Time.at(1300000000.0).utc.utc?
+ end
+
+ # ATM not implemented
+ # assert('Time#utc_offset', '15.2.19.7.29') do
+
+ assert('Time#wday', '15.2.19.7.30') do
+ Time.gm(2012, 12, 23).wday == 0
+ end
+
+ assert('Time#yday', '15.2.19.7.31') do
+ Time.gm(2012, 12, 23).yday == 358
+ end
+
+ assert('Time#year', '15.2.19.7.32') do
+ Time.gm(2012, 12, 23).year == 2012
+ end
+
+ assert('Time#zone', '15.2.19.7.33') do
+ Time.at(1300000000.0).utc.zone == 'UTC'
+ end
+
+ # Not ISO specified
+
+ assert('Time#to_s') do
+ Time.at(1300000000.0).utc.to_s == "Sun Mar 13 07:06:40 UTC 2011"
+ end
+
+ assert('Time#inspect') do
+ Time.at(1300000000.0).utc.inspect == "Sun Mar 13 07:06:40 UTC 2011"
+ end
+end
+