3 Replies - 278 Views - Last Post: 03 February 2018 - 03:27 AM Rate Topic: -----

#1 ufcking18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-June 17

singlylinked list test in J unit fails

Posted 01 February 2018 - 01:21 AM

Hi, I am need with some help with an assignment I received in my comp science 102 course. I took a 101 course in the summer and they did not cover java indepth. For my assignment we are given a singly linked list and told it has to pass a junit test in eclipse. Teacher says we should only have to change the return values that are "null". I have absolutely no clue what I am looking at. Could someone help me with just getting the code to pass one of the test, and then I can figure the rest out. Thanks. Test that fail are:

FAILURE: makeTypeInteger
FAILURE: makeTypeString
FAILURE: addAndIterateMyClass
FAILURE: makeTypeMySubClass
FAILURE: addAndIterateString
FAILURE: addFirstAndLast
FAILURE: makeTypeMyClass

JUNIT TEST CODE
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import edu.mccc.cos210.MyClass;
import edu.mccc.cos210.MySubClass;

public class SinglyLinkedListTests {
	private static StringBuilder report = new StringBuilder();
	@Rule 
	public TestWatcher watchman = new TestWatcher() {
	    @Override
	    protected void failed(Throwable t, Description description) {
	        report.append("  FAILURE: ").append(description.getMethodName()).append("\n");
	    }
	    @Override
	    protected void succeeded(Description description) {
	        report.append("  Success: ").append(description.getMethodName()).append("\n");
	    }
	};
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new SecurityManager());
		}
        report.append("\nTesting: ").append(SinglyLinkedList.class.getSimpleName()).append("\n");
	}
	@AfterClass
	public static void tearDownClass() {
	    System.out.println(report.toString());
	}
	@Test
	public void makeTypeString() {
		ISinglyLinkedList<String> list = new SinglyLinkedList<>();
		assertEquals(true, list.isEmpty());
		list.addFirst("Hello");
		assertEquals(false, list.isEmpty());
		assertEquals(1, list.getSize());
		String s = list.getFirst();
		assertEquals("Hello", s);
	}
	@Test
	public void makeTypeInteger() {
		ISinglyLinkedList<Integer> list = new SinglyLinkedList<>();
		assertEquals(true, list.isEmpty());
		list.addFirst(123);
		assertEquals(false, list.isEmpty());
		assertEquals(1, list.getSize());
		int n = list.getFirst();
		assertEquals(123, n);
	}
	@Test
	public void makeTypeMyClass() {
		ISinglyLinkedList<MyClass> list = new SinglyLinkedList<>();
		MyClass myClass = new MyClass();
		MySubClass mySubClass = new MySubClass();
		assertEquals(true, list.isEmpty());
		list.addFirst(myClass);
		assertEquals(false, list.isEmpty());
		assertEquals(1, list.getSize());
		MyClass mc = list.getFirst();
		assertEquals(myClass.hashCode(), mc.hashCode());
		list.addFirst(mySubClass);
		assertEquals(false, list.isEmpty());
		assertEquals(2, list.getSize());
		MyClass msc = list.getFirst();
		assertEquals(mySubClass.hashCode(), msc.hashCode());
		msc = list.removeFirst();
		assertEquals(mySubClass.hashCode(), msc.hashCode());
		mc = list.getFirst();
		assertEquals(myClass.hashCode(), mc.hashCode());
	}
	@Test (expected=java.util.NoSuchElementException.class)
	public void makeTypeMySubClass() {
		ISinglyLinkedList<MySubClass> list = new SinglyLinkedList<>();
		MySubClass mySubClass = new MySubClass();
		assertEquals(true, list.isEmpty());
		list.addFirst(mySubClass);
		assertEquals(false, list.isEmpty());
		assertEquals(1, list.getSize());
		MySubClass msc = list.removeFirst();
		assertEquals(mySubClass.hashCode(), msc.hashCode());
		list.removeFirst();
		list.getFirst();
	}
	@Test
	public void addAndIterateString() {
		ISinglyLinkedList<String> list = new SinglyLinkedList<>();
		list.addFirst("Apple");
		list.addFirst("Orange");
		list.addFirst("Peach");
		list.addFirst("Pear");
		list.addFirst("Lemon");
		StringBuilder sb1 = new StringBuilder();
		for (String s : list) {
			sb1.append(s);
		}
		assertEquals("LemonPearPeachOrangeApple", sb1.toString());
		final StringBuilder sb2 = new StringBuilder();
		list.stream().filter(s -> s.matches(".*[Pp].*")).forEach(t -> sb2.append(t));
		assertEquals("PearPeachApple", sb2.toString());
	}
	@Test
	public void addAndIterateMyClass() {
		ISinglyLinkedList<MyClass> list = new SinglyLinkedList<>();
		list.addFirst(new MyClass());
		list.addFirst(new MyClass());
		list.addFirst(new MyClass());
		assertEquals(3, list.getSize());
	}
	@Test
	public void addFirstAndLast() {
		ISinglyLinkedList<String> list1 = new SinglyLinkedList<>();
		list1.addFirst("1");
		ISinglyLinkedList<String> list2 = new SinglyLinkedList<>();
		list2.addLast("1");
		ISinglyLinkedList<String> list3 = new SinglyLinkedList<>();
		list3.addFirst("2");
		list3.addFirst("1");
		ISinglyLinkedList<String> list4 = new SinglyLinkedList<>();
		list4.addLast("1");
		list4.addLast("2");
		ISinglyLinkedList<String> list5 = new SinglyLinkedList<>();
		list5.addFirst("1");
		list5.addLast("2");
		ISinglyLinkedList<String> list6 = new SinglyLinkedList<>();
		list6.addLast("2");
		list6.addFirst("1");
		ISinglyLinkedList<String> list7 = new SinglyLinkedList<>();
		list7.addFirst("2");
		list7.addLast("3");
		list7.addFirst("1");
		list7.addLast("4");
		StringBuilder sb1 = new StringBuilder();
		for (String s : list1) {
			sb1.append(s);
		}
		assertEquals("1", sb1.toString());
		StringBuilder sb2 = new StringBuilder();
		for (String s : list2) {
			sb2.append(s);
		}
		assertEquals("1", sb2.toString());
		StringBuilder sb3 = new StringBuilder();
		for (String s : list3) {
			sb3.append(s);
		}
		assertEquals("12", sb3.toString());
		StringBuilder sb4 = new StringBuilder();
		for (String s : list4) {
			sb4.append(s);
		}
		assertEquals("12", sb4.toString());
		StringBuilder sb5 = new StringBuilder();
		for (String s : list5) {
			sb5.append(s);
		}
		assertEquals("12", sb5.toString());
		StringBuilder sb6 = new StringBuilder();
		for (String s : list6) {
			sb6.append(s);
		}
		assertEquals("12", sb6.toString());
		StringBuilder sb7 = new StringBuilder();
		for (String s : list7) {
			sb7.append(s);
		}
		assertEquals("1234", sb7.toString());
	}
}




