summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorguy Decoux <ts@moulon.inra.fr>2009-02-28 20:16:31 +0100
committerguy Decoux <ts@moulon.inra.fr>2009-02-28 20:16:31 +0100
commita5a5f38eb3086869144728ec7d978d448b2f98aa (patch)
treeaad0d4a336e631f803114060a99df25fdfdcdcba /tests
parent008da6a1b8c7bd7712d226754198a0600c1441a5 (diff)
downloadmmap-ruby-a5a5f38eb3086869144728ec7d978d448b2f98aa.zip
mmap-ruby-a5a5f38eb3086869144728ec7d978d448b2f98aa.tar.gz
mmap-0.2.3
Diffstat (limited to 'tests')
-rw-r--r--tests/mmapt.rb167
1 files changed, 164 insertions, 3 deletions
diff --git a/tests/mmapt.rb b/tests/mmapt.rb
index b94358c..6b81505 100644
--- a/tests/mmapt.rb
+++ b/tests/mmapt.rb
@@ -8,14 +8,27 @@ $mmap, $str = nil, nil
Inh = defined?(RUNIT) ? RUNIT : Test::Unit
+Dir["tmp/*"].each do |f|
+ File.unlink(f) if FileTest.file?(f)
+end
+
class TestMmap < Inh::TestCase
- def internal_init
+
+ def internal_read
+ File.readlines("tmp/mmap", nil)[0]
+ end
+
+ def internal_init(io = false)
$mmap.unmap if $mmap
file = "mmap.c"
file = "../mmap.c" unless File.exist? file
File.syscopy file, "tmp/mmap"
- $str = File.readlines("tmp/mmap", nil)[0]
- assert_kind_of(Mmap, $mmap = Mmap.new("tmp/mmap", "rw"), "<open>")
+ $str = internal_read
+ if io
+ assert_kind_of(Mmap, $mmap = Mmap.new(File.new("tmp/mmap", "r+"), "rw"), "<open io>")
+ else
+ assert_kind_of(Mmap, $mmap = Mmap.new("tmp/mmap", "rw"), "<open>")
+ end
end
def test_00_init
@@ -74,6 +87,12 @@ class TestMmap < Inh::TestCase
end
def test_02_aset
+ $mmap[/...../] = "change it"
+ $str[/...../] = "change it"
+ assert_equal($mmap.to_str, $str, "aset regexp")
+ $mmap["ge i"] = "change it"
+ $str["ge i"] = "change it"
+ assert_equal($mmap.to_str, $str, "aset regexp")
max = $str.size * 2
72.times do
ran1 = rand(max)
@@ -193,6 +212,148 @@ class TestMmap < Inh::TestCase
assert_equal(mmap, str, "<each_byte>")
end
+ def test_07_concat
+ internal_init
+ [$mmap, $str].each {|l| l << "bc"; l << 12; l << "ab"}
+ assert_equal($mmap.to_str, $str, "<<")
+ assert_raises(TypeError) { $mmap << 456 }
+ end
+
+ def test_08_extend
+ $mmap.extend(4096)
+ assert_equal($mmap.to_str, $str, "extend")
+ if $str.respond_to?(:insert)
+ 10.times do
+ pos = rand($mmap.size)
+ str = "XX" * rand(66)
+ $str.insert(pos, str)
+ $mmap.insert(pos, str)
+ assert_equal($mmap.to_str, $str, "insert")
+ end
+ end
+ end
+
+ def test_09_msync
+ 3.times do |i|
+ [$mmap, $str].each {|l| l << "x" * 4096 }
+ str = internal_read
+ if str != $mmap.to_str
+ $mmap.msync
+ assert_equal($mmap.to_str, internal_read, "msync")
+ break
+ end
+ end
+ end
+
+ def test_10_protect
+ assert_equal($mmap, $mmap.protect("w"), "protect")
+ assert_equal("a", $mmap[12] = "a", "affect")
+ $str[12] = "a"
+ assert_equal($mmap.to_str, $str, "protect")
+ assert_raises(TypeError) { $mmap << "a" }
+ assert_equal($mmap, $mmap.protect("r"), "protect")
+ assert_raises(TypeError) { $mmap[12] = "a" }
+ assert_raises(TypeError) { $mmap.protect("rw") }
+ end
+
+ def test_11_anonymous
+ if defined?(Mmap::MAP_ANONYMOUS)
+ assert_kind_of(Mmap, $mmap =
+ Mmap.new(nil, "length" => 8192, "offset" => 12,
+ "increment" => 1024, "initialize" => " "))
+ $str = " " * 8192
+ 1024.times do
+ pos = rand(8192)
+ $mmap[pos] = $str[pos] = 32 + rand(64)
+ end
+ assert_equal($mmap.to_str, $str, "insert anonymous")
+ assert_raises(IndexError) { $mmap[12345] = "a" }
+ assert_raises(TypeError) { $mmap << "a" }
+ end
+ end
+
+ def test_12_fileno
+ internal_init(true)
+ test_01_aref
+ $mmap[12] = "3"; $str[12] = "3"
+ assert_equal($mmap.to_str, $str, "insert io")
+ assert_equal(0, $mmap <=> $str, "cmp")
+ assert_raises(TypeError) { $mmap[12] = "ab" }
+ $mmap.freeze
+ if $str.respond_to?(:match)
+ assert_equal($str.match("rb_match_busy").offset(0),
+ $mmap.match("rb_match_busy").offset(0), "match")
+ assert_equal($str.match(/rb_../).offset(0),
+ $mmap.match(/rb_../).offset(0), "match")
+ assert_equal($str.match("rb_match_buzy"),
+ $mmap.match("rb_match_buzy"), "no match")
+ assert_equal($str =~ /rb_match_busy/,
+ $mmap =~ /rb_match_busy/, "match")
+ assert_equal($str =~ /rb_match_buzy/,
+ $mmap =~ /rb_match_buzy/, "no match")
+ end
+ assert_raises(TypeError) { $mmap[12] = "a" }
+ end
+
+ def test_13_div
+ string = "azertyuiopqsdfghjklm"
+ assert_kind_of(Mmap, m0 = Mmap.new("tmp/aa", "a"), "new a")
+ File.open("tmp/bb", "w") {|f| f.puts "aaa" }
+ assert_kind_of(Mmap, m1 = Mmap.new("tmp/bb", "w"), "new a")
+ assert_equal(true, m0.empty?, "empty")
+ assert_equal(true, m1.empty?, "empty")
+ assert_equal(m0, m0 << string, "<<")
+ assert_equal(m1, m1 << string, "<<")
+ assert_equal(false, m0.empty?, "empty")
+ assert_equal(false, m1.empty?, "empty")
+ assert_equal(true, m0 == m1, "==")
+ if string.respond_to?(:casecmp)
+ assert_equal(0, m0.casecmp(string.upcase), "casecmp")
+ assert_equal(0, m0.casecmp(m1), "casecmp")
+ end
+ assert_equal(true, m0 === m1, "===")
+ assert_equal(false, m0 === string, "===")
+ assert_equal(true, m0.eql?(m1), ".eql?")
+ assert_equal(true, m1.eql?(m0), ".eql?")
+ assert_equal(false, m1.eql?(string), ".eql?")
+ assert_equal(m0.hash, m1.hash, "hash")
+ assert_equal(true, m0.include?("azert"), "include")
+ assert_equal(false, m1.include?("aqert"), "include")
+ i = 0
+ m0.scan(/./) {|c| assert_equal(c, string[i,1], "scan"); i += 1}
+ assert_nil(m0.munmap, "munmap")
+ assert_nil(m1.munmap, "munmap")
+ end
+
+ def test_14_other
+ if File.exist?("tmp/aa")
+ string = "azertyuiopqsdfghjklm"
+ assert_kind_of(Mmap, m0 = Mmap.new("tmp/aa", "r"), "new r")
+ assert_equal(string, m0.to_str, "content")
+ assert_raises(TypeError) { m0[0] = 12 }
+ assert_raises(TypeError) { m0 << 12 }
+ assert_nil(m0.munmap, "munmap")
+ if defined?(Mmap::MAP_ANONYMOUS)
+ assert_raises(ArgumentError) { Mmap.new(nil, "w") }
+ assert_kind_of(Mmap, m0 = Mmap.new(nil, 12), "new w")
+ assert_equal(false, m0.empty?, "empty")
+ assert_equal("a", m0[0] = "a", "set")
+ assert_raises(TypeError) { m0 << 12 }
+ if defined?(Mmap::MADV_DONTNEED)
+ assert_nil(m0.advise(Mmap::MADV_DONTNEED), "advise")
+ assert_equal("a", m0[0,1], "get")
+ end
+ assert_equal(m0, m0.sub!(/./) { "y" }, "sub")
+ assert_equal(m0, m0.gsub!(/./) { "x" }, "gsub")
+ assert_equal("x" * 12, m0.to_str, "retrieve")
+ assert_equal("ab", m0[1..2] = "ab", "range")
+ assert_raises(TypeError) { m0[1..2] = "abc" }
+ assert_raises(ArgumentError) { m0.lock }
+ assert_raises(ArgumentError) { Mmap::lockall(0) }
+ assert_nil(m0.munmap, "munmap")
+ end
+ end
+ end
end
if defined?(RUNIT)