Example #1
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;
}
Example #2
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;
}
Example #3
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;
}
void ParseMemoryLeakFile(const char *_inputFilename, const char *_outputFilename)
{
	/* */
	/* Start up */
	/* */

	RedBlackTree<char *, int> combined;
	RedBlackTree<char *, int> frequency;
	int                       unrecognised = 0;

	/* */
	/* Open the file and start parsing */
	/* */

	FILE                     *memoryfile = fopen(_inputFilename, "rb");

	while (memoryfile && !feof(memoryfile))	{
		char thisline[1024];

		fgets(thisline, 1024, memoryfile);

		if (!strncmp(thisline, " Data:", 6) == 0) { /* This line is a data line - useless to us */
			if (strchr(thisline, ':')) {                               /* This line does not have a source file location - useless to us */
				/* Get the size */

				char *lastcomma = strrchr(thisline, ',');
				if (lastcomma == 0) continue;

				char *ssize = lastcomma + 2;
				int   size;
				char  unused[32];

				sscanf(ssize, "%d %s", &size, unused);

				/* Get the source file name */

				char *sourcelocation = thisline;
				char *colon = strrchr(thisline, ':');

				*(colon - 1) = '\x0';

				/* Put the result into our BTree */

				int   result = 0;
				bool  found = combined.find(sourcelocation, result);

				if (found)
					combined.replace(sourcelocation, result + size);
				else
					combined.insert(sourcelocation, size);

				found = frequency.find(sourcelocation, result);

				if (frequency.exists(sourcelocation))
					frequency.replace(sourcelocation, result + size);
				else
					frequency.insert(sourcelocation, 1);
			} else {
				char *lastcomma = strrchr(thisline, ',');

				if (lastcomma) {
					char *ssize = lastcomma + 2;
					int   size;
					char  unused[32];

					sscanf(ssize, "%d %s", &size, unused);

					unrecognised += size;
				}
			}
		}
	}

	fclose(memoryfile);


	/* */
	/* Sort the results into a list */
	/* */

	LList<char *>   sorted;
	DArray<char *> *dataI = combined.ConvertIndexToDArray();
	DArray<int>    *dataD = combined.ConvertToDArray();

	int             totalsize = 0;

	for (size_t i = 0; i < dataI->size(); i++) {
		if (dataI->valid(i)) {
			char *newsource = dataI->get(i);
			int   newsize = dataD->get(i);

			totalsize += newsize;
			bool  inserted = false;

			for (size_t i = 0; i < sorted.size(); i++) {
				char *existingsource = sorted.get(i);
				int   existingsize;

				combined.find(existingsource, existingsize);

				if (newsize <= existingsize) {
					sorted.insert_at(newsource, i);
					inserted = true;
					break;
				}
			}

			if (!inserted)
				sorted.insert(newsource);
		}
	}

	delete dataI;
	delete dataD;


	/* */
	/* Open the output file */
	/* */

	if (sorted.size()) {
		FILE *output = fopen(_outputFilename, "wt");

		/* */
		/* Print out our sorted list */
		/* */

		fprintf(output, "Total recognised memory leaks   : %d Kbytes\n",
		        int( totalsize / 1024 ));
		fprintf(output, "Total unrecognised memory leaks : %d Kbytes\n\n",
		        int( unrecognised / 1024 ));

		for (int k = (int)sorted.size() - 1; k >= 0 && k < (int)sorted.size(); k--) {
			char *source = sorted.get(k);
			CoreAssert(source);
			int   size; combined.find(source, size);
			int   freq; frequency.find(source, freq);

			if (size > 1048576) {
				fprintf(output, "%-95s (%d MB in %d leaks)\n", source,
				        int( size / 1048576 ), freq);
			} else if (size > 2048) {
				fprintf(output, "%-95s (%d KB in %d leaks)\n", source,
				        int( size / 1024 ), freq);
			} else {
				fprintf(output, "%-95s (%d  bytes in %d leaks)\n", source, size,
				        freq);
			}
		}

		/* */
		/* Clean up */

		fclose(output);
	}
}