I need to build Junit test cases for the following code and I am trying to have
ID: 3838365 • Letter: I
Question
I need to build Junit test cases for the following code and I am trying to have one of the tests test that the "assert s1 != null : "Violation of: s1 is not null";", "assert s2 != null : "Violation of: s2 is not null";", and "assert s1.length() >= 1 : "|s1| >= 1";" statements gives an error when a null sequence is passed or when s1's length is >= 1. I don't know the exact way of going about doing this. A couple forums suggested using "Try Catch" but I don't know exactly how that works.
Any help would be appreciated!
public static void itersmooth(Sequence<Integer> s1, Sequence<Integer> s2)
{
assert s1 != null : "Violation of: s1 is not null";
assert s2 != null : "Violation of: s2 is not null";
assert s1.length() >= 1 : "|s1| >= 1";
s2.clear();
if (s1.length() > 1)
{
int inp1 = 1;
for (int inp2 = 0; inp2 < (s1.length() - 1); inp2++)
{
int valone = s1.remove(inp2);
int valtwo = s1.remove(inp1 - 1);
int valoneT = valone / 2;
int valtwoT = valtwo / 2;
int valtemp = valoneT + valtwoT;
if ((valone % 2 != 0 || valtwo % 2 != 0) && (valone > 0 && valtwo > 0))
{
valtemp++;
}
if ((valone % 2 != 0 || valtwo % 2 != 0) && (valone < 0 && valtwo < 0))
{
valtemp--;
}
s2.add(inp2, valtemp);
s1.add(inp2, valone);
s1.add(inp1, valtwo);
inp1++;
}
}
}
Explanation / Answer
Wrote some simple code to test your issue:
import org.junit.Assert;
import org.junit.Test;
public class testseq {
@Test
public void test() {
Sequence<Integer> s1 = new Sequence<>();
s1.add(0, 324);
itersmooth(s1, new Sequence<Integer>());
}
public static void itersmooth(Sequence<Integer> s1, Sequence<Integer> s2) {
Assert.assertNotEquals("Violation of: s1 is not null", s1, null);
Assert.assertNotEquals("Violation of: s2 is not null", s2, null);
Assert.assertTrue("|s1| >= 1", s1.length() >= 1);
s2.clear();
if (s1.length() > 1) {
int inp1 = 1;
for (int inp2 = 0; inp2 < (s1.length() - 1); inp2++) {
int valone = s1.remove(inp2);
int valtwo = s1.remove(inp1 - 1);
int valoneT = valone / 2;
int valtwoT = valtwo / 2;
int valtemp = valoneT + valtwoT;
if ((valone % 2 != 0 || valtwo % 2 != 0)
&& (valone > 0 && valtwo > 0)) {
valtemp++;
}
if ((valone % 2 != 0 || valtwo % 2 != 0)
&& (valone < 0 && valtwo < 0)) {
valtemp--;
}
s2.add(inp2, valtemp);
s1.add(inp2, valone);
s1.add(inp1, valtwo);
inp1++;
}
}
}
}
What i understand from your question is, that you want to give error messages for the cases, where s1 or s2 is null or s1 has a length of less than 1.
I have covered those cases.. So For null scenario, once your starting assert statements for the null check fails, your code won't come into the below section.. And thus, there will be no null pointer exception..
P.S. : There is no need for try catch block.. NullPointerException is a run-time exception which is not recommended to be catched, but instead to be avoided through proper coding.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.