Ejemplo n.º 1
0
int TestRedBlackTree_CString()
{
	RedBlackTree<const char *, const char *> *rbtree = new RedBlackTree<const char *, const char *>();
	char                                     *tmp;

	TEST_ASSERT(rbtree->size() == 0);

	/* If the tree is properly encapsulated, this won't cause an error on test #1. */
	tmp = cc_strdup("first");
	rbtree->insert(tmp, "one");
	free(tmp); tmp = NULL;

	TEST_ASSERT(rbtree->size() == 1);

	rbtree->insert("second", "two");
	rbtree->insert("third", "three");
	rbtree->insert("fourth", "four");

	TEST_ASSERT(rbtree->size() == 4);

	const char *res = NULL;
	TEST_ASSERT(rbtree->find("first", res));

	const char *tmp1 = "one";
	TEST_ASSERT(Compare(res, tmp1) == 0);

	TEST_ASSERT(!rbtree->exists("fifth"));

	TEST_ASSERT(rbtree->exists("second"));
	TEST_ASSERT(rbtree->find("second", res));

	tmp1 = "two";
	TEST_ASSERT(Compare(res, tmp1) == 0);

	TEST_ASSERT(!rbtree->erase("fifth"));
	TEST_ASSERT(rbtree->size() == 4);

	TEST_ASSERT(rbtree->exists("first"));
	TEST_ASSERT(rbtree->erase("first"));
	TEST_ASSERT(rbtree->size() == 3);

	TEST_ASSERT(rbtree->exists("second"));
	TEST_ASSERT(rbtree->erase("second"));
	TEST_ASSERT(rbtree->size() == 2);

	TEST_ASSERT(rbtree->exists("third"));
	TEST_ASSERT(rbtree->erase("third"));
	TEST_ASSERT(rbtree->size() == 1);

	TEST_ASSERT(rbtree->exists("fourth"));
	TEST_ASSERT(rbtree->erase("fourth"));
	TEST_ASSERT(rbtree->size() == 0);

	delete rbtree;
	return 0;
}
Ejemplo n.º 2
0
int TestRedBlackTree_String()
{
	RedBlackTree<std::string, std::string> *rbtree = new RedBlackTree<std::string, std::string>();

	TEST_ASSERT(rbtree->size() == 0);

	rbtree->insert("first", "one");
	rbtree->insert("second", "two");
	rbtree->insert("third", "three");
	rbtree->insert("fourth", "four");

	std::string                             res;

	TEST_ASSERT(rbtree->size() == 4);

	TEST_ASSERT(rbtree->find("first", res));
	TEST_ASSERT(res == "one");

	TEST_ASSERT(!rbtree->exists("fifth"));

	TEST_ASSERT(rbtree->exists("second"));
	TEST_ASSERT(rbtree->find("second", res));

	TEST_ASSERT(res == "two");

	TEST_ASSERT(!rbtree->erase("fifth"));
	TEST_ASSERT(rbtree->size() == 4);

	TEST_ASSERT(rbtree->exists("first"));
	TEST_ASSERT(rbtree->erase("first"));
	TEST_ASSERT(rbtree->size() == 3);

	TEST_ASSERT(rbtree->exists("second"));
	TEST_ASSERT(rbtree->erase("second"));
	TEST_ASSERT(rbtree->size() == 2);

	TEST_ASSERT(rbtree->exists("third"));
	TEST_ASSERT(rbtree->erase("third"));
	TEST_ASSERT(rbtree->size() == 1);

	TEST_ASSERT(rbtree->exists("fourth"));
	TEST_ASSERT(rbtree->erase("fourth"));
	TEST_ASSERT(rbtree->size() == 0);

	delete rbtree;
	return 0;
}
Ejemplo n.º 3
0
int TestRedBlackTree_Int()
{
	RedBlackTree<int, int> *rbtree = new RedBlackTree<int, int>();

	TEST_ASSERT(rbtree->size() == 0);

	rbtree->insert(1, 1);
	rbtree->insert(2, 2);
	rbtree->insert(3, 3);
	rbtree->insert(4, 4);

	int                     res;
	TEST_ASSERT(rbtree->size() == 4);

	TEST_ASSERT(rbtree->find(1, res));
	TEST_ASSERT(res == 1);

	TEST_ASSERT(!rbtree->exists(5));

	TEST_ASSERT(rbtree->find(2, res));
	TEST_ASSERT(res == 2);

	TEST_ASSERT(!rbtree->erase(5));
	TEST_ASSERT(rbtree->size() == 4);

	TEST_ASSERT(rbtree->exists(1));
	TEST_ASSERT(rbtree->erase(1));
	TEST_ASSERT(rbtree->size() == 3);

	TEST_ASSERT(rbtree->exists(2));
	TEST_ASSERT(rbtree->erase(2));
	TEST_ASSERT(rbtree->size() == 2);

	TEST_ASSERT(rbtree->exists(3));
	TEST_ASSERT(rbtree->erase(3));
	TEST_ASSERT(rbtree->size() == 1);

	TEST_ASSERT(rbtree->exists(4));
	TEST_ASSERT(rbtree->erase(4));
	TEST_ASSERT(rbtree->size() == 0);

	delete rbtree;
	return 0;
}
Ejemplo n.º 4
0
int main(int argc, const char *argv[]) {
// Make sure the right number of command line arguments exist.
    if (!(argc == 3 || argc == 2)) {
        cerr << "Usage: " << argv[0] << " <filename> [limit]" << endl;
        return 1;
    }
// Create an ifstream object.
    ifstream input_file(argv[1]);
// If it does not exist, print an error message.
    if (!input_file) {
        cerr << "Error: Cannot open file '" << argv[1] << "' for input."
                << endl;
        return 1;
    }

    if (argc == 3 && !is_number(argv[2])) {
        cerr << "Error: Invalid limit '" << argv[2] << "' received." << endl;
        return 1;
    }

    int limit;
    if (argc == 3) {
        limit = atoi(argv[2]);
    } else {
        limit = 10;
    }

    if (limit < 0) {
        cerr << "Error: Invalid limit '" << argv[2] << "' received." << endl;
        return 1;
    }

    RedBlackTree<string, int> *tree;
    tree = new RedBlackTree<string, int>();
// Add read errors to the list of exceptions the ifstream will handle.
    input_file.exceptions(ifstream::badbit);
    string line;
    unsigned int maxwordwidth = 0;

    try {

        // Use getline to read in a line.
        // See http://www.cplusplus.com/reference/string/string/getline/

        while (getline(input_file, line)) {
            string buf; // Have a buffer string
            stringstream ss(line); // Insert the string into a stream

            vector<string> tokens; // Create vector to hold our words

            while (ss >> buf) {
                tokens.push_back(buf);

                string nopunct = removePunct(buf);

                RedBlackTree<string, int>::iterator it = tree->find(nopunct);
                if (it == tree->end()) {
                    if (nopunct != "")
                        tree->insert(nopunct, 1);
                } else {
                    it->second++;
                }
            }

        }

        // Don't forget to close the file.
        input_file.close();

    } catch (const ifstream::failure &f) {
        cerr << "Error: An I/O error occurred reading '" << argv[1] << "'.";
    }

    vector<Node<string, int> > sorted;
    for (RedBlackTree<string, int>::iterator it = tree->begin();
            it != tree->end(); ++it) {
        Node<string, int> n(it->first, it->second);

        sorted.push_back(n);
    }

    sort(sorted.begin(), sorted.end(), lessthanfun); //loop through sorted from 0 to limit to determine the largest word size
    int wordnum = 1;
    for (vector<Node<string, int> >::iterator it = sorted.begin();
            it != sorted.end(); ++it) {

        if ((it->kv_pair_.first).size() > maxwordwidth) {
            maxwordwidth = (it->kv_pair_.first).size();
        }
        if (wordnum >= limit) {

            break;
        }
        wordnum++;
    }

    wordnum = 1;
    cout << "Total unique words: " << tree->size() << endl;
    int numwidth = numDigits(min((int) tree->size(), limit));
    for (vector<Node<string, int> >::iterator it = sorted.begin();
            it != sorted.end(); ++it) {

        string numstring = "";
        int thisnumwidth = numDigits(wordnum);
        for (int i = 0; i < numwidth - thisnumwidth; i++) {
            numstring += " ";
        }

        stringstream convert;
        convert << wordnum;
        numstring += convert.str();
        string wordoutput = it->kv_pair_.first;

        for (size_t i = 0; i < maxwordwidth - (it->kv_pair_.first).size() + 1;
                i++) {

            wordoutput += " ";
        }
        cout << numstring << ". " << wordoutput << it->kv_pair_.second << endl;
        if (wordnum >= limit) {
            break;

        }
        wordnum++;
    }
    return 0;
}