def test_eq(self): v1 = symexp('10 != 5') v2 = symexp('2 * 5 != 3 + 2') self.assertTrue(v1 == v2) v1 = symexp('foo < bar') v2 = symexp('bar >= foo') self.assertTrue(v1 == v2)
def test_layered(self): v1 = symexp('(1595 == 1595) == (47 == 2)') self.assertTrue(v1.reduce() == 0) v1 = symexp('(foo == foo) == (bar == bar)') self.assertTrue(v1.reduce() == 1) v2 = symexp('(foo == foo) & (bar == bar)') self.assertTrue(v2.reduce() == 1) self.assertTrue(v1.reduce() == v2.reduce())
def test_symboliks_cache_walktree(self): s = symexp('x + 30') solved1 = s.solve() self.assertIsNotNone(s.cache.get('solve')) def swapx(path, sym, ctx): if sym.symtype == SYMT_VAR and sym.name == 'x': return Var('y', 4) s = s.walkTree(swapx) # check the partial cache clear self.assertIsNone(s.cache.get('solve')) self.assertIsNone(s.kids[0].cache.get('solve')) self.assertIsNotNone(s.kids[1].cache.get('solve')) solved2 = s.solve() self.assertEqual(str(s), '(y + 30)') self.assertNotEqual(solved1, solved2) # be *really* mean and manually slice a kid # then confirm that the cache still hits.. s.kids[0] = Var('x', 4) self.assertEqual(s.solve(), solved2)
def test_symboliks_cache_walktree(self): s = symexp('x + 30') solved1 = s.solve() self.assertIsNotNone(s.cache.get('solve')) def swapx(path,sym,ctx): if sym.symtype == SYMT_VAR and sym.name == 'x': return Var('y',4) s = s.walkTree(swapx) # check the partial cache clear self.assertIsNone(s.cache.get('solve')) self.assertIsNone(s.kids[0].cache.get('solve')) self.assertIsNotNone(s.kids[1].cache.get('solve')) solved2 = s.solve() self.assertEqual(str(s),'(y + 30)') self.assertNotEqual(solved1,solved2) # be *really* mean and manually slice a kid # then confirm that the cache still hits.. s.kids[0] = Var('x',4) self.assertEqual(s.solve(), solved2)
def test_symboliks_cache_solve_vars(self): s = symexp('x + 20') self.assertEqual(s.solve(vals={'x':10}), 30) self.assertEqual(s.solve(vals={'x':20}), 40) self.assertIsNone(s.cache.get('solve')) s.solve() self.assertIsNotNone(s.cache.get('solve'))
def test_symboliks_cache_solve_vars(self): s = symexp('x + 20') self.assertEqual(s.solve(vals={'x': 10}), 30) self.assertEqual(s.solve(vals={'x': 20}), 40) self.assertIsNone(s.cache.get('solve')) s.solve() self.assertIsNotNone(s.cache.get('solve'))
def test_update(self): v1 = symexp('foo * bar > bar << baz') self.assertFalse(v1.isDiscrete()) emu = MockEmulator(MockVw()) emu.setSymVariable('foo', Const(5, emu.__width__)) emu.setSymVariable('bar', Const(2, emu.__width__)) emu.setSymVariable('baz', Const(6, emu.__width__)) v1 = v1.update(emu) self.assertTrue(v1.isDiscrete(emu)) self.assertTrue(v1.solve(emu) == 0)
def test_reduce(self): s1 = '((0 & ((foo * 4) >> 2)) == 0)' v1 = symexp('((0 & ((foo * 4) >> 2)) == 0)') self.assertTrue(s1 == str(v1)) self.assertReduce(s1, '1') self.assertTrue(v1.getWidth() == 4) # default width s2 = '((((foo * bar) * baz) + (131 | 40)) == (((foo * bar) * baz) + 171))' v2 = symexp(s2) self.assertTrue(s2 == str(v2)) self.assertTrue(v2.reduce() == 1) self.assertTrue(v2.getWidth() == 4) # default width s3 = '(((((foo - bar) + bar) + (131 | 40)) - 171) == foo)' v3 = symexp(s3) self.assertTrue(s3 == str(v3)) self.assertTrue(v3.reduce() == 1) self.assertTrue(v3.getWidth() == 4) # default width s4 = 'foo - (foo + 3 ** 4) == biz / bar + boo' v4 = symexp(s4) self.assertTrue(v4.reduce() != 0)
def test_walktree(self): v1 = symexp('((foo ** 2) << 1) + (bar * 3) - 4 > 0') def walker(path, kid, ctx): order.append(kid) order = [] v1.walkTree(walker, ctx=order) correct = [ 'foo', '2', '(foo ** 2)', '1', '((foo ** 2) << 1)', 'bar', '3', '(bar * 3)', '(((foo ** 2) << 1) + (bar * 3))', '4', '((((foo ** 2) << 1) + (bar * 3)) - 4)', '0', '(((((foo ** 2) << 1) + (bar * 3)) - 4) > 0)' ] if len(order) != len(correct): self.fail('test_walktree, visit produced unexpected results') for i in xrange(len(order)): self.assertTrue(str(order[i]) == correct[i])
def test_rev(self): v1 = symexp('4 < 5') v2 = symexp('4 >= 5') self.assertTrue(v1.isDiscrete()) self.assertTrue(v2.isDiscrete()) self.assertTrue(str(v1.reverse()) == str(v2)) v1 = symexp('foo > bar') v2 = symexp('foo <= bar') self.assertFalse(v1.isDiscrete()) self.assertFalse(v2.isDiscrete()) self.assertTrue(str(v1.reverse()) == str(v2)) v1 = symexp('foo == bar') v2 = symexp('foo > bar') self.assertFalse(v1.isDiscrete()) self.assertFalse(v2.isDiscrete()) self.assertFalse(str(v1.reverse()) == str(v2))
def test_perf(self): v1 = symexp('biz = bar ** foo + baz') v1.reduce()
def xpandrules(rules): reducers = [] for symtmp, reducer in rules: for symtmp in variants(symexp(symtmp)): reducers.append((symtmp, reducer)) return reducers
''' Apply the current set of operator reducers to the given SymbolikBase. Sym *must* be an instance of the Operator(SymbolikBase) class.. ''' if not reducers.get(sym.symtype): return for symtmp, reducer in reducers.get(sym.symtype): m = ismatch(sym, symtmp) if m != None: #print 'MATCH',str(symtmp) ret = reducer(m, emu=emu) # do this to much simplify reducers... if type(ret) in (int, long): ret = Const(ret, sym.getWidth()) return ret if __name__ == '__main__': import sys for argv in sys.argv[1:]: sym = symexp(argv) print('== %s' % str(sym)) print(' repr: %s' % (repr(sym), )) print(' solve: 0x%.8x' % (sym.solve())) red = sym.reduce(foo=True) print(' reduc: %s' % (str(red), )) print(' red repr: %s' % (repr(red), )) print(' red solve: 0x%.8x' % (red.solve()))
def assertNotReduce(self, s1, s2): sym1 = symexp(s1).reduce() sym2 = symexp(s2) self.assertNotEqual(str(sym1),str(sym2))
def reduceoper(sym,emu=None): ''' Apply the current set of operator reducers to the given SymbolikBase. Sym *must* be an instance of the Operator(SymbolikBase) class.. ''' if not reducers.get(sym.symtype): return for symtmp,reducer in reducers.get(sym.symtype): m = ismatch(sym,symtmp) if m != None: #print 'MATCH',str(symtmp) ret = reducer(m,emu=emu) # do this to much simplify reducers... if type(ret) in (int,long): ret = Const(ret,sym.getWidth()) return ret if __name__ == '__main__': import sys for argv in sys.argv[1:]: sym = symexp(argv) print('== %s' % str(sym)) print(' repr: %s' % (repr(sym),)) print(' solve: 0x%.8x' % (sym.solve())) red = sym.reduce(foo=True) print(' reduc: %s' % (str(red),)) print(' red repr: %s' % (repr(red),)) print(' red solve: 0x%.8x' % (red.solve()))
def xpandrules(rules): reducers = [] for symtmp,reducer in rules: for symtmp in variants( symexp(symtmp) ): reducers.append( (symtmp,reducer) ) return reducers
def test_mixed(self): v1 = symexp('(foo ** 2) << (1 > 0)') emu = MockEmulator(MockVw()) emu.setSymVariable('foo', Const(5, emu.__width__)) v1 = v1.update(emu).reduce() self.assertTrue(v1 == 50)
def assertTruth(self, s1): v1 = symexp(s1).solve() self.assertTrue(v1 == 1)
def assertNotReduce(self, s1, s2): sym1 = symexp(s1).reduce() sym2 = symexp(s2) self.assertNotEqual(str(sym1), str(sym2))
def assertReduce(self, s1, s2): sym1 = symexp(s1).reduce(foo=True) sym2 = symexp(s2) self.assertEqual(str(sym1), str(sym2))