I have a project in my programming class and I'm failing a test case. I look into 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 this do and how does it check my code?
Also while I'm here, what does this 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'm failing these test cases and just want to know why. Thanks
If you are asking about the &=
assignment operator, it keeps the variable true only if the right side argument is also true (if it was already false, it will stay false). It works just like +=
and a &= b
is the same as a = a & b
, the operator &
being the boolean conjuction (AND).
pass &= test(cnt == 5, test++);
is shorthand for
if( ! test( cnt == 5, test ) )
pass = false;
test++;
I assume that it is part of unit testing code and asserts that cnt == 5, also counting the number of tests and the total result (pass or fail).
Using Junit, you do not have to manually take care of the number of tests, or the end result, and you could write
assertEquals("count is correct", 5, cnt);
This will also produce useful output to help figure out exactly what failed (such as the value of an incorrect count).
If you're wondering about &=
, here's a quote from JLS 15.22.2. Boolean Logical Operators &, ^, and |.
For &, the result value is true if both operand values are true; otherwise, the result is false.
The operators &
and |
for booleans are the lesser known cousins of &&
(15.23) and ||
(15.24). The latter two are "conditional" operators; they short-circuit and only evaluate the right hand side if it's actually needed.
The
&&
operator is like&
(§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand istrue
.
&=
is the compound assignment version of &
. See JLS 15.26.2. Compound Assignment Operators.
A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
In other words, these are equivalent (assumed boolean b
):
b &= bool_expr;
b = b & bool_expr;
if (!bool_expr) b = false;
This may also be your issue (java.lang.Class#getDeclaredFields):
Returns an array of
Field
objects reflecting all the fields declared by the class or interface represented by thisClass
object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields.
Your count may not be correct if you want to also count the inherited fields.
It appears to be checking how many fields you have declared in your class, and how many of those are private visibility.
In Java the namespaces for variables and methods don't conflict. So it looks like the driver has both a variable and a method called "test".
Without the test() method and your own source code, it is 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.)
Yes it seems require a few more fields to fulfill the statement, This should fix the namespace.
精彩评论