summaryrefslogtreecommitdiff
path: root/tests/sglsl.scm
blob: fa41b1c9b4be669ac7e81cc099ce6ded46161ab4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
;;; Chickadee Game Toolkit
;;; Copyright © 2022 David Thompson <davet@gnu.org>
;;;
;;; Chickadee is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published
;;; by the Free Software Foundation, either version 3 of the License,
;;; or (at your option) any later version.
;;;
;;; Chickadee is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (tests sglsl)
  #:use-module (tests utils)
  #:use-module (srfi srfi-64)
  #:use-module (chickadee graphics sglsl))

(define* (test-glsl name exp expected)
  (test-equal name
    (with-output-to-string (lambda () (sglsl->glsl exp)))
    expected))

(with-tests "sglsl"
  (test-glsl "true" #true "true")
  (test-glsl "false" #false "false")
  (test-glsl "integers" 4 "4")
  (test-glsl "floats" 4.5 "4.5")
  (test-glsl "rationals->floats" 1/2 "0.5")
  (test-glsl "+" '(+ 1 2) "(1+2)")
  (test-glsl "-" '(- 2 1) "(2-1)")
  (test-glsl "*" '(* 2 2) "(2*2)")
  (test-glsl "/" '(/ 4 2) "(4/2)")
  (test-glsl "=" '(= foo 1) "(foo=1)")
  (test-glsl "+=" '(+= foo 2) "(foo+=2)")
  (test-glsl "-=" '(-= foo 2) "(foo-=2)")
  (test-glsl "*=" '(*= foo 2) "(foo*=2)")
  (test-glsl "/=" '(/= foo 2) "(foo/=2)")
  (test-glsl "==" '(== foo 1) "(foo==1)")
  (test-glsl "!=" '(!= foo 1) "(foo!=1)")
  (test-glsl ">" '(> foo 1) "(foo>1)")
  (test-glsl ">=" '(>= foo 1) "(foo>=1)")
  (test-glsl "<" '(< foo 1) "(foo<1)")
  (test-glsl "<=" '(<= foo 1) "(foo<=1)")
  (test-glsl "||" '(|| foo bar) "(foo||bar)")
  (test-glsl "&&" '(&& foo bar) "(foo&&bar)")
  (test-glsl "++" '(++ foo) "foo++")
  (test-glsl "--" '(-- foo) "foo--")
  (test-glsl "pre++" '(pre++ foo) "++foo")
  (test-glsl "pre--" '(pre-- foo) "--foo")
  (test-glsl "negation" '(! (|| foo bar)) "!(foo||bar)")
  (test-glsl "break" 'break "break")
  (test-glsl "continue" 'continue "continue")
  (test-glsl "discard" 'discard "discard")
  (test-glsl "if" '(if (== foo bar) 1)
             "if((foo==bar)){1}")
  (test-glsl "if/else" '(if (== foo bar) 1 2)
             "if((foo==bar)){1}else{2}")
  (test-glsl "ternary" '(? (== foo bar) 1 2)
             "((foo==bar)?1:2)")
  (test-glsl "for loop" '(for ((= i 0) (< i 10) (++ i)) (= (@ foo i) 0))
             "for((i=0);(i<10);i++){(foo[i]=0);}")
  (test-glsl "variable reference" 'foo "foo")
  (test-glsl "struct dereference" '(-> foo bar baz) "foo.bar.baz")
  (test-glsl "array dereference" '(@ foo 1 2) "foo[1][2]")
  (test-glsl "struct+array dereference" '(@ (-> foo bar) 0) "foo.bar[0]")
  (test-glsl "variable definition with initial value"
             '(var float foo 1.2)
             "float foo=1.2")
  (test-glsl "variable definition with undefined value"
             '(var float foo)
             "float foo")
  (test-glsl "variable definition with array dimensions"
             '(var float (foo 4 4))
             "float foo[4][4]")
  (test-glsl "function definition"
             '(function (float square (float x)) (return (* x x)))
             "float square(float x){return (x*x);}")
  (test-glsl "struct definition"
             '(struct Light ((vec3 position) (vec3 direction)))
             "struct Light{vec3 position;vec3 direction;}")
  (test-glsl "constant definition" '(const float pi 3.14159)
             "const float pi=3.14159")
  (test-glsl "uniform declaration" '(uniform vec3 position)
             "uniform vec3 position")
  (test-glsl "uniform declaration with array dimensions"
             '(uniform vec3 (position 2 3))
             "uniform vec3 position[2][3]")
  (test-glsl "input declaration" '(in vec3 position)
             "in vec3 position")
  (test-glsl "output declaration" '(out vec3 position)
             "out vec3 position")
  (test-glsl "function call" '(vec4 1 2 3 4) "vec4(1,2,3,4)")
  (test-glsl "multiple expressions" '((++ foo) (-- bar))
             "foo++;bar--;"))