Fixed and added a lot of tests
[Mograsim.git] / net.mograsim.logic.ui.am2900 / test / net / mograsim / logic / ui / am2900 / TestUtil.java
1 package net.mograsim.logic.ui.am2900;\r
2 \r
3 public final class TestUtil\r
4 {\r
5         private TestUtil()\r
6         {\r
7 \r
8         }\r
9 \r
10         /**\r
11          * Transforms the last four bits of an int to a string that contains the binary ('1' and '0') representation of the 4 bits\r
12          * \r
13          * @author Christian Femers\r
14          */\r
15         public static String to4bitBin(int x)\r
16         {\r
17                 StringBuilder sb = new StringBuilder(4);\r
18                 sb.append((x & 0b1000) == 0 ? '0' : '1');\r
19                 sb.append((x & 0b0100) == 0 ? '0' : '1');\r
20                 sb.append((x & 0b0010) == 0 ? '0' : '1');\r
21                 sb.append((x & 0b0001) == 0 ? '0' : '1');\r
22                 return sb.toString();\r
23         }\r
24 \r
25         /**\r
26          * Transforms the given boolean to a string that contains the binary ('1' and '0') representation of the bit\r
27          * \r
28          * @author Christian Femers\r
29          */\r
30         public static String to1bitBin(boolean bitIsSet)\r
31         {\r
32                 return bitIsSet ? "1" : "0";\r
33         }\r
34 \r
35         /**\r
36          * Transforms the given int to a string that contains the binary ('1' and '0') representation of the int. "0" is only returned when the\r
37          * int is equal to zero.\r
38          * \r
39          * @author Christian Femers\r
40          */\r
41         public static String to1bitBin(int someInt)\r
42         {\r
43                 return someInt != 0 ? "1" : "0";\r
44         }\r
45 \r
46         /**\r
47          * Transforms a 4 bit signed integer (-8 to 7) to a int representing the same number. (Adding leading 1-bits if the 4 bit int is\r
48          * negative)\r
49          * \r
50          * @author Christian Femers\r
51          */\r
52         public static int signed4ToSigned32(int signed4bit)\r
53         {\r
54                 if ((signed4bit & 0b1000) > 0)\r
55                         return signed4bit | 0xFF_FF_FF_F0;\r
56                 return signed4bit & 0x00_00_00_0F;\r
57         }\r
58 \r
59         /**\r
60          * Transforms a 16 bit signed integer (-32768 to 32767 - a short) to a int representing the same number. (Adding leading 1-bits if the\r
61          * 16 bit int is negative)\r
62          * \r
63          * @author Christian Femers\r
64          */\r
65         public static int signed16ToSigned32(int signed16bit)\r
66         {\r
67                 return (short) signed16bit;\r
68         }\r
69 \r
70         /**\r
71          * Transforms the last n bits of an int to a string that contains the binary ('1' and '0') representation of the n bits\r
72          * \r
73          * @author Christian Femers\r
74          */\r
75         public static String toNbitString(int x, int n)\r
76         {\r
77                 StringBuilder sb = new StringBuilder(n);\r
78                 for (int i = 0; i < n; i++)\r
79                 {\r
80                         sb.append((x >> i) & 1);\r
81                 }\r
82                 return sb.reverse().toString();\r
83         }\r
84 }\r