Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I find no error and the following code , the code fails the test.


I used JUnit for testing. Can someone help me ?

Java
public class BinString {
	
	public BinString() {}
	
	public String convert(String s) {
		return binarise(sum(s));
	}
	
	public int sum(String s){
		if(s=="") 
			return 0;
		if(s.length()==1) 
			return ((int) (s.charAt(0)));
		return ((int) (s.charAt(0)))+sum(s.substring(1));
	}
	
	public String binarise(int x) {
		if(x==0)
			return "";
		if(x%2==1) 
			return "1"+binarise(x/2);
		return "0"+binarise(x/2);
	}
}


What I have tried:

This is my test class

Java
import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class BinStringTest {

	private BinString binString;
	
	@Before protected void setUp(){
		binString = new BinString();
	}
	
	@Test public void testSumFunction() {
		int expected =0;
		
		assertEquals(expected, binString.sum(""));
		expected = 100;
		assertEquals(expected, binString.sum("d"));
		expected = 256;
		assertEquals(expected, binString.sum("Add"));
		
		
	}
	
	@Test public void testBinariseFunction(){
		String expected = "101";
		assertEquals(expected, binString.binarise(5));
		expected = "11111100";
		assertEquals(expected, binString.binarise(252));
				
	}
	
	@Test public void testTotalConversion() {
		String expected = "10000001";
		assertEquals(expected, binString.convert("A"));
	}
	

}
Posted
Updated 21-May-16 6:58am
Comments
Patrice T 21-May-16 11:52am    
What is supposed to do your code ?
How does it fail ? Show actual result and expected result.
Laurentiu Bobora 21-May-16 11:54am    
Class tested together a string of ASCII characters by collecting its values ​​and returns a binary representation of
amount
Laurentiu Bobora 21-May-16 12:04pm    
Do not pass the test, and displays a window with the message :
Method 'initializationError' not found. Opening the test class
Patrice T 21-May-16 12:06pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Sergey Alexandrovich Kryukov 21-May-16 12:29pm    
This is perfectly normal. Failing the test is not the same as "error". This is the whole purpose of unit testing.
In other words, there is nothing you need help with. Well, except using the debugger and improving test development skills.
—SA

1 solution

I think you will want to replace this code
Java
public String binarise(int x) {
    if(x==0)
        return "";
    if(x%2==1)
        return "1"+binarise(x/2);
    return "0"+binarise(x/2);
}

with
Java
public String binarise(int x) {
    if(x==0)
        return "";
    if(x%2==1)
        return binarise(x/2)+"1";
    return binarise(x/2)+"0";
}

as I suspect a bug there.
 
Share this answer
 
Comments
Laurentiu Bobora 22-May-16 5:36am    
I tried the code above and still not pass the test
Patrice T 22-May-16 5:41am    
Do not state what the code don't do, state what the code do and why it is wrong with examples: parameters of test, expected result, real result.
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900