File: test-ipp-hash.py

package info (click to toggle)
python-libtrace 1.6%2Bgit20180219-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,620 kB
  • sloc: ansic: 7,465; python: 3,472; makefile: 54; sh: 49
file content (47 lines) | stat: -rw-r--r-- 1,364 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

import ipp
from plt_testing import *

ipa = ipp.from_s("1.2.3.4")  # 1
ipb = ipp.from_s("1.2.3.5")  # 2
ipc = ipp.from_s("1.2.4.4")  # 3
ipd = ipp.from_s("1.3.3.4")  # 4
ipe = ipp.from_s("2.2.3.4")  # 5

h = {ipa : 1, ipb : 2, ipc : 3, ipd : 4, ipe : 5}

for ip in sorted(h):
      test_println(str("%s -> %d,  %d" % (ip, h[ip], ip.__hash__())), get_tag())

ip01 = ipp.from_s("192.168.10.1/16")
test_println(str("ip01 =            %s" % ip01), get_tag())
test_println(str("ip01.complement = %s" % ip01.complement), get_tag())
test_println(str("ip01.width      = %d" % ip01.width), get_tag())
test_println(str("ip01.is_rfc1918 = %s" % ip01.is_rfc1918), get_tag())

ip88 = ipp.from_s("192.168.0.0/16")
ip89 = ipp.from_s("192.168.0.0")

def test_ip(ip):
    test_println(str("--- trying %s" % ip), get_tag())
    test_println(str("   slash: %s.is_prefix(%s) -> %s" % (
        ip88, ip, ip88.is_prefix(ip))), get_tag())
    r = ip.is_rfc1918
    test_println(str("%s -> %s" % (ip, r)), get_tag())

ip1 = ipp.from_s("130.216.12.34")
ip2 = ipp.from_s("130.216.56.78/32")
ip81 = ipp.from_s("192.168.0.2")
ip82 = ipp.from_s("192.168.1.3/32")

for j in range(1):
    test_ip(ip81)
    test_ip(ip82)
    test_ip(ip1)
    test_ip(ip2)

try:
    r = ip01 == "130.216.56.78"  # Argument is string, not IPprefix
except:
    test_println(str("Exception caught"), get_tag())