How does this java code execute code?

I have a project in my programming class and I don't have a test case. I look in the test driver and find this:

private static boolean testSquareArch() { boolean pass = true; int test = 1; int cnt; Square sq; Class cl; System.out.println("Square architecture tests..."); sq = new Square(true, true, true, true, 0, 0); cl = sq.getClass(); cnt = cl.getFields().length; pass &= test(cnt == 5, test++); //FAILING THIS TEST 

What does it do and how does it check my code?

Also, when I'm here, what does it do?

  // Count and test number of of PACKAGE fields cnt = cl.getDeclaredFields().length - countModifiers(cl.getDeclaredFields(), Modifier.PRIVATE) - countModifiers(cl.getDeclaredFields(), Modifier.PROTECTED) - countModifiers(cl.getDeclaredFields(), Modifier.PUBLIC); pass &= test(cnt == 5, test++); // Test 8 

I canโ€™t check these test cases and just want to know why. Thanks

+4
source share
5 answers

If you are curious about &= , here is a quote from JLS 15.22.2. Logical operators Boolean &, ^ and | .

For &, the result value is true if both operand values โ€‹โ€‹are true; otherwise the result will be false.

Operators & and | for boolean elements are the lesser known cousins && ( 15.23 ) and || ( 15.24 ). The last two are conditional statements; they short-circuit and evaluate only the right side, if it is really necessary.

The && operator is similar to & (ยง15.22.2), but evaluates its right operand only if the value of its left operand is true .

&= is a composite version of destination & . See JLS 15.26.2. Connection assignment operators .

The compound assignment expression E1 op = E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type E1, except that E1 is evaluated only once.

In other words, they are equivalent (assuming boolean b ):

 b &= bool_expr; b = b & bool_expr; if (!bool_expr) b = false; 

This may be your problem ( java.lang.Class # getDeclaredFields ):

Returns an array of Field objects that reflect all the fields declared by the class or interface represented by this Class object. This includes open, secure, standard (batch) access and private fields , but excludes inherited fields .

Your account may be incorrect if you also want to read the inherited fields.

+1
source

If you ask about the assignment operator &= , it only stores the true variable if the right-hand side of the argument is also true (if it was already false, it will remain false). It works just like += , and a &= b matches a = a & b , the & operator is a Boolean union (AND).

  pass &= test(cnt == 5, test++); 

is short for

  if( ! test( cnt == 5, test ) ) pass = false; test++; 

I assume that it is part of the unit testing code and claims that cnt == 5 also counts the number of tests and the total result (pass or fail).

Using Junit, you donโ€™t have to manually track the number of tests or the end result, and you could write

 assertEquals("count is correct", 5, cnt); 

It will also provide a useful conclusion, which will help to find out what exactly failed (for example, the value of an incorrect calculation).

+3
source

It seems to check the number of fields you declared in your class, and how many of them are private visibility.

+1
source

In Java, namespaces for variables and methods do not conflict. Thus, it looks like the driver has both a variable and a method called "test".

Without the test () method and native source code, it's hard to say what is wrong, but it looks like you have the wrong number of fields in your square class. (There should be five of them.)

0
source

Yes, several fields are required to execute the instruction. This should fix the namespace.

0
source

Source: https://habr.com/ru/post/1302203/


All Articles