external 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/History +5 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README +168 -0
  4. data/lib/ext_arc.rb +108 -0
  5. data/lib/ext_arr.rb +727 -0
  6. data/lib/ext_ind.rb +1120 -0
  7. data/lib/external/base.rb +85 -0
  8. data/lib/external/chunkable.rb +105 -0
  9. data/lib/external/enumerable.rb +137 -0
  10. data/lib/external/io.rb +398 -0
  11. data/lib/external.rb +3 -0
  12. data/test/benchmarks/benchmarks_20070918.txt +45 -0
  13. data/test/benchmarks/benchmarks_20070921.txt +91 -0
  14. data/test/benchmarks/benchmarks_20071006.txt +147 -0
  15. data/test/benchmarks/test_copy_file.rb +80 -0
  16. data/test/benchmarks/test_pos_speed.rb +47 -0
  17. data/test/benchmarks/test_read_time.rb +55 -0
  18. data/test/cached_ext_ind_test.rb +219 -0
  19. data/test/check/benchmark_check.rb +441 -0
  20. data/test/check/namespace_conflicts_check.rb +23 -0
  21. data/test/check/pack_check.rb +90 -0
  22. data/test/ext_arc_test.rb +286 -0
  23. data/test/ext_arr/alt_sep.txt +3 -0
  24. data/test/ext_arr/cr_lf_input.txt +3 -0
  25. data/test/ext_arr/input.index +0 -0
  26. data/test/ext_arr/input.txt +1 -0
  27. data/test/ext_arr/inputb.index +0 -0
  28. data/test/ext_arr/inputb.txt +1 -0
  29. data/test/ext_arr/lf_input.txt +3 -0
  30. data/test/ext_arr/lines.txt +19 -0
  31. data/test/ext_arr/without_index.txt +1 -0
  32. data/test/ext_arr_test.rb +534 -0
  33. data/test/ext_ind_test.rb +1472 -0
  34. data/test/external/base_test.rb +74 -0
  35. data/test/external/chunkable_test.rb +182 -0
  36. data/test/external/index/input.index +0 -0
  37. data/test/external/index/inputb.index +0 -0
  38. data/test/external/io_test.rb +414 -0
  39. data/test/external_test_helper.rb +31 -0
  40. data/test/external_test_suite.rb +4 -0
  41. data/test/test_array.rb +1192 -0
  42. metadata +104 -0
