Continue to update test to use unittest.
Replace custom class strstream with StringIO.
This commit is contained in:
parent
badac267cc
commit
c68c5b0aac
2 changed files with 104 additions and 73 deletions
|
|
@ -3,6 +3,8 @@
|
||||||
# Tests accessing of instruction operands.
|
# Tests accessing of instruction operands.
|
||||||
|
|
||||||
from llvm.core import *
|
from llvm.core import *
|
||||||
|
from StringIO import StringIO
|
||||||
|
import logging, unittest
|
||||||
|
|
||||||
m = None
|
m = None
|
||||||
|
|
||||||
|
|
@ -24,13 +26,13 @@ entry:
|
||||||
ret i32 %tmp3
|
ret i32 %tmp3
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
class strstream(object):
|
|
||||||
def __init__(self): pass
|
class TestOperands(unittest.TestCase):
|
||||||
def read(self): return test_module
|
def test_operands(self):
|
||||||
m = Module.from_assembly(strstream())
|
m = Module.from_assembly(StringIO(test_module))
|
||||||
print("-"*60)
|
logging.debug("-"*60)
|
||||||
print(m)
|
logging.debug(m)
|
||||||
print("-"*60)
|
logging.debug("-"*60)
|
||||||
|
|
||||||
test_func = m.get_function_named("test_func")
|
test_func = m.get_function_named("test_func")
|
||||||
prod = m.get_function_named("prod")
|
prod = m.get_function_named("prod")
|
||||||
|
|
@ -38,40 +40,40 @@ prod = m.get_function_named("prod")
|
||||||
#===----------------------------------------------------------------------===
|
#===----------------------------------------------------------------------===
|
||||||
# test operands
|
# test operands
|
||||||
|
|
||||||
print()
|
|
||||||
i1 = test_func.basic_blocks[0].instructions[0]
|
i1 = test_func.basic_blocks[0].instructions[0]
|
||||||
i2 = test_func.basic_blocks[0].instructions[1]
|
i2 = test_func.basic_blocks[0].instructions[1]
|
||||||
print("Testing User.operand_count ..", end=' ')
|
logging.debug("Testing User.operand_count ..")
|
||||||
if i1.operand_count == 3 and i2.operand_count == 2:
|
|
||||||
print("OK")
|
|
||||||
else:
|
|
||||||
print("FAIL")
|
|
||||||
|
|
||||||
print("Testing User.operands ..", end=' ')
|
self.assertEqual(i1.operand_count, 3)
|
||||||
c1 = i1.operands[0] is prod
|
self.assertEqual(i2.operand_count, 2)
|
||||||
c2 = i1.operands[1] is test_func.args[0]
|
|
||||||
c3 = i1.operands[2] is test_func.args[1]
|
logging.debug("Testing User.operands ..")
|
||||||
c4 = i2.operands[0] is i1
|
|
||||||
c5 = i2.operands[1] is test_func.args[2]
|
self.assertIs(i1.operands[-1], prod)
|
||||||
c6 = len(i1.operands) == 3
|
self.assertIs(i1.operands[0], test_func.args[0])
|
||||||
c7 = len(i2.operands) == 2
|
self.assertIs(i1.operands[1], test_func.args[1])
|
||||||
if c1 and c2 and c3 and c5 and c6 and c7:
|
self.assertIs(i2.operands[0], i1)
|
||||||
print("OK")
|
self.assertIs(i2.operands[1], test_func.args[2])
|
||||||
else:
|
self.assertEqual(len(i1.operands), 3)
|
||||||
print("FAIL")
|
self.assertEqual(len(i2.operands), 2)
|
||||||
print()
|
|
||||||
|
|
||||||
#===----------------------------------------------------------------------===
|
#===----------------------------------------------------------------------===
|
||||||
# show test_function
|
# show test_function
|
||||||
|
|
||||||
print("Examining test_function `test_test_func':")
|
logging.debug("Examining test_function `test_test_func':")
|
||||||
|
|
||||||
idx = 1
|
idx = 1
|
||||||
for inst in test_func.basic_blocks[0].instructions:
|
for inst in test_func.basic_blocks[0].instructions:
|
||||||
print("Instruction #%d:" % (idx,))
|
logging.debug("Instruction #%d:", idx)
|
||||||
print(" operand_count =", inst.operand_count)
|
logging.debug(" operand_count = %d", inst.operand_count)
|
||||||
print(" operands:")
|
logging.debug(" operands:")
|
||||||
oidx = 1
|
oidx = 1
|
||||||
for op in inst.operands:
|
for op in inst.operands:
|
||||||
print(" %d: %s" % (oidx, repr(op)))
|
logging.debug(" %d: %s", oidx, repr(op))
|
||||||
oidx += 1
|
oidx += 1
|
||||||
idx += 1
|
idx += 1
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,10 @@ from llvm.core import *
|
||||||
from llvm.passes import *
|
from llvm.passes import *
|
||||||
from llvm.ee import *
|
from llvm.ee import *
|
||||||
|
|
||||||
|
from StringIO import StringIO
|
||||||
|
|
||||||
|
import logging, unittest
|
||||||
|
|
||||||
# A helper class.
|
# A helper class.
|
||||||
class strstream(object):
|
class strstream(object):
|
||||||
def __init__(self, s):
|
def __init__(self, s):
|
||||||
|
|
@ -35,9 +39,18 @@ entry:
|
||||||
ret i32 %tmp
|
ret i32 %tmp
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
m = Module.from_assembly(strstream(asm))
|
|
||||||
print("-"*72)
|
class TestPasses(unittest.TestCase):
|
||||||
print(m)
|
def test_passes(self):
|
||||||
|
m = Module.from_assembly(StringIO(asm))
|
||||||
|
logging.debug("-"*72)
|
||||||
|
logging.debug(m)
|
||||||
|
|
||||||
|
fn_test1 = m.get_function_named('test1')
|
||||||
|
fn_test2 = m.get_function_named('test2')
|
||||||
|
|
||||||
|
original_test1 = str(fn_test1)
|
||||||
|
original_test2 = str(fn_test2)
|
||||||
|
|
||||||
# Let's run a module-level inlining pass. First, create a pass manager.
|
# Let's run a module-level inlining pass. First, create a pass manager.
|
||||||
pm = PassManager.new()
|
pm = PassManager.new()
|
||||||
|
|
@ -51,13 +64,21 @@ pm.add( PASS_FUNCTION_INLINING )
|
||||||
# Run it!
|
# Run it!
|
||||||
pm.run(m)
|
pm.run(m)
|
||||||
|
|
||||||
|
|
||||||
# Done with the pass manager.
|
# Done with the pass manager.
|
||||||
del pm
|
del pm
|
||||||
|
|
||||||
# Print the result. Note the change in @test2.
|
# Print the result. Note the change in @test2.
|
||||||
print("-"*72)
|
logging.debug("-"*72)
|
||||||
print(m)
|
logging.debug(m)
|
||||||
|
|
||||||
|
# Make sure test2 is inlined
|
||||||
|
self.assertNotEqual(str(fn_test2).strip(), original_test2.strip())
|
||||||
|
|
||||||
|
bb_entry = fn_test2.basic_blocks[0]
|
||||||
|
|
||||||
|
self.assertEqual(len(bb_entry.instructions), 1)
|
||||||
|
self.assertEqual(bb_entry.instructions[0].opcode_name, 'ret')
|
||||||
|
|
||||||
# Let's run a DCE pass on the the function 'test1' now. First create a
|
# Let's run a DCE pass on the the function 'test1' now. First create a
|
||||||
# function pass manager.
|
# function pass manager.
|
||||||
|
|
@ -73,5 +94,13 @@ fpm.add( PASS_AGGRESSIVE_DCE )
|
||||||
fpm.run( m.get_function_named('test1') )
|
fpm.run( m.get_function_named('test1') )
|
||||||
|
|
||||||
# Print the result. Note the change in @test1.
|
# Print the result. Note the change in @test1.
|
||||||
print("-"*72)
|
logging.debug("-"*72)
|
||||||
print(m)
|
logging.debug(m)
|
||||||
|
|
||||||
|
# Make sure test1 is modified
|
||||||
|
self.assertNotEqual(str(fn_test1).strip(), original_test1.strip())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue