KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tests > jfun > parsec > CalculatorTestCase


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solution. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8
9 /*
10  * Created on Oct 11, 2005
11  *
12  * Author Ben Yu
13  * ZBS
14  */

15 package tests.jfun.parsec;
16
17 import jfun.parsec.Parser;
18 import junit.framework.TestCase;
19
20 public class CalculatorTestCase extends TestCase {
21   public void test1(){
22     final Parser p = new Calculator().getParser();
23     final String JavaDoc src = "1+1*2-10 2";
24     final Object JavaDoc obj = p.parse(src);
25     assertEquals(obj, new Double JavaDoc(-17));
26   }
27   public void test1a(){
28     final Parser p = new Calculator().getParser();
29     final String JavaDoc src = "1+1 2-10";
30     final Object JavaDoc obj = p.parse(src);
31     assertEquals(obj, new Double JavaDoc(-7));
32   }
33   public void testUnaryOperator(){
34     final Parser p = new Calculator().getParser();
35     final String JavaDoc src = "-1+1*2-10 2";
36     final Object JavaDoc obj = p.parse(src);
37     assertEquals(obj, new Double JavaDoc(-19));
38   }
39   
40   public void test2(){
41     final Parser p = new Calculator().getParser();
42     final String JavaDoc src = "/*this is beginning**/(1+1+ 2.1)+3*(((2)-10))//this is end";
43     final Object JavaDoc obj = p.parse(src);
44     assertEquals(obj, new Double JavaDoc(-19.9));
45   }
46 }
47
Popular Tags