def test_lambda_simple(self): tables = {} ret_ref = Flag() epochs = 8 resp = _parse_lambda_fallback( lambda step: cosine_decay(step, cycle_length=3750, init_lr=1e-3 + 1 if epochs > 2 else 1e-4), tables, ret_ref) self.assertIsInstance( resp, dict, "_parse_lambda_fallback should return a dictionary") self.assertEqual( {}, tables, "_parse_lambda_fallback should not have generated any tables for this lambda" ) self.assertIn('function', resp, "response should contain a function summary") self.assertEqual( r"cosine\_decay(step, cycle\_length=3750, init\_lr=1e{-}3 + 1 if epochs > 2 else 1e{-}4)", resp['function']) self.assertIn('kwargs', resp, "response should contain kwargs") self.assertIsInstance(resp['kwargs'], dict, "kwargs should be a dictionary") self.assertDictEqual({NoEscape('epochs'): NoEscape(r'\seqsplit{8}')}, resp['kwargs'])
def test_multi_lambda_different_refs(self): tables = {} ret_ref = Flag() other, resp = lambda x: np.log2(128) + x, _parse_lambda_fallback(lambda x: np.ceil(128) + x, tables, ret_ref) self.assertIsInstance(resp, dict, "_parse_lambda_fallback should return a dictionary") self.assertEqual({}, tables, "_parse_lambda_fallback should not have generated any tables for this lambda") self.assertIn('function', resp, "response should contain a function summary") self.assertEqual(r"np.ceil(128) + x", resp['function'])
def test_multi_lambda_same_fn(self): tables = {} ret_ref = Flag() other, resp = lambda x: x + 'x1', _parse_lambda_fallback(lambda x: x + 'x1', tables, ret_ref) self.assertIsInstance(resp, dict, "_parse_lambda_fallback should return a dictionary") self.assertEqual({}, tables, "_parse_lambda_fallback should not have generated any tables for this lambda") self.assertIn('function', resp, "response should contain a function summary") self.assertEqual(r"x + 'x1'", resp['function'])
def test_multi_lambda_different_args(self): tables = {} ret_ref = Flag() resp, other = _parse_lambda_fallback(lambda x: x + 5, tables, ret_ref), lambda y: y + 5 self.assertIsInstance(resp, dict, "_parse_lambda_fallback should return a dictionary") self.assertEqual({}, tables, "_parse_lambda_fallback should not have generated any tables for this lambda") self.assertIn('function', resp, "response should contain a function summary") self.assertEqual(r"x + 5", resp['function'])
def test_nested_lambda_different_strings(self): tables = {} ret_ref = Flag() resp = _parse_lambda_fallback(lambda x, y: x(lambda x, y: x(y) + 'x' + 'y') + 'x', tables, ret_ref) self.assertIsInstance(resp, dict, "_parse_lambda_fallback should return a dictionary") self.assertEqual({}, tables, "_parse_lambda_fallback should not have generated any tables for this lambda") self.assertIn('function', resp, "response should contain a function summary") self.assertEqual(r"x(lambda x, y: x(y) + 'x' + 'y') + 'x'", resp['function'])
def test_lambda_inlining(self): tables = {} ret_ref = Flag() resp = _parse_lambda_fallback( lambda a, b=[0, 1, 2, 3], c={'x': "it's"}: b[0] + a * c['x'] - {0 for j in range(5)}, tables, ret_ref) self.assertIsInstance(resp, dict, "_parse_lambda_fallback should return a dictionary") self.assertEqual({}, tables, "_parse_lambda_fallback should not have generated any tables for this lambda") self.assertIn('function', resp, "response should contain a function summary") self.assertEqual(r"b{[}0{]} + a * c{[}'x'{]} {-} \{0 for j in range(5)\}", resp['function'])