Commit 687c10d9 authored by Iustin Pop's avatar Iustin Pop
Browse files

Generalize the OpCode-should-be-in-mcpu test



Currently, the unittest TestDispatchTable in mcpu unittest does a
hard-coded approach to test whether an opcode should be included or
not in the mcpu.Processor dispatch table. This is not flexible, so we
replace it with two changes:

- first, we do not return the base OpCode in opcodes.OP_MAPPING;
  rationale being that we shouldn't need to serialise or deserialise
  this opcode during normal operation
- second, we add a new attribute WITH_LU (other name suggestions
  welcome) to the base opcode set to True, and we explicitly set it to
  False in OpTestDummy, thus automatic skipping of such LUs in the
  unittest (and in other places)

To correct for the new behaviour, the tests in the opcode unittests
are updated to include OpCode explicitly.

Note: we also replace assert_ with assertTrue in the affected unittest
(as assert_ is deprecated in 2.7).
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent bde65914
#
#
# Copyright (C) 2006, 2007, 2008, 2009, 2010 Google Inc.
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -289,12 +289,15 @@ class OpCode(BaseOpCode):
method for details).
@cvar OP_PARAMS: List of opcode attributes, the default values they should
get if not already defined, and types they must match.
@cvar WITH_LU: Boolean that specifies whether this should be included in
mcpu's dispatch table
@ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
the check steps
@ivar priority: Opcode priority for queue
"""
OP_ID = "OP_ABSTRACT"
WITH_LU = True
OP_PARAMS = [
("dry_run", None, ht.TMaybeBool),
("debug_level", None, ht.TOr(ht.TNone, ht.TPositiveInt)),
......@@ -1271,6 +1274,7 @@ class OpTestDummy(OpCode):
("messages", ht.NoDefault, ht.NoType),
("fail", ht.NoDefault, ht.NoType),
]
WITH_LU = False
def _GetOpList():
......@@ -1281,7 +1285,7 @@ def _GetOpList():
"""
return [v for v in globals().values()
if (isinstance(v, type) and issubclass(v, OpCode) and
hasattr(v, "OP_ID"))]
hasattr(v, "OP_ID") and v is not OpCode)]
OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())
#!/usr/bin/python
#
# Copyright (C) 2009 Google Inc.
# Copyright (C) 2009, 2011 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -58,10 +58,10 @@ class TestLockAttemptTimeoutStrategy(unittest.TestCase):
class TestDispatchTable(unittest.TestCase):
def test(self):
for opcls in opcodes.OP_MAPPING.values():
if opcls is opcodes.OpCode or opcls is opcodes.OpTestDummy:
if not opcls.WITH_LU:
continue
self.assert_(opcls in mcpu.Processor.DISPATCH_TABLE,
msg="%s missing handler class" % opcls)
self.assertTrue(opcls in mcpu.Processor.DISPATCH_TABLE,
msg="%s missing handler class" % opcls)
if __name__ == "__main__":
......
#!/usr/bin/python
#
# Copyright (C) 2010 Google Inc.
# Copyright (C) 2010, 2011 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -123,10 +123,10 @@ class TestOpcodes(unittest.TestCase):
def testParams(self):
supported_by_all = set(["debug_level", "dry_run", "priority"])
self.assert_(opcodes.BaseOpCode not in opcodes.OP_MAPPING.values())
self.assert_(opcodes.OpCode in opcodes.OP_MAPPING.values())
self.assertTrue(opcodes.BaseOpCode not in opcodes.OP_MAPPING.values())
self.assertTrue(opcodes.OpCode not in opcodes.OP_MAPPING.values())
for cls in opcodes.OP_MAPPING.values():
for cls in opcodes.OP_MAPPING.values() + [opcodes.OpCode]:
all_slots = cls._all_slots()
self.assertEqual(len(set(all_slots) & supported_by_all), 3,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment