def test_divide_zero(self): try: apply_op(NumOp.DIV, cast(NumericalValue, IntegerValue(1)), cast(NumericalValue, IntegerValue(0))) self.fail() except MathError: pass
def test_multiple_extend_undef(self): e = Environment() e.extend("width", IntegerValue(3)) e.extend("width", IntegerValue(6)) self.assertEqual(6, cast(IntegerValue, e.get_val("width")).value) e.undef("width") self.assertEqual(3, cast(IntegerValue, e.get_val("width")).value)
def test_overwrite(self): m = Memory() l1 = m.get_fresh_loc() m.write(l1, IntegerValue(22)) m.write(l1, IntegerValue(23)) v: Value = m.read(l1) self.assertTrue(isinstance(v, IntegerValue)) self.assertEqual(v.value, 23)
def test_simple_func_divide(self): e = Evaluator() p = ast.Program( ast.Body([ ast.Loader(ast.Var("source"), ast.Source("https://covid-api.com/api/reports")), ast.Assigner(ast.Declare(ast.Type(Types.NUMBER), ast.Var("count")), ast.Value(IntegerValue(20))), ast.Trigger(ast.Var("source"), ast.MathFuncs([ast.SimpleFunc( ast.Var("count"), ast.Operand(ConcreteNumOp.DIV), ast.Value(IntegerValue(2)))])), ])) code, err = e.evaluate(p, duration=7000) self.assertEqual(0, code) self.assertTrue(cast(IntegerValue, e.env.get_val("count")).value < 20)
def example_1() -> ast.Program: """ Instantiates program example 1 as a native AST """ return ast.Program( ast.Body([ # source = live remote "www.coviddata.com/stream" ast.Loader(ast.Var("source"), ast.Source("www.coviddata.com/stream")), # map source "case_date" to number date ast.Mapper(ast.Var("source"), "case_date", ast.Declare(ast.Type(Types.NUMBER), ast.Var("date"))), # number count = 0 ast.Assigner(ast.Declare(ast.Type(Types.NUMBER), ast.Var("count")), ast.Value(IntegerValue(0))), # on new data from source count++ ast.Trigger(ast.Var("source"), ast.MathFuncs([ast.Increment(ast.Var( "count"))])), # plot xy date age titled age_graph ast.Plotter(ast.Graph(ScatterXYGraph()), ast.VarAxis(ast.Var("date")), ast.VarAxis(ast.Var("age")), "age_graph"), # plot line xy date log(count) titled cases_log ast.Plotter(ast.Graph(LineXYGraph()), ast.VarAxis(ast.Var("date")), ast.FuncAxis(ast.BuiltinFunc(NumFunction.LOG, ast.Var( "count"))), "age_graph"), ]))
def test_set_no_extend(self): e = Environment() try: e.set_val("width", IntegerValue(5)) self.fail() except UndefinedVariableError: pass
def apply_op(o: NumOp, a: NumericalValue, b: NumericalValue) -> NumericalValue: if o in operations: result = operations[o](a.value, b.value) if type(a) is IntegerValue and type(b) is IntegerValue: return cast(NumericalValue, IntegerValue(int(result))) else: return cast(NumericalValue, FloatValue(float(result))) else: raise NonExhaustiveTypeCaseError()
def test_undef(self): e = Environment() e.extend("width", IntegerValue(3)) self.assertEqual(3, cast(IntegerValue, e.get_val("width")).value) e.undef("width") try: e.get_val("width") self.fail() except UndefinedVariableError: pass
def test_memory_limit(self): m = Memory() for i in range(Memory.MAX_VALUES): l = m.get_fresh_loc() m.write(l, IntegerValue(1)) try: m.get_fresh_loc() self.fail() except OutOfMemoryError: pass
def test_valid_source(self): e = Evaluator() p = ast.Program( ast.Body([ ast.Loader(ast.Var("source"), ast.Source("https://covid-api.com/api/reports")), ast.Assigner(ast.Declare(ast.Type(Types.NUMBER), ast.Var("count")), ast.Value(IntegerValue(0))), ast.Trigger(ast.Var("source"), ast.MathFuncs([ast.Increment(ast.Var( "count"))])), ])) code, err = e.evaluate(p, duration=4000) self.assertEqual(0, code)
def test_pow_overflow(self): e = Evaluator() p = ast.Program( ast.Body([ ast.Loader(ast.Var("source"), ast.Source("https://covid-api.com/api/reports")), ast.Assigner(ast.Declare(ast.Type(Types.NUMBER), ast.Var("count")), ast.Value(FloatValue(1.1))), ast.Trigger(ast.Var("source"), ast.MathFuncs([ast.SimpleFunc( ast.Var("count"), ast.Operand(ConcreteNumOp.EXP), ast.Value(IntegerValue(2)))])), ])) code, err = e.evaluate(p, duration=10000) self.assertNotEqual(0, code)
def test_minus(self): result: NumericalValue = apply_op( NumOp.MINUS, cast(NumericalValue, IntegerValue(1)), cast(NumericalValue, IntegerValue(2))) self.assertTrue(type(result) is IntegerValue) self.assertTrue(result.equals(IntegerValue(-1)))
def test_pow(self): result: NumericalValue = apply_op( NumOp.EXP, cast(NumericalValue, IntegerValue(2)), cast(NumericalValue, IntegerValue(3))) self.assertTrue(type(result) is IntegerValue) self.assertTrue(result.equals(IntegerValue(8)))
def apply_qk(f: str, x: NumericalValue) -> NumericalValue: result = quick_func[f](x.value) if type(x) is IntegerValue: return cast(NumericalValue, IntegerValue(int(result))) else: return cast(NumericalValue, FloatValue(float(result)))
def test_log_domain(self): try: apply_fn(NumFunction.LOG, cast(NumericalValue, IntegerValue(-1))) self.fail() except MathError: pass
def test_basic(self): e = Environment() e.extend("width", IntegerValue(3)) e.extend("height", IntegerValue(4)) self.assertEqual(3, cast(IntegerValue, e.get_val("width")).value) self.assertEqual(4, cast(IntegerValue, e.get_val("height")).value)
def test_basic_set(self): e = Environment() e.extend("width", IntegerValue(3)) self.assertEqual(3, cast(IntegerValue, e.get_val("width")).value) e.set_val("width", IntegerValue(8)) self.assertEqual(8, cast(IntegerValue, e.get_val("width")).value)
def _rand_mem_() -> Value: # simulate uninitialized memory return IntegerValue(random.randint(0, 100))
def test_plus_with_float(self): result: NumericalValue = apply_op( NumOp.PLUS, cast(NumericalValue, IntegerValue(1)), cast(NumericalValue, FloatValue(2.0))) self.assertTrue(type(result) is FloatValue) self.assertTrue(result.equals(FloatValue(3.0)))