@@ -0,0 +1,80 @@
1
+ require File.join(File.dirname(__FILE__), '../external_test_helper.rb')
2
+ require 'tempfile'
3
+ require 'fileutils'
4
+
5
+ ENV['ALL'] = 'true'
6
+
7
+ class TestCopyFile < Test::Unit::TestCase
8
+ include Benchmark
9
+ include Tap::Test::SubsetMethods
10
+
11
+
12
+ # The lesson from this test is that chunky copy can be
13
+ # faster than FileUtils.cp It's interesting to note
14
+ # that if you look up the code, you can see that a
15
+ # chunky copy is exactly what cp is... with chunks of
16
+ # 1024 bytes.
17
+ def test_copy_vs_chunky_rewrite
18
+ t = Tempfile.new "large_file"
19
+ t.close
20
+ puts t.path
21
+
22
+ prompt_test(:path_to_large_file) do |config|
23
+ path = config[:path_to_large_file]
24
+
25
+ benchmark_test do |x|
26
+ x.report("copy") { FileUtils.cp(path, t.path) }
27
+ assert FileUtils.cmp(path, t.path)
28
+
29
+ # tenmb = 1 * 2097152
30
+ # x.report("chunk 2MB") do
31
+ # File.open(path, "r") do |src|
32
+ # File.open(t.path, "w") do |target|
33
+ # str = ""
34
+ # while src.read(tenmb, str)
35
+ # target << str
36
+ # end
37
+ # end
38
+ # end
39
+ # end
40
+
41
+ tenmb = 4 * 2097152
42
+ x.report("chunk 8MB") do
43
+ File.open(path, "r") do |src|
44
+ File.open(t.path, "w") do |target|
45
+ str = ""
46
+ while src.read(tenmb, str)
47
+ target << str
48
+ end
49
+ end
50
+ end
51
+ end
52
+ assert FileUtils.cmp(path, t.path)
53
+
54
+ # tenmb = 4 * 2097152
55
+ # x.report("chunk 8MB - no buf") do
56
+ # File.open(path, "r") do |src|
57
+ # File.open(t.path, "w") do |target|
58
+ # while str = src.read(tenmb)
59
+ # target << str
60
+ # end
61
+ # end
62
+ # end
63
+ # end
64
+
65
+ # tenmb = 10 * 2097152
66
+ # x.report("chunk 20MB") do
67
+ # File.open(path, "r") do |src|
68
+ # File.open(t.path, "w") do |target|
69
+ # str = ""
70
+ # while src.read(tenmb, str)
71
+ # target << str
72
+ # end
73
+ # end
74
+ # end
75
+ # end
76
+
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,47 @@
1
+ require File.join(File.dirname(__FILE__), '../external_test_helper.rb')
2
+ require 'tempfile'
3
+ require 'enumerator'
4
+ require 'scanf'
5
+
6
+ class TestPosSpeed < Test::Unit::TestCase
7
+ include Benchmark
8
+
9
+ attr_reader :path
10
+
11
+ def setup
12
+ t = Tempfile.new('integers')
13
+ ints = [*(1..100000).to_a]
14
+ t << ints.pack("I*")
15
+ t.close
16
+ @path = t.path
17
+ end
18
+
19
+ def test_pos_speed
20
+ file = File.open(path, "r")
21
+ length = file.stat.size
22
+ positions = Array.new(1000) do
23
+ p = rand(length)
24
+ end
25
+
26
+ benchmark_test(20) do |x|
27
+ x.report("100kx pos=") do
28
+ (1000).times do
29
+ positions.each do |p|
30
+ file.pos = p
31
+ file.pos = p
32
+ end
33
+ end
34
+ end
35
+
36
+ x.report("100kx pos= with check") do
37
+ (1000).times do
38
+ positions.each do |p|
39
+ file.pos = p
40
+ file.pos = p unless file.pos = p
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,55 @@
1
+ require File.join(File.dirname(__FILE__), '../external_test_helper.rb')
2
+ require 'tempfile'
3
+ require 'enumerator'
4
+ require 'scanf'
5
+
6
+ class TestReadTime < Test::Unit::TestCase
7
+ include Benchmark
8
+
9
+ attr_reader :path, :ints
10
+
11
+ def setup
12
+ t = Tempfile.new('integers')
13
+ ints = [*(1..100000).to_a]
14
+ t << ints.pack("I*")
15
+ t.close
16
+ @path = t.path
17
+
18
+ @ints = []
19
+ ints.each_slice(1) do |s|
20
+ @ints << s
21
+ end
22
+ end
23
+
24
+ def test_read_time
25
+ file = File.open(path, "r")
26
+ results = nil
27
+
28
+ benchmark_test(20) do |x|
29
+ x.report("10x full (1)") do
30
+ (10).times do
31
+ file.pos = 0
32
+ str = file.read
33
+ results = []
34
+ str.unpack("I*").each_slice(1) do |s|
35
+ results << s
36
+ end
37
+ end
38
+ end
39
+ assert_equal ints, results
40
+
41
+ x.report("10x singles (1)") do
42
+ (10).times do
43
+ file.pos = 0
44
+
45
+ results = []
46
+ ints.length.times do
47
+ results << file.read(4).unpack("I")
48
+ end
49
+ end
50
+ end
51
+ assert_equal ints, results
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,219 @@
1
+ require File.join(File.dirname(__FILE__), 'ext_ind_test.rb')
2
+
3
+ class CachedExtIndTest < ExtIndTest
4
+ class CachedExtInd < ExtInd
5
+ def initialize(io=nil, options={})
6
+ options = {
7
+ :cached => true
8
+ }.merge(options)
9
+
10
+ super(io, options)
11
+ end
12
+ end
13
+
14
+ def setup
15
+ # cls represents an array
16
+ @cls = CachedExtInd
17
+
18
+ @index = CachedExtInd.new
19
+ @index.cache.concat(framed_array)
20
+ end
21
+
22
+ def teardown
23
+ end
24
+
25
+ #
26
+ # setup tests
27
+ #
28
+
29
+ def test_setup
30
+ assert_equal CachedExtInd, @cls
31
+
32
+ assert_equal 0, index.pos
33
+ assert_equal 5, index.length
34
+ assert_equal 4, index.frame_size
35
+ assert index.cached?
36
+ assert_equal framed_array, index.cache
37
+ end
38
+
39
+
40
+ #
41
+ # initialize tests
42
+ #
43
+
44
+ undef_method :test_index_initialized_to_single_int_format_by_default
45
+
46
+ def test_cache_initialized_to_empty_array_in_cached_mode
47
+ index = @cls.new
48
+
49
+ assert_equal 'I*', index.format
50
+ assert_equal 1, index.frame
51
+ assert_equal 4, index.frame_size
52
+ assert_equal [0], index.nil_value
53
+ assert index.cached?
54
+ assert_equal [], index.cache
55
+ end
56
+
57
+ def test_cache_initialized_to_empty_array_in_cached_mode_regardless_of_format
58
+ index = @cls.new nil, :format => "IQS"
59
+
60
+ assert index.cached?
61
+ assert_equal [], index.cache
62
+ end
63
+
64
+ #
65
+ # class read tests
66
+ #
67
+
68
+ undef_method :test_read_returns_the_index_file_in_frame
69
+
70
+ #
71
+ # class directive size tests
72
+ #
73
+
74
+ undef_method :test_directive_size_returns_the_number_of_bytes_to_pack_a_directive
75
+
76
+ #
77
+ # length test
78
+ #
79
+
80
+ undef_method :test_length_returns_io_length_divided_by_frame_size
81
+
82
+ def test_length_returns_cache_length
83
+ assert_equal index.cache.length, index.length
84
+
85
+ index.cache.clear
86
+ assert_equal 0, index.length
87
+
88
+ index.cache.concat([[1],[2],[3]])
89
+ assert_equal 3, index.length
90
+ end
91
+
92
+ def test_length_is_separate_from_io_length
93
+ index.cache.clear
94
+ assert_equal 0, index.length
95
+
96
+ index.io.length = 10
97
+ assert_equal 0, index.length
98
+ end
99
+
100
+ #
101
+ # pos test
102
+ #
103
+
104
+ undef_method :test_pos_returns_io_pos_divided_by_frame_size
105
+
106
+ def test_pos_is_kept_separate_from_io_pos_in_cached_mode
107
+ assert_equal 0, index.io.pos
108
+ assert_equal 0, index.pos
109
+
110
+ index.io.pos = 4
111
+ assert_equal 4, index.io.pos
112
+ assert_equal 0, index.pos
113
+ end
114
+
115
+ #
116
+ # pos= test
117
+ #
118
+
119
+ undef_method :test_pos_set_sets_io_pos_to_index_value_of_input_times_frame_size
120
+
121
+ def test_pos_set_sets_pos_to_index_value_of_input
122
+ index.pos = 1
123
+ assert_equal 1, index.pos
124
+
125
+ index.pos = -1
126
+ assert_equal 4, index.pos
127
+ end
128
+
129
+ #
130
+ # readbytes test
131
+ #
132
+
133
+ undef_method :test_readbytes_behavior_is_like_io_behavior
134
+
135
+ #
136
+ # unframed_write tests
137
+ #
138
+
139
+ undef_method :test_unframed_write_unframed_writes_packed_array_to_io_at_pos_and_adjusts_io_length
140
+
141
+ def test_unframed_write_unframed_writes_packed_array_to_cache_in_frame_and_does_not_affect_io
142
+ index = @cls.new
143
+ index.unframed_write([1,2,3])
144
+ assert_equal 0, index.io.length
145
+ assert_equal 0, index.io.pos
146
+
147
+ assert_equal [[1],[2],[3]], index.cache
148
+
149
+ index.unframed_write([-2], 1)
150
+ assert_equal 0, index.io.length
151
+ assert_equal 0, index.io.pos
152
+
153
+ assert_equal [[1],[-2],[3]], index.cache
154
+ end
155
+
156
+ def test_unframed_write_pads_with_nil_value_if_position_is_past_length
157
+ index = @cls.new nil, :nil_value => [8]
158
+ assert_equal 0, index.length
159
+
160
+ index.unframed_write([1,2,3], 2)
161
+ assert_equal [[8],[8],[1],[2],[3]], index.cache
162
+ end
163
+
164
+ def test_unframed_write_unframed_writes_nothing_with_empty_array
165
+ assert_equal framed_array, index.cache
166
+
167
+ index.unframed_write([])
168
+ assert_equal framed_array, index.cache
169
+
170
+ index.unframed_write([], 0)
171
+ assert_equal framed_array, index.cache
172
+ end
173
+
174
+ #
175
+ # mixed formats test
176
+ #
177
+
178
+ def test_read_handles_mixed_formats
179
+ index = @cls.new nil, :format => "IQS"
180
+
181
+ index.cache << [1,2,3]
182
+ index.cache << [4,5,6]
183
+ index.cache << [7,8,9]
184
+
185
+ assert_equal [[1,2,3],[4,5,6],[7,8,9]], index.read
186
+
187
+ index.pos = 1
188
+ assert_equal [4,5,6], index.read(1)
189
+ end
190
+
191
+ def test_unframed_write_handles_mixed_formats
192
+ index = @cls.new nil, :format => "IQS"
193
+ a = [1,2,3]
194
+ b = [4,5,6]
195
+ c = [7,8,9]
196
+ d = [-4,-5,-6]
197
+
198
+ index.unframed_write([1,2,3,4,5,6,7,8,9])
199
+ assert_equal [a,b,c], index.cache
200
+
201
+ index.pos = 1
202
+ index.unframed_write([-4,-5,-6])
203
+ assert_equal [a,d,c], index.cache
204
+ end
205
+
206
+ #
207
+ # numeric format range tests
208
+ #
209
+
210
+ undef_method :test_read_and_unframed_write_handles_full_numeric_range_for_numeric_formats
211
+ undef_method :test_read_and_unframed_write_cycle_numerics_beyond_natural_range
212
+ undef_method :test_numerics_cycle_up_to_the_unsigned_max_in_either_sign
213
+
214
+ def test_unframed_write_does_NOT_check_formatting_of_input_in_cached_mode
215
+ index = @cls.new nil, :format => "S"
216
+ assert_raise(RangeError) { [ExtIndTest::ULLONG_MAX].pack("S") }
217
+ assert_nothing_raised { index.unframed_write([ExtIndTest::ULLONG_MAX]) }
218
+ end
219
+ end