def test_get_return(self): fn = FunctionRef("baz", 1, "bazFn") argref, returntype = fn.get_return() self.assertIsNone(argref) self.assertEqual(returntype, types.NoneType) retval = ArgRef(fn, "") retval.add_sample(1) argref, returntype = fn.get_return() self.assertIs(argref, ArgRef(fn, "")) self.assertIs(returntype, int)
def test_arity(self): # e.g., def square(n): # return n * n # arity(square) = 1 fn = FunctionRef("", 1, "square") squarearg = ArgRef(fn, "n") self.assertEqual(fn.arity(), 1)
def test_get_type_prob(self): arg3 = ArgRef(self.fn, "arg3") arg3.add_sample(1) type_dict = arg3.get_type_prob() self.assertEqual(len(type_dict), 1) self.assertAlmostEqual(type_dict[int], 1.0) arg3.add_sample(True) type_dict = arg3.get_type_prob() self.assertEqual(len(type_dict), 2) self.assertAlmostEqual(type_dict[int], 0.5) self.assertAlmostEqual(type_dict[bool], 0.5)
def test_get_type(self): arg1 = ArgRef(self.fn, "arg1") self.assertEqual(arg1.get_type(), types.NoneType) arg1.add_sample(1) self.assertEqual(arg1.get_type(), int) arg1.add_sample(True) self.assertEqual(arg1.get_type(), TaggedUnion([int, bool])) arg1.add_sample([1, 2, 3]) expected_type = TaggedUnion([ParameterizedList([int]), int, bool]) computed_type = arg1.get_type() self.assertEqual(computed_type, expected_type) # Test the case where we non-equal directories. arg2 = ArgRef(self.fn, "arg2") arg2.add_sample(Foo()) self.assertEqual(arg2.get_type(), Foo) # Observing another instance of Foo doesn't change the type: arg2.add_sample(Foo()) self.assertEqual(arg2.get_type(), Foo) # However, changing Foo's dir should change things baz = Foo() baz.baz = "asdf" arg2.add_sample(baz) self.assertNotEqual(arg2.get_type(), Foo)
def add_sample(self): arg0 = ArgRef(self.fn, "arg0") arg0.add_sample(1) self.assertEqual(len(arg0.samples), 1)
def test_signaturenature(self): import types fn = FunctionRef("", -1, "map") arg1 = ArgRef(fn, "fn") arg2 = ArgRef(fn, "coll") retval = ArgRef(fn, "") arg1.add_sample(lambda x: x*x) arg2.add_sample([1,2,3]) retval.add_sample([1,4,9]) fn.set_signaturenature() self.assertEqual(fn.signature[""], (-1, ParameterizedList([int]))) self.assertEqual(fn.signature["fn"], (0, types.FunctionType)) self.assertEqual(fn.signature["coll"], (1, ParameterizedList([int])))
def test_get_num_samples(self): fn = FunctionRef("bar", 1, "barFn") arg1 = ArgRef(fn, "arg1") retval = ArgRef(fn, "") # Pretend we observed an input, but an exception was thrown. arg1.add_sample(True) self.assertEqual(fn.get_num_samples(), 1) # Observing an output shouldn't change anything retval.add_sample("asdf") self.assertEqual(fn.get_num_samples(), 1) # Observing a successful run should increment by one. arg1.add_sample(False) retval.add_sample("foo") self.assertEqual(fn.get_num_samples(), 2)