Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import unittest
- from kawa import Process
- from utils import to_kawa_str
- import sys
- class TestKawa(unittest.TestCase):
- def __init__(self, *args, **kwargs):
- super(TestKawa, self).__init__(*args, **kwargs)
- self.P = Process()
- def __check_test_multiple(self, test_arr):
- for expr, true_res, kawa_res in test_arr:
- # evaluate result
- eval_res = self.P.evaluate_line(expr)
- # check
- self.assertEqual(eval_res, true_res)
- # evaluate result into kawa string (what should be printed)
- eval_kawa_res = to_kawa_str(eval_res)
- # check
- self.assertEqual(eval_kawa_res, kawa_res)
- def test_add(self):
- arr = [("(+ 2 2)", [4], "4"),
- ("(+ 20)", [20], "20"),
- ("(+ 1 2 3 4 5 6 7 8 9 10)", [55], "55")]
- self.__check_test_multiple(arr)
- def test_sub(self):
- arr = [("(- 5 3)", [2], "2"),
- ("(- 420)", [-420], "-420"),
- ("(- 1 2 3 4 5 6 7 8 9 10)", [-53], "-53")]
- self.__check_test_multiple(arr)
- def test_mul(self):
- arr = [("(* 2 2)", [4], "4"),
- ("(* 0 0 0 0)", [0], "0"),
- ("(* 1 2 3 4 5 6 7 8 9 10)", [3628800], "3628800")]
- self.__check_test_multiple(arr)
- def test_div(self):
- arr = [("(/ 8 2)", [4], "4"),
- ("(/ 0 2)", [0], "0")]
- self.__check_test_multiple(arr)
- def test_rec(self):
- arr = [("(+ 1 (+ 2 (+ 3 (+ 4 (+ 5 (+ 6 (+ 7 (+ 8 9))))))))", [45], "45"),
- ("(* 2 3 (- 4 4))", [0], "0"),
- ("(* 2 (+ 3 4 5) 3 (- 10 5) 4 (/ 4 2))", [2880], "2880")]
- self.__check_test_multiple(arr)
- def test_list_methods(self):
- arr = [("(list 1 2 3)", [[1, 2, 3]], "(1 2 3)"),
- ("'(1 2 3)", [[1, 2, 3]], "(1 2 3)"),
- ("(length '(1 2 3))", [3], "3"),
- ("(list? '(1 2 3))", [True], "#t"),
- ("(null? '())", [True], "#t"),
- ("(null? '(1 2 3))", [False], "#f"),
- ("(append '(1 2 3) '(4 5))", [[1, 2, 3, 4, 5]], "(1 2 3 4 5)"),
- ("(cons '(1 2) '(3 ))", [[[1, 2], 3]], "((1 2) 3)"),
- ("(car '(1 2 3))", [1], "1"),
- ("(cdr '(1 2 3))", [[2, 3]], "(2 3)"),
- ("(caar '((1 2) 3 (4 5)))", [1], "1"),
- ("(cddar '((1 2) 3 (4 5)))", [[4, 5]], "(4 5)"),
- ("(cadadr '((1 (2 (3 4))) 5 (6 7)))", [[[3, 4]]], "((3 4))")]
- self.__check_test_multiple(arr)
- def test_boolean(self):
- arr = [("#t", [True], "#t"),
- ("(and #t #t)", [True], "#t"),
- ("(and #t #f)", [False], "#f"),
- ("(or #f #t)", [True], "#t"),
- ("(or #f #f)", [False], "#f")]
- self.__check_test_multiple(arr)
- def test_lambda(self):
- arr = [("((lambda (x) x) 10)", [10], "10"),
- ("((lambda (x) (+ x x)) 10)", [20], "20"),
- ("((lambda (x y) (+ x y)) 10 10)", [20], "20")]
- self.__check_test_multiple(arr)
- def test_map(self):
- arr = [("(map car '((1 2)(3 4)))", [[1, 3]], "(1 3)"),
- ("(map null? '((1 2)()))", [[False, True]], "(#f #t)")]
- self.__check_test_multiple(arr)
- def test_if_statement(self):
- arr = [("(if (= 5 5) 10)", [10], "10"),
- ("(if (> 6 5) 20)", [20], "20"),
- ("(if (< 4 5) 30)", [30], "30"),
- ("(if (<= 5 5) 40)", [40], "40"),
- ("(if (>= 5 5) 50)", [50], "50"),
- ("(if (= 5 55) 0 60)", [60], "60"),
- ("(if (>= 5 5) 50)", [50], "50"),
- ("(if (> (+ 10 20) (- 30 20)) '(1 3 5 7) '(2 4 6 8))", [[1, 3, 5, 7]], "(1 3 5 7)")]
- self.__check_test_multiple(arr)
- def test_apply(self):
- arr = [("(apply + '(1 2 3))", [6], "6"),
- ("(apply cons '(12 (34)))", [[12, 34]], "(12 34)")]
- self.__check_test_multiple(arr)
- def test_helper(self):
- arr = [("(remainder 10 7)", [3], "3"),
- ("(remainder 7 7)", [0], "0"),
- ("(quotient 225 25)", [9], "9"),
- ("(quotient 78 10)", [7], "7"),
- ("(sqrt 25)", [5.0], "5.0"),
- ("(expt 5 3)", [125], "125"),
- ("(positive? 25)", [True], "#t"),
- ("(negative? 25)", [False], "#f"),
- ("(zero? 0)", [True], "#t"),
- ("(zero? 25)", [False], "#f"),
- ("(even? 666)", [True], "#t"),
- ("(odd? 666)", [False], "#f"),
- ("(eq? 10 10)", [True], "#t"),
- ("(equal? '(12 34) '(12 34))", [True], "#t")]
- self.__check_test_multiple(arr)
- def test_define(self):
- arr = [("(define (sum x y) (+ x y)) (sum 5 10)", [None, 15], "15"),
- ("(define (sum x y) (+ x y)) (sum 5 10)", [None, 15], "15")]
- self.__check_test_multiple(arr)
- if __name__ == "__main__":
- unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement