1   /*
2    * Copyright (C) 2006 uguu@users.sourceforge.jp, All Rights Reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    *
8    *    1. Redistributions of source code must retain the above copyright
9    *       notice, this list of conditions and the following disclaimer.
10   *
11   *    2. Redistributions in binary form must reproduce the above copyright
12   *       notice, this list of conditions and the following disclaimer in the
13   *       documentation and/or other materials provided with the distribution.
14   *
15   *    3. Neither the name of Clarkware Consulting, Inc. nor the names of its
16   *       contributors may be used to endorse or promote products derived
17   *       from this software without prior written permission. For written
18   *       permission, please contact clarkware@clarkware.com.
19   *
20   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
21   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
23   * CLARKWARE CONSULTING OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28   * NEGLIGENCE OR OTHERWISE) ARISING IN  ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  package jp.sourceforge.rpn_computer.test;
33  
34  import jp.sourceforge.rpn_computer.RpnCommand;
35  import jp.sourceforge.rpn_computer.RpnCommandList;
36  import jp.sourceforge.rpn_computer.RpnCompiler;
37  import jp.sourceforge.rpn_computer.RpnNode;
38  import jp.sourceforge.rpn_computer.RpnParser;
39  import jp.sourceforge.rpn_computer.command.AddCommand;
40  import jp.sourceforge.rpn_computer.command.DivideCommand;
41  import jp.sourceforge.rpn_computer.command.MultiplyCommand;
42  import jp.sourceforge.rpn_computer.command.PushStackCommand;
43  import jp.sourceforge.rpn_computer.command.SubtractCommand;
44  import junit.framework.TestCase;
45  
46  /**
47   * コンパイルのテストを行います。
48   * 
49   * @author uguu@users.sourceforge.jp
50   */
51  public class RpnCompilerTest extends TestCase {
52  
53      /**
54       * インスタンスを初期化します。
55       * 
56       * @param name
57       *            テストの名前。
58       */
59      public RpnCompilerTest(String name) {
60          super(name);
61      }
62  
63      /**
64       * 正常系テスト1。
65       */
66      public void testNormal1() {
67          String exp = "(1.2 + 3.4) * (5.6 - 7.8 / (9.0 + 0.1))";
68  
69          RpnParser parser = new RpnParser();
70          RpnNode node = parser.parse(exp);
71  
72          RpnCompiler compiler = new RpnCompiler();
73          RpnCommandList cl = compiler.compile(node);
74          RpnCommand[] commands = cl.getCommands();
75  
76          assertEquals(11, commands.length);
77  
78          RpnCommand c = commands[0];
79          assertTrue(c instanceof PushStackCommand);
80          assertEquals("push_stack[1.2]", c.toString());
81  
82          c = commands[1];
83          assertTrue(c instanceof PushStackCommand);
84          assertEquals("push_stack[3.4]", c.toString());
85  
86          c = commands[2];
87          assertTrue(c instanceof AddCommand);
88          assertEquals("add", c.toString());
89  
90          c = commands[3];
91          assertTrue(c instanceof PushStackCommand);
92          assertEquals("push_stack[5.6]", c.toString());
93  
94          c = commands[4];
95          assertTrue(c instanceof PushStackCommand);
96          assertEquals("push_stack[7.8]", c.toString());
97  
98          c = commands[5];
99          assertTrue(c instanceof PushStackCommand);
100         assertEquals("push_stack[9.0]", c.toString());
101 
102         c = commands[6];
103         assertTrue(c instanceof PushStackCommand);
104         assertEquals("push_stack[0.1]", c.toString());
105 
106         c = commands[7];
107         assertTrue(c instanceof AddCommand);
108         assertEquals("add", c.toString());
109 
110         c = commands[8];
111         assertTrue(c instanceof DivideCommand);
112         assertEquals("divide", c.toString());
113 
114         c = commands[9];
115         assertTrue(c instanceof SubtractCommand);
116         assertEquals("subtract", c.toString());
117 
118         c = commands[10];
119         assertTrue(c instanceof MultiplyCommand);
120         assertEquals("multiply", c.toString());
121     }
122 
123     /**
124      * 異常系テスト1。{@link RpnCompiler#compile(RpnNode)}メソッドのnodeがnullのとき、{@link NullPointerException}例外がスローされることを確認します。
125      */
126     public void testFail1() {
127         RpnCompiler compiler = new RpnCompiler();
128         try {
129             compiler.compile(null);
130             fail();
131         } catch (NullPointerException e) {
132             assertEquals("nodeがnullです。", e.getMessage());
133         }
134     }
135 }