|
23 | 23 |
|
24 | 24 | import java.io.IOException; |
25 | 25 | import java.io.InputStream; |
| 26 | +import java.lang.reflect.InvocationTargetException; |
| 27 | +import java.util.Random; |
26 | 28 |
|
27 | 29 | import static com.igormaznitsa.jbbp.TestUtils.getField; |
28 | | -import static org.junit.Assert.assertTrue; |
29 | | -import static org.junit.Assert.fail; |
| 30 | +import static org.junit.Assert.*; |
30 | 31 |
|
31 | 32 | public class JBBPToJBBPToJava6ConverterExpressionTest extends AbstractJBBPToJava6ConverterTest { |
32 | 33 |
|
| 34 | + private final JBBPBitInputStream UNLIMITED_STREAM = new JBBPBitInputStream(new InputStream() { |
| 35 | + @Override |
| 36 | + public int read() throws IOException { |
| 37 | + return RND.nextInt(); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + |
33 | 42 | private void assertExpression(final int etalonValue, final String expression) throws Exception { |
34 | | - assertTrue("Etalon value must not be zero or egative one : " + etalonValue, etalonValue > 0); |
| 43 | + assertTrue("Etalon value must not be zero or negative one : " + etalonValue, etalonValue > 0); |
35 | 44 | final Object obj = compileAndMakeInstance(String.format("byte [%s] data;", expression)); |
36 | 45 |
|
37 | | - final JBBPBitInputStream in = new JBBPBitInputStream(new InputStream() { |
38 | | - @Override |
39 | | - public int read() throws IOException { |
40 | | - return RND.nextInt(); |
41 | | - } |
42 | | - }); |
43 | | - |
44 | | - callRead(obj, in); |
| 46 | + callRead(obj, UNLIMITED_STREAM); |
45 | 47 |
|
46 | 48 | final int detectedlength = getField(obj, "data", byte[].class).length; |
47 | 49 |
|
@@ -76,11 +78,87 @@ public void testShifts() throws Exception { |
76 | 78 | assertExpression(123456 >> (3 << 2), "123456>>(3<<2)"); |
77 | 79 | } |
78 | 80 |
|
| 81 | + @Test |
| 82 | + public void testBrackets() throws Exception { |
| 83 | + assertExpression(3*(9/2), "3*(9/2)"); |
| 84 | + } |
| 85 | + |
79 | 86 | @Test |
80 | 87 | public void testComplex() throws Exception { |
81 | 88 | assertExpression(3 * 2 + 8 << 4 - 3, "3*2+8<<4-3"); |
82 | 89 | assertExpression(3 * 2 + 8 << 4 - 3 & 7, "3*2+8<<4-3&7"); |
| 90 | + assertExpression(60|7-~17%1, "60|7-~17%1"); |
83 | 91 | assertExpression((11 * (8 - 7)) % 13 + (1234 >> 3 << 2) >>> 1 + (13 - 1) / 2 + ((11 + 22) * 33 / 44 % 55) - (123 & 345 | 234 ^ ~123) & 255, "(11 * (8 - 7)) % 13 + ( 1234>>3<<2)>>>1 + (13 - 1) / 2 + ((11 + 22) * 33 / 44 % 55) - (123 & 345 | 234 ^ ~123) & 255"); |
84 | 92 | } |
85 | 93 |
|
| 94 | + @Test |
| 95 | + public void testSynthesidExpression() throws Exception { |
| 96 | + final Random rnd = new Random(5111975); |
| 97 | + final String [] operatorsTwo = new String [] {"-","+","*","/","%",">>",">>>","<<","^","|","&"}; |
| 98 | + final String [] operatorsOne = new String [] {"-","+","~"}; |
| 99 | + |
| 100 | + int rightCounter = 0; |
| 101 | + |
| 102 | + for(int i=0;i<1000;i++){ |
| 103 | + final StringBuilder buffer = new StringBuilder(); |
| 104 | + if (rnd.nextInt(100)>60) { |
| 105 | + buffer.append(operatorsOne[rnd.nextInt(operatorsOne.length)]); |
| 106 | + } |
| 107 | + buffer.append(1+rnd.nextInt(100)); |
| 108 | + |
| 109 | + buffer.append(operatorsTwo[rnd.nextInt(operatorsTwo.length)]); |
| 110 | + |
| 111 | + final int totalItems = rnd.nextInt(100)+1; |
| 112 | + int brakeCounter = 0; |
| 113 | + |
| 114 | + for(int j=0;j<totalItems;j++){ |
| 115 | + if (rnd.nextInt(100)>80) { |
| 116 | + buffer.append(operatorsOne[rnd.nextInt(operatorsOne.length)]); |
| 117 | + } |
| 118 | + buffer.append(1+rnd.nextInt(100)); |
| 119 | + buffer.append(operatorsTwo[rnd.nextInt(operatorsTwo.length)]); |
| 120 | + |
| 121 | + if (rnd.nextInt(100)>80) { |
| 122 | + buffer.append('('); |
| 123 | + brakeCounter++; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + buffer.append(1+rnd.nextInt(100)); |
| 128 | + |
| 129 | + while(brakeCounter>0){ |
| 130 | + buffer.append(')'); |
| 131 | + brakeCounter--; |
| 132 | + } |
| 133 | + |
| 134 | + String expression = buffer.toString().replace("--","-").replace("++","+"); |
| 135 | + |
| 136 | + Object theInstance; |
| 137 | + final StringBuilder src = new StringBuilder(); |
| 138 | + try { |
| 139 | + theInstance = compileAndMakeInstanceSrc("byte [" + expression + "] array;", " public static int makeExpressionResult(){ return " + expression + ";}",src); |
| 140 | + } catch(Exception ex){ |
| 141 | + fail("Can't compile : "+expression); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + try { |
| 146 | + final int etalon = (Integer) theInstance.getClass().getMethod("makeExpressionResult").invoke(null); |
| 147 | + if (etalon > 0 && etalon < 100000) { |
| 148 | + System.out.println("Testing expression : " + expression); |
| 149 | + assertEquals(src.toString(),etalon,getField(callRead(theInstance,new JBBPBitInputStream(UNLIMITED_STREAM)),"array", byte[].class).length); |
| 150 | + rightCounter ++; |
| 151 | + } |
| 152 | + }catch (InvocationTargetException ex){ |
| 153 | + if (!(ex.getCause() instanceof ArithmeticException)) { |
| 154 | + ex.printStackTrace(); |
| 155 | + fail("Unexpected exception : "+ex.getCause()); |
| 156 | + return; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + System.out.println("Totally generated right expressions : "+rightCounter); |
| 162 | + } |
| 163 | + |
86 | 164 | } |
0 commit comments