| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.types.samples; |
| 34 | | | |
| 35 | | | import java.math.BigDecimal; |
| 36 | | | |
| 37 | | | import org.jcoderz.commons.ArgumentFractionDigitsViolationException; |
| 38 | | | import org.jcoderz.commons.ArgumentMalformedException; |
| 39 | | | import org.jcoderz.commons.ArgumentMaxValueViolationException; |
| 40 | | | import org.jcoderz.commons.ArgumentMinValueViolationException; |
| 41 | | | |
| 42 | | | import junit.framework.TestCase; |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | @author |
| 48 | | | |
| 49 | 100 | | public 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 | | | {@link } |
| 57 | | | |
| 58 | | | public void testHashCode () |
| 59 | | | { |
| 60 | 75 | (1) | assertFalse("Different values should have different hashCode.", |
| 61 | | | SampleFixPoint.fromString("1").hashCode() |
| 62 | | | == SampleFixPoint.fromString("2").hashCode()); |
| 63 | 100 | | } |
| 64 | | | |
| 65 | | | |
| 66 | | | {@link } |
| 67 | | | |
| 68 | | | public void testHashCodeSame () |
| 69 | | | { |
| 70 | 100 | | assertEquals("Same values should have different hashCode.", |
| 71 | | | SampleFixPoint.fromString("1").hashCode(), |
| 72 | | | SampleFixPoint.fromString("1").hashCode()); |
| 73 | 100 | | } |
| 74 | | | |
| 75 | | | {@link } |
| 76 | | | |
| 77 | | | public void testIntValue () |
| 78 | | | { |
| 79 | 100 | | assertEquals("Int value should not change.", INT_TEST_VALUE, |
| 80 | | | SampleFixPoint.valueOf(INT_TEST_VALUE).intValue()); |
| 81 | 100 | | } |
| 82 | | | |
| 83 | | | |
| 84 | | | {@link } |
| 85 | | | |
| 86 | | | public void testLongValue () |
| 87 | | | { |
| 88 | 100 | | assertEquals("Long value should not change.", LONG_TEST_VALUE, |
| 89 | | | SampleFixPoint.valueOf(LONG_TEST_VALUE).longValue()); |
| 90 | 100 | | } |
| 91 | | | |
| 92 | | | |
| 93 | | | {@link } |
| 94 | | | |
| 95 | | | public void testFromString () |
| 96 | | | { |
| 97 | 100 | | assertEquals("From string should not change value.", LONG_TEST_VALUE, |
| 98 | | | SampleFixPoint.fromString( |
| 99 | | | String.valueOf(LONG_TEST_VALUE)).longValue()); |
| 100 | 100 | | } |
| 101 | | | |
| 102 | | | |
| 103 | | | {@link } |
| 104 | | | |
| 105 | | | public void testFromStringDecimal () |
| 106 | | | { |
| 107 | 100 | | final BigDecimal test = new BigDecimal("-1.23"); |
| 108 | 100 | | assertEquals("From string should not change value.", test, |
| 109 | | | SampleFixPoint.fromString(String.valueOf(test)).toBigDecimal()); |
| 110 | 100 | | } |
| 111 | | | |
| 112 | | | |
| 113 | | | {@link } |
| 114 | | | |
| 115 | | | public void testValueOfBigDecimal () |
| 116 | | | { |
| 117 | 100 | | final BigDecimal test = new BigDecimal("-1.23"); |
| 118 | 100 | | assertEquals("From big decimal should not change value.", test, |
| 119 | | | SampleFixPoint.valueOf(test).toBigDecimal()); |
| 120 | 100 | | } |
| 121 | | | |
| 122 | | | |
| 123 | | | |
| 124 | | | |
| 125 | | | public void testFractionDigits () |
| 126 | | | { |
| 127 | | | try |
| 128 | | | { |
| 129 | 0 | (2) | SampleFixPoint s = SampleFixPoint.fromString("1.234"); |
| 130 | 0 | (3) | fail("Expected exception but got " + s + "."); |
| 131 | | | } |
| 132 | 100 | | catch (ArgumentFractionDigitsViolationException ex) |
| 133 | | | { |
| 134 | | | |
| 135 | 0 | | } |
| 136 | 100 | | } |
| 137 | | | |
| 138 | | | |
| 139 | | | |
| 140 | | | |
| 141 | | | public void testMaxValue () |
| 142 | | | { |
| 143 | | | try |
| 144 | | | { |
| 145 | 0 | (4) | SampleFixPoint s = SampleFixPoint.fromString("12345.67"); |
| 146 | 0 | | fail("Expected exception but got " + s + "."); |
| 147 | | | } |
| 148 | 100 | | catch (ArgumentMaxValueViolationException ex) |
| 149 | | | { |
| 150 | | | |
| 151 | 0 | | } |
| 152 | 100 | | } |
| 153 | | | |
| 154 | | | |
| 155 | | | |
| 156 | | | |
| 157 | | | public void testMinValue () |
| 158 | | | { |
| 159 | | | try |
| 160 | | | { |
| 161 | 0 | (5) | SampleFixPoint s = SampleFixPoint.fromString("-345.67"); |
| 162 | 0 | | fail("Expected exception but got " + s + "."); |
| 163 | | | } |
| 164 | 100 | | catch (ArgumentMinValueViolationException ex) |
| 165 | | | { |
| 166 | | | |
| 167 | 0 | | } |
| 168 | 100 | | } |
| 169 | | | |
| 170 | | | |
| 171 | | | |
| 172 | | | |
| 173 | | | public void testMalformedString () |
| 174 | | | { |
| 175 | | | try |
| 176 | | | { |
| 177 | 0 | (6) | SampleFixPoint s = SampleFixPoint.fromString("0xFF12"); |
| 178 | 0 | | fail("Expected exception but got " + s + "."); |
| 179 | | | } |
| 180 | 100 | | catch (ArgumentMalformedException ex) |
| 181 | | | { |
| 182 | | | |
| 183 | 0 | | } |
| 184 | 100 | | } |
| 185 | | | |
| 186 | | | |
| 187 | | | |
| 188 | | | |
| 189 | | | public void testComparison () |
| 190 | | | { |
| 191 | 100 | | assertEquals("Comparing equal values", 0, |
| 192 | | (7) | SampleFixPoint.fromString("0.01") |
| 193 | | | .compareTo(SampleFixPoint.fromString("0.01"))); |
| 194 | 75 | | assertTrue("Comparing different values", |
| 195 | | | SampleFixPoint.fromString("0.01") |
| 196 | | | .compareTo(SampleFixPoint.fromString("0.02")) < 0); |
| 197 | 75 | | assertTrue("Comparing different values", |
| 198 | | | SampleFixPoint.fromString("0.02") |
| 199 | | | .compareTo(SampleFixPoint.fromString("0.01")) > 0); |
| 200 | 100 | | } |
| 201 | | | |
| 202 | | | |
| 203 | | | |
| 204 | | | |
| 205 | | | public void testStatics () |
| 206 | | | { |
| 207 | 100 | | assertEquals("SampleFixPoint.MAX_VALUE_SCALED", |
| 208 | | (8) | 999, SampleFixPoint.MAX_VALUE_SCALED); |
| 209 | 100 | | assertEquals("SampleFixPoint.MIN_VALUE_SCALED", |
| 210 | | (9) | -100, SampleFixPoint.MIN_VALUE_SCALED); |
| 211 | 100 | | assertEquals("SampleFixPoint.MAX_VALUE", |
| 212 | | | "999.99", SampleFixPoint.MAX_VALUE.toString()); |
| 213 | 100 | | assertEquals("SampleFixPoint.MIN_VALUE", |
| 214 | | | "-100.50", SampleFixPoint.MIN_VALUE.toString()); |
| 215 | 100 | | assertEquals("SampleFixPoint.SCALE", |
| 216 | | (10) | 2, SampleFixPoint.SCALE); |
| 217 | 100 | | assertEquals("SampleFixPoint.DECIMAL_SCALE", |
| 218 | | (11) | 100, SampleFixPoint.DECIMAL_SCALE); |
| 219 | 100 | | assertEquals("SampleFixPoint.TOTAL_DIGITS", |
| 220 | | (12) | 5, SampleFixPoint.TOTAL_DIGITS); |
| 221 | 100 | | } |
| 222 | | | } |