int main() {
  LListInt * list = new LListInt();

  // Check if the list is initially empty.
  if (list->empty()) {
    cout << "SUCCESS: List is empty initially." << endl;
  } else {
    cout << "FAIL: List is not empty initially when it should be." << endl;
  }

  // Remove an item from an empty list
  //list->remove(0);

  // Insert an item from invalid location
  //list->insert(-1, 1);
  //list->insert(3, 3);

  // Insert an item at the head.
  list->insert(0, 3);

  // Check if the list is still empty.
  if (!list->empty()) {
    cout << "SUCCESS: List is not empty after one insertion." << endl;
  } else {
    cout << "FAIL: List is empty after one insertion." << endl;
  }

  if (list->size() == 1) {
    cout << "SUCCESS: List has size 1 after one insertion." << endl;
  } else {
    cout << "FAIL: List has size " << list->size() << " after one insertion.";
    cout << endl;
  }


  // Insert an item from invalid location
  //list->insert(3, 3);

  // Insert an item to the end of an existing list
  list->insert(1, 2);
  list->insert(2, 1);

  if (list->size() == 3) {
    cout << "SUCCESS: List has size 3 after three insertion." << endl;
  } else {
    cout << "FAIL: List has size " << list->size() << " after three insertion.";
    cout << endl;
  }

  // Check if the value is correct.
  if (list->get(0) == 3) {
    cout << "SUCCESS: 3 is at the 0th index of the list." << endl;
  } else {
    cout << "FAIL: 3 is not at the 0th index of the list, " << list->get(0);
    cout << " is instead." << endl;
  }

  // Check if the value is correct.
  if (list->get(1) == 2) {
    cout << "SUCCESS: 2 is at the 1st index of the list." << endl;
  } else {
    cout << "FAIL: 2 is not at the 1st index of the list, " << list->get(2);
    cout << " is instead." << endl;
  }

  // Check if the value is correct.
  if (list->get(2) == 1) {
    cout << "SUCCESS: 1 is at the 2nd index of the list." << endl;
  } else {
    cout << "FAIL: 1 is not at the 2nd index of the list, " << list->get(2);
    cout << " is instead." << endl;
  }

  // Insert an item into middle of an existing list
  list->insert(1, 3);
  list->insert(0, 10);

  if (list->size() == 5) {
    cout << "SUCCESS: List has size 5 after five insertion." << endl;
  } else {
    cout << "FAIL: List has size " << list->size() << " after five insertion.";
    cout << endl;
  }

  // Check if the value is correct.
  if (list->get(0) == 10) {
    cout << "SUCCESS: 10 is at the 0th index of the list." << endl;
  } else {
    cout << "FAIL: 10 is not at the 0th index of the list, " << list->get(0);
    cout << " is instead." << endl;
  }

  // Check if the value is correct.
  if (list->get(1) == 3) {
    cout << "SUCCESS: 3 is at the 1st index of the list." << endl;
  } else {
    cout << "FAIL: 3 is not at the 1st index of the list, " << list->get(1);
    cout << " is instead." << endl;
  }

  // Check if the value is correct.
  if (list->get(2) == 3) {
    cout << "SUCCESS: 3 is at the 2nd index of the list." << endl;
  } else {
    cout << "FAIL: 3 is not at the 2nd index of the list, " << list->get(2);
    cout << " is instead." << endl;
  }


  // Remove item from invalid location
  //list->remove(-1);
  //list->remove(6);

  // Remove item from ends of the list
  list->remove(0);
  list->remove(3);

  if (list->size() == 3) {
    cout << "SUCCESS: List has size 3 after two removals." << endl;
  } else {
    cout << "FAIL: List has size " << list->size() << " after two removals.";
    cout << endl;
  }

  // Check if the value is correct.
  if (list->get(0) == 3) {
    cout << "SUCCESS: 3 is at the 0th index of the list." << endl;
  } else {
    cout << "FAIL: 3 is not at the 0th index of the list, " << list->get(0);
    cout << " is instead." << endl;
  }

  // Check if the value is correct.
  if (list->get(2) == 2) {
    cout << "SUCCESS: 2 is at the 3rd index of the list." << endl;
  } else {
    cout << "FAIL: 2 is not at the 3rd index of the list, " << list->get(2);
    cout << " is instead." << endl;
  }

  // Remove item from middle of an array
  list->remove(1); 

  if (list->size() == 2) {
    cout << "SUCCESS: List has size 2 after three removals." << endl;
  } else {
    cout << "FAIL: List has size " << list->size() << " after three removals.";
    cout << endl;
  }

  // Check if the value is correct.
  if (list->get(1) == 2) {
    cout << "SUCCESS: 2 is at the 1st index of the list." << endl;
  } else {
    cout << "FAIL: 2 is not at the 1st index of the list, " << list->get(1);
    cout << " is instead." << endl;
  }  

  // Clean list
  list->clear();

  if (list->empty()) {
    cout << "SUCCESS: List is empty after clear()." << endl;
  } else {
    cout << "FAIL: List is not empty after clear() when it should be." << endl;
  }


  // Clean up memory.
  delete list;
}
int main(){
	LListInt* list = new LListInt();

	cout << "BEGINNING 15 TEST CASES..." << endl;

	// We can assume that the following will work if and only if get(), size() and empty() work
	// get() and set() will throw a core dump if given bad location

	// NOMINAL
	// 1. Check if the list is initially empty.
	if (list->empty()) cout << "SUCCESS: (1) List is empty initially." << endl;
	else cout << "FAIL: (1) List is not empty initially when it should be." << endl;

	// OFFNOMINAL
	// 2. Check: insert into empty list with bad location
	list->insert(1, 5);
	if(list->empty()) cout << "SUCCESS: (2) List is still empty." << endl;
	else cout << "FAIL: (2) List now has " << list->size() << " element(s)" << endl;

	// NOMINAL
	// 3. Check: insert into empty list with loc = 0
	list->insert(0,5); // 5
	if(list->size()==1){
		if(list->get(0)==5) cout << "SUCCESS";
		else cout << "FAIL";
		string temp = printlist(list);
		cout << ": (3) list is " << temp << endl;

	}
	else cout << "FAIL: (3) list has size " << list->size() << " instead of 1" << endl;

	// BOUNDARY
	// 4. Check: insert element to end of list. (loc == size)
	list->insert(1,8); // 5, 8
	if(list->size()==2){
		if(list->get(0)==5 && list->get(1)==8) cout << "SUCCESS";
		else cout << "FAIL";
		string temp = printlist(list);
		cout << ": (4) list is " << temp << endl;
	}
	else cout << "FAIL: (4) list has size " << list->size() << " instead of 2" << endl;

	// BOUNDARY
	// 5. Check: insert element to front of list
	list->insert(0,2); // 2, 5, 8
	if(list->size()==3){
		if(list->get(0)==2 && list->get(1)==5 && list->get(2)==8) cout << "SUCCESS";
		else cout << "FAIL";
		string temp = printlist(list);
		cout << ": (5) list is " << temp << endl;
	}
	else cout << "FAIL: (5) list has size " << list->size() << " instead of 3" << endl;

	// NOMINAL
	// 6. Check: insert element to middle of list
	list->insert(2,3); // 2, 5, 3, 8
	if(list->size()==4){
		if(list->get(0)==2 && list->get(1)==5 && list->get(2)==3 && list->get(3)==8)
			cout << "SUCCESS";
		else cout << "FAIL";
		string temp = printlist(list);
		cout << ": (6) list is " << temp << endl;
	}
	else cout << "FAIL: (6) list has size " << list->size() << " instead of 4" << endl;

	// OFFNOMINAL
	// 7. Check: insert element in bad location
	list->insert(5,0); // 2, 5, 3, 8
	if(list->size()==4){
		if(list->get(0)==2 && list->get(1)==5 && list->get(2)==3 && list->get(3)==8)
			cout << "SUCCESS";
		else cout << "FAIL";
		string temp = printlist(list);
		cout << ": (7) list is " << temp << endl;
	}
	else if(list->size()==5) 
		cout << "FAIL: (7) the 5th element " << list->get(5) << " was incorrectly inserted" << endl;
	else cout << "FAIL: (7) list has size " << list->size() << " instead of 4" << endl;

	// OFFNOMINAL
	// 8. Check: remove element from loc == size (bad location)
	list->remove(4);
	if(list->size()==4){
		if(list->get(0)==2 && list->get(1)==5 && list->get(2)==3 && list->get(3)==8)
			cout << "SUCCESS";
		else cout << "FAIL";
		string temp = printlist(list);
		cout << ": (8) list is " << temp << endl;
	}
	else cout << "FAIL: (8) list has size " << list->size() << " instead of 4" << endl;

	// OFFNOMINAL
	// 9. Check: remove element from loc > size (bad location)
	list->remove(5);
	if(list->size()==4){
		if(list->get(0)==2 && list->get(1)==5 && list->get(2)==3 && list->get(3)==8)
			cout << "SUCCESS";
		else cout << "FAIL";
		string temp = printlist(list);
		cout << ": (9) list is " << temp << endl;
	}
	else cout << "FAIL: (9) list has size " << list->size() << " instead of 4" << endl;

	// BOUNDARY
	// 10. Check: remove element from end of list
	list->remove(3); // 2, 5, 3
	if(list->size()==3){
		if(list->get(0)==2 && list->get(1)==5 && list->get(2)==3)
			cout << "SUCCESS";
		else cout << "FAIL";
		string temp = printlist(list);
		cout << ": (10) list is " << temp << endl;
	}
	else cout << "FAIL: (10) list has size " << list->size() << " instead of 3" << endl;

	// BOUNDARY
	// 11. Check: removing from beginning of list
	list->remove(0); // 5, 3
	if(list->size()==2){
		if(list->get(0)==5 && list->get(1)==3)
			cout << "SUCCESS";
		else cout << "FAIL";
		string temp = printlist(list);
		cout << ": (11) list is " << temp << endl;
	}
	else cout << "FAIL: (11) list has size " << list->size() << " instead of 2" << endl;

	// NOMINAL
	// 12. Check: removing from middle of list
	list->insert(1,9); // 5, 9, 3
	list->remove(1); // 5, 3
	if(list->size()==2){
		if(list->get(0)==5 && list->get(1)==3)
			cout << "SUCCESS";
		else cout << "FAIL";
		string temp = printlist(list);
		cout << ": (12) list is " << temp << endl;
	}
	else cout << "FAIL: (12) list has size " << list->size() << " instead of 2" << endl;

	// 13. Check: setting
	list->set(0, 3); // 3, 3
	list->set(1, 5); // 3, 5
	if(list->size()==2){
		if(list->get(1)==5 && list->get(0)==3)
			cout << "SUCCESS";
		else cout << "FAIL";
		string temp = printlist(list);
		cout << ": (13) list is " << temp << endl;
	}
	else cout << "FAIL: (13) list has size " << list->size() << " instead of 2" << endl;

	// NOMINAL
	// 14. Check: clear
	list->clear();
	string temp = printlist(list);
	if (list->empty()) cout << "SUCCESS: (14) List is emptied." << endl;
	else cout << "FAIL: (14) List is not empty: " << temp << endl;

	//OFFNOMINAL
	// 15. Check: remove
	list->remove(0);
	temp = printlist(list);
	if (list->empty()) cout << "SUCCESS: (15) List is empty and ok." << endl;
	else cout << "FAIL: (15) List is not empty: " << temp << endl;

	cout << "ALL TESTS ARE COMPLETE. NOW DELETING LIST." << endl;

	delete list;

	return 0;

}
Exemplo n.º 3
0
int main() {
	LListInt * list = new LListInt();
 	list->insert(0, 3);
  	//insert an item to the empty list. 

  	if (list->size() == 1) {
    	cout << "SUCCESS: List has size 1 after one insertion." << endl;
  	} 
  	else {
    	cout << "FAIL: List has size " << list->size() << " after one insertion.";
    	cout << endl;
  	}

	if (list->get(0) == 3) {
    	cout << "SUCCESS: 3 is at the 0th index of the list." << endl;
  	} 
  	else {
    	cout << "FAIL: 3 is not at the 0th index of the list, " << list->get(0);
    	cout << " is instead." << endl;
  	}

  	list->insert(1,4);
  	//insert an item at the valid location SIZE
  	//the tail will be changed and the size would be changed to 2
  	if (list->size() == 2) {
    	cout << "SUCCESS: List has size 2 after one insertion." << endl;
  	} 
  	else {
    	cout << "FAIL: List has size " << list->size() << " after one insertion.";
    	cout << endl;
  	}
  	//the second item will have the value 2;
  	if (list->get(1) == 4) {
    	cout << "SUCCESS: 4 is at the 1st index of the list." << endl;
  	} 
  	else {
    	cout << "FAIL: 4 is not at the 1st index of the list, " << list->get(1);
    	cout << " is instead." << endl;
  	}

  	list->insert(1,5);
  	//add one item at the location between 0 to Size-1
  	if(list->size() == 3){
  		cout << "SUCCESS: List has size 3 after one insertion." << endl;
  	} 
  	else {
    	cout << "FAIL: List has size " << list->size() << " after one insertion.";
    	cout << endl;
  	}

  	if (list->get(1) == 5) {
    	cout << "SUCCESS: 5 is at the 1st index of the list." << endl;
  	} 
  	else {
    	cout << "FAIL: 5 is not at the 1st index of the list, " << list->get(1);
    	cout << " is instead." << endl;
  	}
  	//5 should be the new value of the 1st index
  	if (list->get(2) == 4) {
    	cout << "SUCCESS: 4 is at the 2nd index of the list." << endl;
  	} 
  	else {
    	cout << "FAIL: 4 is not at the 2nd index of the list, " << list->get(2);
    	cout << " is instead." << endl;
  	}
  	//4 would be pushed to the next position at the 2nd.
	//the substitution is successful.

	list->insert(5,4);
	list->insert(-2,4);
	//insert an item to the array at the location out of valid range
	if(list->size() == 3){
  		cout << "SUCCESS: List has size 3 after two insertion." << endl;
  	} 
  	else {
    	cout << "FAIL: List has size " << list->size() << " after one insertion.";
    	cout << endl;
  	}

	list->remove(1);
	//delete the item in location 1 in the valid range
	if(list->size() == 2){
  		cout << "SUCCESS: List has size 2 after one remove." << endl;
  	} 
  	else {
    	cout << "FAIL: List has size " << list->size() << " after one insertion.";
    	cout << endl;
  	}
  	//the size should be 2 now.

	if (list->get(1) == 4) {
    	cout << "SUCCESS: 4 is at the 1st index of the list." << endl;
  	} 
  	else {
    	cout << "FAIL: 4 is not at the 1st index of the list, " << list->get(1);
    	cout << " is instead." << endl;
  	}
  	//the item with value 4 will substitute the value at location 1 before

  	list->remove(4);
  	list->remove(-2);
  	//delete the item at the location out of the valid range
  	if(list->size() == 2){
  		cout << "SUCCESS: List has size 2 after one remove." << endl;
  	} 
  	else {
    	cout << "FAIL: List has size " << list->size() << " after one insertion.";
    	cout << endl;
  	}
  	//the size should keep 2 now.

  	list->insert(2,5);
  	//insert item with value 5 to the list.
  	list->remove(2);
  	//remove the last item(boundary)
  	if(list->size() == 2){
  		cout << "SUCCESS: List has size 2 after one remove." << endl;
  	} 
  	else {
    	cout << "FAIL: List has size " << list->size() << " after one insertion.";
    	cout << endl;
  	}
  	//the last item is removed and the size will become 2
  	if (list->get(1) == 4) {
    	cout << "SUCCESS: 4 is at the 1st index of the list." << endl;
  	} 
  	else {
    	cout << "FAIL: 4 is not at the 1st index of the list, " << list->get(1);
    	cout << " is instead." << endl;
  	}

  	list->insert(2,5);
  	list->remove(0);
  	//remove the first boundary item
  	//the rest items should move left by one spot
  	if(list->size() == 2){
  		cout << "SUCCESS: List has size 2 after one remove." << endl;
  	} 
  	else {
    	cout << "FAIL: List has size " << list->size() << " after one insertion.";
    	cout << endl;
  	}
  	//the first item is removed and the size will become 2
  	if (list->get(1) == 5) {
    	cout << "SUCCESS: 5 is at the 1st index of the list." << endl;
  	} 
  	else {
    	cout << "FAIL: 5 is not at the 1st index of the list, " << list->get(1);
    	cout << " is instead." << endl;
  	}
  	list->clear();
  	if(list->size() == 0){
  		cout << "SUCCESS: List has size 0 after one clear." << endl;
  	} 
  	else {
    	cout << "FAIL: List has size " << list->size() << " after one insertion.";
    	cout << endl;
  	}
  	list->remove(0);
  	if(list->size() == 0){
  		cout << "SUCCESS: List has size 0 after one clear." << endl;
  	} 
  	else {
    	cout << "FAIL: List has size " << list->size() << " after one insertion.";
    	cout << endl;
  	}
  	delete list;
  	return 0;
}