summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-io/test
diff options
context:
space:
mode:
authordearblue <[email protected]>2020-02-01 16:25:39 +0900
committerdearblue <[email protected]>2020-02-02 21:20:25 +0900
commit4c6d524c473ebb9174d0183dc1d1ac0530337314 (patch)
tree1aec9457c2862557440a3d718f459020334e0383 /mrbgems/mruby-io/test
parent3c67d9b1c0e4970db1d88fccdf7f26c781aa2c5f (diff)
downloadmruby-4c6d524c473ebb9174d0183dc1d1ac0530337314.tar.gz
mruby-4c6d524c473ebb9174d0183dc1d1ac0530337314.zip
Implement `IO#pread` and `IO#pwrite`
It is available by default in environments where `__unix__` is defined. Other environments are enabled by defining `MRB_WITH_IO_PREAD_PWRITE` (requires an implementation of `pread()` and `pwrite()` functions). In any case, you can disable it by defining `MRB_WITHOUT_IO_PREAD_PWRITE`.
Diffstat (limited to 'mrbgems/mruby-io/test')
-rw-r--r--mrbgems/mruby-io/test/io.rb28
-rw-r--r--mrbgems/mruby-io/test/mruby_io_test.c9
2 files changed, 37 insertions, 0 deletions
diff --git a/mrbgems/mruby-io/test/io.rb b/mrbgems/mruby-io/test/io.rb
index e3024cf9a..458d2cdc2 100644
--- a/mrbgems/mruby-io/test/io.rb
+++ b/mrbgems/mruby-io/test/io.rb
@@ -564,6 +564,34 @@ assert('IO#sysseek') do
end
end
+assert('IO#pread') do
+ skip "IO#pread is not implemented on this configuration" unless MRubyIOTestUtil::MRB_WITH_IO_PREAD_PWRITE
+
+ IO.open(IO.sysopen($mrbtest_io_rfname, 'r'), 'r') do |io|
+ assert_equal $mrbtest_io_msg.byteslice(5, 8), io.pread(8, 5)
+ assert_equal 0, io.pos
+ assert_equal $mrbtest_io_msg.byteslice(1, 5), io.pread(5, 1)
+ assert_equal 0, io.pos
+ assert_raise(RuntimeError) { io.pread(20, -9) }
+ end
+end
+
+assert('IO#pwrite') do
+ skip "IO#pwrite is not implemented on this configuration" unless MRubyIOTestUtil::MRB_WITH_IO_PREAD_PWRITE
+
+ IO.open(IO.sysopen($mrbtest_io_wfname, 'w+'), 'w+') do |io|
+ assert_equal 6, io.pwrite("Warld!", 7)
+ assert_equal 0, io.pos
+ assert_equal 7, io.pwrite("Hello, ", 0)
+ assert_equal 0, io.pos
+ assert_equal "Hello, Warld!", io.read
+ assert_equal 6, io.pwrite("world!", 7)
+ assert_equal 13, io.pos
+ io.pos = 0
+ assert_equal "Hello, world!", io.read
+ end
+end
+
assert('IO.pipe') do
begin
called = false
diff --git a/mrbgems/mruby-io/test/mruby_io_test.c b/mrbgems/mruby-io/test/mruby_io_test.c
index 7e272d45a..581472eaa 100644
--- a/mrbgems/mruby-io/test/mruby_io_test.c
+++ b/mrbgems/mruby-io/test/mruby_io_test.c
@@ -64,6 +64,7 @@ mkdtemp(char *temp)
#include "mruby/error.h"
#include "mruby/string.h"
#include "mruby/variable.h"
+#include <mruby/ext/io.h>
static mrb_value
mrb_io_test_io_setup(mrb_state *mrb, mrb_value self)
@@ -219,6 +220,12 @@ mrb_io_win_p(mrb_state *mrb, mrb_value klass)
#endif
}
+#ifdef MRB_WITH_IO_PREAD_PWRITE
+# define MRB_WITH_IO_PREAD_PWRITE_ENABLED TRUE
+#else
+# define MRB_WITH_IO_PREAD_PWRITE_ENABLED FALSE
+#endif
+
void
mrb_mruby_io_gem_test(mrb_state* mrb)
{
@@ -229,4 +236,6 @@ mrb_mruby_io_gem_test(mrb_state* mrb)
mrb_define_class_method(mrb, io_test, "mkdtemp", mrb_io_test_mkdtemp, MRB_ARGS_REQ(1));
mrb_define_class_method(mrb, io_test, "rmdir", mrb_io_test_rmdir, MRB_ARGS_REQ(1));
mrb_define_class_method(mrb, io_test, "win?", mrb_io_win_p, MRB_ARGS_NONE());
+
+ mrb_define_const(mrb, io_test, "MRB_WITH_IO_PREAD_PWRITE", mrb_bool_value(MRB_WITH_IO_PREAD_PWRITE_ENABLED));
}