Advertisement
CSenshi

Kawa - Unit Tests

Feb 29th, 2020
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.97 KB | None | 0 0
  1. import unittest
  2. from kawa import Process
  3. from utils import to_kawa_str
  4. import sys
  5.  
  6.  
  7. class TestKawa(unittest.TestCase):
  8.  
  9.     def __init__(self, *args, **kwargs):
  10.         super(TestKawa, self).__init__(*args, **kwargs)
  11.         self.P = Process()
  12.  
  13.     def __check_test_multiple(self, test_arr):
  14.         for expr, true_res, kawa_res in test_arr:
  15.             # evaluate result
  16.             eval_res = self.P.evaluate_line(expr)
  17.             # check
  18.             self.assertEqual(eval_res, true_res)
  19.  
  20.             # evaluate result into kawa string (what should be printed)
  21.             eval_kawa_res = to_kawa_str(eval_res)
  22.             # check
  23.             self.assertEqual(eval_kawa_res, kawa_res)
  24.  
  25.     def test_add(self):
  26.         arr = [("(+ 2 2)", [4], "4"),
  27.                ("(+ 20)", [20], "20"),
  28.                ("(+ 1 2 3 4 5 6 7 8 9 10)", [55], "55")]
  29.         self.__check_test_multiple(arr)
  30.  
  31.     def test_sub(self):
  32.         arr = [("(- 5 3)", [2], "2"),
  33.                ("(- 420)", [-420], "-420"),
  34.                ("(- 1 2 3 4 5 6 7 8 9 10)", [-53], "-53")]
  35.         self.__check_test_multiple(arr)
  36.  
  37.     def test_mul(self):
  38.         arr = [("(* 2 2)", [4], "4"),
  39.                ("(* 0 0 0 0)", [0], "0"),
  40.                ("(* 1 2 3 4 5 6 7 8 9 10)", [3628800], "3628800")]
  41.         self.__check_test_multiple(arr)
  42.  
  43.     def test_div(self):
  44.         arr = [("(/ 8 2)", [4], "4"),
  45.                ("(/ 0 2)", [0], "0")]
  46.         self.__check_test_multiple(arr)
  47.  
  48.     def test_rec(self):
  49.         arr = [("(+ 1 (+ 2 (+ 3 (+ 4 (+ 5 (+ 6 (+ 7 (+ 8 9))))))))", [45], "45"),
  50.                ("(* 2 3 (- 4 4))", [0], "0"),
  51.                ("(* 2 (+ 3 4 5) 3 (- 10 5) 4 (/ 4 2))", [2880], "2880")]
  52.         self.__check_test_multiple(arr)
  53.  
  54.     def test_list_methods(self):
  55.         arr = [("(list 1 2 3)", [[1, 2, 3]], "(1 2 3)"),
  56.                ("'(1 2 3)", [[1, 2, 3]], "(1 2 3)"),
  57.                ("(length '(1 2 3))", [3], "3"),
  58.                ("(list? '(1 2 3))", [True], "#t"),
  59.                ("(null? '())", [True], "#t"),
  60.                ("(null? '(1 2 3))", [False], "#f"),
  61.                ("(append '(1 2 3) '(4 5))", [[1, 2, 3, 4, 5]], "(1 2 3 4 5)"),
  62.                ("(cons '(1 2) '(3 ))", [[[1, 2], 3]], "((1 2) 3)"),
  63.                ("(car '(1 2 3))", [1], "1"),
  64.                ("(cdr '(1 2 3))", [[2, 3]], "(2 3)"),
  65.                ("(caar '((1 2) 3 (4 5)))", [1], "1"),
  66.                ("(cddar '((1 2) 3 (4 5)))", [[4, 5]], "(4 5)"),
  67.                ("(cadadr '((1 (2 (3 4))) 5 (6 7)))", [[[3, 4]]], "((3 4))")]
  68.         self.__check_test_multiple(arr)
  69.  
  70.     def test_boolean(self):
  71.         arr = [("#t", [True], "#t"),
  72.                ("(and #t #t)", [True], "#t"),
  73.                ("(and #t #f)", [False], "#f"),
  74.                ("(or #f #t)", [True], "#t"),
  75.                ("(or #f #f)", [False], "#f")]
  76.         self.__check_test_multiple(arr)
  77.  
  78.     def test_lambda(self):
  79.         arr = [("((lambda (x) x) 10)", [10], "10"),
  80.                ("((lambda (x) (+ x x)) 10)", [20], "20"),
  81.                ("((lambda (x y) (+ x y)) 10 10)", [20], "20")]
  82.         self.__check_test_multiple(arr)
  83.  
  84.     def test_map(self):
  85.         arr = [("(map car '((1 2)(3 4)))", [[1, 3]], "(1 3)"),
  86.                ("(map null? '((1 2)()))", [[False, True]], "(#f #t)")]
  87.         self.__check_test_multiple(arr)
  88.  
  89.     def test_if_statement(self):
  90.         arr = [("(if (= 5 5) 10)", [10], "10"),
  91.                ("(if (> 6 5) 20)", [20], "20"),
  92.                ("(if (< 4 5) 30)", [30], "30"),
  93.                ("(if (<= 5 5) 40)", [40], "40"),
  94.                ("(if (>= 5 5) 50)", [50], "50"),
  95.                ("(if (= 5 55) 0 60)", [60], "60"),
  96.                ("(if (>= 5 5) 50)", [50], "50"),
  97.                ("(if (> (+ 10 20) (- 30 20)) '(1 3 5 7) '(2 4 6 8))", [[1, 3, 5, 7]], "(1 3 5 7)")]
  98.         self.__check_test_multiple(arr)
  99.  
  100.     def test_apply(self):
  101.         arr = [("(apply + '(1 2 3))", [6], "6"),
  102.                ("(apply cons '(12 (34)))", [[12, 34]], "(12 34)")]
  103.         self.__check_test_multiple(arr)
  104.  
  105.     def test_helper(self):
  106.         arr = [("(remainder 10 7)", [3], "3"),
  107.                ("(remainder 7 7)", [0], "0"),
  108.                ("(quotient 225 25)", [9], "9"),
  109.                ("(quotient 78 10)", [7], "7"),
  110.                ("(sqrt 25)", [5.0], "5.0"),
  111.                ("(expt 5 3)", [125], "125"),
  112.                ("(positive? 25)", [True], "#t"),
  113.                ("(negative? 25)", [False], "#f"),
  114.                ("(zero? 0)", [True], "#t"),
  115.                ("(zero? 25)", [False], "#f"),
  116.                ("(even? 666)", [True], "#t"),
  117.                ("(odd? 666)", [False], "#f"),
  118.                ("(eq? 10 10)", [True], "#t"),
  119.                ("(equal? '(12 34) '(12 34))", [True], "#t")]
  120.         self.__check_test_multiple(arr)
  121.  
  122.     def test_define(self):
  123.         arr = [("(define (sum x y) (+ x y)) (sum 5 10)", [None, 15], "15"),
  124.                ("(define (sum x y) (+ x y)) (sum 5 10)", [None, 15], "15")]
  125.         self.__check_test_multiple(arr)
  126.  
  127.  
  128. if __name__ == "__main__":
  129.     unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement