コード例 #1
0
void bagTester() 
{
	LinkedBag<string> bag;
	cout << "Testing the Link-Based Bag:" << endl;
	cout << "isEmpty: returns " << bag.isEmpty() << "; should be 1 (true)" << endl;
	cout << bag;

	string numbers[] = { "one", "two", "three", "four", "five", "one" };
	cout << "Add 6 items to the bag: " << endl;
	for (int i = 0; i < sizeof(numbers)/sizeof(numbers[0]); i++)
	{
		bag.add(numbers[i]);
	}  // end for

	cout << bag;
	cout << "isEmpty: returns " << boolalpha << bag.isEmpty() << "; should be 0 (false)" << endl;
	cout << "getCurrentSize: returns " << bag.getCurrentSize() << "; should be 6" << endl;
	cout << "Try to add another entry: add(\"extra\") returns " << bag.add(string("extra")) << endl;
	cout << "contains(\"three\"): returns " << boolalpha << bag.contains(string("three")) << "; should be 1 (true)" << endl;
	cout << "contains(\"ten\"): returns " << bag.contains(string("ten")) << "; should be 0 (false)" << endl;
	cout << "getFrequencyOf(\"one\"): returns " << bag.getFrequencyOf(string("one")) << " should be 2" << endl;
	cout << "remove(\"one\"): returns " << boolalpha << bag.remove(string("one")) << "; should be 1 (true)" << endl;
	cout << "getFrequencyOf(\"one\"): returns " << bag.getFrequencyOf(string("one")) << " should be 1" << endl;
	cout << "remove(\"one\"): returns " << boolalpha << bag.remove(string("one")) << "; should be 1 (true)" << endl;
	cout << "remove(\"one\"): returns " << boolalpha << bag.remove(string("one")) << "; should be 0 (false)" << endl;
	cout << endl;

	cout << bag;

	cout << "After clearing the bag, ";
	bag.clear();

	cout << "isEmpty: returns " << bag.isEmpty() << "; should be 1 (true)" << endl;
}  // end bagTester
コード例 #2
0
void copyConstructorTester()
{
	LinkedBag<string> bag;
	string numbers[] = { "zero", "one", "two", "three", "four", "five" };
	for (int i = 0; i < bag.getCurrentSize(); i++)
	{
		cout << "Adding " << numbers[i] << endl;
		bool success = bag.add(numbers[i]);
		if (!success)
			cout << "Failed to add " << numbers[i] << " to the bag." << endl;
	}
	cout << bag;

	LinkedBag<string> copyOfBag = bag;
	cout << "Copy of bag: ";
	cout << copyOfBag;

	cout << "The copied bag: ";
	cout << bag;
}  // end copyConstructorTester