spec/digest_spec.rb in digest-murmurhash-0.1.0 vs spec/digest_spec.rb in digest-murmurhash-0.2.1
- old
+ new
@@ -1,60 +1,82 @@
require 'spec_helper'
-describe Digest::MurmurHash do
- let :murmur do
- Digest::MurmurHash.new
+describe "Digest::MurmurHash1 and 2" do
+ let :all do
+ [MurmurHash1, MurmurHash2, MurmurHash2A]
end
it "initialize" do
- expect(Digest::MurmurHash.new).to be_a_kind_of(Digest::Base)
+ expect(MurmurHash1.new).to be_a_kind_of(Digest::StringBuffer)
+ expect(MurmurHash2.new).to be_a_kind_of(Digest::StringBuffer)
+ expect(MurmurHash2A.new).to be_a_kind_of(Digest::StringBuffer)
end
+ it "digest" do
+ expect(MurmurHash1.digest("a" * 1024)).to eq("\xa1\x52\x2e\x5f".force_encoding("ASCII-8BIT"))
+ expect(MurmurHash2.digest("a" * 1024)).to eq("\xd0\x0c\x31\x2f".force_encoding("ASCII-8BIT"))
+ expect(MurmurHash2A.digest("a" * 1024)).to eq("\xd5\x2d\xb1\x67".force_encoding("ASCII-8BIT"))
+ end
+
it "hexdigest" do
- expect(Digest::MurmurHash.hexdigest("a" * 1024)).to eq("a1522e5f")
+ expect(MurmurHash1.hexdigest("a" * 1024)).to eq("a1522e5f")
+ expect(MurmurHash2.hexdigest("a" * 1024)).to eq("d00c312f")
+ expect(MurmurHash2A.hexdigest("a" * 1024)).to eq("d52db167")
end
- it "update and reset and hexdigest" do
- murmur.update("m")
- murmur.update("u")
- murmur.update("r")
- murmur.update("m")
- murmur.update("u")
- murmur.update("r")
- murmur.update("hash")
- expect(murmur.hexdigest).to eq("c709abd5");
- expect(murmur.hexdigest).to eq("c709abd5");
- expect(murmur.hexdigest!).to eq("c709abd5");
- expect(murmur.hexdigest).to eq("00000000");
+ it "rawdigest" do
+ expect(MurmurHash1.rawdigest("a" * 1024)).to eq(0xa1522e5f)
+ expect(MurmurHash2.rawdigest("a" * 1024)).to eq(0xd00c312f)
+ expect(MurmurHash2A.rawdigest("a" * 1024)).to eq(0xd52db167)
end
- it "dup" do
- murmur1 = Digest::MurmurHash.new
- murmur2 = Digest::MurmurHash.new
- murmur1.update("murmur")
- murmur2 = murmur1.dup
- murmur2.update("hash")
- expect(murmur2.hexdigest).to eq("c709abd5")
+ it "update and reset and hexdigest" do
+ {
+ MurmurHash1 => "c709abd5",
+ MurmurHash2 => "33f67c7e",
+ MurmurHash2A => "df25554b",
+ }.each do |c, should|
+ murmur = c.new
+ murmur.update("m").update("u").update("r")
+ murmur << "m" << "u" << "r"
+ murmur << "hash"
+ expect(murmur.hexdigest).to eq(should);
+ expect(murmur.hexdigest).to eq(should);
+ expect(murmur.hexdigest!).to eq(should);
+ expect(murmur.hexdigest).to eq("00000000");
+ end
end
it "==" do
- ["", "murmur", "murmurhash" * 1024].each do |str|
- murmur1 = Digest::MurmurHash.new
- murmur2 = Digest::MurmurHash.new
- expect(murmur1.update(str) == murmur2.update(str)).to be_true
+ all.each do |c|
+ ["", "murmur", "murmurhash" * 1024].each do |str|
+ murmur1 = c.new
+ murmur2 = c.new
+ expect(murmur1.update(str) == murmur2.update(str)).to be_true
+ end
end
end
- it "length" do
- expect(murmur.length).to eq(4);
+ it "dup" do
+ all.each do |c|
+ murmur1 = c.new
+ murmur2 = c.new
+ 10.times {
+ murmur1 = murmur1.update("murmurhash" * 100).dup
+ }
+ murmur2.update(("murmurhash" * 100) * 10)
+ expect(murmur1 == murmur2).to be_true
+ end
end
- it "block_length" do
- # MurmurHash don't use block.
- # Therefore `block_length` return chunk size for calculate MurmurHash (equal 4)
- expect(murmur.block_length).to eq(4);
+ it "length" do
+ expect(MurmurHash1.new.length).to eq(4);
+ expect(MurmurHash2.new.length).to eq(4);
+ expect(MurmurHash2A.new.length).to eq(4);
end
it "to_i" do
- expect(murmur.update("murmurhash").to_i).to eq(0xc709abd5);
+ expect(MurmurHash1.new.update("murmurhash").to_i).to eq(0xc709abd5);
+ expect(MurmurHash2.new.update("murmurhash").to_i).to eq(0x33f67c7e);
+ expect(MurmurHash2A.new.update("murmurhash").to_i).to eq(0xdf25554b);
end
end