// TestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"
#include "Get.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog

CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestDlg)
	m_Notify = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDlg)
	DDX_Text(pDX, IDC_Notify, m_Notify);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_Test, OnTest)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers

BOOL CTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTestDlg::OnTest() 
{
	//	Create the end of the queue
	CGet Value;
	CoInitialize(0);

	//	Create the Queue
	IGet*Get;
	if (FAILED(CoCreateInstance(CLSID_Queue, 0, CLSCTX_INPROC_SERVER, IID_IGet, (void**)&Get)))
		return;

	//	Link the two
	IQueue*Queue;
	if (FAILED(Get->QueryInterface(IID_IQueue, (void**)&Queue)) ||
		FAILED(Queue->put_Interface(&Value)))
	{
		ASSERT(0);
		Get->Release();
		return;
	}

	//	Do the tests
	Test1(&Value, Get);
	Test2(&Value, Get);
	Test3(&Value, Get);

	//	Get rid of the queue
	Queue->Release();
	Get->Release();

	CoUninitialize();
}

//	Quick test
void CTestDlg::Test1(CGet*aEnd, IGet*aGet)
{
	UpdateData();
	m_Notify = "Simple test";
	UpdateData(FALSE);
	aEnd->AddList((UCHAR*)"ABCDEFGHIJKL", 12);

	char c;
	char d = 0;
	UINT Length;
	UINT Time;
	for (c = 'A'; c <='L'; c++)
	{
		aGet->Get((UCHAR*)&d, &(Length = 1), &(Time = 1000));
		if (Length != 1)
			ASSERT(0);
		if (c != d)
			ASSERT(0);
	}
	UpdateData();
	m_Notify = "Simple test done";
	UpdateData(FALSE);
	Sleep(1000);
	UpdateData();
	m_Notify = "";
	UpdateData(FALSE);
}

//	Overflow test
void CTestDlg::Test2(CGet*aEnd, IGet*aGet)
{
	UpdateData();
	m_Notify = "Overflow test";
	UpdateData(FALSE);
	short Test[4000];
	UINT Place;
	for (Place = 0; Place < (sizeof Test / sizeof Test[0]); Place++)
		Test[Place] = Place + 1;
	aEnd->AddList((UCHAR*)Test, sizeof Test);

	short d = 0;
	UINT Length;
	UINT Time;
	for (Place = 0; Place < (sizeof Test / sizeof Test[0]); Place++)
	{
		aGet->Get((UCHAR*)&d, &(Length = sizeof d), &(Time = 1000));
		if (Length != sizeof d)
			ASSERT(0);
		if ((Place + 1) != (UINT)d)
			ASSERT(0);
	}
	UpdateData();
	m_Notify = "Overflow test done";
	UpdateData(FALSE);
	Sleep(1000);
	UpdateData();
	m_Notify = "";
	UpdateData(FALSE);
}

//	Misc test
void CTestDlg::Test3(CGet*aEnd, IGet*aGet)
{
	UpdateData();
	m_Notify = "Misc test";
	UpdateData(FALSE);
	IQueue*Queue;
	if (FAILED(aGet->QueryInterface(IID_IQueue, (void**)&Queue)))
		ASSERT(0);
	UINT Free;
	if (FAILED(Queue->Free(&Free)))
		ASSERT(0);
	ASSERT(Free == (4 * 1024 - 1));
	aEnd->AddList((UCHAR*)"ABCD", 4);
	Sleep(1000);	//	Give it time to absorb the input
	if (FAILED(Queue->Free(&Free)))
		ASSERT(0);
	ASSERT(Free == (4 * 1024 - 1 - 4));
	if (FAILED(aGet->Clear()))
		ASSERT(0);
	if (FAILED(Queue->Free(&Free)))
		ASSERT(0);
	ASSERT(Free == (4 * 1024 - 1));
	IGet*Interface;
	if (FAILED(Queue->get_Interface(&Interface)))
		ASSERT(0);
	ASSERT(Interface == aEnd);
	Interface->Release();
	if (FAILED(aGet->Interrupt()))
		ASSERT(0);
	Queue->Release();
	UpdateData();
	m_Notify = "Misc test done";
	UpdateData(FALSE);
	Sleep(1000);
	UpdateData();
	m_Notify = "";
	UpdateData(FALSE);
}

