#include <iostream>
#include <time.h>
#include <cppunit/TestCase.h>
#include <cppunit/ui/text/TextTestRunner.h>
#define ARRAY_SIZE 10
using namespace std;

class BubbleSort
{
public:
	BubbleSort();
	virtual ~BubbleSort();
	void print();
	void sort();
	unsigned operator[](const int index){return m_array[index];}
private:
	unsigned *m_array;
};

class BubbleSortTest: public CppUnit::TestCase
{
public:
	void runTest()
	{
		BubbleSort b;
        b.sort();
		for (int i = 0; i < ARRAY_SIZE-1; i ++)
			CPPUNIT_ASSERT_MESSAGE("Before isn't smaller than after.\n", b[i] <= b[i+1]);			
	}
};

BubbleSort::BubbleSort()
:m_array(new unsigned[ARRAY_SIZE])
{
	cout << "address1: " << m_array << endl;
	srand((unsigned)time(NULL));
	for (int i = 0; i < ARRAY_SIZE; i ++)
		this->m_array[i] = rand();
}

BubbleSort::~BubbleSort()
{
	delete[] this->m_array;
}

void BubbleSort::print()
{
	for (int i = 0; i < ARRAY_SIZE; i ++)
		cout << this->m_array[i] << " ";
	cout << endl;
}

void BubbleSort::sort()
{
	unsigned tmp = 0;
	for (int i = 0; i < ARRAY_SIZE-1; i ++)
	{
		for (int j = 0; j < ARRAY_SIZE-1; j ++)
		{
			if (this->m_array[j] > this->m_array[j+1])
			{
				tmp = this->m_array[j];
				this->m_array[j] = this->m_array[j+1];
				this->m_array[j+1] = tmp;
			}
		}
	}
}

int main()
{
	BubbleSort sort1;
	sort1.print();
	sort1.sort();
	sort1.print();
	
	BubbleSortTest test;
	CppUnit::TextTestRunner runner;
	runner.addTest(&test);
	runner.run();
	system("pause");
	return 0;

}