mirror of
https://github.com/JoelBender/bacpypes
synced 2025-09-28 22:15:23 +08:00
merging in issue-38, but I'm leaving the issue open until the rest of the datatypes are finished
This commit is contained in:
commit
c461566fb2
|
@ -451,6 +451,8 @@ class Atomic(object):
|
|||
_app_tag = None
|
||||
|
||||
def __cmp__(self, other):
|
||||
# sys.stderr.write("__cmp__ %r %r\n" % (self, other))
|
||||
|
||||
# hoop jump it
|
||||
if not isinstance(other, self.__class__):
|
||||
other = self.__class__(other)
|
||||
|
@ -463,6 +465,26 @@ class Atomic(object):
|
|||
else:
|
||||
return 0
|
||||
|
||||
def __lt__(self, other):
|
||||
# sys.stderr.write("__lt__ %r %r\n" % (self, other))
|
||||
|
||||
# hoop jump it
|
||||
if not isinstance(other, self.__class__):
|
||||
other = self.__class__(other)
|
||||
|
||||
# now compare the values
|
||||
return (self.value < other.value)
|
||||
|
||||
def __eq__(self, other):
|
||||
sys.stderr.write("__eq__ %r %r\n" % (self, other))
|
||||
|
||||
# hoop jump it
|
||||
if not isinstance(other, self.__class__):
|
||||
other = self.__class__(other)
|
||||
|
||||
# now compare the values
|
||||
return self.value == other.value
|
||||
|
||||
#
|
||||
# Null
|
||||
#
|
||||
|
@ -738,7 +760,7 @@ class OctetString(Atomic):
|
|||
_app_tag = Tag.octetStringAppTag
|
||||
|
||||
def __init__(self, arg=None):
|
||||
self.value = ''
|
||||
self.value = bytes()
|
||||
|
||||
if arg is None:
|
||||
pass
|
||||
|
|
7
tests/test_primitive_data/__init__.py
Normal file
7
tests/test_primitive_data/__init__.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
"""
|
||||
Test Primitive Data Module
|
||||
"""
|
||||
|
||||
from . import test_integer
|
0
tests/test_primitive_data/test_bit_string.py
Normal file
0
tests/test_primitive_data/test_bit_string.py
Normal file
123
tests/test_primitive_data/test_boolean.py
Normal file
123
tests/test_primitive_data/test_boolean.py
Normal file
|
@ -0,0 +1,123 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Test Primitive Data Boolean
|
||||
---------------------------
|
||||
"""
|
||||
|
||||
import unittest
|
||||
|
||||
from bacpypes.debugging import bacpypes_debugging, ModuleLogger, xtob
|
||||
from bacpypes.primitivedata import Boolean, Tag
|
||||
|
||||
# some debugging
|
||||
_debug = 0
|
||||
_log = ModuleLogger(globals())
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def boolean_tag(value):
|
||||
"""Convert an integer to an boolean application tag."""
|
||||
if _debug: boolean_tag._debug("boolean_tag %r", value)
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, int(value), xtob(''))
|
||||
if _debug: boolean_endec._debug(" - tag: %r", tag)
|
||||
|
||||
return tag
|
||||
|
||||
@bacpypes_debugging
|
||||
def boolean_encode(obj):
|
||||
"""Encode an Boolean object into a tag."""
|
||||
if _debug: boolean_encode._debug("boolean_encode %r", obj)
|
||||
|
||||
tag = Tag()
|
||||
obj.encode(tag)
|
||||
if _debug: boolean_endec._debug(" - tag: %r", tag)
|
||||
|
||||
return tag
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def boolean_decode(tag):
|
||||
"""Decode an boolean application tag into an boolean."""
|
||||
if _debug: boolean_decode._debug("boolean_decode %r", tag)
|
||||
|
||||
obj = Boolean(tag)
|
||||
if _debug: boolean_decode._debug(" - obj: %r", obj)
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def boolean_endec(v, x):
|
||||
"""Pass the value to Boolean, construct a tag from the hex string,
|
||||
and compare results of encode and decoding each other."""
|
||||
if _debug: boolean_endec._debug("boolean_endec %r %r", v, x)
|
||||
|
||||
tag = boolean_tag(x)
|
||||
if _debug: boolean_endec._debug(" - tag: %r, %r", tag, tag.tagData)
|
||||
|
||||
obj = Boolean(v)
|
||||
if _debug: boolean_endec._debug(" - obj: %r, %r", obj, obj.value)
|
||||
|
||||
assert boolean_encode(obj) == tag
|
||||
assert boolean_decode(tag) == obj
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
class TestBoolean(unittest.TestCase):
|
||||
|
||||
def test_boolean(self):
|
||||
if _debug: TestBoolean._debug("test_boolean")
|
||||
|
||||
obj = Boolean()
|
||||
assert obj.value == False
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
Boolean("some string")
|
||||
with self.assertRaises(TypeError):
|
||||
Boolean(1.0)
|
||||
|
||||
def test_boolean_bool(self):
|
||||
if _debug: TestBoolean._debug("test_boolean_bool")
|
||||
|
||||
obj = Boolean(False)
|
||||
assert obj.value == False
|
||||
assert str(obj) == "Boolean(False)"
|
||||
|
||||
obj = Boolean(True)
|
||||
assert obj.value == True
|
||||
assert str(obj) == "Boolean(True)"
|
||||
|
||||
def test_boolean_tag(self):
|
||||
if _debug: TestBoolean._debug("test_boolean_tag")
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 1, xtob('01'))
|
||||
obj = Boolean(tag)
|
||||
assert obj.value == 1
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.integerAppTag, 0, xtob(''))
|
||||
with self.assertRaises(ValueError):
|
||||
Boolean(tag)
|
||||
|
||||
tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
|
||||
with self.assertRaises(ValueError):
|
||||
Boolean(tag)
|
||||
|
||||
tag = Tag(Tag.openingTagClass, 0)
|
||||
with self.assertRaises(ValueError):
|
||||
Boolean(tag)
|
||||
|
||||
def test_boolean_copy(self):
|
||||
if _debug: TestBoolean._debug("test_boolean_copy")
|
||||
|
||||
obj1 = Boolean(True)
|
||||
obj2 = Boolean(obj1)
|
||||
assert obj2.value == True
|
||||
|
||||
def test_boolean_endec(self):
|
||||
if _debug: TestBoolean._debug("test_boolean_endec")
|
||||
|
||||
boolean_endec(False, False)
|
||||
boolean_endec(True, True)
|
0
tests/test_primitive_data/test_character_string.py
Normal file
0
tests/test_primitive_data/test_character_string.py
Normal file
0
tests/test_primitive_data/test_date.py
Normal file
0
tests/test_primitive_data/test_date.py
Normal file
143
tests/test_primitive_data/test_double.py
Normal file
143
tests/test_primitive_data/test_double.py
Normal file
|
@ -0,0 +1,143 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Test Primitive Data Double
|
||||
--------------------------
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import struct
|
||||
import math
|
||||
|
||||
from bacpypes.debugging import bacpypes_debugging, ModuleLogger, xtob
|
||||
from bacpypes.primitivedata import Double, Tag
|
||||
|
||||
# some debugging
|
||||
_debug = 0
|
||||
_log = ModuleLogger(globals())
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def double_tag(x):
|
||||
"""Convert a hex string to an double application tag."""
|
||||
if _debug: double_tag._debug("double_tag %r", x)
|
||||
|
||||
b = xtob(x)
|
||||
tag = Tag(Tag.applicationTagClass, Tag.doubleAppTag, len(b), b)
|
||||
if _debug: double_endec._debug(" - tag: %r", tag)
|
||||
|
||||
return tag
|
||||
|
||||
@bacpypes_debugging
|
||||
def double_encode(obj):
|
||||
"""Encode an Double object into a tag."""
|
||||
if _debug: double_encode._debug("double_encode %r", obj)
|
||||
|
||||
tag = Tag()
|
||||
obj.encode(tag)
|
||||
if _debug: double_endec._debug(" - tag: %r", tag)
|
||||
|
||||
return tag
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def double_decode(tag):
|
||||
"""Decode an double application tag into a double."""
|
||||
if _debug: double_decode._debug("double_decode %r", tag)
|
||||
|
||||
obj = Double(tag)
|
||||
if _debug: double_decode._debug(" - obj: %r", obj)
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def double_endec(v, x):
|
||||
"""Pass the value to Double, construct a tag from the hex string,
|
||||
and compare results of encode and decoding each other."""
|
||||
if _debug: double_endec._debug("double_endec %r %r", v, x)
|
||||
|
||||
tag = double_tag(x)
|
||||
if _debug: double_endec._debug(" - tag: %r, %r", tag, tag.tagData)
|
||||
|
||||
obj = Double(v)
|
||||
if _debug: double_endec._debug(" - obj: %r, %r", obj, obj.value)
|
||||
|
||||
assert double_encode(obj) == tag
|
||||
if _debug: real_endec._debug(" - tags match")
|
||||
|
||||
if math.isnan(v):
|
||||
assert math.isnan(double_decode(tag).value)
|
||||
if _debug: double_endec._debug(" - both NaN")
|
||||
else:
|
||||
assert double_decode(tag) == obj
|
||||
if _debug: double_endec._debug(" - objects match")
|
||||
|
||||
@bacpypes_debugging
|
||||
class TestDouble(unittest.TestCase):
|
||||
|
||||
def test_double(self):
|
||||
if _debug: TestDouble._debug("test_double")
|
||||
|
||||
obj = Double()
|
||||
assert obj.value == 0.0
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
Double("some string")
|
||||
|
||||
def test_double_double(self):
|
||||
if _debug: TestDouble._debug("test_double_double")
|
||||
|
||||
obj = Double(1.0)
|
||||
assert obj.value == 1.0
|
||||
assert str(obj) == "Double(1)"
|
||||
|
||||
obj = Double(73.5)
|
||||
assert obj.value == 73.5
|
||||
assert str(obj) == "Double(73.5)"
|
||||
|
||||
def test_double_tag(self):
|
||||
if _debug: TestDouble._debug("test_double_tag")
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.doubleAppTag, 8, xtob('3ff0000000000000'))
|
||||
obj = Double(tag)
|
||||
assert obj.value == 1.0
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0, xtob(''))
|
||||
with self.assertRaises(ValueError):
|
||||
Double(tag)
|
||||
|
||||
tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
|
||||
with self.assertRaises(ValueError):
|
||||
Double(tag)
|
||||
|
||||
tag = Tag(Tag.openingTagClass, 0)
|
||||
with self.assertRaises(ValueError):
|
||||
Double(tag)
|
||||
|
||||
def test_double_copy(self):
|
||||
if _debug: TestDouble._debug("test_double_copy")
|
||||
|
||||
obj1 = Double(12)
|
||||
obj2 = Double(obj1)
|
||||
assert obj2.value == 12
|
||||
|
||||
def test_double_endec(self):
|
||||
if _debug: TestDouble._debug("test_double_endec")
|
||||
|
||||
with self.assertRaises(struct.error):
|
||||
obj = Double(double_tag(''))
|
||||
|
||||
double_endec(0, '0000000000000000')
|
||||
double_endec(1, '3ff0000000000000')
|
||||
double_endec(-1, 'bff0000000000000')
|
||||
|
||||
double_endec(73.5, '4052600000000000')
|
||||
|
||||
inf = float('inf')
|
||||
double_endec(inf, '7ff0000000000000')
|
||||
double_endec(-inf, 'fff0000000000000')
|
||||
|
||||
nan = float('nan')
|
||||
double_endec(nan, '7ff8000000000000')
|
0
tests/test_primitive_data/test_enumerated.py
Normal file
0
tests/test_primitive_data/test_enumerated.py
Normal file
139
tests/test_primitive_data/test_integer.py
Normal file
139
tests/test_primitive_data/test_integer.py
Normal file
|
@ -0,0 +1,139 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Test Primitive Data Integer
|
||||
---------------------------
|
||||
"""
|
||||
|
||||
import unittest
|
||||
|
||||
from bacpypes.debugging import bacpypes_debugging, ModuleLogger, xtob
|
||||
from bacpypes.primitivedata import Integer, Tag
|
||||
|
||||
# some debugging
|
||||
_debug = 0
|
||||
_log = ModuleLogger(globals())
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def integer_tag(x):
|
||||
"""Convert a hex string to an integer application tag."""
|
||||
if _debug: integer_tag._debug("integer_tag %r", x)
|
||||
|
||||
b = xtob(x)
|
||||
tag = Tag(Tag.applicationTagClass, Tag.integerAppTag, len(b), b)
|
||||
if _debug: integer_endec._debug(" - tag: %r", tag)
|
||||
|
||||
return tag
|
||||
|
||||
@bacpypes_debugging
|
||||
def integer_encode(obj):
|
||||
"""Encode an Integer object into a tag."""
|
||||
if _debug: integer_encode._debug("integer_encode %r", obj)
|
||||
|
||||
tag = Tag()
|
||||
obj.encode(tag)
|
||||
if _debug: integer_endec._debug(" - tag: %r", tag)
|
||||
|
||||
return tag
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def integer_decode(tag):
|
||||
"""Decode an integer application tag into an integer."""
|
||||
if _debug: integer_decode._debug("integer_decode %r", tag)
|
||||
|
||||
obj = Integer(tag)
|
||||
if _debug: integer_decode._debug(" - obj: %r", obj)
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def integer_endec(v, x):
|
||||
"""Pass the value to Integer, construct a tag from the hex string,
|
||||
and compare results of encode and decoding each other."""
|
||||
if _debug: integer_endec._debug("integer_endec %r %r", v, x)
|
||||
|
||||
tag = integer_tag(x)
|
||||
if _debug: integer_endec._debug(" - tag: %r, %r", tag, tag.tagData)
|
||||
|
||||
obj = Integer(v)
|
||||
if _debug: integer_endec._debug(" - obj: %r, %r", obj, obj.value)
|
||||
|
||||
assert integer_encode(obj) == tag
|
||||
assert integer_decode(tag) == obj
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
class TestInteger(unittest.TestCase):
|
||||
|
||||
def test_integer(self):
|
||||
if _debug: TestInteger._debug("test_integer")
|
||||
|
||||
obj = Integer()
|
||||
assert obj.value == 0
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
Integer("some string")
|
||||
with self.assertRaises(TypeError):
|
||||
Integer(1.0)
|
||||
|
||||
def test_integer_int(self):
|
||||
if _debug: TestInteger._debug("test_integer_int")
|
||||
|
||||
obj = Integer(1)
|
||||
assert obj.value == 1
|
||||
assert str(obj) == "Integer(1)"
|
||||
|
||||
obj = Integer(-1)
|
||||
assert obj.value == -1
|
||||
assert str(obj) == "Integer(-1)"
|
||||
|
||||
def test_integer_tag(self):
|
||||
if _debug: TestInteger._debug("test_integer_tag")
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.integerAppTag, 1, xtob('01'))
|
||||
obj = Integer(tag)
|
||||
assert obj.value == 1
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0, xtob(''))
|
||||
with self.assertRaises(ValueError):
|
||||
Integer(tag)
|
||||
|
||||
tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
|
||||
with self.assertRaises(ValueError):
|
||||
Integer(tag)
|
||||
|
||||
tag = Tag(Tag.openingTagClass, 0)
|
||||
with self.assertRaises(ValueError):
|
||||
Integer(tag)
|
||||
|
||||
def test_integer_copy(self):
|
||||
if _debug: TestInteger._debug("test_integer_copy")
|
||||
|
||||
obj1 = Integer(12)
|
||||
obj2 = Integer(obj1)
|
||||
assert obj2.value == 12
|
||||
|
||||
def test_integer_endec(self):
|
||||
if _debug: TestInteger._debug("test_integer_endec")
|
||||
|
||||
with self.assertRaises(IndexError):
|
||||
obj = Integer(integer_tag(''))
|
||||
|
||||
integer_endec(0, '00')
|
||||
integer_endec(1, '01')
|
||||
integer_endec(127, '7f')
|
||||
integer_endec(-128, '80')
|
||||
integer_endec(-1, 'ff')
|
||||
|
||||
integer_endec(32767, '7fff')
|
||||
integer_endec(-32768, '8000')
|
||||
|
||||
integer_endec(8388607, '7fffff')
|
||||
integer_endec(-8388608, '800000')
|
||||
|
||||
integer_endec(2147483647, '7fffffff')
|
||||
integer_endec(-2147483648, '80000000')
|
106
tests/test_primitive_data/test_null.py
Normal file
106
tests/test_primitive_data/test_null.py
Normal file
|
@ -0,0 +1,106 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Test Primitive Data Null
|
||||
------------------------
|
||||
"""
|
||||
|
||||
import unittest
|
||||
|
||||
from bacpypes.debugging import bacpypes_debugging, ModuleLogger, xtob
|
||||
from bacpypes.primitivedata import Null, Tag
|
||||
|
||||
# some debugging
|
||||
_debug = 0
|
||||
_log = ModuleLogger(globals())
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def null_tag(x):
|
||||
"""Convert a hex string to an integer application tag."""
|
||||
if _debug: null_tag._debug("null_tag %r", x)
|
||||
|
||||
b = xtob(x)
|
||||
tag = Tag(Tag.applicationTagClass, Tag.nullAppTag, len(b), b)
|
||||
if _debug: integer_endec._debug(" - tag: %r", tag)
|
||||
|
||||
return tag
|
||||
|
||||
@bacpypes_debugging
|
||||
def null_encode(obj):
|
||||
"""Encode an Integer object into a tag."""
|
||||
if _debug: null_encode._debug("null_encode %r", obj)
|
||||
|
||||
tag = Tag()
|
||||
obj.encode(tag)
|
||||
if _debug: null_encode._debug(" - tag: %r", tag)
|
||||
|
||||
return tag
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def null_decode(tag):
|
||||
"""Decode an integer application tag into an integer."""
|
||||
if _debug: null_decode._debug("null_decode %r", tag)
|
||||
|
||||
obj = Null(tag)
|
||||
if _debug: null_decode._debug(" - obj: %r", obj)
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def null_endec(v, x):
|
||||
"""Pass the value to Integer, construct a tag from the hex string,
|
||||
and compare results of encode and decoding each other."""
|
||||
if _debug: null_endec._debug("null_endec %r %r", v, x)
|
||||
|
||||
tag = null_tag(x)
|
||||
if _debug: null_endec._debug(" - tag: %r, %r", tag, tag.tagData)
|
||||
|
||||
obj = Null(v)
|
||||
if _debug: null_endec._debug(" - obj: %r, %r", obj, obj.value)
|
||||
|
||||
assert null_encode(obj) == tag
|
||||
assert null_decode(tag) == obj
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
class TestNull(unittest.TestCase):
|
||||
|
||||
def test_null(self):
|
||||
if _debug: TestInteger._debug("test_null")
|
||||
|
||||
obj = Null()
|
||||
assert obj.value == ()
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
Null("some string")
|
||||
with self.assertRaises(TypeError):
|
||||
Null(1.0)
|
||||
|
||||
def test_null_null(self):
|
||||
if _debug: TestInteger._debug("test_null_null")
|
||||
|
||||
obj = Null(())
|
||||
assert obj.value == ()
|
||||
|
||||
def test_null_tag(self):
|
||||
if _debug: TestInteger._debug("test_null_tag")
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.nullAppTag, 0, xtob(''))
|
||||
obj = Null(tag)
|
||||
assert obj.value == ()
|
||||
|
||||
def test_null_copy(self):
|
||||
if _debug: TestInteger._debug("test_null_copy")
|
||||
|
||||
obj1 = Null()
|
||||
obj2 = Null(obj1)
|
||||
assert obj2.value == ()
|
||||
|
||||
def test_null_endec(self):
|
||||
if _debug: TestInteger._debug("test_null_endec")
|
||||
|
||||
null_endec((), '')
|
0
tests/test_primitive_data/test_object_identifier.py
Normal file
0
tests/test_primitive_data/test_object_identifier.py
Normal file
0
tests/test_primitive_data/test_object_type.py
Normal file
0
tests/test_primitive_data/test_object_type.py
Normal file
126
tests/test_primitive_data/test_octet_string.py
Normal file
126
tests/test_primitive_data/test_octet_string.py
Normal file
|
@ -0,0 +1,126 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Test Primitive Data Octet String
|
||||
--------------------------------
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import struct
|
||||
|
||||
from bacpypes.debugging import bacpypes_debugging, ModuleLogger, xtob
|
||||
from bacpypes.primitivedata import OctetString, Tag
|
||||
|
||||
# some debugging
|
||||
_debug = 0
|
||||
_log = ModuleLogger(globals())
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def octet_string_tag(x):
|
||||
"""Convert a hex string to an octet_string application tag."""
|
||||
if _debug: octet_string_tag._debug("octet_string_tag %r", x)
|
||||
|
||||
b = xtob(x)
|
||||
tag = Tag(Tag.applicationTagClass, Tag.octetStringAppTag, len(b), b)
|
||||
if _debug: octet_string_endec._debug(" - tag: %r", tag)
|
||||
|
||||
return tag
|
||||
|
||||
@bacpypes_debugging
|
||||
def octet_string_encode(obj):
|
||||
"""Encode an OctetString object into a tag."""
|
||||
if _debug: octet_string_encode._debug("octet_string_encode %r", obj)
|
||||
|
||||
tag = Tag()
|
||||
obj.encode(tag)
|
||||
if _debug: octet_string_endec._debug(" - tag: %r", tag)
|
||||
|
||||
return tag
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def octet_string_decode(tag):
|
||||
"""Decode an octet_string application tag into an octet string."""
|
||||
if _debug: octet_string_decode._debug("octet_string_decode %r", tag)
|
||||
|
||||
obj = OctetString(tag)
|
||||
if _debug: octet_string_decode._debug(" - obj: %r", obj)
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def octet_string_endec(x):
|
||||
"""Pass the value to OctetString, construct a tag from the hex string,
|
||||
and compare results of encode and decoding each other."""
|
||||
if _debug: octet_string_endec._debug("octet_string_endec %r", x)
|
||||
|
||||
tag = octet_string_tag(x)
|
||||
if _debug: octet_string_endec._debug(" - tag: %r, %r", tag, tag.tagData)
|
||||
|
||||
obj = OctetString(xtob(x))
|
||||
if _debug: octet_string_endec._debug(" - obj: %r, %r", obj, obj.value)
|
||||
|
||||
assert octet_string_encode(obj) == tag
|
||||
assert octet_string_decode(tag) == obj
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
class TestOctetString(unittest.TestCase):
|
||||
|
||||
def test_octet_string(self):
|
||||
if _debug: TestOctetString._debug("test_octet_string")
|
||||
|
||||
obj = OctetString()
|
||||
assert obj.value == xtob('')
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
OctetString(1)
|
||||
|
||||
def test_octet_string_octet_string(self):
|
||||
if _debug: TestOctetString._debug("test_octet_string_octet_string")
|
||||
|
||||
obj = OctetString(xtob('01'))
|
||||
assert obj.value == xtob('01')
|
||||
assert str(obj) == "OctetString(X'01')"
|
||||
|
||||
obj = OctetString(xtob('01020304'))
|
||||
assert obj.value == xtob('01020304')
|
||||
assert str(obj) == "OctetString(X'01020304')"
|
||||
|
||||
def test_octet_string_tag(self):
|
||||
if _debug: TestOctetString._debug("test_octet_string_tag")
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.octetStringAppTag, 1, xtob('00'))
|
||||
obj = OctetString(tag)
|
||||
assert obj.value == xtob('00')
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0, xtob(''))
|
||||
with self.assertRaises(ValueError):
|
||||
OctetString(tag)
|
||||
|
||||
tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
|
||||
with self.assertRaises(ValueError):
|
||||
OctetString(tag)
|
||||
|
||||
tag = Tag(Tag.openingTagClass, 0)
|
||||
with self.assertRaises(ValueError):
|
||||
OctetString(tag)
|
||||
|
||||
def test_octet_string_copy(self):
|
||||
if _debug: TestOctetString._debug("test_octet_string_copy")
|
||||
|
||||
obj1 = OctetString(xtob('01'))
|
||||
obj2 = OctetString(obj1)
|
||||
assert obj2.value == xtob('01')
|
||||
|
||||
def test_octet_string_endec(self):
|
||||
if _debug: TestOctetString._debug("test_octet_string_endec")
|
||||
|
||||
octet_string_endec('')
|
||||
octet_string_endec('01')
|
||||
octet_string_endec('0102')
|
||||
octet_string_endec('010203')
|
||||
octet_string_endec('01020304')
|
144
tests/test_primitive_data/test_real.py
Normal file
144
tests/test_primitive_data/test_real.py
Normal file
|
@ -0,0 +1,144 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Test Primitive Data Real
|
||||
------------------------
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import struct
|
||||
import math
|
||||
|
||||
from bacpypes.debugging import bacpypes_debugging, ModuleLogger, xtob
|
||||
from bacpypes.primitivedata import Real, Tag
|
||||
|
||||
# some debugging
|
||||
_debug = 0
|
||||
_log = ModuleLogger(globals())
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def real_tag(x):
|
||||
"""Convert a hex string to an real application tag."""
|
||||
if _debug: real_tag._debug("real_tag %r", x)
|
||||
|
||||
b = xtob(x)
|
||||
tag = Tag(Tag.applicationTagClass, Tag.realAppTag, len(b), b)
|
||||
if _debug: real_tag._debug(" - tag: %r", tag)
|
||||
|
||||
return tag
|
||||
|
||||
@bacpypes_debugging
|
||||
def real_encode(obj):
|
||||
"""Encode an Real object into a tag."""
|
||||
if _debug: real_encode._debug("real_encode %r", obj)
|
||||
|
||||
tag = Tag()
|
||||
obj.encode(tag)
|
||||
if _debug: real_encode._debug(" - tag: %r, %r", tag, tag.tagData)
|
||||
|
||||
return tag
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def real_decode(tag):
|
||||
"""Decode an real application tag into an real."""
|
||||
if _debug: real_decode._debug("real_decode %r", tag)
|
||||
|
||||
obj = Real(tag)
|
||||
if _debug: real_decode._debug(" - obj: %r, %r", obj, obj.value)
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def real_endec(v, x):
|
||||
"""Pass the value to Real, construct a tag from the hex string,
|
||||
and compare results of encode and decoding each other."""
|
||||
if _debug: real_endec._debug("real_endec %r %r", v, x)
|
||||
|
||||
tag = real_tag(x)
|
||||
if _debug: real_endec._debug(" - tag: %r, %r", tag, tag.tagData)
|
||||
|
||||
obj = Real(v)
|
||||
if _debug: real_endec._debug(" - obj: %r, %r", obj, obj.value)
|
||||
|
||||
assert real_encode(obj) == tag
|
||||
if _debug: real_endec._debug(" - tags match")
|
||||
|
||||
if math.isnan(v):
|
||||
assert math.isnan(real_decode(tag).value)
|
||||
if _debug: real_endec._debug(" - both NaN")
|
||||
else:
|
||||
assert real_decode(tag) == obj
|
||||
if _debug: real_endec._debug(" - objects match")
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
class TestReal(unittest.TestCase):
|
||||
|
||||
def test_real(self):
|
||||
if _debug: TestReal._debug("test_real")
|
||||
|
||||
obj = Real()
|
||||
assert obj.value == 0.0
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
Real("some string")
|
||||
|
||||
def test_real_real(self):
|
||||
if _debug: TestReal._debug("test_real_real")
|
||||
|
||||
obj = Real(1.0)
|
||||
assert obj.value == 1.0
|
||||
assert str(obj) == "Real(1)"
|
||||
|
||||
obj = Real(73.5)
|
||||
assert obj.value == 73.5
|
||||
assert str(obj) == "Real(73.5)"
|
||||
|
||||
def test_real_tag(self):
|
||||
if _debug: TestReal._debug("test_real_tag")
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.realAppTag, 1, xtob('3f800000'))
|
||||
obj = Real(tag)
|
||||
assert obj.value == 1.0
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0, xtob(''))
|
||||
with self.assertRaises(ValueError):
|
||||
Real(tag)
|
||||
|
||||
tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
|
||||
with self.assertRaises(ValueError):
|
||||
Real(tag)
|
||||
|
||||
tag = Tag(Tag.openingTagClass, 0)
|
||||
with self.assertRaises(ValueError):
|
||||
Real(tag)
|
||||
|
||||
def test_real_copy(self):
|
||||
if _debug: TestReal._debug("test_real_copy")
|
||||
|
||||
obj1 = Real(12)
|
||||
obj2 = Real(obj1)
|
||||
assert obj2.value == 12
|
||||
|
||||
def test_real_endec(self):
|
||||
if _debug: TestReal._debug("test_real_endec")
|
||||
|
||||
with self.assertRaises(struct.error):
|
||||
obj = Real(real_tag(''))
|
||||
|
||||
real_endec(0, '00000000')
|
||||
real_endec(1, '3f800000')
|
||||
real_endec(-1, 'bf800000')
|
||||
|
||||
real_endec(73.5, '42930000')
|
||||
|
||||
inf = float('inf')
|
||||
real_endec(inf, '7f800000')
|
||||
real_endec(-inf, 'ff800000')
|
||||
|
||||
nan = float('nan')
|
||||
real_endec(nan, '7fc00000')
|
0
tests/test_primitive_data/test_tag.py
Normal file
0
tests/test_primitive_data/test_tag.py
Normal file
0
tests/test_primitive_data/test_time.py
Normal file
0
tests/test_primitive_data/test_time.py
Normal file
138
tests/test_primitive_data/test_unsigned.py
Normal file
138
tests/test_primitive_data/test_unsigned.py
Normal file
|
@ -0,0 +1,138 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Test Primitive Data Unsigned
|
||||
----------------------------
|
||||
"""
|
||||
|
||||
import unittest
|
||||
|
||||
from bacpypes.debugging import bacpypes_debugging, ModuleLogger, xtob
|
||||
from bacpypes.primitivedata import Unsigned, Tag
|
||||
|
||||
# some debugging
|
||||
_debug = 0
|
||||
_log = ModuleLogger(globals())
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def unsigned_tag(x):
|
||||
"""Convert a hex string to an unsigned application tag."""
|
||||
if _debug: unsigned_tag._debug("unsigned_tag %r", x)
|
||||
|
||||
b = xtob(x)
|
||||
tag = Tag(Tag.applicationTagClass, Tag.unsignedAppTag, len(b), b)
|
||||
if _debug: unsigned_endec._debug(" - tag: %r", tag)
|
||||
|
||||
return tag
|
||||
|
||||
@bacpypes_debugging
|
||||
def unsigned_encode(obj):
|
||||
"""Encode an Unsigned object into a tag."""
|
||||
if _debug: unsigned_encode._debug("unsigned_encode %r", obj)
|
||||
|
||||
tag = Tag()
|
||||
obj.encode(tag)
|
||||
if _debug: unsigned_endec._debug(" - tag: %r", tag)
|
||||
|
||||
return tag
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def unsigned_decode(tag):
|
||||
"""Decode an unsigned application tag into an unsigned."""
|
||||
if _debug: unsigned_decode._debug("unsigned_decode %r", tag)
|
||||
|
||||
obj = Unsigned(tag)
|
||||
if _debug: unsigned_decode._debug(" - obj: %r", obj)
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
def unsigned_endec(v, x):
|
||||
"""Pass the value to Unsigned, construct a tag from the hex string,
|
||||
and compare results of encode and decoding each other."""
|
||||
if _debug: unsigned_endec._debug("unsigned_endec %r %r", v, x)
|
||||
|
||||
tag = unsigned_tag(x)
|
||||
if _debug: unsigned_endec._debug(" - tag: %r, %r", tag, tag.tagData)
|
||||
|
||||
obj = Unsigned(v)
|
||||
if _debug: unsigned_endec._debug(" - obj: %r, %r", obj, obj.value)
|
||||
|
||||
assert unsigned_encode(obj) == tag
|
||||
assert unsigned_decode(tag) == obj
|
||||
|
||||
|
||||
@bacpypes_debugging
|
||||
class TestUnsigned(unittest.TestCase):
|
||||
|
||||
def test_unsigned(self):
|
||||
if _debug: TestUnsigned._debug("test_unsigned")
|
||||
|
||||
obj = Unsigned()
|
||||
assert obj.value == 0
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
Unsigned("some string")
|
||||
with self.assertRaises(TypeError):
|
||||
Unsigned(1.0)
|
||||
|
||||
def test_unsigned_int(self):
|
||||
if _debug: TestUnsigned._debug("test_unsigned_int")
|
||||
|
||||
obj = Unsigned(1)
|
||||
assert obj.value == 1
|
||||
assert str(obj) == "Unsigned(1)"
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
Unsigned(-1)
|
||||
|
||||
def test_unsigned_tag(self):
|
||||
if _debug: TestUnsigned._debug("test_unsigned_tag")
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.unsignedAppTag, 1, xtob('01'))
|
||||
obj = Unsigned(tag)
|
||||
assert obj.value == 1
|
||||
|
||||
tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0, xtob(''))
|
||||
with self.assertRaises(ValueError):
|
||||
Unsigned(tag)
|
||||
|
||||
tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
|
||||
with self.assertRaises(ValueError):
|
||||
Unsigned(tag)
|
||||
|
||||
tag = Tag(Tag.openingTagClass, 0)
|
||||
with self.assertRaises(ValueError):
|
||||
Unsigned(tag)
|
||||
|
||||
def test_unsigned_copy(self):
|
||||
if _debug: TestUnsigned._debug("test_unsigned_copy")
|
||||
|
||||
obj1 = Unsigned(12)
|
||||
obj2 = Unsigned(obj1)
|
||||
assert obj2.value == 12
|
||||
|
||||
def test_unsigned_endec(self):
|
||||
if _debug: TestUnsigned._debug("test_unsigned_endec")
|
||||
|
||||
# with self.assertRaises(IndexError):
|
||||
# obj = Unsigned(unsigned_tag(''))
|
||||
|
||||
unsigned_endec(0, '00')
|
||||
unsigned_endec(1, '01')
|
||||
unsigned_endec(127, '7f')
|
||||
unsigned_endec(128, '80')
|
||||
unsigned_endec(255, 'ff')
|
||||
|
||||
unsigned_endec(32767, '7fff')
|
||||
unsigned_endec(32768, '8000')
|
||||
|
||||
unsigned_endec(8388607, '7fffff')
|
||||
unsigned_endec(8388608, '800000')
|
||||
|
||||
unsigned_endec(2147483647, '7fffffff')
|
||||
unsigned_endec(2147483648, '80000000')
|
Loading…
Reference in New Issue
Block a user