Adding all the files for an integer version of Funky: Groovy
[Funky.git] / test / lib / Funky / Groovy.cpp
blobdbaf90db3fc1948e61c77de4b13951d21d7ed8ef
1 /* Funky: a light-weight embeddable programming language
2 * Copyright (c) 2007, Ronald Landheer-Cieslak
3 * All rights reserved
4 *
5 * This is free software. You may distribute it and/or modify it and
6 * distribute modified forms provided that the following terms are met:
8 * * Redistributions of the source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution;
13 * * None of the names of the authors of this software may be used to endorse
14 * or promote this software, derived software or any distribution of this
15 * software or any distribution of which this software is part, without
16 * prior written permission from the authors involved;
17 * * Unless you have received a written statement from Ronald Landheer-Cieslak
18 * that says otherwise, the terms of the GNU General Public License, as
19 * published by the Free Software Foundation, version 2 or (at your option)
20 * any later version, also apply.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 #include "Groovy.h"
35 #include "Stubs/Functor.h"
36 #include <Funky/Exceptions/ParseError.h>
38 CPPUNIT_TEST_SUITE_REGISTRATION( GroovyTest );
40 void GroovyTest::setUp()
42 parms_[0] = 0;
43 parms_[1] = 1;
44 parms_[2] = 2;
47 //! Clean up after the test run.
48 void GroovyTest::tearDown()
52 void GroovyTest::tryScript01(){CPPUNIT_ASSERT(groovy_.eval("(test, 1)"));}
53 void GroovyTest::tryScript02(){CPPUNIT_ASSERT(groovy_.eval("(add, 1, (neg, 1))") == 0);}
54 void GroovyTest::tryScript03(){CPPUNIT_ASSERT(groovy_.eval("(add, 1, (neg, @0))", std::vector< int >(parms_, parms_ + 1)) == 1);}
55 void GroovyTest::tryScript04(){CPPUNIT_ASSERT(groovy_.eval("(add, (neg, (add, 6, 6)), 12)") == 0);}
56 void GroovyTest::tryScript05(){CPPUNIT_ASSERT(groovy_.eval("(add, (add, 6, 6), -12)") == 0);}
57 void GroovyTest::tryScript06(){CPPUNIT_ASSERT(groovy_.eval("(!sub-2, (add, @0, (neg, @1)))(sub, 1, 1)") == 0);}
58 void GroovyTest::tryScript07(){CPPUNIT_ASSERT(groovy_.eval("(!sub, (add, @0, (neg, @1)))(sub, 1, 1)") == 0);}
59 void GroovyTest::tryScript08(){CPPUNIT_ASSERT(groovy_.eval("(if, 0, 1, 2)") == 2);}
60 void GroovyTest::tryScript09(){CPPUNIT_ASSERT(groovy_.eval("(if, 1, 1, 2)") == 1);}
61 void GroovyTest::tryScript10(){CPPUNIT_ASSERT(groovy_.eval("(add, (shift), (add, (shift), (shift)))", std::vector< int >(parms_, parms_ + 3)) == 3);}
62 void GroovyTest::tryScript11(){CPPUNIT_ASSERT(groovy_.eval("(shift)", std::vector< int >(parms_, parms_ + 3)) == 0);}
63 void GroovyTest::tryScript12(){CPPUNIT_ASSERT(groovy_.eval("(defined, @3)", std::vector< int >(parms_, parms_ + 3)) == 0);}
64 void GroovyTest::tryScript13(){CPPUNIT_ASSERT(groovy_.eval("(defined, @2)", std::vector< int >(parms_, parms_ + 3)) == 1);}
65 void GroovyTest::tryScript14(){CPPUNIT_ASSERT(groovy_.eval("(defined, @1)", std::vector< int >(parms_, parms_ + 3)) == 1);}
66 void GroovyTest::tryScript15(){CPPUNIT_ASSERT(groovy_.eval("(defined, @0)", std::vector< int >(parms_, parms_ + 3)) == 1);}
67 void GroovyTest::tryScript16(){CPPUNIT_ASSERT(groovy_.eval("(!sub, (add, @0, (if, (defined, @1), @1, 0)))(sub, 1)") == 1);}
68 void GroovyTest::tryScript17(){CPPUNIT_ASSERT(groovy_.eval("(shift)", std::vector< int >(parms_ + 1, parms_ + 3)) == 1);}
69 void GroovyTest::tryScript18(){CPPUNIT_ASSERT(groovy_.eval("(add, @@)", std::vector< int >(parms_ + 1, parms_ + 3)) == 3);}
70 void GroovyTest::tryScript19()
72 CPPUNIT_ASSERT(groovy_.eval(
73 "(!sum, "
74 " (if, (defined, @0),"
75 " (add,"
76 " (shift),"
77 " (if, (defined, @0), (sum, @@), 0)),"
78 " 0"
79 " )"
80 ")"
81 "(sum, @@)",
82 std::vector< int >(parms_, parms_ + 3)) == 3);
85 void GroovyTest::tryScript20()
87 groovy_.installFunction("S", Stubs::Functor< std::vector< int > >(), 2, 2);
88 CPPUNIT_ASSERT(groovy_.eval("(add, (S, @@))", std::vector< int >(parms_ + 1, parms_ + 3)) == 3);
91 void GroovyTest::tryParseError01()
93 CPPUNIT_ASSERT_THROW(groovy_.eval("("), Funky::Exceptions::ParseError);
96 void GroovyTest::tryParseError02()
98 bool caught(false);
99 try
101 groovy_.eval("(0)");
103 catch (const Funky::Exceptions::ParseError & e)
105 caught = true;
106 CPPUNIT_ASSERT(e.reason_ == Funky::Exceptions::ParseError::function_name_expected__);
108 CPPUNIT_ASSERT(caught);
111 void GroovyTest::tryParseError03()
113 bool caught(false);
116 groovy_.eval("(!,0)");
118 catch (const Funky::Exceptions::ParseError & e)
120 caught = true;
121 CPPUNIT_ASSERT(e.reason_ == Funky::Exceptions::ParseError::function_name_expected__);
123 CPPUNIT_ASSERT(caught);
126 void GroovyTest::tryParseError04()
128 bool caught(false);
131 groovy_.eval("!,0)");
133 catch (const Funky::Exceptions::ParseError & e)
135 caught = true;
136 CPPUNIT_ASSERT(e.reason_ == Funky::Exceptions::ParseError::left_parenthesis_expected__);
138 CPPUNIT_ASSERT(caught);
141 void GroovyTest::tryParseError05()
143 bool caught(false);
146 groovy_.eval("(foo,)");
148 catch (const Funky::Exceptions::ParseError & e)
150 caught = true;
151 CPPUNIT_ASSERT(e.reason_ == Funky::Exceptions::ParseError::literal_parameter_or_statement_expected__);
153 CPPUNIT_ASSERT(caught);
156 void GroovyTest::tryParseError06()
158 bool caught(false);
161 groovy_.eval("(foo,0");
163 catch (const Funky::Exceptions::ParseError & e)
165 caught = true;
166 CPPUNIT_ASSERT(e.reason_ == Funky::Exceptions::ParseError::right_parenthesis_expected__);
168 CPPUNIT_ASSERT(caught);
171 void GroovyTest::tryParseError07()
173 bool caught(false);
176 groovy_.eval("(!sub-2, (add, @0, (neg, @1)))");
178 catch (const Funky::Exceptions::ParseError & e)
180 caught = true;
181 CPPUNIT_ASSERT(e.reason_ == Funky::Exceptions::ParseError::left_parenthesis_expected__);
183 CPPUNIT_ASSERT(caught);
186 void GroovyTest::tryParseError08()
188 bool caught(false);
191 groovy_.eval("(test, 1.0)");
193 catch (const Funky::Exceptions::ParseError & e)
195 caught = true;
196 CPPUNIT_ASSERT(e.reason_ == Funky::Exceptions::ParseError::right_parenthesis_expected__);
198 CPPUNIT_ASSERT(caught);