summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenzo Colitti <lorenzo@google.com>2016-02-24 18:00:42 +0000
committerandroid-build-merger <android-build-merger@google.com>2016-02-24 18:00:42 +0000
commit2029734a224a8e99691b7731d1195688167a6687 (patch)
tree205b97234177ae1ac4f8650eebd1ca33343feb7e
parentac97673f901dd2aef48e5daa56f3c00642ad21c3 (diff)
parentf3713292851ca8d234a84d2ac54f8e78876bba88 (diff)
downloadtests-2029734a224a8e99691b7731d1195688167a6687.tar.gz
Support checking structs for equality.
am: f371329285 * commit 'f3713292851ca8d234a84d2ac54f8e78876bba88': Support checking structs for equality.
-rw-r--r--net/test/cstruct.py31
-rwxr-xr-xnet/test/cstruct_test.py60
2 files changed, 81 insertions, 10 deletions
diff --git a/net/test/cstruct.py b/net/test/cstruct.py
index 62f0823..91cd72e 100644
--- a/net/test/cstruct.py
+++ b/net/test/cstruct.py
@@ -57,7 +57,7 @@ def CalcNumElements(fmt):
return len(elements)
-def Struct(name, fmt, fields, substructs={}):
+def Struct(name, fmt, fieldnames, substructs={}):
"""Function that returns struct classes."""
class Meta(type):
@@ -77,12 +77,12 @@ def Struct(name, fmt, fields, substructs={}):
# Name of the struct.
_name = name
# List of field names.
- _fields = fields
+ _fieldnames = fieldnames
# Dict mapping field indices to nested struct classes.
_nested = {}
- if isinstance(_fields, str):
- _fields = _fields.split(" ")
+ if isinstance(_fieldnames, str):
+ _fieldnames = _fieldnames.split(" ")
# Parse fmt into _format, converting any S format characters to "XXs",
# where XX is the length of the struct type's packed representation.
@@ -121,14 +121,14 @@ def Struct(name, fmt, fields, substructs={}):
self._Parse(values)
else:
# Initializing from a tuple.
- if len(values) != len(self._fields):
- raise TypeError("%s has exactly %d fields (%d given)" %
- (self._name, len(self._fields), len(values)))
+ if len(values) != len(self._fieldnames):
+ raise TypeError("%s has exactly %d fieldnames (%d given)" %
+ (self._name, len(self._fieldnames), len(values)))
self._SetValues(values)
def _FieldIndex(self, attr):
try:
- return self._fields.index(attr)
+ return self._fieldnames.index(attr)
except ValueError:
raise AttributeError("'%s' has no attribute '%s'" %
(self._name, attr))
@@ -143,6 +143,15 @@ def Struct(name, fmt, fields, substructs={}):
def __len__(cls):
return cls._length
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __eq__(self, other):
+ return (isinstance(other, self.__class__) and
+ self._name == other._name and
+ self._fieldnames == other._fieldnames and
+ self._values == other._values)
+
@staticmethod
def _MaybePackStruct(value):
if hasattr(value, "__metaclass__"):# and value.__metaclass__ == Meta:
@@ -156,12 +165,14 @@ def Struct(name, fmt, fields, substructs={}):
def __str__(self):
def FieldDesc(index, name, value):
- if isinstance(value, str) and any (c not in string.printable for c in value):
+ if isinstance(value, str) and any(
+ c not in string.printable for c in value):
value = value.encode("hex")
return "%s=%s" % (name, value)
descriptions = [
- FieldDesc(i, n, v) for i, (n, v) in enumerate(zip(self._fields, self._values))]
+ FieldDesc(i, n, v) for i, (n, v) in
+ enumerate(zip(self._fieldnames, self._values))]
return "%s(%s)" % (self._name, ", ".join(descriptions))
diff --git a/net/test/cstruct_test.py b/net/test/cstruct_test.py
new file mode 100755
index 0000000..2d5a408
--- /dev/null
+++ b/net/test/cstruct_test.py
@@ -0,0 +1,60 @@
+#!/usr/bin/python
+#
+# Copyright 2016 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import unittest
+
+import cstruct
+
+
+# These aren't constants, they're classes. So, pylint: disable=invalid-name
+TestStructA = cstruct.Struct("TestStructA", "=BI", "byte1 int2")
+TestStructB = cstruct.Struct("TestStructB", "=BI", "byte1 int2")
+
+
+class CstructTest(unittest.TestCase):
+
+ def CheckEquals(self, a, b):
+ self.assertEquals(a, b)
+ self.assertEquals(b, a)
+ assert a == b
+ assert b == a
+ assert not (a != b) # pylint: disable=g-comparison-negation,superfluous-parens
+ assert not (b != a) # pylint: disable=g-comparison-negation,superfluous-parens
+
+ def CheckNotEquals(self, a, b):
+ self.assertNotEquals(a, b)
+ self.assertNotEquals(b, a)
+ assert a != b
+ assert b != a
+ assert not (a == b) # pylint: disable=g-comparison-negation,superfluous-parens
+ assert not (b == a) # pylint: disable=g-comparison-negation,superfluous-parens
+
+ def testEqAndNe(self):
+ a1 = TestStructA((1, 2))
+ a2 = TestStructA((2, 3))
+ a3 = TestStructA((1, 2))
+ b = TestStructB((1, 2))
+ self.CheckNotEquals(a1, b)
+ self.CheckNotEquals(a2, b)
+ self.CheckNotEquals(a1, a2)
+ self.CheckNotEquals(a2, a3)
+ for i in [a1, a2, a3, b]:
+ self.CheckEquals(i, i)
+ self.CheckEquals(a1, a3)
+
+
+if __name__ == "__main__":
+ unittest.main()