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’re wondering about
&=, here’s a quote from JLS 15.22.2. Boolean Logical Operators &, ^, and |.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.&=is the compound assignment version of&. See JLS 15.26.2. Compound Assignment Operators.In other words, these are equivalent (assumed
boolean b):This may also be your issue (java.lang.Class#getDeclaredFields):
Your count may not be correct if you want to also count the inherited fields.