summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2021-07-30 11:36:20 +0200
committerBenoit Daloze <eregontp@gmail.com>2021-07-30 11:36:20 +0200
commitff6c17602841bff57d3a489b8119c9643dbc7ebe (patch)
tree8a30881a31ffe29c18a02c8dc7c68af8da60cc65 /spec
parentfd96503f7bf0ef8262691eac190fe53344ce55fc (diff)
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.
Diffstat (limited to 'spec')
-rw-r--r--spec/ruby/optional/capi/ext/string_spec.c22
-rw-r--r--spec/ruby/optional/capi/fixtures/read.txt2
-rw-r--r--spec/ruby/optional/capi/string_spec.rb6
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 f2245be0ef..b7bfcf8c88 100644
--- a/spec/ruby/optional/capi/ext/string_spec.c
+++ b/spec/ruby/optional/capi/ext/string_spec.c
@@ -4,6 +4,7 @@
#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) {
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 f976cfd9b9..f7065a35d0 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 151dfefef7..c6ab1ca0fe 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
@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