summaryrefslogtreecommitdiffstats
path: root/tests/mmapt.rb
blob: b94358c501dde0c9914133eedd156f4076b8b13c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/ruby
$LOAD_PATH.unshift *%w{.. . tests}
require 'mmap'
require 'ftools'
require 'runit_'

$mmap, $str = nil, nil

Inh = defined?(RUNIT) ? RUNIT : Test::Unit

class TestMmap < Inh::TestCase
   def internal_init
      $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>")
   end

   def test_00_init
      internal_init
      assert_equal($mmap.length, $str.length, "<lenght>")
   end

   def test_01_aref
      max = $str.size * 2
      72.times do
	 ran1 = rand(max)
	 assert_equal($mmap[ran1], $str[ran1], "<aref>");
	 assert_equal($mmap[-ran1], $str[-ran1], "<aref>");
	 ran2 = rand(max)
	 assert_equal($mmap[ran1, ran2], $str[ran1, ran2], "<double aref>");
	 assert_equal($mmap[-ran1, ran2], $str[-ran1, ran2], "<double aref>");
	 assert_equal($mmap[ran1, -ran2], $str[ran1, -ran2], "<double aref>");
	 assert_equal($mmap[-ran1, -ran2], $str[-ran1, -ran2], "<double aref>");
	 assert_equal($mmap[ran1 .. ran2], $str[ran1 .. ran2], "<double aref>");
	 assert_equal($mmap[-ran1 .. ran2], $str[-ran1 .. ran2], "<double aref>");
	 assert_equal($mmap[ran1 .. -ran2], $str[ran1 .. -ran2], "<double aref>");
	 assert_equal($mmap[-ran1 .. -ran2], $str[-ran1 .. -ran2], "<double aref>");
      end
      assert_equal($mmap[/random/], $str[/random/], "<aref regexp>")
      assert_equal($mmap[/real/], $str[/real/], "<aref regexp>")
      assert_equal($mmap[/none/], $str[/none/], "<aref regexp>")
   end

   def internal_aset(a, b = nil, c = true)
      access = if b
		  repl = ''
		  rand(12).times do
	              repl << (65 + rand(25))
                  end
		  if c 
		     "[a, b] = '#{repl}'"
		  else
		     "[a .. b] = '#{repl}'"
		  end
	       else
		  "[a] = #{(65 + rand(25))}"
	       end
      begin
	 eval "$str#{access}"
      rescue IndexError, RangeError
	 begin
	    eval "$mmap#{access}"
	 rescue IndexError, RangeError
	 else
	    flunk("*must* fail with IndexError")
	 end
      else
	 eval "$mmap#{access}"
      end
      assert_equal($mmap.to_str, $str, "<internal aset>")
   end

   def test_02_aset
      max = $str.size * 2
      72.times do
	 ran1 = rand(max)
	 internal_aset(ran1)
	 internal_aset(-ran1)
	 ran2 = rand(max)
	 internal_aset(ran1, ran2)
	 internal_aset(ran1, -ran2)
	 internal_aset(-ran1, ran2)
	 internal_aset(-ran1, -ran2)
	 internal_aset(ran1, ran2, false)
	 internal_aset(ran1, -ran2, false)
	 internal_aset(-ran1, ran2, false)
	 internal_aset(-ran1, -ran2, false)
      end
      internal_init
   end

   def internal_slice(a, b = nil, c = true)
      access = if b
		  if c 
		     ".slice!(a, b)"
		  else
		     ".slice!(a .. b)"
		  end
	       else
		  ".slice!(a)"
	       end
      begin
	 eval "$str#{access}"
      rescue IndexError, RangeError
	 begin
	    eval "$mmap#{access}"
	 rescue IndexError, RangeError
	 else
	    flunk("*must* fail with IndexError")
	 end
      else
	 eval "$mmap#{access}"
      end
      assert_equal($mmap.to_str, $str, "<internal aset>")
   end

   def test_03_slice
      max = $str.size * 2
      72.times do
	 ran1 = rand(max)
	 internal_slice(ran1)
	 internal_slice(-ran1)
	 ran2 = rand(max)
	 internal_slice(ran1, ran2)
	 internal_slice(ran1, -ran2)
	 internal_slice(-ran1, ran2)
	 internal_slice(-ran1, -ran2)
	 internal_slice(ran1, ran2, false)
	 internal_slice(ran1, -ran2, false)
	 internal_slice(-ran1, ran2, false)
	 internal_slice(-ran1, -ran2, false)
      end
      internal_init
   end

   def test_04_reg
      assert_equal($mmap.scan(/include/), $str.scan(/include/), "<scan>")
      assert_equal($mmap.index("rb_raise"), $str.index("rb_raise"), "<index>")
      assert_equal($mmap.rindex("rb_raise"), $str.rindex("rb_raise"), "<rindex>")
      assert_equal($mmap.index(/rb_raise/), $str.index(/rb_raise/), "<index>")
      assert_equal($mmap.rindex(/rb_raise/), $str.rindex(/rb_raise/), "<rindex>")
      ('a' .. 'z').each do |i|
	 assert_equal($mmap.index(i), $str.index(i), "<index>")
	 assert_equal($mmap.rindex(i), $str.rindex(i), "<rindex>")
	 assert_equal($mmap.index(i), $str.index(/#{i}/), "<index>")
	 assert_equal($mmap.rindex(i), $str.rindex(/#{i}/), "<rindex>")
      end
      $mmap.sub!(/GetMmap/, 'XXXX'); $str.sub!(/GetMmap/, 'XXXX')
      assert_equal($mmap.to_str, $str, "<after sub!>")
      $mmap.gsub!(/GetMmap/, 'XXXX'); $str.gsub!(/GetMmap/, 'XXXX')
      assert_equal($mmap.to_str, $str, "<after gsub!>")
      $mmap.gsub!(/YYYY/, 'XXXX'); $str.gsub!(/YYYY/, 'XXXX')
      assert_equal($mmap.to_str, $str, "<after gsub!>")
      assert_equal($mmap.split(/\w+/), $str.split(/\w+/), "<split>")
      assert_equal($mmap.split(/\W+/), $str.split(/\W+/), "<split>")
      assert_equal($mmap.crypt("abc"), $str.crypt("abc"), "<crypt>")
      internal_init
   end

   def internal_modify idmod, *args
      if res = $str.method(idmod)[*args]
	 assert_equal($mmap.method(idmod)[*args].to_str, res, "<#{idmod}>")
      else
	 assert_equal($mmap.method(idmod)[*args], res, "<#{idmod}>")
      end
   end

   def test_05_modify
      internal_modify(:reverse!)
      internal_modify(:upcase!)
      internal_modify(:downcase!)
      internal_modify(:capitalize!)
      internal_modify(:swapcase!)
      internal_modify(:strip!)
      internal_modify(:chop!)
      internal_modify(:chomp!)
      internal_modify(:squeeze!)
      internal_modify(:tr!, 'abcdefghi', '123456789')
      internal_modify(:tr_s!, 'jklmnopqr', '123456789')
      internal_modify(:delete!, 'A-Z')
   end

   def test_06_iterate
      internal_init
      mmap = []; $mmap.each {|l| mmap << l}
      str = []; $str.each {|l| str << l}
      assert_equal(mmap, str, "<each>")
      mmap = []; $mmap.each_byte {|l| mmap << l}
      str = []; $str.each_byte {|l| str << l}
      assert_equal(mmap, str, "<each_byte>")
   end

end

if defined?(RUNIT)
   RUNIT::CUI::TestRunner.run(TestMmap.suite)
end