3 ;;; Tests for code generator
5 ;;; (c) 2009 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This file is part of the Sensible Object Design, an object system for C.
12 ;;; SOD is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2 of the License, or
15 ;;; (at your option) any later version.
17 ;;; SOD is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with SOD; if not, write to the Free Software Foundation,
24 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 (cl:in-package #:sod-test)
28 ;;;--------------------------------------------------------------------------
30 (defclass gcd-codegen-test (test-case)
32 (add-test *sod-test-suite* (get-suite gcd-codegen-test))
34 (defun make-gcd (codegen)
36 (codegen-push codegen)
37 (loop for (name init) in '(("aa" 1) ("bb" 0))
38 do (ensure-var codegen name c-type-int init))
39 (codegen-push codegen)
40 (with-temporary-var (codegen r c-type-int)
41 (emit-inst codegen(make-set-inst r "u%v"))
42 (with-temporary-var (codegen q c-type-int)
43 (emit-inst codegen (make-set-inst q "u/v"))
44 (with-temporary-var (codegen a c-type-int)
46 (list (make-set-inst a "aa")
47 (make-set-inst "aa" "bb")
49 (format nil "~A - ~A*bb" a q))))))
50 (emit-insts codegen (list (make-set-inst "u" "v")
51 (make-set-inst "v" r))))
52 (emit-inst codegen (make-while-inst "v" (codegen-pop-block codegen)))
53 (emit-inst codegen (make-if-inst "a" (make-set-inst "*a" "aa") nil))
54 (deliver-expr codegen :return "u")
55 (codegen-pop-function codegen "gcd"
61 (codegen-push codegen)
62 (loop for (name init) in '(("u" "atoi(argv[1])")
65 do (ensure-var codegen name c-type-int init))
66 (ensure-var codegen "g" c-type-int
67 (make-call-inst "gcd" (list "u" "v" "&a")))
68 (deliver-call codegen :void "printf"
69 "\"%d*%d == %d (mod %d)\\n\"" "a" "u" "g" "v")
70 (deliver-expr codegen :return 0)
71 (codegen-pop-function codegen "main"
74 ("argv" ([] string))))))
76 (defmethod set-up ((test gcd-codegen-test))
77 (with-slots (codegen) test
78 (setf codegen (make-instance 'codegen))
81 (def-test-method check-output ((test gcd-codegen-test) :run nil)
82 (assert-princ (codegen-functions (slot-value test 'codegen))
83 "(static int gcd(int u, int v, int *a)
97 bb = sod__v2 - sod__v1*bb;
106 static int main(int argc, char *argv[])
108 int u = atoi(argv[1]);
109 int v = atoi(argv[2]);
111 int g = gcd(u, v, &a);
113 printf(\"%d*%d == %d (mod %d)\\n\", a, u, g, v);
119 ;;;----- That's all, folks --------------------------------------------------