View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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  package org.yaml.snakeyaml.types;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  /**
22   * @see http://yaml.org/type/float.html
23   */
24  public class FloatTagTest extends AbstractTest {
25  
26      public void testFloat() {
27          assertEquals(new Double(6.8523015e+5), getMapValue("canonical: 6.8523015e+5", "canonical"));
28          assertEquals(new Double(6.8523015e+5),
29                  getMapValue("exponentioal: 685.230_15e+03", "exponentioal"));
30          assertEquals(new Double(6.8523015e+5), getMapValue("fixed: 685_230.15", "fixed"));
31          assertEquals(new Double(6.8523015e+5),
32                  getMapValue("sexagesimal: 190:20:30.15", "sexagesimal"));
33          assertEquals(Double.NEGATIVE_INFINITY,
34                  getMapValue("negative infinity: -.inf", "negative infinity"));
35          assertEquals(Double.NaN, getMapValue("not a number: .NaN", "not a number"));
36      }
37  
38      public void testFloatShorthand() {
39          assertEquals(new Double(1), getMapValue("number: !!float 1", "number"));
40      }
41  
42      public void testFloatTag() {
43          assertEquals(new Double(1), getMapValue("number: !<tag:yaml.org,2002:float> 1", "number"));
44      }
45  
46      public void testFloatOut() {
47          Map<String, Object> map = new HashMap<String, Object>();
48          map.put("number", new Double(1));
49          String output = dump(map);
50          assertEquals("{number: 1.0}\n", output);
51      }
52  
53      public void testBasicDoubleScalarLoad() {
54          assertEquals(new Double(47.0), load("47.0"));
55          assertEquals(new Double(0.0), load("0.0"));
56          assertEquals(new Double(-1.0), load("-1.0"));
57      }
58  
59      public void testDumpStr() {
60          assertEquals("'1.0'\n", dump("1.0"));
61      }
62  
63      public void testDump() {
64          assertEquals("1.0\n", dump(1.0));
65      }
66  
67      /**
68       * to test http://code.google.com/p/snakeyaml/issues/detail?id=130
69       */
70      public void testScientificFloatWithoutDecimalDot() {
71          assertEquals(new Double(8e-06), load("8e-06"));
72          assertEquals(new Double(8e06), load("8e06"));
73          assertEquals(new Double(8e06), load("8e+06"));
74          assertEquals(new Double(8000e06), load("8_000e06"));
75          assertEquals(new Double(123e-06), load("123e-06"));
76      }
77  }