View Javadoc

1   /**
2    * Copyright (c) 2008-2011, http://www.snakeyaml.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.yaml.snakeyaml.types;
18  
19  import java.io.IOException;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  /**
24   * @see http://yaml.org/type/float.html
25   */
26  public class FloatTagTest extends AbstractTest {
27  
28      public void testFloat() throws IOException {
29          assertEquals(new Double(6.8523015e+5), getMapValue("canonical: 6.8523015e+5", "canonical"));
30          assertEquals(new Double(6.8523015e+5),
31                  getMapValue("exponentioal: 685.230_15e+03", "exponentioal"));
32          assertEquals(new Double(6.8523015e+5), getMapValue("fixed: 685_230.15", "fixed"));
33          assertEquals(new Double(6.8523015e+5),
34                  getMapValue("sexagesimal: 190:20:30.15", "sexagesimal"));
35          assertEquals(Double.NEGATIVE_INFINITY,
36                  getMapValue("negative infinity: -.inf", "negative infinity"));
37          assertEquals(Double.NaN, getMapValue("not a number: .NaN", "not a number"));
38      }
39  
40      public void testFloatShorthand() throws IOException {
41          assertEquals(new Double(1), getMapValue("number: !!float 1", "number"));
42      }
43  
44      public void testFloatTag() throws IOException {
45          assertEquals(new Double(1), getMapValue("number: !<tag:yaml.org,2002:float> 1", "number"));
46      }
47  
48      public void testFloatOut() throws IOException {
49          Map<String, Object> map = new HashMap<String, Object>();
50          map.put("number", new Double(1));
51          String output = dump(map);
52          assertEquals("{number: 1.0}\n", output);
53      }
54  
55      public void testBasicDoubleScalarLoad() {
56          assertEquals(new Double(47.0), load("47.0"));
57          assertEquals(new Double(0.0), load("0.0"));
58          assertEquals(new Double(-1.0), load("-1.0"));
59      }
60  
61      public void testDumpStr() {
62          assertEquals("'1.0'\n", dump("1.0"));
63      }
64  
65      public void testDump() {
66          assertEquals("1.0\n", dump(1.0));
67      }
68  
69      /**
70       * to test http://code.google.com/p/snakeyaml/issues/detail?id=130
71       */
72      public void testScientificFloatWithoutDecimalDot() throws IOException {
73          assertEquals(new Double(8e-06), load("8e-06"));
74          assertEquals(new Double(8e06), load("8e06"));
75          assertEquals(new Double(8e06), load("8e+06"));
76          assertEquals(new Double(8000e06), load("8_000e06"));
77          assertEquals(new Double(123e-06), load("123e-06"));
78      }
79  }