diff options
author | Juho Mäkinen <juho.makinen@applifier.com> | 2016-11-04 13:28:07 +0200 |
---|---|---|
committer | Romain Tartière <romain@blogreen.org> | 2017-08-30 09:14:12 +0200 |
commit | eb017ad756f4ba8db98d20dbb777baddb6836d01 (patch) | |
tree | 0e4d1182fde04d1d398572863b71749eaf409430 /test | |
parent | 032e8247f7840a490b226d6fbee89b4799b88660 (diff) | |
download | ipaddress-eb017ad756f4ba8db98d20dbb777baddb6836d01.tar.gz |
Add allocate() method
The allocate() method allows to use an IPAddress::IPv4 or ::IPv6 object
to allocate individual host addresses so that the IPAddress object
tracks the state of which address was previously allocated.
Example:
ip = IPAddress("10.0.0.0/24")
ip.allocate
#=> "10.0.0.1/24"
ip.allocate
#=> "10.0.0.2/24"
There is support for both IPv4 and IPv6 with full test coverage.
Diffstat (limited to 'test')
-rw-r--r-- | test/ipaddress/ipv4_test.rb | 24 | ||||
-rw-r--r-- | test/ipaddress/ipv6_test.rb | 24 |
2 files changed, 48 insertions, 0 deletions
diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb index 19264e2..df60bb1 100644 --- a/test/ipaddress/ipv4_test.rb +++ b/test/ipaddress/ipv4_test.rb @@ -595,6 +595,30 @@ class IPv4Test < Minitest::Test assert_equal "192.168.200.0/24", ip.to_string end + def test_allocate_addresses + ip = @klass.new("10.0.0.0/24") + ip1 = ip.allocate + ip2 = ip.allocate + ip3 = ip.allocate + assert_equal "10.0.0.1/24", ip1.to_string + assert_equal "10.0.0.2/24", ip2.to_string + assert_equal "10.0.0.3/24", ip3.to_string + end + + def test_allocate_can_skip_addresses + ip = @klass.new("10.0.0.0/24") + ip1 = ip.allocate(2) + assert_equal "10.0.0.3/24", ip1.to_string + end + + def test_allocate_will_raise_stopiteration + ip = @klass.new("10.0.0.0/30") + ip.allocate(3) + assert_raises (StopIteration) do + ip.allocate + end + end + end # class IPv4Test diff --git a/test/ipaddress/ipv6_test.rb b/test/ipaddress/ipv6_test.rb index 7eaa765..3990129 100644 --- a/test/ipaddress/ipv6_test.rb +++ b/test/ipaddress/ipv6_test.rb @@ -234,6 +234,30 @@ class IPv6Test < Minitest::Test assert_equal expected, arr end + def test_allocate_addresses + ip = @klass.new("2001:db8::4/125") + ip1 = ip.allocate + ip2 = ip.allocate + ip3 = ip.allocate + assert_equal "2001:db8::1", ip1.compressed + assert_equal "2001:db8::2", ip2.compressed + assert_equal "2001:db8::3", ip3.compressed + end + + def test_allocate_can_skip_addresses + ip = @klass.new("2001:db8::4/125") + ip1 = ip.allocate(2) + assert_equal "2001:db8::3", ip1.compressed + end + + def test_allocate_will_raise_stopiteration + ip = @klass.new("2001:db8::4/125") + ip.allocate(6) + assert_raises (StopIteration) do + ip.allocate + end + end + def test_method_compare ip1 = @klass.new("2001:db8:1::1/64") ip2 = @klass.new("2001:db8:2::1/64") |