Skip to content

Commit 31d623c

Browse files
add some tests for net (#1936)
Signed-off-by: Lan Liang <[email protected]>
1 parent 3cf0e7d commit 31d623c

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

kclvm/tests/test_units/runtime/kclvm_runtime.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,4 @@ def Invoke(self, method: str, *args, **kwargs) -> any:
9797
if __name__ == "__main__":
9898
dylib = KclvmRuntimeDylib()
9999
dylib.Invoke(f"print", "hello kclvm")
100+

kclvm/tests/test_units/runtime/net/test_net.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,57 @@
22

33
import typing
44
import unittest
5+
import sys
6+
import os
57

8+
# Add the parent directory to the path to import kclvm_runtime
9+
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
10+
sys.path.append(parent_dir)
611
import kclvm_runtime
712

813
# https://github.com/python/cpython/blob/main/Lib/test
9-
1014
_Dylib = kclvm_runtime.KclvmRuntimeDylib()
1115

1216

1317
class kclx_Net:
1418
def __init__(self, dylib_=None):
1519
self.dylib = dylib_ if dylib_ else _Dylib
16-
20+
def is_global_unicast_IP(self, value: str) -> bool:
21+
return self.dylib.Invoke(f"net.is_global_unicast_IP", value)
22+
def is_link_local_multicast_IP(self, value: str) -> bool:
23+
return self.dylib.Invoke(f"net.is_link_local_multicast_IP", value)
24+
def is_interface_local_multicast_IP(self, value: str) -> bool:
25+
return self.dylib.Invoke(f"net.is_interface_local_multicast_IP", value)
26+
def is_multicast_IP(self, value: str) -> bool:
27+
return self.dylib.Invoke(f"net.is_multicast_IP", value)
28+
def is_loopback_IP(self, value: str) -> bool:
29+
return self.dylib.Invoke(f"net.is_loopback_IP", value)
30+
def is_link_local_unicast_IP(self, value: str) -> bool:
31+
return self.dylib.Invoke(f"net.is_link_local_unicast_IP", value)
32+
def is_unspecified_IP(self, value: str) -> bool:
33+
return self.dylib.Invoke(f"net.is_unspecified_IP", value)
34+
35+
36+
kclxnet = kclx_Net(_Dylib)
1737

1838
class BaseTest(unittest.TestCase):
19-
def test_foo(self):
20-
self.assertTrue(True)
21-
39+
def test_is_interface_local_multicast_IP(self):
40+
self.assertFalse(kclxnet.is_interface_local_multicast_IP("224.0.0.0"))
41+
self.assertTrue(kclxnet.is_interface_local_multicast_IP("ff11::1"))
42+
def test_is_link_local_multicast_IP(self):
43+
self.assertTrue(kclxnet.is_link_local_multicast_IP("ff12::1"))
44+
def test_is_global_unicast_IP(self):
45+
self.assertTrue(kclxnet.is_global_unicast_IP("2607:f8b0:4005:802::200e"))
46+
self.assertTrue(kclxnet.is_global_unicast_IP("64:ff9b::800:1"))
47+
self.assertTrue(kclxnet.is_global_unicast_IP("220.181.108.89"))
48+
def test_is_multicast_IP(self):
49+
self.assertTrue(kclxnet.is_multicast_IP("239.255.255.255"))
50+
def test_is_loopback_IP(self):
51+
self.assertTrue(kclxnet.is_loopback_IP("127.0.0.1"))
52+
def test_is_link_local_unicast_IP(self):
53+
self.assertTrue(kclxnet.is_link_local_unicast_IP("fe80::2012:1"))
54+
def test_is_unspecified_IP(self):
55+
self.assertTrue(kclxnet.is_unspecified_IP("0.0.0.0"))
2256

2357
if __name__ == "__main__":
2458
unittest.main()

test/grammar/builtins/net/is_ip_2/main.k

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ isip2 = net.is_link_local_multicast_IP("224.0.0.0")
66
isip3 = net.is_link_local_unicast_IP("fe80::2012:1")
77
isip4 = net.is_global_unicast_IP("220.181.108.89")
88
isip5 = net.is_unspecified_IP("0.0.0.0")
9+
10+
isip6 = net.is_interface_local_multicast_IP("224.0.0.0")
11+
isip7 = net.is_interface_local_multicast_IP("ff11::1")
12+
isip8 = net.is_link_local_multicast_IP("ff12::1")
13+
isip9 = net.is_global_unicast_IP("2607:f8b0:4005:802::200e")
14+
isip10 = net.is_global_unicast_IP("64:ff9b::800:1")

test/grammar/builtins/net/is_ip_2/stdout.golden

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@ isip1: true
33
isip2: true
44
isip3: true
55
isip4: true
6-
isip5: true
6+
isip5: true
7+
isip6: false
8+
isip7: true
9+
isip8: true
10+
isip9: true
11+
isip10: true

0 commit comments

Comments
 (0)