<commit_before>/*
 * simple_testing.cpp
 *
 *  Created on: Dec 21, 2012
 *      Author: michael.hunter
 */
#include "simple_testing.hpp"
#include "debug_new_and_delete.hpp"

#include <exception>
#include <iostream>
#include <time.h>
#include <stdio.h>

std::vector<SimpleTest*>* SimpleTest::allTests = 0;
std::string* SimpleTest::lastFileName_ = 0;

SimpleTest::SimpleTest(const char* testName, const char* fileName, int line)
	: isFailed_(false), testName_(testName), fileName_(fileName),
	testStartLine_(line), passCount_(0), failCount_(0)
{
	if (!allTests)
		allTests = new std::vector<SimpleTest*>();
	allTests->push_back(this);
}

void SimpleTest::checkMemory(int line)
{
	switch (checkConsistency())    {
	case 1: fail("Heap corrupt", line); break;
	case 2: fail("Free header corrupt", line); break;
	case 3: fail("Free memory modified", line); break;
	}

	if (isMultipleFree)
		fail("The same pointer has been freed twice", line);
}

void SimpleTest::executeTest()
{
	try
	{
		if (!lastFileName_ || (*lastFileName_ != fileName_))
		{
			if (!lastFileName_)
				lastFileName_ = new std::string(fileName_);
			else
				*lastFileName_ = fileName_;
			std::cout << "-------------- " << fileName_ << "----------------" << std::endl;
		}
		size_t bytesBefore = bytesAllocatedCount;
		passCount_ = 0;
		failCount_ = 0;
		run();
		checkMemory(testStartLine_);
		if (bytesBefore != bytesAllocatedCount)
			fail("Memory leak during test", testStartLine_);
		std::cout << testName_ << ": " << passCount_  << "/" << (passCount_ + failCount_) << std::endl;
	}
	catch (std::exception& e)
	{
		fail(e.what(), testStartLine_);
	}
	catch (...)
	{
		fail("Test threw an exception", testStartLine_);
	}
}

int SimpleTest::runAllTests()
{
	if (!allTests)
	{
		std::cout << "No Tests" << std:: endl;
		return 0;
	}

	std::cout << "Running " << allTests->size() << " tests" << std:: endl;
	for (auto test : *allTests)
	{

		test->executeTest();
		if (test->isFailed_)
			return 1;
	}
	std::cout << "All tests passed" << std::endl;
	return 0;
}

void SimpleTest::assert_(bool proposition, const char* msg, int line)
{
	if (proposition)
		pass(msg);
	else
		fail(msg, line);
}

void SimpleTest::pass(const char* passText)
{
	passCount_ ++;
	//std::cout << "Passed: " << passText << std::endl;
}

void SimpleTest::fail(const char* failText, int line)
{
	isFailed_ = true;
	failCount_ ++;
#if _MSC_VER
	std::cout << fileName_ << "(" << line << "): error: " << testName_ << " failed: " << failText << std::endl;
#else
	std::cout << fileName_ << ":" << line << ": error: " << testName_ << " failed: " << failText << std::endl;
#endif
}

int main(int argc, char *argv[])
{
	int result = SimpleTest::runAllTests();

	const char* successFile = nullptr;
	for (int i = 0; i < argc - 1; i ++)
		if (strcmp(argv[i], "-s") == 0)
			successFile = argv[i+1];

	if (result == 0 && successFile)
	{
		struct tm *current;
		time_t now;
		time(&now);
		current = localtime(&now);
		FILE* fp = fopen(successFile, "w");
		fprintf(fp, "Last successful build: %04u-%02i-%02i %02i:%02i:%02i\n",
			current->tm_year + 1900,
			current->tm_mon,
			current->tm_mday,
			current->tm_hour,
			current->tm_min,
			current->tm_sec
			);
		fclose(fp);
	}

	return result;
}
<commit_msg>Report number of tests when successful<commit_after>/*
 * simple_testing.cpp
 *
 *  Created on: Dec 21, 2012
 *      Author: michael.hunter
 */
