Add Lazy, a lazy-evaluating version of Groovy
[Funky.git] / test / lib / Funky / Lazy.cpp
blobc7cbd7fa455b9b299288f41241adcf03fa53787a
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 "Lazy.h"
35 #include "Stubs/Functor.h"
36 #include <Funky/Exceptions/ParseError.h>
38 CPPUNIT_TEST_SUITE_REGISTRATION( LazyTest );
40 void LazyTest::setUp()
42 parms_[0] = 0;
43 parms_[1] = 1;
44 parms_[2] = 2;
47 //! Clean up after the test run.
48 void LazyTest::tearDown()
52 void LazyTest::tryScript01(){CPPUNIT_ASSERT(lazy_.eval("(test, 1)"));}
53 void LazyTest::tryScript02(){CPPUNIT_ASSERT(lazy_.eval("(add, 1, (neg, 1))") == 0);}
54 void LazyTest::tryScript03(){CPPUNIT_ASSERT(lazy_.eval("(add, 1, (neg, @0))", Funky::Lazy::Arguments(parms_, parms_ + 1)) == 1);}
55 void LazyTest::tryScript04(){CPPUNIT_ASSERT(lazy_.eval("(add, (neg, (add, 6, 6)), 12)") == 0);}
56 void LazyTest::tryScript05(){CPPUNIT_ASSERT(lazy_.eval("(add, (add, 6, 6), -12)") == 0);}
57 void LazyTest::tryScript06(){CPPUNIT_ASSERT(lazy_.eval("(!sub-2, (add, @0, (neg, @1)))(sub, 1, 1)") == 0);}
58 void LazyTest::tryScript07(){CPPUNIT_ASSERT(lazy_.eval("(!sub, (add, @0, (neg, @1)))(sub, 1, 1)") == 0);}
59 void LazyTest::tryScript08(){CPPUNIT_ASSERT(lazy_.eval("(if, 0, 1, 2)") == 2);}
60 void LazyTest::tryScript09(){CPPUNIT_ASSERT(lazy_.eval("(if, 1, 1, 2)") == 1);}
61 void LazyTest::tryScript10(){CPPUNIT_ASSERT(lazy_.eval("(add, (shift), (add, (shift), (shift)))", Funky::Lazy::Arguments(parms_, parms_ + 3)) == 3);}
62 void LazyTest::tryScript11(){CPPUNIT_ASSERT(lazy_.eval("(shift)", Funky::Lazy::Arguments(parms_, parms_ + 3)) == 0);}
63 void LazyTest::tryScript12(){CPPUNIT_ASSERT(lazy_.eval("(defined, @3)", Funky::Lazy::Arguments(parms_, parms_ + 3)) == 0);}
64 void LazyTest::tryScript13(){CPPUNIT_ASSERT(lazy_.eval("(defined, @2)", Funky::Lazy::Arguments(parms_, parms_ + 3)) == 1);}
65 void LazyTest::tryScript14(){CPPUNIT_ASSERT(lazy_.eval("(defined, @1)", Funky::Lazy::Arguments(parms_, parms_ + 3)) == 1);}
66 void LazyTest::tryScript15(){CPPUNIT_ASSERT(lazy_.eval("(defined, @0)", Funky::Lazy::Arguments(parms_, parms_ + 3)) == 1);}
67 void LazyTest::tryScript16(){CPPUNIT_ASSERT(lazy_.eval("(!sub, (add, @0, (if, (defined, @1), @1, 0)))(sub, 1)") == 1);}
68 void LazyTest::tryScript17(){CPPUNIT_ASSERT(lazy_.eval("(shift)", Funky::Lazy::Arguments(parms_ + 1, parms_ + 3)) == 1);}
69 void LazyTest::tryScript18(){CPPUNIT_ASSERT(lazy_.eval("(add, @@)", Funky::Lazy::Arguments(parms_ + 1, parms_ + 3)) == 3);}
70 void LazyTest::tryScript19()
72 CPPUNIT_ASSERT(lazy_.eval(
73 "(!sum, "
74 " (if, (defined, @0),"
75 " (add,"
76 " (shift),"
77 " (if, (defined, @0), (sum, @@), 0)),"
78 " 0"
79 " )"
80 ")"
81 "(sum, @@)",
82 Funky::Lazy::Arguments(parms_, parms_ + 3)) == 3);
85 void LazyTest::tryScript20()
87 lazy_.installFunction("S", Stubs::Functor< Funky::Lazy::Arguments >(), 2, 2);
88 CPPUNIT_ASSERT(lazy_.eval("(add, (S, @@))", Funky::Lazy::Arguments(parms_ + 1, parms_ + 3)) == 3);
91 void LazyTest::tryParseError01()
93 CPPUNIT_ASSERT_THROW(lazy_.eval("("), Funky::Exceptions::ParseError);
96 void LazyTest::tryParseError02()
98 bool caught(false);
99 try
101 lazy_.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 LazyTest::tryParseError03()
113 bool caught(false);
116 lazy_.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 LazyTest::tryParseError04()
128 bool caught(false);
131 lazy_.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 LazyTest::tryParseError05()
143 bool caught(false);
146 lazy_.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 LazyTest::tryParseError06()
158 bool caught(false);
161 lazy_.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 LazyTest::tryParseError07()
173 bool caught(false);
176 lazy_.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 LazyTest::tryParseError08()
188 bool caught(false);
191 lazy_.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);