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

ruby-changes:67004

From: Benoit <ko1@a...>
Date: Fri, 30 Jul 2021 18:38:34 +0900 (JST)
Subject: [ruby-changes:67004] ff6c176028 (master): Tweak rb_str_modify_expand() + read() spec to try to find out why it fails on some platforms

https://git.ruby-lang.org/ruby.git/commit/?id=ff6c176028

From ff6c17602841bff57d3a489b8119c9643dbc7ebe Mon Sep 17 00:00:00 2001
From: Benoit Daloze <eregontp@g...>
Date: Fri, 30 Jul 2021 11:36:20 +0200
Subject: Tweak rb_str_modify_expand() + read() spec to try to find out why it
 fails on some platforms

* Use a longer string as <= 23 characters it's embedded on CRuby and
  the value of rb_str_capacity() is implementation-specific.
---
 spec/ruby/optional/capi/ext/string_spec.c | 22 ++++++++++++++++------
 spec/ruby/optional/capi/fixtures/read.txt |  2 +-
 spec/ruby/optional/capi/string_spec.rb    |  6 ++++--
 3 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c
index f2245be..b7bfcf8 100644
--- a/spec/ruby/optional/capi/ext/string_spec.c
+++ b/spec/ruby/optional/capi/ext/string_spec.c
@@ -4,6 +4,7 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/optional/capi/ext/string_spec.c#L4
 #include <fcntl.h>
 #include <string.h>
 #include <stdarg.h>
+#include <errno.h>
 
 #include "ruby/encoding.h"
 
@@ -388,15 +389,24 @@ VALUE string_spec_RSTRING_PTR_after_yield(VALUE self, VALUE str) { https://github.com/ruby/ruby/blob/trunk/spec/ruby/optional/capi/ext/string_spec.c#L389
 VALUE string_spec_RSTRING_PTR_read(VALUE self, VALUE str, VALUE path) {
   char *cpath = StringValueCStr(path);
   int fd = open(cpath, O_RDONLY);
-  rb_str_modify_expand(str, 10);
+  VALUE capacities = rb_ary_new();
+  if (fd < 0) {
+    rb_syserr_fail(errno, "open");
+  }
+
+  rb_str_modify_expand(str, 30);
+  rb_ary_push(capacities, SIZET2NUM(rb_str_capacity(str)));
   char *buffer = RSTRING_PTR(str);
-  read(fd, buffer, 10);
-  rb_str_modify_expand(str, 21);
+  read(fd, buffer, 30);
+
+  rb_str_modify_expand(str, 53);
+  rb_ary_push(capacities, SIZET2NUM(rb_str_capacity(str)));
   char *buffer2 = RSTRING_PTR(str);
-  read(fd, buffer2 + 10, 11);
-  rb_str_set_len(str, 21);
+  read(fd, buffer2 + 30, 53 - 30);
+
+  rb_str_set_len(str, 53);
   close(fd);
-  return str;
+  return capacities;
 }
 
 VALUE string_spec_StringValue(VALUE self, VALUE str) {
diff --git a/spec/ruby/optional/capi/fixtures/read.txt b/spec/ruby/optional/capi/fixtures/read.txt
index f976cfd..f7065a3 100644
--- a/spec/ruby/optional/capi/fixtures/read.txt
+++ b/spec/ruby/optional/capi/fixtures/read.txt
@@ -1 +1 @@
-fixture file contents
+fixture file contents to test read() with RSTRING_PTR
diff --git a/spec/ruby/optional/capi/string_spec.rb b/spec/ruby/optional/capi/string_spec.rb
index 151dfef..c6ab1ca 100644
--- a/spec/ruby/optional/capi/string_spec.rb
+++ b/spec/ruby/optional/capi/string_spec.rb
@@ -599,10 +599,12 @@ describe "C-API String function" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/optional/capi/string_spec.rb#L599
       @s.RSTRING_PTR_short_memcpy(str).should == "Infinity"
     end
 
-    it "allows read to update string contents" do
+    it "allows read() to update the string contents" do
       filename = fixture(__FILE__, "read.txt")
       str = ""
-      @s.RSTRING_PTR_read(str, filename).should == "fixture file contents"
+      capacities = @s.RSTRING_PTR_read(str, filename)
+      capacities.should == [30, 53]
+      str.should == "fixture file contents to test read() with RSTRING_PTR"
     end
   end
 
-- 
cgit v1.1


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

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