#include "simple_testing.hpp"
#include "debug_new_and_delete.hpp"

#include <exception>
#include <iostream>
#include <time.h>
#include <stdio.h>

std::vector<SimpleTest*>* SimpleTest::allTests = 0;
std::string* SimpleTest::lastFileName_ = 0;

SimpleTest::SimpleTest(const char* testName, const char* fileName, int line)
	: isFailed_(false), testName_(testName), fileName_(fileName),
	testStartLine_(line), passCount_(0), failCount_(0)
{
	if (!allTests)
		allTests = new std::vector<SimpleTest*>();
	allTests->push_back(this);
}

void SimpleTest::checkMemory(int line)
{
	switch (checkConsistency())    {
	case 1: fail("Heap corrupt", line); break;
	case 2: fail("Free header corrupt", line); break;
	case 3: fail("Free memory modified", line); break;
	}

	if (isMultipleFree)
		fail("The same pointer has been freed twice", line);
}

void SimpleTest::executeTest()
{
	try
	{
		if (!lastFileName_ || (*lastFileName_ != fileName_))
		{
			if (!lastFileName_)
				lastFileName_ = new std::string(fileName_);
			else
				*lastFileName_ = fileName_;
			std::cout << "-------------- " << fileName_ << "----------------" << std::endl;
		}
		size_t bytesBefore = bytesAllocatedCount;
		passCount_ = 0;
		failCount_ = 0;
		run();
		checkMemory(testStartLine_);
		if (bytesBefore != bytesAllocatedCount)
			fail("Memory leak during test", testStartLine_);
		std::cout << testName_ << ": " << passCount_  << "/" << (passCount_ + failCount_) << std::endl;
	}
	catch (std::exception& e)
	{
		fail(e.what(), testStartLine_);
	}
	catch (...)
	{
		fail("Test threw an exception", testStartLine_);
	}
}

int SimpleTest::runAllTests()
{
	if (!allTests)
	{
		std::cout << "No Tests" << std:: endl;
		return 0;
	}

	std::cout << "Running " << allTests->size() << " tests" << std:: endl;
	for (auto test : *allTests)
	{

		test->executeTest();
		if (test->isFailed_)
			return 1;
	}
	std::cout << "All " << allTests->size() << " tests passed" << std::endl;
	return 0;
}

void SimpleTest::assert_(bool proposition, const char* msg, int line)
{
	if (proposition)
		pass(msg);
	else
		fail(msg, line);
}

void SimpleTest::pass(const char* passText)
{
	passCount_ ++;
	//std::cout << "Passed: " << passText << std::endl;
}

void SimpleTest::fail(const char* failText, int line)
{
	isFailed_ = true;
	failCount_ ++;
#if _MSC_VER
	std::cout << fileName_ << "(" << line << "): error: " << testName_ << " failed: " << failText << std::endl;
#else
	std::cout << fileName_ << ":" << line << ": error: " << testName_ << " failed: " << failText << std::endl;
#endif
}

int main(int argc, char *argv[])
{
	int result = SimpleTest::runAllTests();

	const char* successFile = nullptr;
	for (int i = 0; i < argc - 1; i ++)
		if (strcmp(argv[i], "-s") == 0)
			successFile = argv[i+1];

	if (result == 0 && successFile)
	{
		struct tm *current;
		time_t now;
		time(&now);
		current = localtime(&now);
		FILE* fp = fopen(successFile, "w");
		fprintf(fp, "Last successful build: %04u-%02i-%02i %02i:%02i:%02i\n",
			current->tm_year + 1900,
			current->tm_mon,
			current->tm_mday,
			current->tm_hour,
			current->tm_min,
			current->tm_sec
			);
		fclose(fp);
	}

	return result;
}

{'fraction_non_alphanumeric': 0.18694452957401164, 'fraction_numerical': 0.01195219123505976, 'mean_word_length': 4.05968992248062, 'pattern_counts': {'":': 6, '<': 169, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1024899', 'n_tokens_mistral': 2505, 'n_tokens_neox': 2336, 'n_words': 560}