[前][次][番号順一覧][スレッド一覧]

ruby-changes:54150

From: mrkn <ko1@a...>
Date: Thu, 13 Dec 2018 06:17:09 +0900 (JST)
Subject: [ruby-changes:54150] mrkn:r66371 (trunk): Add test cases of rb_arithmetic_sequence_extract

mrkn	2018-12-13 06:17:04 +0900 (Thu, 13 Dec 2018)

  New Revision: 66371

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66371

  Log:
    Add test cases of rb_arithmetic_sequence_extract

  Added directories:
    trunk/ext/-test-/arith_seq/
    trunk/ext/-test-/arith_seq/extract/
    trunk/test/-ext-/arith_seq/
  Added files:
    trunk/ext/-test-/arith_seq/extract/extconf.rb
    trunk/ext/-test-/arith_seq/extract/extract.c
    trunk/test/-ext-/arith_seq/test_arith_seq_extract.rb
Index: ext/-test-/arith_seq/extract/extconf.rb
===================================================================
--- ext/-test-/arith_seq/extract/extconf.rb	(nonexistent)
+++ ext/-test-/arith_seq/extract/extconf.rb	(revision 66371)
@@ -0,0 +1,2 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/arith_seq/extract/extconf.rb#L1
+# frozen_string_literal: false
+create_makefile("-test-/arith_seq/extract")
Index: ext/-test-/arith_seq/extract/extract.c
===================================================================
--- ext/-test-/arith_seq/extract/extract.c	(nonexistent)
+++ ext/-test-/arith_seq/extract/extract.c	(revision 66371)
@@ -0,0 +1,27 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/arith_seq/extract/extract.c#L1
+#include "ruby/ruby.h"
+
+static VALUE
+arith_seq_s_extract(VALUE mod, VALUE obj)
+{
+  rb_arithmetic_sequence_components_t x;
+  VALUE ret;
+  int f, r;
+
+  r = rb_arithmetic_sequence_extract(obj, &x);
+
+  ret = rb_ary_new2(5);
+  rb_ary_store(ret, 0, r ? x.begin : Qnil);
+  rb_ary_store(ret, 1, r ? x.end   : Qnil);
+  rb_ary_store(ret, 2, r ? x.step  : Qnil);
+  rb_ary_store(ret, 3, r ? INT2FIX(x.exclude_end) : Qnil);
+  rb_ary_store(ret, 4, INT2FIX(r));
+
+  return ret;
+}
+
+void
+Init_extract(void)
+{
+    VALUE cArithSeq = rb_path2class("Enumerator::ArithmeticSequence");
+    rb_define_singleton_method(cArithSeq, "__extract__", arith_seq_s_extract, 1);
+}
Index: test/-ext-/arith_seq/test_arith_seq_extract.rb
===================================================================
--- test/-ext-/arith_seq/test_arith_seq_extract.rb	(nonexistent)
+++ test/-ext-/arith_seq/test_arith_seq_extract.rb	(revision 66371)
@@ -0,0 +1,40 @@ https://github.com/ruby/ruby/blob/trunk/test/-ext-/arith_seq/test_arith_seq_extract.rb#L1
+# frozen_string_literal: false
+require 'test/unit'
+
+class Test_ArithSeq < Test::Unit::TestCase
+  def test_extract_with_arith_seq
+    assert_separately([], <<-"end;") #do
+      require '-test-/arith_seq/extract'
+
+      b, e, s, f, r = Enumerator::ArithmeticSequence.__extract__(1.step(10, 2))
+      assert_equal([1, 10, 2, 0, 1], [b, e, s, f, r])
+
+      b, e, s, f, r = Enumerator::ArithmeticSequence.__extract__((1..10) % 2)
+      assert_equal([1, 10, 2, 0, 1], [b, e, s, f, r])
+
+      b, e, s, f, r = Enumerator::ArithmeticSequence.__extract__((1...10) % 2)
+      assert_equal([1, 10, 2, 1, 1], [b, e, s, f, r])
+    end;
+  end
+
+  def test_extract_with_range
+    assert_separately([], <<-"end;") #do
+      require '-test-/arith_seq/extract'
+
+      b, e, s, f, r = Enumerator::ArithmeticSequence.__extract__(1..10)
+      assert_equal([1, 10, 1, 0, 1], [b, e, s, f, r])
+
+      b, e, s, f, r = Enumerator::ArithmeticSequence.__extract__(1...10)
+      assert_equal([1, 10, 1, 1, 1],  [b, e, s, f, r])
+    end;
+  end
+
+  def test_extract_with_others
+    assert_separately([], <<-"end;") #do
+      require '-test-/arith_seq/extract'
+
+      b, e, s, f, r = Enumerator::ArithmeticSequence.__extract__(nil)
+      assert_equal([nil, nil, nil, nil, 0], [b, e, s,  f, r])
+    end;
+  end
+end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]