1 /*
2  * Copyright (c) 2018 Helmut Neemann
3  * Use of this source code is governed by the GPL v3 license
4  * that can be found in the LICENSE file.
5  */
6 package de.neemann.digital.core;
7 
8 import de.neemann.digital.core.element.ElementAttributes;
9 import de.neemann.digital.core.element.Keys;
10 import junit.framework.TestCase;
11 
12 public class IntFormatTest extends TestCase {
13 
testHex()14     public void testHex() {
15         ValueFormatter vf = IntFormat.HEX_FORMATTER;
16         assertEquals("0x1", vf.formatToView(new Value(1, 1)));
17         assertEquals("0x1", vf.formatToView(new Value(1, 2)));
18         assertEquals("0x1", vf.formatToView(new Value(1, 3)));
19         assertEquals("0x1", vf.formatToView(new Value(1, 4)));
20         assertEquals("0xF", vf.formatToView(new Value(-1, 4)));
21         assertEquals("0x01", vf.formatToView(new Value(1, 5)));
22         assertEquals("0x1F", vf.formatToView(new Value(-1, 5)));
23         assertEquals("0xFFF", vf.formatToView(new Value(-1, 12)));
24         assertEquals("0x1FFF", vf.formatToView(new Value(-1, 13)));
25         assertEquals("0x3FFF", vf.formatToView(new Value(-1, 14)));
26         assertEquals("0x7FFF", vf.formatToView(new Value(-1, 15)));
27         assertEquals("0xFFFF", vf.formatToView(new Value(-1, 16)));
28         assertEquals("0xFEDCBA9876543210", vf.formatToView(new Value(0xFEDCBA9876543210L, 64)));
29     }
30 
testBin()31     public void testBin() {
32         assertFalse(IntFormat.bin.dependsOnAttributes());
33         ValueFormatter vf = IntFormat.bin.createFormatter(null);
34         assertEquals("0b1", vf.formatToView(new Value(1, 1)));
35         assertEquals("0b01", vf.formatToView(new Value(1, 2)));
36         assertEquals("0b001", vf.formatToView(new Value(1, 3)));
37         assertEquals("0b111", vf.formatToView(new Value(-1, 3)));
38         assertEquals("0b1111111111111111111111111111111111111111111111111111111111111111", vf.formatToView(new Value(-1, 64)));
39         assertEquals("0b1000111111111111111111111111111111111111111111111111111111111111", vf.formatToView(new Value(0x8fffffffffffffffL, 64)));
40     }
41 
testDec()42     public void testDec() {
43         assertFalse(IntFormat.dec.dependsOnAttributes());
44         assertFalse(IntFormat.decSigned.dependsOnAttributes());
45 
46         assertEquals("3", IntFormat.dec.createFormatter(null).formatToView(new Value(-1, 2)));
47         assertEquals("-1", IntFormat.decSigned.createFormatter(null).formatToView(new Value(-1, 2)));
48     }
49 
testDef()50     public void testDef() {
51         ValueFormatter vf = IntFormat.DEFAULT_FORMATTER;
52         assertEquals("3", vf.formatToView(new Value(3, 64)));
53         assertEquals("0x113", vf.formatToView(new Value(0x113, 64)));
54         assertEquals("1A3", vf.formatToView(new Value(0x1A3, 64)));
55         assertEquals("FFFFFFFFFFFFFFFF", vf.formatToView(new Value(-1, 64)));
56     }
57 
testFixedPoint()58     public void testFixedPoint() {
59         ValueFormatter f1 = IntFormat.fixed.createFormatter(new ElementAttributes().set(Keys.FIXED_POINT, 1));
60         ValueFormatter f2 = IntFormat.fixed.createFormatter(new ElementAttributes().set(Keys.FIXED_POINT, 2));
61         ValueFormatter fs1 = IntFormat.fixedSigned.createFormatter(new ElementAttributes().set(Keys.FIXED_POINT, 1));
62         ValueFormatter fs2 = IntFormat.fixedSigned.createFormatter(new ElementAttributes().set(Keys.FIXED_POINT, 2));
63 
64         assertEquals("1.5", f1.formatToView(new Value(3, 8)));
65         assertEquals("0.75", f2.formatToView(new Value(3, 8)));
66         assertEquals("-1.5", fs1.formatToView(new Value(-3, 8)));
67         assertEquals("-0.75", fs2.formatToView(new Value(-3, 8)));
68         assertEquals("126.5", f1.formatToView(new Value(-3, 8)));
69         assertEquals("63.25", f2.formatToView(new Value(-3, 8)));
70 
71         assertEquals("1.5:1", f1.formatToEdit(new Value(3, 8)));
72         assertEquals("0.75:2", f2.formatToEdit(new Value(3, 8)));
73         assertEquals("-1.5:1", fs1.formatToEdit(new Value(-3, 8)));
74         assertEquals("-0.75:2", fs2.formatToEdit(new Value(-3, 8)));
75         assertEquals("126.5:1", f1.formatToEdit(new Value(-3, 8)));
76         assertEquals("63.25:2", f2.formatToEdit(new Value(-3, 8)));
77 
78         assertEquals("Z", f1.formatToView(new Value(8)));
79         assertEquals("Z", f1.formatToEdit(new Value(8)));
80     }
81 
testFloatingPoint()82     public void testFloatingPoint() {
83         assertFalse(IntFormat.floating.dependsOnAttributes());
84         ValueFormatter vf = IntFormat.floating.createFormatter(null);
85         Value three32 = new Value(Float.floatToIntBits((float) 3), 32);
86         Value three64 = new Value(Double.doubleToLongBits(3), 64);
87 
88         assertEquals("3.0", vf.formatToView(three32));
89         assertEquals("3.0", vf.formatToView(three64));
90 
91         assertEquals("3.0", vf.formatToEdit(three32));
92         assertEquals("3.0d", vf.formatToEdit(three64));
93 
94         assertEquals("Z", vf.formatToView(new Value(32)));
95         assertEquals("Z", vf.formatToEdit(new Value(32)));
96         assertEquals("Z", vf.formatToView(new Value(64)));
97         assertEquals("Z", vf.formatToEdit(new Value(64)));
98     }
99 
100     /**
101      * Ensures that it is possible to convert a string representation obtained by {@link ValueFormatter#formatToEdit(Value)}
102      * back to the same value by {@link Bits#decode(String)}
103      */
testBitDecodeConstraint()104     public void testBitDecodeConstraint() throws Bits.NumberFormatException {
105         for (IntFormat f : IntFormat.values()) {
106             if (f.equals(IntFormat.ascii)) {
107                 checkConstraint(f, tableAscii); // ascii supports only 16 bit
108             } else if (f.equals(IntFormat.fixed) || f.equals(IntFormat.fixedSigned)) {
109                 checkConstraintFixedPoint(f, tableFixedPoint);
110             } else if (f.equals(IntFormat.floating)) {
111                 checkConstraint(f, tableFloat);
112             } else {
113                 checkConstraint(f, table);
114             }
115         }
116     }
117 
checkConstraintFixedPoint(IntFormat f, Value[] table)118     private void checkConstraintFixedPoint(IntFormat f, Value[] table) throws Bits.NumberFormatException {
119         for (int digits = 1; digits < 5; digits++) {
120             ValueFormatter format = f.createFormatter(new ElementAttributes().set(Keys.FIXED_POINT, digits));
121             for (Value val : table) {
122                 final String str = format.formatToEdit(val);
123                 final Value conv = new Value(Bits.decode(str), val.getBits());
124                 assertTrue(f.name() + ":" + val + " != " + conv, val.isEqual(conv));
125             }
126         }
127     }
128 
129     private static final Value[] table = new Value[]{
130             new Value(1, 2),
131             new Value(-1, 2),
132             new Value(1, 64),
133             new Value(10, 8),
134             new Value(17, 8),
135             new Value(-1, 64),
136             new Value(0x4fffffffffffffffL, 63),
137             new Value(0x8fffffffffffffffL, 64),
138     };
139 
140     private static final Value[] tableFixedPoint = new Value[]{
141             new Value(1, 2),
142             new Value(-1, 2),
143             new Value(1, 64),
144             new Value(10, 8),
145             new Value(17, 8),
146             new Value(-1, 64),
147     };
148 
149     private static final Value[] tableFloat = new Value[]{
150             new Value(1, 2),
151             new Value(Float.floatToIntBits(-1), 32),
152             new Value(Float.floatToIntBits(1.2f), 32),
153             new Value(Double.doubleToLongBits(-1), 64),
154             new Value(Double.doubleToLongBits(1.2f), 64),
155     };
156 
157     private static final Value[] tableAscii = new Value[]{
158             new Value(65, 8),
159             new Value(66, 8),
160             new Value(1000, 16),
161             new Value(-1, 7),
162             new Value(-1, 7),
163     };
164 
checkConstraint(IntFormat format, Value[] table)165     private void checkConstraint(IntFormat format, Value[] table) throws Bits.NumberFormatException {
166         for (Value val : table) {
167             final String str = format.createFormatter(null).formatToEdit(val);
168             final Value conv = new Value(Bits.decode(str), val.getBits());
169             assertTrue(format.getClass().getSimpleName() + ":" + val + " != " + conv, val.isEqual(conv));
170         }
171     }
172 
testStrLen()173     public void testStrLen() {
174         assertFalse(IntFormat.hex.dependsOnAttributes());
175         ValueFormatter vf = IntFormat.hex.createFormatter(null);
176         assertEquals(6, vf.strLen(16));
177         assertEquals(6, vf.strLen(15));
178         assertEquals(6, vf.strLen(14));
179         assertEquals(6, vf.strLen(13));
180         assertEquals(5, vf.strLen(12));
181 
182         vf = IntFormat.bin.createFormatter(null);
183         assertEquals(18, vf.strLen(16));
184         assertEquals(17, vf.strLen(15));
185         assertEquals(16, vf.strLen(14));
186 
187         vf = IntFormat.dec.createFormatter(null);
188         assertEquals(3, vf.strLen(8));
189         assertEquals(3, vf.strLen(9));
190         assertEquals(4, vf.strLen(10));
191         assertEquals(19, vf.strLen(60));
192         assertEquals(19, vf.strLen(61));
193         assertEquals(19, vf.strLen(62));
194         assertEquals(19, vf.strLen(63));
195         assertEquals(20, vf.strLen(64));
196 
197         vf = IntFormat.decSigned.createFormatter(null);
198         assertEquals(4, vf.strLen(8));
199         assertEquals(4, vf.strLen(9));
200         assertEquals(4, vf.strLen(10));
201         assertEquals(5, vf.strLen(11));
202         assertEquals(20, vf.strLen(62));
203         assertEquals(20, vf.strLen(63));
204         assertEquals(20, vf.strLen(64));
205 
206         vf = IntFormat.oct.createFormatter(null);
207         assertEquals(4, vf.strLen(4));
208         assertEquals(4, vf.strLen(5));
209         assertEquals(4, vf.strLen(6));
210         assertEquals(5, vf.strLen(7));
211         assertEquals(5, vf.strLen(8));
212         assertEquals(5, vf.strLen(9));
213         assertEquals(6, vf.strLen(10));
214     }
215 
testDragInt()216     public void testDragInt() {
217         ValueFormatter f = IntFormat.HEX_FORMATTER;
218         checkDragInt(f, 1, 2, 0.004);
219         checkDragInt(f, 2, 3, 0.004);
220         checkDragInt(f, 1, 11, 0.04);
221         checkDragInt(f, 2, 12, 0.04);
222         checkDragInt(f, 1, 103, 0.4);
223         checkDragInt(f, 2, 104, 0.4);
224     }
225 
testDragSigned()226     public void testDragSigned() {
227         assertFalse(IntFormat.decSigned.dependsOnAttributes());
228         ValueFormatter f = IntFormat.decSigned.createFormatter(null);
229         checkDragInt(f, 1, 2, 0.004);
230         checkDragInt(f, 2, 3, 0.004);
231         checkDragInt(f, 1, 0, -0.004);
232         checkDragInt(f, 2, 1, -0.004);
233     }
234 
checkDragInt(ValueFormatter f, long initial, long expected, double inc)235     private void checkDragInt(ValueFormatter f, long initial, long expected, double inc) {
236         assertEquals(expected, f.dragValue(initial, 8, inc));
237     }
238 
testDragFloat()239     public void testDragFloat() {
240         assertFalse(IntFormat.floating.dependsOnAttributes());
241         ValueFormatter f = IntFormat.floating.createFormatter(null);
242         checkDragFloat(f, 1, 1.001, 0.004);
243         checkDragFloat(f, 2, 2.002, 0.004);
244         checkDragFloat(f, 1, 1.002, 0.04);
245         checkDragFloat(f, 2, 2.004, 0.04);
246         checkDragFloat(f, 1, 1.4, 0.4);
247         checkDragFloat(f, 2, 2.8, 0.4);
248         checkDragFloat(f, 1, 3000, 1);
249         checkDragFloat(f, 2, 7000, 1);
250         checkDragFloat(f, 1, -3000, -1);
251         checkDragFloat(f, 2, -7000, -1);
252     }
253 
checkDragFloat(ValueFormatter f, double initial, double expected, double inc)254     private void checkDragFloat(ValueFormatter f, double initial, double expected, double inc) {
255         long d = f.dragValue(Float.floatToIntBits((float) initial), 32, inc);
256         float result = Float.intBitsToFloat((int) d);
257         assertEquals((float) expected, result, 1e-5);
258 
259         d = f.dragValue(Double.doubleToLongBits(initial), 64, inc);
260         double resultd = Double.longBitsToDouble(d);
261         assertEquals(expected, resultd, 1e-5);
262     }
263 }
264