Advertisement
drorgrebel

MMN_15_Tester

Jan 20th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.55 KB | None | 0 0
  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. /**
  4. * Write a description of class StringList_Tester here.
  5. *
  6. * @author (dror)
  7. * @version (maman_15_Tester)
  8. */
  9. public class Maman15_Tester
  10. {
  11. //Test CharNode
  12. @Test
  13. public void testCharNode(){
  14. //Test ctor
  15. CharNode n1 = new CharNode( 'b', 10, null);
  16. assertEquals("n1: " , n1.getData() , 'b');
  17. assertEquals("n1: " , n1.getValue(), 10 );
  18. assertEquals("n1: " , n1.getNext(), null );
  19.  
  20. //get/Set next nodes and test
  21. n1.setNext( new CharNode('c', 1, null));
  22. assertEquals("n1->next: " , n1.getNext().getData() , 'c');
  23. assertEquals("n1->next: " , n1.getNext().getValue() , 1);
  24. assertEquals("n1->next: " , n1.getNext().getNext() , null);
  25.  
  26. //set-1
  27. String teststring = "JAVA is Beautiful too\n";
  28. int i = 0;
  29. CharNode head = new CharNode( teststring.charAt(i),1,null);
  30. CharNode currnode = head;
  31. i++;
  32. while( i < teststring.length()){
  33. char currchar = teststring.charAt(i);
  34. if( currchar == currnode.getData()){
  35. currnode.setValue( currnode.getValue()+1);
  36. }
  37. else{//create a new node
  38. currnode.setNext( new CharNode( currchar,1,null));
  39. currnode = currnode.getNext();
  40. }
  41. i++;
  42. }
  43. //test-1
  44. for(i=0, currnode = head; i< teststring.length();){
  45. int count = currnode.getValue();
  46. while(count > 0){
  47. assertEquals("compare characters " , currnode.getData(), teststring.charAt(i));
  48. i++;
  49. count--;
  50. }
  51. currnode = currnode.getNext();
  52. }
  53.  
  54. }//End testCharNode
  55.  
  56.  
  57. //Test ListString Constructors and toString()
  58. @Test
  59. public void testStringListCntr(){
  60. //Test StringList( ) for an Empty-list( contains no CharNode, this is the default list)
  61. StringList l1 = new StringList(); //create an empty-list.
  62. assertEquals("l1: Default StringList" , l1.toString() , "\"\""); //equals ""
  63. //Test StringList( "" ) for an empty-String.
  64. StringList l2 = new StringList(""); //Also empty-string
  65. assertEquals("l2" , l2.toString() , "\"\""); //equals
  66.  
  67. //Creat a CharNode chain ans set it to a StringNode object;
  68. String teststring = "JAVA is Beautiful too\n";
  69. int i = 0;
  70. CharNode head = new CharNode( teststring.charAt(i),1,null);
  71. CharNode currnode = head;
  72. i++;
  73. while( i < teststring.length()){
  74. char currchar = teststring.charAt(i);
  75. if( currchar == currnode.getData()){
  76. currnode.setValue( currnode.getValue()+1);
  77. }
  78. else{//create a new node
  79. currnode.setNext( new CharNode( currchar,1,null));
  80. currnode = currnode.getNext();
  81. }
  82. i++;
  83. }
  84. //Create a List:
  85. StringList l3 = new StringList(head);
  86. assertEquals("l3" , l3.toString() , "\""+teststring+"\"");
  87. StringList l4 = new StringList("Name, Last-name, and Falmily name, please.");
  88. assertEquals("l4" , l4.toString() , "\"Name, Last-name, and Falmily name, please.\"");
  89. String l5_str = "abababababaababababaaaabbbbbbababab";
  90. StringList l5 = new StringList(l5_str);
  91. assertEquals("l5" , l5.toString() , "\""+l5_str+"\"");
  92. }//End testStringListCntr
  93.  
  94.  
  95. //Test ListString "Partial" Copy-constructor StringList( CharNode node)
  96. @Test
  97. public void testCopyCtor_2(){
  98. //Initiate Nodes
  99. CharNode c[] = new CharNode[10];
  100. c[0] = new CharNode('M',1,null);
  101. c[1] = new CharNode('a',1,null);
  102. c[2] = new CharNode('t',1,null);
  103. c[3] = new CharNode('a',1,null);
  104. c[4] = new CharNode('m',1,null);
  105. c[5] = new CharNode('_',1,null);
  106. c[6] = new CharNode('1',1,null);
  107. c[7] = new CharNode('5',1,null);
  108. //Conect nodes to be chain
  109. for(int i=0; i<7; i++){
  110. //Conect to chain:
  111. c[i].setNext(c[i+1]);
  112. }
  113. //Create a new StringList:
  114. StringList l1 = new StringList( c[0]);
  115. assertEquals("l1" , l1.toString() , "\"Matam_15\"");
  116. //Create a new StringList using
  117. StringList l2 = new StringList( c[6]);
  118. assertEquals("l2" , l2.toString() , "\"15\"");
  119. //Create a new StringList using
  120. StringList l3 = new StringList( c[7]);
  121. assertEquals("l3" , l3.toString() , "\"5\"");
  122. //Test for no-aliassing:
  123. c[7] = new CharNode('_',1,null);
  124. assertEquals("l3" , l3.toString() , "\"5\"");
  125. //Test on Empty-String
  126. StringList l4 = new StringList();
  127. assertEquals("l4" , l4.toString() , "\"\"");
  128.  
  129.  
  130.  
  131.  
  132.  
  133. //Test on null CharNode
  134. CharNode nullnode = null;
  135. StringList l5 = new StringList(nullnode);
  136. assertEquals("l5" , l5.toString() , "\"\"");
  137.  
  138.  
  139. }//End testPartialCtor
  140.  
  141.  
  142. //Test ListString Copy-constructor
  143. @Test
  144. public void testCopyCtor_3(){
  145. //Create a new StringList:
  146. StringList l1 = new StringList( "abcdefgh");
  147. StringList l2 = new StringList( l1 );
  148. assertEquals("l2" , l1.toString() , l2.toString());
  149. assertEquals("l2" , l2.toString() , "\"abcdefgh\"");
  150. //Create a new StringList:
  151. StringList l3 = new StringList(); //null-string
  152. StringList l4 = new StringList( l3 );
  153. assertEquals("l4" , l4.toString() ,"\"\""); //Empty-list
  154. //Create a new StringList:
  155. StringList l5 = new StringList(""); //Empty-String
  156. StringList l6 = new StringList( l5 );
  157. assertEquals("l6" , l6.toString() , "\"\""); //List contain empty-string
  158.  
  159.  
  160. }//End testPartialCtor
  161.  
  162. //Test ListString Copy-constructor
  163. @Test
  164. public void testCharAt(){
  165. //Create a new StringList:
  166. String l1_str = "abcdefgh123456";
  167. StringList l1 = new StringList( l1_str);
  168. //l1.toString() adds " before string and after string. reduce 2 from character-count.
  169. for(int i=0; i< l1.toString().length()-2; i++)
  170. assertEquals("l1 charAt [" + i + "]" , l1.charAt(i) , l1_str.charAt(i));
  171. //Create a new StringList:
  172. String l2_str = "Matam_15_20441_Winter_2018A";
  173. StringList l2 = new StringList( l2_str);
  174. //l2.toString() adds " before string and after string. reduce 2 from character-count.
  175. for(int i=0; i< l2.toString().length()-2; i++){
  176. assertEquals("l2 charAt [" + i + "]" , l2.charAt(i) , l2_str.charAt(i));
  177. }
  178. boolean result = l2_str.charAt(1) == l2.charAt(1); //should be yes.
  179. assertEquals(" l2_str at [1] and l2 at [1]", true, result);
  180. result = l2_str.charAt(1)== l2.charAt(3); //should be yes.
  181. assertEquals(" l2_str at [1] and l2 at [3]", true, result);
  182. result = l2_str.charAt(1)== l2.charAt(2); //should be no: "a" != "t"
  183. assertEquals(" l2_str at [1] and l2 at [2]", false, result);
  184. }//End testCharAt
  185.  
  186.  
  187. //Test ListString concat
  188. @Test
  189. public void testConcat(){
  190. //Create a new StringList:
  191. String empty_str = "";
  192. String l1_str = "abcdefgh";
  193. String l2_str = "123456";
  194. //Create empty-string-list
  195. StringList empty_string_list = new StringList( empty_str);
  196. assertEquals("empty-string-list " , "\"\"" , empty_string_list.toString());
  197. //Concat two empty-string-list. Expected: empty-string-list
  198. StringList concat1 = empty_string_list.concat( empty_string_list);
  199. assertEquals("Concat1: empty-string-list with empty-string-list: " , "\"\"" , concat1.toString());
  200. //validate no-alias:
  201. empty_string_list = new StringList("aaaa");
  202. assertEquals("Concat1: check no-alias: " , "\"aaaa\"" , empty_string_list.toString());
  203. assertEquals("Concat1: check no-alias: " , "\"\"" , concat1.toString());
  204.  
  205. //Create empty-string-list
  206. StringList null_string_list = new StringList();
  207. assertEquals("null-string-list " , "\"\"" , null_string_list.toString());
  208. //Concat two null-string-list. Expected: null-string-list
  209. StringList concat2 = null_string_list.concat( null_string_list);
  210. assertEquals("Concat2: null-string-list with null-string-list: " , "\"\"" , concat2.toString());
  211.  
  212. //Concat simple string and empty-string lists.
  213. StringList simple_string_list = new StringList("abab");
  214. StringList concat3 = simple_string_list.concat( null_string_list );
  215. assertEquals("concat3: " , "\"abab\"" , concat3.toString());
  216.  
  217. //Concat two string lists.
  218. StringList l1 = new StringList( l1_str );
  219. StringList l2 = new StringList( l2_str );
  220. StringList concat4 = l1.concat( l2 );
  221. assertEquals("concat4: " , "\"abcdefgh123456\"" , concat4.toString());
  222. concat4 = l2.concat( l1 );
  223. assertEquals("concat4(2): " , "\"123456abcdefgh\"" , concat4.toString());
  224. }//End testConcat
  225.  
  226.  
  227. //Test ListString indexOf_1
  228. @Test
  229. public void testIndexOf_1(){
  230. //Create a new StringList:
  231. String l1_str = "Maccabi Tel-Aviv for ever";
  232. // M,a,c,c,a,b,i, ,T,e,l ,- ,A ,v ,i ,v , ,f ,o ,r , ,e ,v ,e , r
  233. 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};
  234. StringList l1 = new StringList(l1_str);
  235. for(int i =0 ; i< l1_str.length(); i++)
  236. {
  237. char ch = l1_str.charAt(i);
  238. assertEquals("indexOf("+i+"): "+ l1_str.charAt(i), l1.indexOf(ch) ,indexes[i] ); //index of ch( l1_str.charAt(i) equals to indexes(i)
  239. }
  240. //check Empty-string-list:
  241. StringList l2 = new StringList("");
  242. char ch = '\0';
  243. assertEquals("indexOf(\"\"): "+ '\0', l1.indexOf(ch) ,-1 );
  244. //check null-string-list:
  245. StringList l3 = new StringList();
  246. ch = 't';
  247. assertEquals("indexOf(t): ", l3.indexOf(ch) ,-1 );
  248. }//End indexOf_1
  249.  
  250.  
  251. //Test ListString indexOf_2
  252. @Test
  253. public void testIndexOf_2(){
  254. //Create a new StringList:
  255. String l1_str = "Maccabi Tel-Aviv for ever";
  256. // M,a,c,c,a,b,i, ,T,e,l ,- ,A ,v ,i ,v , ,f ,o ,r , ,e ,v ,e , r
  257. 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};
  258. StringList l1 = new StringList(l1_str);
  259. assertEquals(" 'c' fromIndex=3 " , l1.indexOf('c',3 ) ,3 ); // 'c' fromIndex=3 appears at indx=3.
  260. assertEquals(" 'c' fromIndex=2 " , l1.indexOf('c',2 ) ,2 ); // 'c' fromIndex=2 appears at indx=2.
  261. assertEquals(" 'e' fromIndex=10 " , l1.indexOf('e',10) ,21 ); // 'e' fromIndex=10 appears at indx=3.
  262. assertEquals(" 'e' fromIndex=9 " , l1.indexOf('e',9 ) ,9 ); // 'e' fromIndex=9 appears at indx=9.
  263. assertEquals(" 'v' fromIndex=13 " , l1.indexOf('v',13) ,13 ); // 'v' fromIndex=13 appears at indx=13.
  264. assertEquals(" 'v' fromIndex=16 " , l1.indexOf('v',16) ,22 ); // 'v' fromIndex=16 appears at indx=22.
  265.  
  266. assertEquals(" 'Z' fromIndex=0 " , l1.indexOf('Z',0 ) ,-1 ); // 'Z' fromIndex=0 appears at indx=-1.
  267. assertEquals(" 'r' fromIndex=24 " , l1.indexOf('r',24 ) ,24 ); // 'r' fromIndex=0 appears at indx=24.
  268. assertEquals(" 'r' fromIndex=26 " , l1.indexOf('r',26 ) ,-1 ); // 'r' fromIndex=26, out-of-list. should return -1.
  269. //Next may be problematic. Negative indexes shoudn't be allowed.
  270. assertEquals(" 'f' fromIndex=-2 " , l1.indexOf('r',-2 ) ,-1 ); // 'r' fromIndex=-2, out-of-list. should return -1.
  271.  
  272. String l2_str = "aaaaaaaaa";
  273. StringList l2 = new StringList(l2_str);
  274. assertEquals(" 'Z' fromIndex=0 " , l2.indexOf('Z',0 ) ,-1 ); // 'Z' fromIndex=0 appears at indx=-1.
  275. for(int i=0; i< 9; i++){
  276. assertEquals(" 'a' fromIndex=" + i , l2.indexOf('a',i ) ,i); // 'a' fromIndex=i appears at indx= i.
  277. }
  278. StringList l3 = new StringList();
  279. assertEquals(" 'a' fromIndex=0 " , l3.indexOf('a',0 ) ,-1 ); // 'a' fromIndex=0 appears at indx=-1.
  280.  
  281. }//End indexOf_2
  282.  
  283.  
  284. //Test equals
  285. @Test
  286. public void testEquals(){
  287. //Create a new StringList:
  288. String l1_str = "Java 20441 2018A";
  289. String l2_str = "JAVA 20441 2018A";
  290. StringList l1 = new StringList(l1_str);
  291. StringList l2 = new StringList(l2_str);
  292. assertEquals(" compare l1 to l1 " , l1.equals(l1), true); // l1 is equal to l1.
  293. assertEquals(" compare l1 to l2 " , l1.equals(l2), false); // l2 is not equal to l1.
  294. assertEquals(" compare l2 to l1 " , l2.equals(l1), false); // l1 is not equal to l2.
  295. assertEquals(" compare l2 to l2 " , l2.equals(l2), true); // l2 is equal to l2.
  296.  
  297. }//End testEquals
  298.  
  299.  
  300. //Test substring(i)
  301. @Test
  302. public void testSubstring_i(){
  303. //Create a new StringList:
  304. String l1_str = "Java 20441 2018A";
  305. String l1_sub_str = "20441 2018A";
  306. String l2_str = "JAVA 20441 2018A";
  307. StringList l1 = new StringList(l1_str);
  308. StringList l1_sub = new StringList(l1_sub_str);
  309. assertEquals(" l1.substring(5) " , l1.substring(5).toString(), l1_sub.toString() ); //equal strings
  310. for(int i=0; i< 15; i++){
  311. StringList temp = new StringList( l1_str.substring(i) );
  312. StringList substr = new StringList( l1_str ).substring(i);
  313. assertEquals("l1.substring("+ i +")" , temp.toString(), substr.toString() );
  314. }
  315.  
  316. }//End testSubstring_i
  317.  
  318. //Test substring(i,j)
  319. @Test
  320. public void testSubstring_i_j(){
  321. //Create a new StringList:
  322. String l1_str = "Java 20441 2018A";
  323. String l1_sub_5_7 = "20";
  324. String l2_str = "JAVA 20441 2018A";
  325. StringList l1 = new StringList(l1_str);
  326. StringList l1_sub = new StringList(l1_sub_5_7);
  327. assertEquals(" l1.substring(5,7) " , l1.substring(5,7).toString(), "\""+l1_sub_5_7.toString()+"\"" ); //equal strings
  328. for(int i=0; i< 10; i++){
  329. StringList temp = new StringList( l1_str );
  330. temp = temp.substring(i,10);
  331. String l1_sub_i_10 = l1_str.substring(i,10);
  332. assertEquals("l1.substring(0,"+ i +")" , temp.toString(), "\""+l1_sub_i_10 +"\"" );
  333. }
  334.  
  335. }//End testSubstring_i_j
  336.  
  337. //Test length
  338. @Test
  339. public void testLength(){
  340. //Create a new StringList:
  341. String l1_str = "Maccabi Maccabi <accabi, wye-why-wye...";
  342. StringList l1 = new StringList(l1_str);
  343. StringList l2 = new StringList();
  344. assertEquals(" l1.length " , l1.length(), (l1_str ).length() ); //equal strings
  345. assertEquals(" l2.length " , l2.length(), ("").length() ); //equal strings
  346. assertEquals(" l2.toString() " , l2.toString(), ("\"\"").toString() ); //equal strings
  347. for(int i=0; i< 20; i++){
  348. StringList temp = new StringList( l1_str );
  349. temp = temp.substring(i,20);
  350. String l1_sub_i_20 = l1_str.substring(i,20);
  351. assertEquals("l1.substring("+ i+ ",20).length()" , temp.length(), l1_sub_i_20.length() );
  352. assertEquals("l1.substring("+ i +",20).toString()" , temp.toString(), ("\""+l1_sub_i_20+"\"").toString() );
  353. }
  354.  
  355. }//End testSubstring_i_j
  356.  
  357.  
  358.  
  359. }//End Tester_maman_15
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement