

#ifdef NDEBUG
#undef NDEBUG
#endif

#ifndef IWEAR_DEBUG
#define IWEAR_DEBUG
#endif

#include <iostream>
#include "iwear/exceptions.h"
#include "iwear/relative_ptr.h"

using namespace iwear;

relative_ptr<int> to = 0;

int main ( )
{
    try
    {
	int i;
	int* u = &i;
	{
	    int* tor = new int[80];
//	    std::cout << "to = " << (void*) to << std::endl;
	    to = tor;
//	    std::cout << "tor = " << (void*) tor << std::endl;
//	    std::cout << "to = " << (void*) to << std::endl;
	    IWASSERT(to == tor);
	    *tor = 0;
	    *to = 0;
	    tor = 0;
	    for( int i = 0; i < 10000; ++i )
	    {
		new int[80];
/*		usleep(1);
		std::cout << new int[80]<< "\n";*/
	    }

	}
	{
	    iwear::relative_ptr<int> p = 0;
	    iwear::relative_ptr<int> h = u;
	    IWASSERT(p == 0);
	    IWASSERT(0 == p);
	    IWASSERT( h == u );
	    IWASSERT( u == h );
	    IWASSERT( h != 0 );
	    IWASSERT( 0 != h );
	}
	{
	    iwear::relative_ptr<int,short> p = 0;
	    char c[2];
	    iwear::relative_ptr<int,short> h = u;
	    IWASSERT(p == 0);
	    IWASSERT(0 == p);
	    IWASSERT( h == u );
	    IWASSERT( u == h );
	    IWASSERT( h != 0 );
	    IWASSERT( 0 != h );
	}
	{
	    iwear::relative_ptr<int,unsigned char> p = 0;
	    iwear::relative_ptr<int,unsigned char> h = u;
	    std::cout << (void*)u << std::endl;
	    std::cout << (void*)&h << std::endl;
	    std::cout << (void*)(u-(int*)&h) << std::endl;
	    IWASSERT(p == 0);
	    IWASSERT(0 == p);
	    std::cout << (void*) u << std::endl;
	    std::cout << (void*) h << std::endl;
	    int* r = (int*)0x01000;
	    r += 1;
	    std::cout << (void*)r << std::endl;
	    IWASSERT( h == u );
	    IWASSERT( u == h );
	    IWASSERT( h != 0 );
	    IWASSERT( 0 != h );
	}
    }
    catch( const std::exception& e )
    {
	std::cout << "Exception: \n" << e << std::endl;
    }
    *to = 0;
}
