diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
commit | a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5 (patch) | |
tree | 9eef7f720314ebaff56845a74e203770e62284e4 /spec/rubyspec/library/ipaddr/operator_spec.rb | |
parent | 52df1d0d3370919711c0577aaa42d1a864709885 (diff) | |
download | ruby-a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5.tar.gz |
Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
These files can therefore be updated like any other file in MRI.
Instructions are provided in spec/README.
[Feature #13156] [ruby-core:79246]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/rubyspec/library/ipaddr/operator_spec.rb')
-rw-r--r-- | spec/rubyspec/library/ipaddr/operator_spec.rb | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/spec/rubyspec/library/ipaddr/operator_spec.rb b/spec/rubyspec/library/ipaddr/operator_spec.rb new file mode 100644 index 0000000000..3f54efd486 --- /dev/null +++ b/spec/rubyspec/library/ipaddr/operator_spec.rb @@ -0,0 +1,80 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'ipaddr' + +describe "IPAddr Operator" do + IN6MASK32 = "ffff:ffff::" + IN6MASK128 = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" + + before do + @in6_addr_any = IPAddr.new() + @a = IPAddr.new("3ffe:505:2::/48") + @b = IPAddr.new("0:0:0:1::") + @c = IPAddr.new(IN6MASK32) + end + + it "bitwises or" do + (@a | @b).to_s.should == "3ffe:505:2:1::" + a = @a + a |= @b + a.to_s.should == "3ffe:505:2:1::" + @a.to_s.should == "3ffe:505:2::" + (@a | 0x00000000000000010000000000000000).to_s.should == "3ffe:505:2:1::" + end + + it "bitwises and" do + (@a & @c).to_s.should == "3ffe:505::" + a = @a + a &= @c + a.to_s.should == "3ffe:505::" + @a.to_s.should == "3ffe:505:2::" + (@a & 0xffffffff000000000000000000000000).to_s.should == "3ffe:505::" + end + + it "bitshifts right" do + (@a >> 16).to_s.should == "0:3ffe:505:2::" + a = @a + a >>= 16 + a.to_s.should == "0:3ffe:505:2::" + @a.to_s.should == "3ffe:505:2::" + end + + it "bitshifts left" do + (@a << 16).to_s.should == "505:2::" + a = @a + a <<= 16 + a.to_s.should == "505:2::" + @a.to_s.should == "3ffe:505:2::" + end + + it "inverts" do + a = ~@in6_addr_any + a.to_s.should == IN6MASK128 + @in6_addr_any.to_s.should == "::" + end + + it "tests for equality" do + @a.should == IPAddr.new("3ffe:505:2::") + @a.should_not == IPAddr.new("3ffe:505:3::") + end + + it "sets a mask" do + a = @a.mask(32) + a.to_s.should == "3ffe:505::" + @a.to_s.should == "3ffe:505:2::" + end + + it "checks whether an addres is included in a range" do + @a.should include(IPAddr.new("3ffe:505:2::")) + @a.should include(IPAddr.new("3ffe:505:2::1")) + @a.should_not include(IPAddr.new("3ffe:505:3::")) + net1 = IPAddr.new("192.168.2.0/24") + net1.should include(IPAddr.new("192.168.2.0")) + net1.should include(IPAddr.new("192.168.2.255")) + net1.should_not include(IPAddr.new("192.168.3.0")) + # test with integer parameter + int = (192 << 24) + (168 << 16) + (2 << 8) + 13 + + net1.should include(int) + net1.should_not include(int+255) + end +end |