Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static org.junit.Assert.*;
- import org.junit.Test;
- /**
- * Write a description of class StringList_Tester here.
- *
- * @author (dror)
- * @version (maman_15_Tester)
- */
- public class Maman15_Tester
- {
- //Test CharNode
- @Test
- public void testCharNode(){
- //Test ctor
- CharNode n1 = new CharNode( 'b', 10, null);
- assertEquals("n1: " , n1.getData() , 'b');
- assertEquals("n1: " , n1.getValue(), 10 );
- assertEquals("n1: " , n1.getNext(), null );
- //get/Set next nodes and test
- n1.setNext( new CharNode('c', 1, null));
- assertEquals("n1->next: " , n1.getNext().getData() , 'c');
- assertEquals("n1->next: " , n1.getNext().getValue() , 1);
- assertEquals("n1->next: " , n1.getNext().getNext() , null);
- //set-1
- String teststring = "JAVA is Beautiful too\n";
- int i = 0;
- CharNode head = new CharNode( teststring.charAt(i),1,null);
- CharNode currnode = head;
- i++;
- while( i < teststring.length()){
- char currchar = teststring.charAt(i);
- if( currchar == currnode.getData()){
- currnode.setValue( currnode.getValue()+1);
- }
- else{//create a new node
- currnode.setNext( new CharNode( currchar,1,null));
- currnode = currnode.getNext();
- }
- i++;
- }
- //test-1
- for(i=0, currnode = head; i< teststring.length();){
- int count = currnode.getValue();
- while(count > 0){
- assertEquals("compare characters " , currnode.getData(), teststring.charAt(i));
- i++;
- count--;
- }
- currnode = currnode.getNext();
- }
- }//End testCharNode
- //Test ListString Constructors and toString()
- @Test
- public void testStringListCntr(){
- //Test StringList( ) for an Empty-list( contains no CharNode, this is the default list)
- StringList l1 = new StringList(); //create an empty-list.
- assertEquals("l1: Default StringList" , l1.toString() , "\"\""); //equals ""
- //Test StringList( "" ) for an empty-String.
- StringList l2 = new StringList(""); //Also empty-string
- assertEquals("l2" , l2.toString() , "\"\""); //equals
- //Creat a CharNode chain ans set it to a StringNode object;
- String teststring = "JAVA is Beautiful too\n";
- int i = 0;
- CharNode head = new CharNode( teststring.charAt(i),1,null);
- CharNode currnode = head;
- i++;
- while( i < teststring.length()){
- char currchar = teststring.charAt(i);
- if( currchar == currnode.getData()){
- currnode.setValue( currnode.getValue()+1);
- }
- else{//create a new node
- currnode.setNext( new CharNode( currchar,1,null));
- currnode = currnode.getNext();
- }
- i++;
- }
- //Create a List:
- StringList l3 = new StringList(head);
- assertEquals("l3" , l3.toString() , "\""+teststring+"\"");
- StringList l4 = new StringList("Name, Last-name, and Falmily name, please.");
- assertEquals("l4" , l4.toString() , "\"Name, Last-name, and Falmily name, please.\"");
- String l5_str = "abababababaababababaaaabbbbbbababab";
- StringList l5 = new StringList(l5_str);
- assertEquals("l5" , l5.toString() , "\""+l5_str+"\"");
- }//End testStringListCntr
- //Test ListString "Partial" Copy-constructor StringList( CharNode node)
- @Test
- public void testCopyCtor_2(){
- //Initiate Nodes
- CharNode c[] = new CharNode[10];
- c[0] = new CharNode('M',1,null);
- c[1] = new CharNode('a',1,null);
- c[2] = new CharNode('t',1,null);
- c[3] = new CharNode('a',1,null);
- c[4] = new CharNode('m',1,null);
- c[5] = new CharNode('_',1,null);
- c[6] = new CharNode('1',1,null);
- c[7] = new CharNode('5',1,null);
- //Conect nodes to be chain
- for(int i=0; i<7; i++){
- //Conect to chain:
- c[i].setNext(c[i+1]);
- }
- //Create a new StringList:
- StringList l1 = new StringList( c[0]);
- assertEquals("l1" , l1.toString() , "\"Matam_15\"");
- //Create a new StringList using
- StringList l2 = new StringList( c[6]);
- assertEquals("l2" , l2.toString() , "\"15\"");
- //Create a new StringList using
- StringList l3 = new StringList( c[7]);
- assertEquals("l3" , l3.toString() , "\"5\"");
- //Test for no-aliassing:
- c[7] = new CharNode('_',1,null);
- assertEquals("l3" , l3.toString() , "\"5\"");
- //Test on Empty-String
- StringList l4 = new StringList();
- assertEquals("l4" , l4.toString() , "\"\"");
- //Test on null CharNode
- CharNode nullnode = null;
- StringList l5 = new StringList(nullnode);
- assertEquals("l5" , l5.toString() , "\"\"");
- }//End testPartialCtor
- //Test ListString Copy-constructor
- @Test
- public void testCopyCtor_3(){
- //Create a new StringList:
- StringList l1 = new StringList( "abcdefgh");
- StringList l2 = new StringList( l1 );
- assertEquals("l2" , l1.toString() , l2.toString());
- assertEquals("l2" , l2.toString() , "\"abcdefgh\"");
- //Create a new StringList:
- StringList l3 = new StringList(); //null-string
- StringList l4 = new StringList( l3 );
- assertEquals("l4" , l4.toString() ,"\"\""); //Empty-list
- //Create a new StringList:
- StringList l5 = new StringList(""); //Empty-String
- StringList l6 = new StringList( l5 );
- assertEquals("l6" , l6.toString() , "\"\""); //List contain empty-string
- }//End testPartialCtor
- //Test ListString Copy-constructor
- @Test
- public void testCharAt(){
- //Create a new StringList:
- String l1_str = "abcdefgh123456";
- StringList l1 = new StringList( l1_str);
- //l1.toString() adds " before string and after string. reduce 2 from character-count.
- for(int i=0; i< l1.toString().length()-2; i++)
- assertEquals("l1 charAt [" + i + "]" , l1.charAt(i) , l1_str.charAt(i));
- //Create a new StringList:
- String l2_str = "Matam_15_20441_Winter_2018A";
- StringList l2 = new StringList( l2_str);
- //l2.toString() adds " before string and after string. reduce 2 from character-count.
- for(int i=0; i< l2.toString().length()-2; i++){
- assertEquals("l2 charAt [" + i + "]" , l2.charAt(i) , l2_str.charAt(i));
- }
- boolean result = l2_str.charAt(1) == l2.charAt(1); //should be yes.
- assertEquals(" l2_str at [1] and l2 at [1]", true, result);
- result = l2_str.charAt(1)== l2.charAt(3); //should be yes.
- assertEquals(" l2_str at [1] and l2 at [3]", true, result);
- result = l2_str.charAt(1)== l2.charAt(2); //should be no: "a" != "t"
- assertEquals(" l2_str at [1] and l2 at [2]", false, result);
- }//End testCharAt
- //Test ListString concat
- @Test
- public void testConcat(){
- //Create a new StringList:
- String empty_str = "";
- String l1_str = "abcdefgh";
- String l2_str = "123456";
- //Create empty-string-list
- StringList empty_string_list = new StringList( empty_str);
- assertEquals("empty-string-list " , "\"\"" , empty_string_list.toString());
- //Concat two empty-string-list. Expected: empty-string-list
- StringList concat1 = empty_string_list.concat( empty_string_list);
- assertEquals("Concat1: empty-string-list with empty-string-list: " , "\"\"" , concat1.toString());
- //validate no-alias:
- empty_string_list = new StringList("aaaa");
- assertEquals("Concat1: check no-alias: " , "\"aaaa\"" , empty_string_list.toString());
- assertEquals("Concat1: check no-alias: " , "\"\"" , concat1.toString());
- //Create empty-string-list
- StringList null_string_list = new StringList();
- assertEquals("null-string-list " , "\"\"" , null_string_list.toString());
- //Concat two null-string-list. Expected: null-string-list
- StringList concat2 = null_string_list.concat( null_string_list);
- assertEquals("Concat2: null-string-list with null-string-list: " , "\"\"" , concat2.toString());
- //Concat simple string and empty-string lists.
- StringList simple_string_list = new StringList("abab");
- StringList concat3 = simple_string_list.concat( null_string_list );
- assertEquals("concat3: " , "\"abab\"" , concat3.toString());
- //Concat two string lists.
- StringList l1 = new StringList( l1_str );
- StringList l2 = new StringList( l2_str );
- StringList concat4 = l1.concat( l2 );
- assertEquals("concat4: " , "\"abcdefgh123456\"" , concat4.toString());
- concat4 = l2.concat( l1 );
- assertEquals("concat4(2): " , "\"123456abcdefgh\"" , concat4.toString());
- }//End testConcat
- //Test ListString indexOf_1
- @Test
- public void testIndexOf_1(){
- //Create a new StringList:
- String l1_str = "Maccabi Tel-Aviv for ever";
- // M,a,c,c,a,b,i, ,T,e,l ,- ,A ,v ,i ,v , ,f ,o ,r , ,e ,v ,e , r
- int indexes[] = new int[]{ 0,1,2,2,1,5,6,7,8,9,10,11,12,13,6 ,13,7 ,17,18,19,7 ,9 ,13,9 ,19};
- StringList l1 = new StringList(l1_str);
- for(int i =0 ; i< l1_str.length(); i++)
- {
- char ch = l1_str.charAt(i);
- assertEquals("indexOf("+i+"): "+ l1_str.charAt(i), l1.indexOf(ch) ,indexes[i] ); //index of ch( l1_str.charAt(i) equals to indexes(i)
- }
- //check Empty-string-list:
- StringList l2 = new StringList("");
- char ch = '\0';
- assertEquals("indexOf(\"\"): "+ '\0', l1.indexOf(ch) ,-1 );
- //check null-string-list:
- StringList l3 = new StringList();
- ch = 't';
- assertEquals("indexOf(t): ", l3.indexOf(ch) ,-1 );
- }//End indexOf_1
- //Test ListString indexOf_2
- @Test
- public void testIndexOf_2(){
- //Create a new StringList:
- String l1_str = "Maccabi Tel-Aviv for ever";
- // M,a,c,c,a,b,i, ,T,e,l ,- ,A ,v ,i ,v , ,f ,o ,r , ,e ,v ,e , r
- int indexes[] = new int[]{ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
- StringList l1 = new StringList(l1_str);
- assertEquals(" 'c' fromIndex=3 " , l1.indexOf('c',3 ) ,3 ); // 'c' fromIndex=3 appears at indx=3.
- assertEquals(" 'c' fromIndex=2 " , l1.indexOf('c',2 ) ,2 ); // 'c' fromIndex=2 appears at indx=2.
- assertEquals(" 'e' fromIndex=10 " , l1.indexOf('e',10) ,21 ); // 'e' fromIndex=10 appears at indx=3.
- assertEquals(" 'e' fromIndex=9 " , l1.indexOf('e',9 ) ,9 ); // 'e' fromIndex=9 appears at indx=9.
- assertEquals(" 'v' fromIndex=13 " , l1.indexOf('v',13) ,13 ); // 'v' fromIndex=13 appears at indx=13.
- assertEquals(" 'v' fromIndex=16 " , l1.indexOf('v',16) ,22 ); // 'v' fromIndex=16 appears at indx=22.
- assertEquals(" 'Z' fromIndex=0 " , l1.indexOf('Z',0 ) ,-1 ); // 'Z' fromIndex=0 appears at indx=-1.
- assertEquals(" 'r' fromIndex=24 " , l1.indexOf('r',24 ) ,24 ); // 'r' fromIndex=0 appears at indx=24.
- assertEquals(" 'r' fromIndex=26 " , l1.indexOf('r',26 ) ,-1 ); // 'r' fromIndex=26, out-of-list. should return -1.
- //Next may be problematic. Negative indexes shoudn't be allowed.
- assertEquals(" 'f' fromIndex=-2 " , l1.indexOf('r',-2 ) ,-1 ); // 'r' fromIndex=-2, out-of-list. should return -1.
- String l2_str = "aaaaaaaaa";
- StringList l2 = new StringList(l2_str);
- assertEquals(" 'Z' fromIndex=0 " , l2.indexOf('Z',0 ) ,-1 ); // 'Z' fromIndex=0 appears at indx=-1.
- for(int i=0; i< 9; i++){
- assertEquals(" 'a' fromIndex=" + i , l2.indexOf('a',i ) ,i); // 'a' fromIndex=i appears at indx= i.
- }
- StringList l3 = new StringList();
- assertEquals(" 'a' fromIndex=0 " , l3.indexOf('a',0 ) ,-1 ); // 'a' fromIndex=0 appears at indx=-1.
- }//End indexOf_2
- //Test equals
- @Test
- public void testEquals(){
- //Create a new StringList:
- String l1_str = "Java 20441 2018A";
- String l2_str = "JAVA 20441 2018A";
- StringList l1 = new StringList(l1_str);
- StringList l2 = new StringList(l2_str);
- assertEquals(" compare l1 to l1 " , l1.equals(l1), true); // l1 is equal to l1.
- assertEquals(" compare l1 to l2 " , l1.equals(l2), false); // l2 is not equal to l1.
- assertEquals(" compare l2 to l1 " , l2.equals(l1), false); // l1 is not equal to l2.
- assertEquals(" compare l2 to l2 " , l2.equals(l2), true); // l2 is equal to l2.
- }//End testEquals
- //Test substring(i)
- @Test
- public void testSubstring_i(){
- //Create a new StringList:
- String l1_str = "Java 20441 2018A";
- String l1_sub_str = "20441 2018A";
- String l2_str = "JAVA 20441 2018A";
- StringList l1 = new StringList(l1_str);
- StringList l1_sub = new StringList(l1_sub_str);
- assertEquals(" l1.substring(5) " , l1.substring(5).toString(), l1_sub.toString() ); //equal strings
- for(int i=0; i< 15; i++){
- StringList temp = new StringList( l1_str.substring(i) );
- StringList substr = new StringList( l1_str ).substring(i);
- assertEquals("l1.substring("+ i +")" , temp.toString(), substr.toString() );
- }
- }//End testSubstring_i
- //Test substring(i,j)
- @Test
- public void testSubstring_i_j(){
- //Create a new StringList:
- String l1_str = "Java 20441 2018A";
- String l1_sub_5_7 = "20";
- String l2_str = "JAVA 20441 2018A";
- StringList l1 = new StringList(l1_str);
- StringList l1_sub = new StringList(l1_sub_5_7);
- assertEquals(" l1.substring(5,7) " , l1.substring(5,7).toString(), "\""+l1_sub_5_7.toString()+"\"" ); //equal strings
- for(int i=0; i< 10; i++){
- StringList temp = new StringList( l1_str );
- temp = temp.substring(i,10);
- String l1_sub_i_10 = l1_str.substring(i,10);
- assertEquals("l1.substring(0,"+ i +")" , temp.toString(), "\""+l1_sub_i_10 +"\"" );
- }
- }//End testSubstring_i_j
- //Test length
- @Test
- public void testLength(){
- //Create a new StringList:
- String l1_str = "Maccabi Maccabi <accabi, wye-why-wye...";
- StringList l1 = new StringList(l1_str);
- StringList l2 = new StringList();
- assertEquals(" l1.length " , l1.length(), (l1_str ).length() ); //equal strings
- assertEquals(" l2.length " , l2.length(), ("").length() ); //equal strings
- assertEquals(" l2.toString() " , l2.toString(), ("\"\"").toString() ); //equal strings
- for(int i=0; i< 20; i++){
- StringList temp = new StringList( l1_str );
- temp = temp.substring(i,20);
- String l1_sub_i_20 = l1_str.substring(i,20);
- assertEquals("l1.substring("+ i+ ",20).length()" , temp.length(), l1_sub_i_20.length() );
- assertEquals("l1.substring("+ i +",20).toString()" , temp.toString(), ("\""+l1_sub_i_20+"\"").toString() );
- }
- }//End testSubstring_i_j
- }//End Tester_maman_15
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement