Proposed ANSI Compliance (Update 5/3/99)

 

The following are 6 programs designed to test various stages of propsed ANSI standard compliance. Please try to compile the programs to help determine what level you are at. Note that if you get one program to work, you don't need to test the programs that appear after it. In otherwords, if Program 1 works for you, there is no need to test the rest.

These programs are intended for Morgan Stanley students only. If you are interested in testing your compilers for the latest ANSI proposed standards, there are several much better programs specifically designed for that purpose. Let me know how these programs work out for you.

/////////////////////////////////////////////////////////////
// Program 1
// This simple program will test all the abilities needed of a C++
// compiler for our class.

#include <iostream>
#include <typeinfo>
#include <string>
#include <map>

using namespace std; // If this causes an error, try again with this line commented out.

template <class T, int i> class ArClass {	// template test
public:
   T Buffer[i];
   T& operator[](int z) { return( Buffer[z] ); }
};

class Base { 
public:
	virtual char * GetType() { return "base"; }
};

class Derived : public Base
{
public:
	virtual char *GetType() { return "derived"; }
};

void main()
{

	// exception handling
	try {
		throw "Exception testing";
	} 
	catch( char * E )
	{
		cout << E << endl;
	}

	// template test
	ArClass<char,10> X;
	X[0]='T';
	X[1]='T';
	X[2]='\0';
	cout << X.Buffer << endl;

	// Standard Template Library testing
	map<string,string> Quote;
 // If the line above gives you an error, comment it 
 // out, and uncomment the following line:
	// map<string,string,less<string>, allocator<string> > Quote;
	Quote["Mario"] = "I'm testing";
	cout << Quote["Mario"] << endl;

	// RTTI Testing	
	Base * P = new Derived;
	if( dynamic_cast<Derived*>(P) != 0 )
		cout << "RTTI looks good" << endl;
	else
		cout << "RTTI don't look so good" << endl;
	delete P;
}

////////////////////////////////////////////////////////////
// Program 2
// This program tests RTTI for C++ proposed standard
//

#include <iostream>
#include <typeinfo>
#include <string>

using namespace std;  //  If this causes an error, try again with this line commented out.

class Window {
public:
	void SetText( string NewText ) { Text = NewText; }
	string GetText() { return( Text ); }
	virtual ~Window() {}
private:
	string Text;
};

class CheckBox : public Window
{
public:
	CheckBox() { Checked=false; }
	void SetCheck( bool State ) { Checked = State; }
	bool GetCheck() { return( Checked ); }
	virtual ~CheckBox() {}
private:
	bool Checked;
};

void main()
{
	Window * Windows[10];
	CheckBox Tmp;  // An object of the testing type is needed (for typeid) to test against.
	CheckBox * Chk; // This pointer is used for dynamic_cast testing.
	int i;

	// Create a random distribution of Window and Checkbox classes
	for( i=0; i<10; i++ )
		if( (rand()%2) == 0 )
			Windows[i] = new CheckBox;
		else
			Windows[i] = new Window;

	for( i=0; i < 10; i++ )
	{
		// Print the type that Windows[i] points to, using type_info
		cout << i << " points to a " << typeid(*Windows[i]).name() << endl;
		// Use typeid to carry out special code for CheckBoxes
		if( typeid( *Windows[i] ) == typeid( Tmp ) )
			((CheckBox*)Windows[i])->SetCheck(true);
		// Another type of RTTI, using dynamic_cast:
		if( (Chk=dynamic_cast<CheckBox*>(Windows[i])) != NULL )
			((CheckBox*)Windows[i])->SetCheck(false);
	}
	for( i=0; i<10; i++ )
		delete Windows[i];
}

//////////////////////////////////////////////////////////////////////
// Program 3
// This program tests latest proposed C++ standard template library.
// When run, it should output 'A', 'B','C', 'D', and then 'A' again.
//
#include <iostream>
#include <vector>
#include <list>
#include <string>

using namespace std; // If this causes an error, try again with this line commented out.

void main()
{
	list<string> SList;

	list<string>::iterator Elem;

	Elem = SList.begin();

	SList.insert( Elem, "B" );
	SList.insert( Elem, "D" );
	SList.insert( Elem, "C" );
	SList.insert( Elem, "A" );
	
	SList.sort();	

	for( Elem = SList.begin(); Elem!=SList.end(); Elem++ )
		cout << *Elem << endl;	

	cout << SList.front() << endl;
	try
	{
		throw "A test";
	}
	catch( char * E )
	{
		cout << E << endl;
	}
}

///////////////////////////////////////////////////////////////////////
// Program 4
// This simple program will just exception handling.
// It should compile cleanly, and when run output "This is a test".
//

#include <iostream.h>

void main()
{
	try
	{
		throw "This is a test";
	}
	catch( char / E )
	{
		cout << E << endl;
	}
}

/////////////////////////////////////////////////////////////////////////
// Program 5
// This program tests template support in C++ compilers
//
#include <iostream.h>

template <class T, int i> class ArClass {
public:
   T Buffer[i];
   T& operator[](int z) { return( Buffer[z] ); }
};

void main()
{
	ArClass<char,10> X;
	X[0]='H';
	X[1]='i';
	X[2]='\0';
	cout << X.Buffer << endl;
}

////////////////////////////////////////////////////////////////////////
// Program 6
// This simple program will just test basic C++ compliance
///
#include <iostream.h>

void main()
{
	cout << "Hello world" << endl;
}