Java functional testing

0 comments

A. Java/C++/C#/C

 

//ASSUMPTION: the array input is sorted. Ex: int[] input={1,3,4} or int[]={2,3,3,4,5,5,5,6}, etc.

 

public class MyArrayProcessing {

 

    public static int[] whatAmIdoing(int[] input){

         

        int j = 0;

        int i = 1;

        

        if(input.length < 2){

            return input;

        }

        while(i < input.length){

            if(input[i] == input[j]){

                i++;

            }else{

                input[++j] = input[i++];

            }   

        }

        int[] output = new int[j+1];

        for(int k=0; k<output.length; k++){

            output[k] = input[k];

        }

         

        return output;

    }

 

 

1. Q: What is the “whatAmIdoing” method doing (input is assumed to be sorted)?

 

2. Q: How would you test “whatAmIDoing?” What specific functional test cases would you try?

 

3. Q: Implement 2 test cases from 2) in the language/scripting of your choice.

   

 

 

 

B. Java/C++/C#/C

 

//The reverseList method reverses a singly linked list of Node(s). For example: head -> 1 -> 5 -> 3 -> null

// should become head -> 3 -> 5 -> 1 -> null. But reverseList has a defect, and does not reverse a list properly.

 

public class Node{

   public int data;

   public Node next;

   public Node(int data, Node next){

      this.data = data;

      this.next = next;

   }

}

 

public class MyReverseList {

 public Node reverseList (Node head)

 {

  if (head == NULL || head.next == NULL)

      return;

 

  Node Scnd = head.next;

  Node Thrd = Scnd.next;  

  Scnd.next = head;

  head.next = NULL;

 

  if (Thrd == NULL)

     return;  

 

  Node Crrnt = Thrd;

  Node Prvus = Scnd;

 

  while (Crrnt != NULL)

   { 

      Node Next = Crrnt.next;

      Currnt.next = Prvus;

      Prvus = Crrnt;

      Crrnt = Next;  

   }

 }

}

 

 

1. Q: Please correct the defect in reverseList so it reverses a linked-list correctly.

 

2. Q: How would you test reverseList? What specific functional test cases would you try?

 

3. Q: Implement 2 test cases from 2) in the language/scripting of your choice.

  

 

 

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}