snappy 0.0.14-java → 0.2.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/main.yml +34 -0
- data/.github/workflows/publish.yml +34 -0
- data/Gemfile +4 -0
- data/README.md +28 -4
- data/Rakefile +32 -29
- data/ext/api.c +6 -1
- data/ext/extconf.rb +21 -24
- data/lib/snappy.rb +6 -4
- data/lib/snappy/hadoop.rb +22 -0
- data/lib/snappy/hadoop/reader.rb +62 -0
- data/lib/snappy/hadoop/writer.rb +51 -0
- data/lib/snappy/reader.rb +19 -11
- data/lib/snappy/shim.rb +30 -0
- data/lib/snappy/version.rb +3 -1
- data/lib/snappy/writer.rb +8 -9
- data/snappy.gemspec +17 -37
- data/test/hadoop/snappy_hadoop_reader_test.rb +115 -0
- data/test/hadoop/snappy_hadoop_writer_test.rb +48 -0
- data/test/snappy_hadoop_test.rb +26 -0
- data/test/snappy_reader_test.rb +148 -0
- data/test/snappy_test.rb +95 -0
- data/test/snappy_writer_test.rb +55 -0
- data/test/test_helper.rb +7 -0
- data/vendor/snappy/CMakeLists.txt +297 -0
- data/vendor/snappy/CONTRIBUTING.md +26 -0
- data/vendor/snappy/COPYING +1 -1
- data/vendor/snappy/NEWS +60 -0
- data/vendor/snappy/{README → README.md} +29 -16
- data/vendor/snappy/cmake/SnappyConfig.cmake.in +33 -0
- data/vendor/snappy/cmake/config.h.in +62 -0
- data/vendor/snappy/docs/README.md +72 -0
- data/vendor/snappy/snappy-c.h +3 -3
- data/vendor/snappy/snappy-internal.h +113 -32
- data/vendor/snappy/snappy-sinksource.cc +33 -0
- data/vendor/snappy/snappy-sinksource.h +51 -6
- data/vendor/snappy/snappy-stubs-internal.cc +1 -1
- data/vendor/snappy/snappy-stubs-internal.h +160 -45
- data/vendor/snappy/snappy-stubs-public.h.in +23 -47
- data/vendor/snappy/snappy-test.cc +31 -24
- data/vendor/snappy/snappy-test.h +46 -103
- data/vendor/snappy/snappy.cc +786 -431
- data/vendor/snappy/snappy.h +37 -14
- data/vendor/snappy/snappy_compress_fuzzer.cc +59 -0
- data/vendor/snappy/snappy_uncompress_fuzzer.cc +57 -0
- data/vendor/snappy/snappy_unittest.cc +441 -290
- metadata +35 -75
- data/.travis.yml +0 -4
- data/test/test-snappy-reader.rb +0 -129
- data/test/test-snappy-writer.rb +0 -55
- data/test/test-snappy.rb +0 -58
- data/vendor/snappy/ChangeLog +0 -1916
- data/vendor/snappy/Makefile.am +0 -23
- data/vendor/snappy/autogen.sh +0 -7
- data/vendor/snappy/configure.ac +0 -133
- data/vendor/snappy/m4/gtest.m4 +0 -74
- data/vendor/snappy/testdata/alice29.txt +0 -3609
- data/vendor/snappy/testdata/asyoulik.txt +0 -4122
- data/vendor/snappy/testdata/baddata1.snappy +0 -0
- data/vendor/snappy/testdata/baddata2.snappy +0 -0
- data/vendor/snappy/testdata/baddata3.snappy +0 -0
- data/vendor/snappy/testdata/fireworks.jpeg +0 -0
- data/vendor/snappy/testdata/geo.protodata +0 -0
- data/vendor/snappy/testdata/html +0 -1
- data/vendor/snappy/testdata/html_x_4 +0 -1
- data/vendor/snappy/testdata/kppkn.gtb +0 -0
- data/vendor/snappy/testdata/lcet10.txt +0 -7519
- data/vendor/snappy/testdata/paper-100k.pdf +2 -600
- data/vendor/snappy/testdata/plrabn12.txt +0 -10699
- data/vendor/snappy/testdata/urls.10K +0 -10000
data/lib/snappy/reader.rb
CHANGED
@@ -1,41 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "snappy/shim"
|
4
|
+
require "snappy/writer"
|
5
|
+
|
1
6
|
module Snappy
|
2
7
|
class Reader
|
3
8
|
attr_reader :io, :magic, :default_version, :minimum_compatible_version
|
4
9
|
|
5
10
|
def initialize(io)
|
6
|
-
@io = io
|
7
|
-
@io.set_encoding Encoding::ASCII_8BIT unless RUBY_VERSION =~ /^1\.8/
|
11
|
+
@io = Snappy.set_encoding io
|
8
12
|
read_header!
|
9
13
|
yield self if block_given?
|
10
14
|
end
|
11
15
|
|
12
16
|
def each
|
17
|
+
return to_enum unless block_given?
|
18
|
+
|
13
19
|
until @io.eof?
|
14
20
|
if @chunked
|
15
|
-
size = @io.read(4).
|
16
|
-
yield Snappy.inflate(@io.read(size))
|
21
|
+
size = @io.read(4).unpack1("N")
|
22
|
+
yield Snappy.inflate(@io.read(size))
|
17
23
|
else
|
18
|
-
yield Snappy.inflate
|
24
|
+
yield Snappy.inflate(@io.read)
|
19
25
|
end
|
20
26
|
end
|
21
27
|
end
|
22
28
|
|
23
29
|
def read
|
24
|
-
|
30
|
+
buff = StringIO.new
|
25
31
|
each do |chunk|
|
26
|
-
|
32
|
+
buff << chunk
|
27
33
|
end
|
28
|
-
|
34
|
+
buff.string
|
29
35
|
end
|
30
36
|
|
31
|
-
def each_line(sep_string
|
37
|
+
def each_line(sep_string = $/)
|
38
|
+
return to_enum(:each_line, sep_string) unless block_given?
|
39
|
+
|
32
40
|
last = ""
|
33
41
|
each do |chunk|
|
34
42
|
chunk = last + chunk
|
35
43
|
lines = chunk.split(sep_string)
|
36
44
|
last = lines.pop
|
37
45
|
lines.each do |line|
|
38
|
-
yield line
|
46
|
+
yield line
|
39
47
|
end
|
40
48
|
end
|
41
49
|
yield last
|
@@ -46,7 +54,7 @@ module Snappy
|
|
46
54
|
def read_header!
|
47
55
|
header = @io.read Snappy::Writer::MAGIC.length
|
48
56
|
if header.length == Snappy::Writer::MAGIC.length && header == Snappy::Writer::MAGIC
|
49
|
-
@magic, @default_version, @minimum_compatible_version = header, *@io.read(8).unpack(
|
57
|
+
@magic, @default_version, @minimum_compatible_version = header, *@io.read(8).unpack("NN")
|
50
58
|
@chunked = true
|
51
59
|
else
|
52
60
|
@io.rewind
|
data/lib/snappy/shim.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Snappy
|
4
|
+
module_function
|
5
|
+
|
6
|
+
if RUBY_VERSION[0..2] == "1.8"
|
7
|
+
def set_encoding(io)
|
8
|
+
io
|
9
|
+
end
|
10
|
+
|
11
|
+
def b(str)
|
12
|
+
str
|
13
|
+
end
|
14
|
+
else
|
15
|
+
def set_encoding(io)
|
16
|
+
io.set_encoding Encoding::ASCII_8BIT
|
17
|
+
io
|
18
|
+
end
|
19
|
+
|
20
|
+
if ::String.instance_methods.include? :b
|
21
|
+
def b(str)
|
22
|
+
str.b
|
23
|
+
end
|
24
|
+
else
|
25
|
+
def b(str)
|
26
|
+
str.force_encoding Encoding::ASCII_8BIT
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/snappy/version.rb
CHANGED
data/lib/snappy/writer.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'snappy/shim'
|
4
|
+
|
1
5
|
module Snappy
|
2
6
|
class Writer
|
3
|
-
|
4
|
-
MAGIC = "\x82SNAPPY\x0"
|
5
|
-
else
|
6
|
-
MAGIC = "\x82SNAPPY\x0".force_encoding Encoding::ASCII_8BIT
|
7
|
-
end
|
7
|
+
MAGIC = Snappy.b("\x82SNAPPY\x0")
|
8
8
|
DEFAULT_VERSION = 1
|
9
9
|
MINIMUM_COMPATIBLE_VERSION = 1
|
10
10
|
DEFAULT_BLOCK_SIZE = 32 * 1024
|
@@ -13,9 +13,8 @@ module Snappy
|
|
13
13
|
|
14
14
|
def initialize(io, block_size = DEFAULT_BLOCK_SIZE)
|
15
15
|
@block_size = block_size
|
16
|
-
@buffer =
|
17
|
-
@io = io
|
18
|
-
@io.set_encoding Encoding::ASCII_8BIT unless RUBY_VERSION =~ /^1\.8/
|
16
|
+
@buffer = String.new
|
17
|
+
@io = Snappy.set_encoding io
|
19
18
|
write_header!
|
20
19
|
if block_given?
|
21
20
|
yield self
|
@@ -34,7 +33,7 @@ module Snappy
|
|
34
33
|
compressed = Snappy.deflate(@buffer)
|
35
34
|
@io << [compressed.size, compressed].pack("Na#{compressed.size}")
|
36
35
|
@io.flush
|
37
|
-
@buffer
|
36
|
+
@buffer.clear
|
38
37
|
end
|
39
38
|
|
40
39
|
alias_method :flush, :dump!
|
data/snappy.gemspec
CHANGED
@@ -1,56 +1,36 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
5
|
+
require "snappy/version"
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
7
8
|
spec.name = "snappy"
|
8
9
|
spec.version = Snappy::VERSION
|
9
10
|
spec.authors = ["miyucy"]
|
10
11
|
spec.email = ["fistfvck@gmail.com"]
|
11
|
-
spec.description =
|
12
|
-
spec.summary =
|
12
|
+
spec.description = "libsnappy binding for Ruby"
|
13
|
+
spec.summary = "libsnappy binding for Ruby"
|
13
14
|
spec.homepage = "http://github.com/miyucy/snappy"
|
14
15
|
spec.license = "MIT"
|
15
16
|
|
16
|
-
spec.
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
19
|
+
|
20
|
+
spec.test_files = `git ls-files -z -- test`.split("\x0")
|
21
|
+
spec.files = `git ls-files -z`.split("\x0")
|
22
|
+
spec.files -= spec.test_files
|
23
|
+
spec.files -= ["vendor/snappy"]
|
24
|
+
spec.files += Dir["vendor/snappy/**/*"].reject { |e| e.start_with? "vendor/snappy/testdata" }
|
25
|
+
|
17
26
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
27
|
spec.require_paths = ["lib"]
|
20
28
|
|
21
29
|
if defined?(JRUBY_VERSION)
|
22
30
|
spec.files += %w[lib/snappy_ext.jar]
|
23
|
-
spec.platform =
|
24
|
-
spec.add_dependency
|
31
|
+
spec.platform = "java"
|
32
|
+
spec.add_dependency "snappy-jars", "~> 1.1.0"
|
25
33
|
else
|
26
34
|
spec.extensions = ["ext/extconf.rb"]
|
27
35
|
end
|
28
|
-
|
29
|
-
spec.add_development_dependency "bundler", "~> 1.3"
|
30
|
-
spec.add_development_dependency "rake"
|
31
|
-
spec.add_development_dependency "minitest"
|
32
|
-
|
33
|
-
# get an array of submodule dirs by executing 'pwd' inside each submodule
|
34
|
-
`git submodule --quiet foreach pwd`.split($\).each do |submodule_path|
|
35
|
-
# for each submodule, change working directory to that submodule
|
36
|
-
Dir.chdir(submodule_path) do
|
37
|
-
|
38
|
-
# issue git ls-files in submodule's directory
|
39
|
-
submodule_files = `git ls-files`.split($\)
|
40
|
-
|
41
|
-
# prepend the submodule path to create absolute file paths
|
42
|
-
submodule_files_fullpaths = submodule_files.map do |filename|
|
43
|
-
"#{submodule_path}/#{filename}"
|
44
|
-
end
|
45
|
-
|
46
|
-
# remove leading path parts to get paths relative to the gem's root dir
|
47
|
-
# (this assumes, that the gemspec resides in the gem's root dir)
|
48
|
-
submodule_files_paths = submodule_files_fullpaths.map do |filename|
|
49
|
-
filename.gsub "#{File.dirname(__FILE__)}/", ""
|
50
|
-
end
|
51
|
-
|
52
|
-
# add relative paths to gem.files
|
53
|
-
spec.files += submodule_files_paths
|
54
|
-
end
|
55
|
-
end
|
56
36
|
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
require "stringio"
|
5
|
+
|
6
|
+
class SnappyHadoopReaderTest < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@buffer = StringIO.new
|
9
|
+
Snappy::Hadoop::Writer.new @buffer do |w|
|
10
|
+
w << "foo"
|
11
|
+
w << "bar"
|
12
|
+
w << "baz"
|
13
|
+
w << "quux"
|
14
|
+
end
|
15
|
+
@buffer.rewind
|
16
|
+
end
|
17
|
+
|
18
|
+
def subject
|
19
|
+
@subject ||= Snappy::Hadoop::Reader.new @buffer
|
20
|
+
end
|
21
|
+
|
22
|
+
sub_test_case "#initialize" do
|
23
|
+
test "should yield itself to the block" do
|
24
|
+
yielded = nil
|
25
|
+
returned = Snappy::Hadoop::Reader.new @buffer do |r|
|
26
|
+
yielded = r
|
27
|
+
end
|
28
|
+
assert_equal returned, yielded
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
sub_test_case "#io" do
|
33
|
+
test "should be a constructor argument" do
|
34
|
+
assert_equal @buffer, subject.io
|
35
|
+
end
|
36
|
+
|
37
|
+
test "should not receive `length' in initializing" do
|
38
|
+
dont_allow(@buffer).length
|
39
|
+
Snappy::Hadoop::Reader.new @buffer
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
sub_test_case "#each" do
|
44
|
+
def setup
|
45
|
+
@buffer = StringIO.new
|
46
|
+
Snappy::Hadoop::Writer.new @buffer do |w|
|
47
|
+
w << "foo"
|
48
|
+
w << "bar"
|
49
|
+
w.dump!
|
50
|
+
w << "baz"
|
51
|
+
w << "quux"
|
52
|
+
end
|
53
|
+
@buffer.rewind
|
54
|
+
end
|
55
|
+
|
56
|
+
test "should yield each chunk" do
|
57
|
+
chunks = []
|
58
|
+
subject.each do |chunk|
|
59
|
+
chunks << chunk
|
60
|
+
end
|
61
|
+
assert_equal %w[foobar bazquux], chunks
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
sub_test_case "#read" do
|
66
|
+
test "should return the bytes" do
|
67
|
+
assert_equal "foobarbazquux", subject.read
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
sub_test_case "#each_line" do
|
72
|
+
def setup
|
73
|
+
@buffer = StringIO.new
|
74
|
+
Snappy::Hadoop::Writer.new @buffer do |w|
|
75
|
+
w << "foo\n"
|
76
|
+
w << "bar"
|
77
|
+
w.dump!
|
78
|
+
w << "baz\n"
|
79
|
+
w << "quux\n"
|
80
|
+
end
|
81
|
+
@buffer.rewind
|
82
|
+
end
|
83
|
+
|
84
|
+
test "should yield each line" do
|
85
|
+
lines = []
|
86
|
+
subject.each_line do |line|
|
87
|
+
lines << line
|
88
|
+
end
|
89
|
+
assert_equal %w[foo barbaz quux], lines
|
90
|
+
end
|
91
|
+
|
92
|
+
test "should return enumerator w/o block" do
|
93
|
+
eacher = subject.each_line
|
94
|
+
assert_instance_of Enumerator, eacher
|
95
|
+
lines = []
|
96
|
+
loop { lines << eacher.next }
|
97
|
+
assert_equal %w[foo barbaz quux], lines
|
98
|
+
end
|
99
|
+
|
100
|
+
test "each_line split by sep_string" do
|
101
|
+
buffer = StringIO.new
|
102
|
+
Snappy::Hadoop::Writer.new buffer do |w|
|
103
|
+
w << %w[a b c].join(",")
|
104
|
+
w.dump!
|
105
|
+
w << %w[d e f].join(",")
|
106
|
+
end
|
107
|
+
buffer.rewind
|
108
|
+
reader = Snappy::Hadoop::Reader.new buffer
|
109
|
+
eacher = reader.each_line(",")
|
110
|
+
lines = []
|
111
|
+
loop { lines << eacher.next }
|
112
|
+
assert_equal %w[a b cd e f], lines
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
require "stringio"
|
5
|
+
|
6
|
+
class SnappyHadoopWriterTest < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@buffer = StringIO.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def subject
|
12
|
+
@subject ||= Snappy::Hadoop::Writer.new @buffer
|
13
|
+
end
|
14
|
+
|
15
|
+
sub_test_case "#initialize" do
|
16
|
+
test "should yield itself to the block" do
|
17
|
+
yielded = nil
|
18
|
+
returned = Snappy::Hadoop::Writer.new @buffer do |w|
|
19
|
+
yielded = w
|
20
|
+
end
|
21
|
+
assert_equal returned, yielded
|
22
|
+
end
|
23
|
+
|
24
|
+
test "should write the header" do
|
25
|
+
Snappy::Hadoop::Writer.new @buffer do |w|
|
26
|
+
w << "foo"
|
27
|
+
end
|
28
|
+
assert_equal "\u0000\u0000\u0000\u0003\u0000\u0000\u0000\u0005\u0003\bfoo", @buffer.string
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
sub_test_case "#io" do
|
33
|
+
test "should be a constructor argument" do
|
34
|
+
io = StringIO.new
|
35
|
+
assert_equal io, Snappy::Hadoop::Writer.new(io).io
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
sub_test_case "#block_size" do
|
40
|
+
test "should default to DEFAULT_BLOCK_SIZE" do
|
41
|
+
assert_equal Snappy::Hadoop::Writer::DEFAULT_BLOCK_SIZE, subject.block_size
|
42
|
+
end
|
43
|
+
|
44
|
+
test "should be settable via the constructor" do
|
45
|
+
assert_equal 42, Snappy::Hadoop::Writer.new(StringIO.new, 42).block_size
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
require "stringio"
|
5
|
+
|
6
|
+
class SnappyHadoopTest < Test::Unit::TestCase
|
7
|
+
T = [*"a".."z", *"A".."Z", *"0".."9"].freeze
|
8
|
+
|
9
|
+
def random_data(length = 1024)
|
10
|
+
Array.new(length) { T.sample }.join
|
11
|
+
end
|
12
|
+
|
13
|
+
test "well done" do
|
14
|
+
s = random_data
|
15
|
+
assert_equal s, Snappy::Hadoop.inflate(Snappy::Hadoop.deflate(s))
|
16
|
+
end
|
17
|
+
|
18
|
+
test "well done(pair)" do
|
19
|
+
s = random_data
|
20
|
+
[
|
21
|
+
%i[deflate inflate]
|
22
|
+
].each do |(i, o)|
|
23
|
+
assert_equal s, Snappy::Hadoop.__send__(o, Snappy::Hadoop.__send__(i, s))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
require "stringio"
|
5
|
+
|
6
|
+
class SnappyReaderTest < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@buffer = StringIO.new
|
9
|
+
Snappy::Writer.new @buffer do |w|
|
10
|
+
w << "foo"
|
11
|
+
w << "bar"
|
12
|
+
w << "baz"
|
13
|
+
w << "quux"
|
14
|
+
end
|
15
|
+
@buffer.rewind
|
16
|
+
end
|
17
|
+
|
18
|
+
def subject
|
19
|
+
@subject ||= Snappy::Reader.new @buffer
|
20
|
+
end
|
21
|
+
|
22
|
+
sub_test_case "#initialize" do
|
23
|
+
test "should yield itself to the block" do
|
24
|
+
yielded = nil
|
25
|
+
returned = Snappy::Reader.new @buffer do |r|
|
26
|
+
yielded = r
|
27
|
+
end
|
28
|
+
assert_equal returned, yielded
|
29
|
+
end
|
30
|
+
|
31
|
+
test "should read the header" do
|
32
|
+
assert_equal Snappy::Writer::MAGIC, subject.magic
|
33
|
+
assert_equal Snappy::Writer::DEFAULT_VERSION, subject.default_version
|
34
|
+
assert_equal Snappy::Writer::MINIMUM_COMPATIBLE_VERSION, subject.minimum_compatible_version
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
sub_test_case :initialize_without_headers do
|
39
|
+
def setup
|
40
|
+
@buffer = StringIO.new
|
41
|
+
@buffer << Snappy.deflate("HelloWorld" * 10)
|
42
|
+
@buffer.rewind
|
43
|
+
end
|
44
|
+
|
45
|
+
test "should inflate with a magic header" do
|
46
|
+
assert_equal "HelloWorld" * 10, subject.read
|
47
|
+
end
|
48
|
+
|
49
|
+
test "should not receive `length' in eaching" do
|
50
|
+
dont_allow(@buffer).length
|
51
|
+
subject.read
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
sub_test_case "#io" do
|
56
|
+
test "should be a constructor argument" do
|
57
|
+
assert_equal @buffer, subject.io
|
58
|
+
end
|
59
|
+
|
60
|
+
test "should not receive `length' in initializing" do
|
61
|
+
dont_allow(@buffer).length
|
62
|
+
Snappy::Reader.new @buffer
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
sub_test_case "#each" do
|
67
|
+
def setup
|
68
|
+
@buffer = StringIO.new
|
69
|
+
Snappy::Writer.new @buffer do |w|
|
70
|
+
w << "foo"
|
71
|
+
w << "bar"
|
72
|
+
w.dump!
|
73
|
+
w << "baz"
|
74
|
+
w << "quux"
|
75
|
+
end
|
76
|
+
@buffer.rewind
|
77
|
+
end
|
78
|
+
|
79
|
+
test "should yield each chunk" do
|
80
|
+
chunks = []
|
81
|
+
subject.each do |chunk|
|
82
|
+
chunks << chunk
|
83
|
+
end
|
84
|
+
assert_equal %w[foobar bazquux], chunks
|
85
|
+
end
|
86
|
+
|
87
|
+
test "should return enumerator w/o block" do
|
88
|
+
eacher = subject.each
|
89
|
+
assert_instance_of Enumerator, eacher
|
90
|
+
chunks = []
|
91
|
+
chunks << eacher.next
|
92
|
+
chunks << eacher.next
|
93
|
+
assert_raise(StopIteration) { eacher.next }
|
94
|
+
assert_equal %w[foobar bazquux], chunks
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
sub_test_case "#read" do
|
99
|
+
test "should return the bytes" do
|
100
|
+
assert_equal "foobarbazquux", subject.read
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
sub_test_case "#each_line" do
|
105
|
+
def setup
|
106
|
+
@buffer = StringIO.new
|
107
|
+
Snappy::Writer.new @buffer do |w|
|
108
|
+
w << "foo\n"
|
109
|
+
w << "bar"
|
110
|
+
w.dump!
|
111
|
+
w << "baz\n"
|
112
|
+
w << "quux\n"
|
113
|
+
end
|
114
|
+
@buffer.rewind
|
115
|
+
end
|
116
|
+
|
117
|
+
test "should yield each line" do
|
118
|
+
lines = []
|
119
|
+
subject.each_line do |line|
|
120
|
+
lines << line
|
121
|
+
end
|
122
|
+
assert_equal %w[foo barbaz quux], lines
|
123
|
+
end
|
124
|
+
|
125
|
+
test "should return enumerator w/o block" do
|
126
|
+
eacher = subject.each_line
|
127
|
+
assert_instance_of Enumerator, eacher
|
128
|
+
lines = []
|
129
|
+
loop { lines << eacher.next }
|
130
|
+
assert_equal %w[foo barbaz quux], lines
|
131
|
+
end
|
132
|
+
|
133
|
+
test "each_line split by sep_string" do
|
134
|
+
buffer = StringIO.new
|
135
|
+
Snappy::Writer.new buffer do |w|
|
136
|
+
w << %w[a b c].join(",")
|
137
|
+
w.dump!
|
138
|
+
w << %w[d e f].join(",")
|
139
|
+
end
|
140
|
+
buffer.rewind
|
141
|
+
reader = Snappy::Reader.new buffer
|
142
|
+
eacher = reader.each_line(",")
|
143
|
+
lines = []
|
144
|
+
loop { lines << eacher.next }
|
145
|
+
assert_equal %w[a b cd e f], lines
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|