root/trunk/test/java/org/jcoderz/commons/types/samples/SampleFixPointTest.java

Revision 1011, 7.3 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2008, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.types.samples;
34
35import java.math.BigDecimal;
36
37import org.jcoderz.commons.ArgumentFractionDigitsViolationException;
38import org.jcoderz.commons.ArgumentMalformedException;
39import org.jcoderz.commons.ArgumentMaxValueViolationException;
40import org.jcoderz.commons.ArgumentMinValueViolationException;
41
42import junit.framework.TestCase;
43
44/**
45 * Test the FixPoint class generator.
46 *
47 * @author Andreas Mandel
48 */
49public class SampleFixPointTest
50    extends TestCase
51{
52    private static final int INT_TEST_VALUE = -100;
53    private static final long LONG_TEST_VALUE = 40;
54
55    /**
56     * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#hashCode()}.
57     */
58    public void testHashCode ()
59    {
60        assertFalse("Different values should have different hashCode.",
61            SampleFixPoint.fromString("1").hashCode()
62            == SampleFixPoint.fromString("2").hashCode());
63    }
64
65    /**
66     * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#hashCode()}.
67     */
68    public void testHashCodeSame ()
69    {
70        assertEquals("Same values should have different hashCode.",
71            SampleFixPoint.fromString("1").hashCode(),
72            SampleFixPoint.fromString("1").hashCode());
73    }
74    /**
75     * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#intValue()}.
76     */
77    public void testIntValue ()
78    {
79        assertEquals("Int value should not change.", INT_TEST_VALUE,
80            SampleFixPoint.valueOf(INT_TEST_VALUE).intValue());
81    }
82
83    /**
84     * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#longValue()}.
85     */
86    public void testLongValue ()
87    {
88        assertEquals("Long value should not change.", LONG_TEST_VALUE,
89            SampleFixPoint.valueOf(LONG_TEST_VALUE).longValue());
90    }
91
92    /**
93     * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#fromString(java.lang.String)}.
94     */
95    public void testFromString ()
96    {
97        assertEquals("From string should not change value.", LONG_TEST_VALUE,
98            SampleFixPoint.fromString(
99                String.valueOf(LONG_TEST_VALUE)).longValue());
100    }
101
102    /**
103     * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#fromString(java.lang.String)}.
104     */
105    public void testFromStringDecimal ()
106    {
107        final BigDecimal test = new BigDecimal("-1.23");
108        assertEquals("From string should not change value.", test,
109            SampleFixPoint.fromString(String.valueOf(test)).toBigDecimal());
110    }
111
112    /**
113     * Test method for {@link org.jcoderz.commons.types.samples.SampleFixPoint#valueOf(java.math.BigDecimal)}.
114     */
115    public void testValueOfBigDecimal ()
116    {
117        final BigDecimal test = new BigDecimal("-1.23");
118        assertEquals("From big decimal should not change value.", test,
119            SampleFixPoint.valueOf(test).toBigDecimal());
120    }
121
122    /**
123     * Test method for fraction digit overflow.
124     */
125    public void testFractionDigits ()
126    {
127        try
128        {
129            SampleFixPoint s = SampleFixPoint.fromString("1.234");
130            fail("Expected exception but got " + s + ".");
131        }
132        catch (ArgumentFractionDigitsViolationException ex)
133        {
134            // Expected...
135        }
136    }
137
138    /**
139     * Test method for max value overflow.
140     */
141    public void testMaxValue ()
142    {
143        try
144        {
145            SampleFixPoint s = SampleFixPoint.fromString("12345.67");
146            fail("Expected exception but got " + s + ".");
147        }
148        catch (ArgumentMaxValueViolationException ex)
149        {
150            // Expected...
151        }
152    }
153
154    /**
155     * Test method for min value overflow.
156     */
157    public void testMinValue ()
158    {
159        try
160        {
161            SampleFixPoint s = SampleFixPoint.fromString("-345.67");
162            fail("Expected exception but got " + s + ".");
163        }
164        catch (ArgumentMinValueViolationException ex)
165        {
166            // Expected...
167        }
168    }
169
170    /**
171     * Test method for malformed string.
172     */
173    public void testMalformedString ()
174    {
175        try
176        {
177            SampleFixPoint s = SampleFixPoint.fromString("0xFF12");
178            fail("Expected exception but got " + s + ".");
179        }
180        catch (ArgumentMalformedException ex)
181        {
182            // Expected...
183        }
184    }
185
186    /**
187     * Test method for comparison.
188     */
189    public void testComparison ()
190    {
191        assertEquals("Comparing equal values", 0,
192            SampleFixPoint.fromString("0.01")
193            .compareTo(SampleFixPoint.fromString("0.01")));
194        assertTrue("Comparing different values",
195            SampleFixPoint.fromString("0.01")
196                .compareTo(SampleFixPoint.fromString("0.02")) < 0);
197        assertTrue("Comparing different values",
198            SampleFixPoint.fromString("0.02")
199                .compareTo(SampleFixPoint.fromString("0.01")) > 0);
200    }
201
202    /**
203     * Test value of static members.
204     */
205    public void testStatics ()
206    {
207        assertEquals("SampleFixPoint.MAX_VALUE_SCALED",
208            999, SampleFixPoint.MAX_VALUE_SCALED);
209        assertEquals("SampleFixPoint.MIN_VALUE_SCALED",
210            -100, SampleFixPoint.MIN_VALUE_SCALED);
211        assertEquals("SampleFixPoint.MAX_VALUE",
212            "999.99", SampleFixPoint.MAX_VALUE.toString());
213        assertEquals("SampleFixPoint.MIN_VALUE",
214            "-100.50", SampleFixPoint.MIN_VALUE.toString());
215        assertEquals("SampleFixPoint.SCALE",
216            2, SampleFixPoint.SCALE);
217        assertEquals("SampleFixPoint.DECIMAL_SCALE",
218            100, SampleFixPoint.DECIMAL_SCALE);
219        assertEquals("SampleFixPoint.TOTAL_DIGITS",
220            5, SampleFixPoint.TOTAL_DIGITS);
221    }
222}
Note: See TracBrowser for help on using the browser.