Continue to update test to use unittest.

Replace custom class strstream with StringIO.
This commit is contained in:
Siu Kwan Lam 2012-07-21 20:27:14 -07:00
commit c68c5b0aac
2 changed files with 104 additions and 73 deletions

View file

@ -3,6 +3,8 @@
# Tests accessing of instruction operands.
from llvm.core import *
from StringIO import StringIO
import logging, unittest
m = None
@ -24,54 +26,54 @@ entry:
ret i32 %tmp3
}
"""
class strstream(object):
def __init__(self): pass
def read(self): return test_module
m = Module.from_assembly(strstream())
print("-"*60)
print(m)
print("-"*60)
test_func = m.get_function_named("test_func")
prod = m.get_function_named("prod")
class TestOperands(unittest.TestCase):
def test_operands(self):
m = Module.from_assembly(StringIO(test_module))
logging.debug("-"*60)
logging.debug(m)
logging.debug("-"*60)
#===----------------------------------------------------------------------===
# test operands
test_func = m.get_function_named("test_func")
prod = m.get_function_named("prod")
print()
i1 = test_func.basic_blocks[0].instructions[0]
i2 = test_func.basic_blocks[0].instructions[1]
print("Testing User.operand_count ..", end=' ')
if i1.operand_count == 3 and i2.operand_count == 2:
print("OK")
else:
print("FAIL")
#===----------------------------------------------------------------------===
# test operands
print("Testing User.operands ..", end=' ')
c1 = i1.operands[0] is prod
c2 = i1.operands[1] is test_func.args[0]
c3 = i1.operands[2] is test_func.args[1]
c4 = i2.operands[0] is i1
c5 = i2.operands[1] is test_func.args[2]
c6 = len(i1.operands) == 3
c7 = len(i2.operands) == 2
if c1 and c2 and c3 and c5 and c6 and c7:
print("OK")
else:
print("FAIL")
print()
#===----------------------------------------------------------------------===
# show test_function
i1 = test_func.basic_blocks[0].instructions[0]
i2 = test_func.basic_blocks[0].instructions[1]
logging.debug("Testing User.operand_count ..")
self.assertEqual(i1.operand_count, 3)
self.assertEqual(i2.operand_count, 2)
logging.debug("Testing User.operands ..")
self.assertIs(i1.operands[-1], prod)
self.assertIs(i1.operands[0], test_func.args[0])
self.assertIs(i1.operands[1], test_func.args[1])
self.assertIs(i2.operands[0], i1)
self.assertIs(i2.operands[1], test_func.args[2])
self.assertEqual(len(i1.operands), 3)
self.assertEqual(len(i2.operands), 2)
#===----------------------------------------------------------------------===
# show test_function
logging.debug("Examining test_function `test_test_func':")
idx = 1
for inst in test_func.basic_blocks[0].instructions:
logging.debug("Instruction #%d:", idx)
logging.debug(" operand_count = %d", inst.operand_count)
logging.debug(" operands:")
oidx = 1
for op in inst.operands:
logging.debug(" %d: %s", oidx, repr(op))
oidx += 1
idx += 1
if __name__ == '__main__':
unittest.main()
print("Examining test_function `test_test_func':")
idx = 1
for inst in test_func.basic_blocks[0].instructions:
print("Instruction #%d:" % (idx,))
print(" operand_count =", inst.operand_count)
print(" operands:")
oidx = 1
for op in inst.operands:
print(" %d: %s" % (oidx, repr(op)))
oidx += 1
idx += 1