SINGLYLINKEDLIST CODE:
public class SinglyLinkedList<T> implements ISinglyLinkedList<T> {
	protected SNode head = null;
	protected SNode tail = null;
	private int size = 0;
	@Override
	public void addFirst(T data) {
	}
	@Override
	public T getFirst() {
		if (isEmpty()) {
			throw new java.util.NoSuchElementException();
		}
		return null;
	}
	@Override
	public T removeFirst() {
		return null;
	}
	@Override
	public void addLast(T data) {
	}
	@Override
	public T getLast() {
		if (isEmpty()) {
			throw new java.util.NoSuchElementException();
		}
		return null;
	}
	@Override
	public int getSize() {
		return size;
	}
	@Override
	public boolean isEmpty() {
		return size == 0;
	}
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		SNode node = head;
		sb.append("[ ");
		while (node != null) {
			sb.append(node.getData().toString() + " ");
			node = node.getNextNode();
		}
		sb.append("]");
		return sb.toString();
	}
	@Override
	public java.util.Iterator<T> iterator() {
		return new SIterator();
	}
	private class SIterator implements java.util.Iterator<T> {
		private SNode next = head;
		@Override
		public boolean hasNext() {
			if (next == null) {
				return false;
			}
			return true;
		}
		@Override
		public T next() {
			if (next == null) {
				throw new java.util.NoSuchElementException();
			}
			T data = next.getData();
			next = next.getNextNode();
			return data;
		}
	}
	protected class SNode {
		private SNode nextNode;
		private T data;
		public SNode(T data) {
			setData(data);
		}
		public SNode getNextNode() {
			return nextNode;
		}
		public void setNextNode(SNode nextNode) {
			this.nextNode = nextNode;
		}
		public T getData() {
			return data;
		}
		public void setData(T data) {
			this.data = data;
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: singlylinked list test in J unit fails

#2 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6500
  • View blog
  • Posts: 26,282
  • Joined: 12-December 12

Re: singlylinked list test in J unit fails

Posted 01 February 2018 - 01:50 AM

You need to implement the code that manipulates the list, addFirst, addLast, etc. It is unlikely that implementing one or two of the methods will cause any tests to pass, particularly because each test uses a number of methods (either directly or indirectly).

Given this, are you sure that you were advised to only implement those that return null? This would only complete part of the behaviours and I believe that all the tests would still fail. I suspect you need to complete them all.

Look at what's available in the definition of an SNode. Consider, for example, the method addFirst. How would you add an item as the first item of the list (and maintain its singly-linked behaviour).



Presumably you've covered singly-linked lists? What they are and how they work. If this is an obstacle then read up on the subject and study some separate, basic examples.
Was This Post Helpful? 1
  • +
  • -

#3 ufcking18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-June 17

Re: singlylinked list test in J unit fails

Posted 01 February 2018 - 10:33 AM

Hi, thanks for the help. I never learned about list. I took a 5 week 101 college course in the summer that only covered up to basic array and list uses. I would ask my professor for help, but he teaches at multiple colleges and has no office hours. Also, you are correct, I messed around with the returns and no longer get 7 failures, but now 7 errors when running the j unit test.

This post has been edited by ndc85430: 02 February 2018 - 08:34 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#4 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 703
  • View blog
  • Posts: 2,875
  • Joined: 13-June 14

Re: singlylinked list test in J unit fails

Posted 03 February 2018 - 03:27 AM

Some comments on your tests:

1. When you're adding a new test, think about what case you're covering. If you're covering something that's already been covered, it doesn't make sense to add that new test. For example, you have makeTypeString and makeTypeInteger which are covering the same thing, just with different types. The behaviour of the class is independent of the type (which is why you have a generic type parameter), so one of those tests is redundant.

2. For each test case, you should be able to describe the behaviour you're testing in a sentence ("a new list should be empty", "trying to retrieve an item from an empty list should throw" and so on). Use these as your test names*, because that makes it clear what's going on. At the moment, your tests look like they're doing too much - even in the simple case of adding and retrieving an item, you're doing more assertions than is necessary (all you really need to check is that you can get the value out and possibly that the size is correct). This is very noisy and makes the tests hard to read.

3. The tests are also hard to read for other reasons: some of them are very long and there is little to no vertical whitespace. The former can be improved by focusing the tests on specific behaviours as discussed above and for the latter, consider structuring your tests in given/when/then style (see, e.g. this) with a blank line between each section.

* The camel case convention for naming methods in Java might make it harder to read test names. Some people go against that convention for tests and use snake case instead. I've been programming in Kotlin recently and in that language, it's possible to put spaces in function names, so I do that just for tests.

This post has been edited by ndc85430: 03 February 2018 - 04:23 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1