def test_null(self): program = "(null? 1)" self.assertEvaluatesTo(program, Boolean(False)) program = "(null? '())" self.assertEvaluatesTo(program, Boolean(True)) program = "(null? '(1 2))" self.assertEvaluatesTo(program, Boolean(False))
def equality(arguments): for argument in arguments: if not isinstance(argument, Number): raise SchemeTypeError("Numerical equality test is only defined for numbers, " "you gave me %s." % argument.__class__) if argument != arguments[0]: return Boolean(False) return Boolean(True)
def inexact(arguments): check_argument_number('inexact?', arguments, 1, 1) if isinstance(arguments[0], FloatingPoint): return Boolean(True) elif isinstance(arguments[0], Integer): return Boolean(False) else: raise SchemeTypeError("exact? only takes integers or floating point " "numbers as arguments, you gave me ""%s." % \ len(arguments))
def char_less_than(arguments): check_argument_number('char<?', arguments, 2, 2) if not isinstance(arguments[0], Character) or not isinstance( arguments[1], Character): raise SchemeTypeError("char<? takes only character arguments, got a " "%s and a %s." % (arguments[0].__class__, arguments[1].__class__)) if arguments[0].value < arguments[1].value: return Boolean(True) return Boolean(False)
def test_equivalence(arguments): check_argument_number('eqv?', arguments, 2, 2) if isinstance(arguments[0], Atom): # __eq__ is defined on Atom return Boolean(arguments[0] == arguments[1]) if isinstance(arguments[0], Cons): # __eq__ on Cons is deep equality return Boolean(arguments[0] is arguments[1]) else: # __eq__ is defined on Nil # todo: test vectors return Boolean(arguments[0] == arguments[1])
def test_equality(self): program = "(=)" self.assertEvaluatesTo(program, Boolean(True)) program = "(= 0)" self.assertEvaluatesTo(program, Boolean(True)) program = "(= 0 0)" self.assertEvaluatesTo(program, Boolean(True)) program = "(= 0 1)" self.assertEvaluatesTo(program, Boolean(False)) program = "(= 1.0 1)" self.assertEvaluatesTo(program, Boolean(True))
def test_eqv(self): program = "(eqv? 1 1)" self.assertEvaluatesTo(program, Boolean(True)) program = "(eqv? (quote foo) (quote foo))" self.assertEvaluatesTo(program, Boolean(True)) program = "(eqv? car car)" self.assertEvaluatesTo(program, Boolean(True)) program = "(eqv? (quote ()) (quote ()))" self.assertEvaluatesTo(program, Boolean(True)) program = "(eqv? (cons 1 2) (cons 1 2))" self.assertEvaluatesTo(program, Boolean(False))
def if_function(arguments, environment): check_argument_number('if', arguments, 2, 3) condition, environment = eval_s_expression(arguments[0], environment) # everything except an explicit false boolean is true if not condition == Boolean(False): then_expression = arguments[1] return eval_s_expression(then_expression, environment) else: if len(arguments) == 3: else_expression = arguments[2] return eval_s_expression(else_expression, environment)
def test_equality(self): program = "(char=? #\\ #\\space)" self.assertEvaluatesTo(program, Boolean(True))
def test_even_predicate(self): program = "(even? 6)" self.assertEvaluatesTo(program, Boolean(True)) program = "(even? 7)" self.assertEvaluatesTo(program, Boolean(False))
def test_less_or_equal(self): program = "(<= 1 1)" self.assertEvaluatesTo(program, Boolean(True)) program = "(<= 1 0)" self.assertEvaluatesTo(program, Boolean(False))
def test_and(self): program = "(and (= 2 2) (> 2 1))" self.assertEvaluatesTo(program, Boolean(True)) program = "(and \"foo\" \"bar\")" self.assertEvaluatesTo(program, String("bar"))
def test_not(self): program = '(not #f)' self.assertEvaluatesTo(program, Boolean(True)) program = '(not \'())' self.assertEvaluatesTo(program, Boolean(False))
def test_exact(self): program = "(exact? 1)" self.assertEvaluatesTo(program, Boolean(True)) program = "(exact? 1.0)" self.assertEvaluatesTo(program, Boolean(False))
def test_real(self): program = "(real? 1.2)" self.assertEvaluatesTo(program, Boolean(True))
def test_rational(self): program = "(rational? 1.1)" self.assertEvaluatesTo(program, Boolean(True))
def test_complex(self): program = "(complex? 0)" self.assertEvaluatesTo(program, Boolean(True))
def test_greater_or_equal(self): program = "(>= 1 1)" self.assertEvaluatesTo(program, Boolean(True)) program = "(>= 1 3 2)" self.assertEvaluatesTo(program, Boolean(False))
def test_greater_than(self): program = "(> 1 1)" self.assertEvaluatesTo(program, Boolean(False)) program = "(> 11 10 0)" self.assertEvaluatesTo(program, Boolean(True))
def test_char_less_than(self): program = "(char<? #\\A #\\B)" self.assertEvaluatesTo(program, Boolean(True))
def test_string_predicate(self): program = '(string? "foo")' self.assertEvaluatesTo(program, Boolean(True)) program = '(string? (quote ("foo")))' self.assertEvaluatesTo(program, Boolean(False))
def test_inexact(self): program = "(inexact? 0)" self.assertEvaluatesTo(program, Boolean(False)) program = "(inexact? 0.0)" self.assertEvaluatesTo(program, Boolean(True))
def test_is_boolean(self): program = "(boolean? #t)" self.assertEvaluatesTo(program, Boolean(True)) program = "(boolean? 1)" self.assertEvaluatesTo(program, Boolean(False))
def test_boolean(self): program = "#t" self.assertEvaluatesTo(program, Boolean(True))
def test_is_vector(self): program = "(vector? '())" self.assertEvaluatesTo(program, Boolean(False)) program = "(vector? (make-vector 1))" self.assertEvaluatesTo(program, Boolean(True))
def test_negative_predicate(self): program = "(negative? -1)" self.assertEvaluatesTo(program, Boolean(True)) program = "(negative? 3)" self.assertEvaluatesTo(program, Boolean(False))
def test_is_list(self): program = "(list? '())" self.assertEvaluatesTo(program, Boolean(True)) program = "(list? 1)" self.assertEvaluatesTo(program, Boolean(False))
def test_odd_predicate(self): program = "(odd? 1)" self.assertEvaluatesTo(program, Boolean(True)) program = "(odd? 0)" self.assertEvaluatesTo(program, Boolean(False))
def test_zero_predicate(self): program = "(zero? 0)" self.assertEvaluatesTo(program, Boolean(True))
def test_less_than(self): program = "(< 1 1)" self.assertEvaluatesTo(program, Boolean(False)) program = "(< 1 2 4)" self.assertEvaluatesTo(program, Boolean(True))