Fixed and added a lot of tests
[Mograsim.git] / net.mograsim.logic.ui.am2900 / test / net / mograsim / logic / ui / am2900 / TestUtil.java
index 23eb10f..8388cae 100644 (file)
@@ -9,6 +9,8 @@ public final class TestUtil
 \r
        /**\r
         * Transforms the last four bits of an int to a string that contains the binary ('1' and '0') representation of the 4 bits\r
+        * \r
+        * @author Christian Femers\r
         */\r
        public static String to4bitBin(int x)\r
        {\r
@@ -19,4 +21,64 @@ public final class TestUtil
                sb.append((x & 0b0001) == 0 ? '0' : '1');\r
                return sb.toString();\r
        }\r
+\r
+       /**\r
+        * Transforms the given boolean to a string that contains the binary ('1' and '0') representation of the bit\r
+        * \r
+        * @author Christian Femers\r
+        */\r
+       public static String to1bitBin(boolean bitIsSet)\r
+       {\r
+               return bitIsSet ? "1" : "0";\r
+       }\r
+\r
+       /**\r
+        * 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
+        * int is equal to zero.\r
+        * \r
+        * @author Christian Femers\r
+        */\r
+       public static String to1bitBin(int someInt)\r
+       {\r
+               return someInt != 0 ? "1" : "0";\r
+       }\r
+\r
+       /**\r
+        * 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
+        * negative)\r
+        * \r
+        * @author Christian Femers\r
+        */\r
+       public static int signed4ToSigned32(int signed4bit)\r
+       {\r
+               if ((signed4bit & 0b1000) > 0)\r
+                       return signed4bit | 0xFF_FF_FF_F0;\r
+               return signed4bit & 0x00_00_00_0F;\r
+       }\r
+\r
+       /**\r
+        * 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
+        * 16 bit int is negative)\r
+        * \r
+        * @author Christian Femers\r
+        */\r
+       public static int signed16ToSigned32(int signed16bit)\r
+       {\r
+               return (short) signed16bit;\r
+       }\r
+\r
+       /**\r
+        * Transforms the last n bits of an int to a string that contains the binary ('1' and '0') representation of the n bits\r
+        * \r
+        * @author Christian Femers\r
+        */\r
+       public static String toNbitString(int x, int n)\r
+       {\r
+               StringBuilder sb = new StringBuilder(n);\r
+               for (int i = 0; i < n; i++)\r
+               {\r
+                       sb.append((x >> i) & 1);\r
+               }\r
+               return sb.reverse().toString();\r
+       }\r
 }\r