Ejemplo n.º 1
0
/** \brief function to test a ptr_t
 */
nunit_res_t	ptr_testclass_t::ptr_counted(const nunit_testclass_ftor_t &testclass_ftor) throw()
{
	// log to debug
	KLOG_DBG("enter");

	// create a ptr_counted_t
	ptr_counted_t<const std::string>	ptr1(nipmem_new std::string("hello world."));
	// run some check on ptr1
	NUNIT_ASSERT( ptr1.get_ptr() != NULL );
	NUNIT_ASSERT( ptr1.count() == 1 );
	NUNIT_ASSERT( *ptr1 == "hello world." );

	// copy the ptr1 into ptr2 INSIDE bracket to ctor/dtor ptr2
{
	ptr_counted_t<const std::string>	ptr2(ptr1);
	// run some check on ptr2
	NUNIT_ASSERT( ptr2.get_ptr() == ptr1.get_ptr() );
	NUNIT_ASSERT( ptr1.count() == 2 );
	NUNIT_ASSERT( ptr2.count() == 2 );
	NUNIT_ASSERT( *ptr2 == "hello world." );
}

	// now that ptr2 has been destructed, ptr1.count() MUST be 1 again	
	NUNIT_ASSERT( ptr1.count() == 1 );
	NUNIT_ASSERT( *ptr1 == "hello world." );

	// return no error
	return NUNIT_RES_OK;
}
Ejemplo n.º 2
0
void	test_uni1()
{
  constexpr int	value = 101;
  my::unique_ptr<int>	ptr(value);
  if (*ptr == value)
    std::cout << "OK ";
  else
    std::cout << "KO ";
  int			*i(new int(value + value));
  my::unique_ptr<int>	ptr2(i);
  delete i;
  if (*ptr2 == value + value)
    std::cout << "OK ";
  else
    std::cout << "KO ";
  my::unique_ptr<int>	ptr3(std::move(ptr));
  if (*ptr3 == value)
    std::cout << "OK ";
  else
    std::cout << "KO ";
  if (!ptr.get())
    std::cout << "OK ";
  else
    std::cout << "KO ";
  std::cout << std::endl;
}
Ejemplo n.º 3
0
void TestSharedPointers(void)
{
	shared_ptr<CPrintable> ptr1(new CPrintable("1")); // create object 1
	shared_ptr<CPrintable> ptr2(new CPrintable("2")); // create object 2
	ptr1 = ptr2; // destroy object 1
	ptr2 = shared_ptr<CPrintable>(new CPrintable("3")); // used as a return value
	ProcessObject(ptr1); // call a function
	CPrintable o1("bad");
	//ptr1 = &o1; // Syntax error! It's on the stack....
	//
	CPrintable *o2 = new CPrintable("bad2");
	//ptr1 = o2; // Syntax error! Use the next line to do this...
	ptr1 = shared_ptr<CPrintable>(o2);
	// You can even use shared_ptr on ints!
	shared_ptr<int> a(new int);
	shared_ptr<int> b(new int);
	*a = 5;
	*b = 6;
	const int *q = a.get(); // use this for reading in multithreaded code
							// this is especially cool - you can also use it in lists.
	std::list< shared_ptr<int> > intList;
	std::list< shared_ptr<IPrintable> > printableList;
	for (int i = 0; i<100; ++i)
	{
		intList.push_back(shared_ptr<int>(new int(rand())));
		printableList.push_back(shared_ptr<IPrintable>(new CPrintable("list")));
	}
}
Ejemplo n.º 4
0
void fit_smart_ptr_tests::test_case_get()
{
    auto ptr1 = smart_make_shared<char>(45);
    assert(ptr1.get() != nullptr);

    fit_smart_ptr<char> ptr2(nullptr);
    assert(ptr2.get() == nullptr);

    fit_smart_ptr<char> ptr3(nullptr);
    assert(ptr3.get() == nullptr);

    auto ptr4(ptr1);
    assert(ptr4.get() == ptr1.get());

    ptr2 = ptr4;
    assert(ptr2.get() == ptr4.get() && ptr4.get() == ptr1.get());
    ptr3 = ptr1;
    assert(ptr3.get() == ptr1.get());
    *ptr1 = 123;
    assert(ptr3.get() == ptr1.get());

    ptr4 = nullptr;
    assert(ptr4.get() == nullptr);
    ptr3 = fit_smart_ptr<char>(nullptr);
    assert(ptr3.get() == nullptr);

    ptr1 = ptr2 = ptr3;
    assert(ptr1.get() == nullptr);
    assert(ptr2.get() == nullptr);
}
Ejemplo n.º 5
0
void smart_ptr_tests::test_case_get()
{
    smart_ptr<char> ptr1(new char(45));
    assert(ptr1.get() != nullptr);

    smart_ptr<char> ptr2(nullptr);
    assert(ptr2.get() == nullptr);

    smart_ptr<char> ptr3(nullptr);
    assert(ptr3.get() == nullptr);

    auto ptr4(ptr1);
    assert(ptr4.get() == ptr1.get());

    ptr2 = ptr4;
    assert(ptr2.get() == ptr4.get() && ptr4.get() == ptr1.get());
    ptr3 = ptr1;
    assert(ptr3.get() == ptr1.get());
    *ptr1 = 123;
    assert(ptr3.get() == ptr1.get());

    ptr4 = nullptr;
    assert(ptr4.get() == nullptr);
    ptr3 = smart_ptr<char>(nullptr);
    assert(ptr3.get() == nullptr);

    ptr1 = ptr2 = ptr3;
    assert(ptr1.get() == nullptr);
    assert(ptr2.get() == nullptr);
}
Ejemplo n.º 6
0
void smart_ptr_tests::test_case_comparisions()
{
    smart_ptr<char> ptr1(new char(45));
    assert(ptr1 != nullptr);

    smart_ptr<char> ptr2(nullptr);
    assert(ptr2 == nullptr);

    smart_ptr<char> ptr3(nullptr);
    assert(ptr3 == nullptr);

    auto ptr4 = ptr1;
    assert(ptr4 == ptr1);

    ptr2 = ptr4;
    assert(ptr2 == ptr4 && ptr4 == ptr1);
    ptr3 = ptr1;
    assert(ptr3 == ptr1);
    *ptr1 = 123;
    assert(ptr3 == ptr1);

    ptr4 = nullptr;
    assert(ptr4 == nullptr);
    ptr3 = smart_ptr<char>(nullptr);
    assert(ptr3 == nullptr);

    assert(nullptr != ptr1);
    ptr1 = ptr2 = ptr3;
    assert(ptr1 == nullptr);
    assert(ptr2 == nullptr);

    assert(nullptr == ptr1);
}
main()
{
    MyClass t1;
    MyClass t2;

    MyDelegate ptr1( &t1, &MyClass::MyMethod );
    MyDelegate ptr2( &t2, &MyClass::MyMethod );

    int retval1, retval2;

    retval1 = ptr1( "apples" );
    retval2 = ptr2( "cherries" );

    printf( "Object 1 returned %d, object 2 returned %d\n", retval1, retval2 );
    return 0;
}
Ejemplo n.º 8
0
void smart_ptr_tests::test_case_copy_and_assignment()
{
    smart_ptr<char> ptr1(new char(45));
    assert(*ptr1 == 45);

    smart_ptr<char> ptr2(nullptr);
    smart_ptr<char> ptr3(nullptr);
    auto ptr4(ptr1);
    assert(*ptr4 == 45);

    ptr2 = ptr4;
    assert(*ptr2 == 45);
    ptr3 = ptr1;
    assert(*ptr3 == 45);
    *ptr1 = 123;
    assert(*ptr2 == 123);
    ptr3 = ptr1;
    assert(*ptr3 == 123);

    // nullptr is r-value so move operator=
    ptr4 = smart_ptr<char>(nullptr);
    ptr4 = nullptr;
    ptr3 = smart_ptr<char>(nullptr);
    ptr1 = ptr2 = ptr3;

    smart_ptr<const char> ptr5(new char(12));
    ptr5 = ptr5 = ptr5;
    assert(*ptr5 == 12);
}
Ejemplo n.º 9
0
argument
damp(EXTERNAL_ARG(ls1), EXTERNAL_ARG(ls2), EXTERNAL_ARG(fact))
{
   DECLARE_ARG(ls1, float_list *);
   DECLARE_ARG(ls2, float_list *);
   DECLARE_ARG(fact, float_val);
   float_list *ptr1((float_list*)ls1);
   float_list *ptr2((float_list*)ls2);
   
   float_list *nil(float_list::null_list());
   
   if(float_list::is_null(ptr1) || float_list::is_null(ptr2)) {
      
      RETURN_ARG(nil);
   }
   
   stack_float_list vals;
   
   while(!float_list::is_null(ptr1) && !float_list::is_null(ptr2)) {
      const float_val h1(ptr1->get_head());
      const float_val h2(ptr2->get_head());
      const float_val c(std::log(fact * std::exp(h2) +
         (1.0 - fact) * std::exp(h1)));
      
      vals.push(c);
      
      ptr1 = ptr1->get_tail();
      ptr2 = ptr2->get_tail();
   }
   
   float_list *ptr(from_stack_to_list<stack_float_list,float_list>(vals));
   
   RETURN_ARG(ptr);
}
Ejemplo n.º 10
0
TEST_F(test_shared_ptr, allocated2x)
{
  std::shared_ptr<double_and_int> ptr2(new double_and_int);
  std::shared_ptr<double_and_int> ptr(ptr2);
  EXPECT_EQ(ptr.use_count(), 2);
  EXPECT_TRUE(ptr != NULL);
}
Ejemplo n.º 11
0
void fit_smart_ptr_tests::test_case_copy_and_assignment()
{
    auto ptr1 = smart_make_shared<char>(45);
    assert(*ptr1 == 45);
    assert(ptr1.use_count() == 1);

    fit_smart_ptr<char> ptr2(nullptr);
    fit_smart_ptr<char> ptr3(nullptr);
    auto ptr4(ptr1);
    assert(*ptr4 == 45);
    assert(ptr4.use_count() == 2);
    assert(ptr1.use_count() == 2);

    ptr2 = ptr4;
    assert(*ptr2 == 45);
    ptr3 = ptr1;
    assert(*ptr3 == 45);
    *ptr1 = 123;
    assert(*ptr2 == 123);
    ptr3 = ptr1;
    assert(*ptr3 == 123);

    // nullptr is r-value so move operator=
    ptr4 = fit_smart_ptr<char>(nullptr);
    ptr4 = nullptr;
    ptr3 = fit_smart_ptr<char>(nullptr);
    ptr1 = ptr2 = ptr3;

    fit_smart_ptr<char> ptr5 = smart_make_shared<char>(12);
    ptr5 = ptr5 = ptr5;
    assert(*ptr5 == 12);
}
Ejemplo n.º 12
0
TEST_F(test_shared_ptr, default_constructor_NULL)
{
  std::shared_ptr<double_and_int> ptr2(NULL);
  std::shared_ptr<double_and_int> ptr(ptr2);
  EXPECT_EQ(ptr.use_count(), 0);
  EXPECT_TRUE(ptr == NULL);
}
Ejemplo n.º 13
0
void fit_smart_ptr_tests::test_case_comparisions()
{
    auto ptr1 = smart_make_shared<char>(45);
    assert(ptr1 != nullptr);

    fit_smart_ptr<char> ptr2(nullptr);
    assert(ptr2 == nullptr);

    fit_smart_ptr<char> ptr3(nullptr);
    assert(ptr3 == nullptr);

    auto ptr4 = ptr1;
    assert(ptr4 == ptr1);

    ptr2 = ptr4;
    assert(ptr2 == ptr4 && ptr4 == ptr1);
    ptr3 = ptr1;
    assert(ptr3 == ptr1);
    *ptr1 = 123;
    assert(ptr3 == ptr1);

    ptr4 = nullptr;
    assert(ptr4 == nullptr);
    ptr3 = fit_smart_ptr<char>(nullptr);
    assert(ptr3 == nullptr);

    assert(nullptr != ptr1);
    ptr1 = ptr2 = ptr3;
    assert(ptr1 == nullptr);
    assert(ptr2 == nullptr);

    assert(nullptr == ptr1);
}
Ejemplo n.º 14
0
/**
This function is used for collated/folded string comparisons,
{aByteSize1, aPtr1} - string 1, UTF16 encoded. aByteSize1 - length in bytes.
{aByteSize2, aPtr2} - string 2, UTF16 encoded. aByteSize2 - length in bytes.
aLevel - 0,1,2,3 - collation level; -1 - folded string comparison;
The function returns negative, zero or positive if the first string is less than, equal to, or 
greater than the second string.

@internalComponent
*/
static TInt Compare(TInt aLevel, TInt aByteSize1, const void* aPtr1, TInt aByteSize2, const void* aPtr2)
	{
	TPtrC16 ptr1(static_cast <const TUint16*> (aPtr1), (TUint)aByteSize1 /sizeof(TUint16));
	TPtrC16 ptr2(static_cast <const TUint16*> (aPtr2), (TUint)aByteSize2 /sizeof(TUint16));
	
	return aLevel >= 0 ? ptr1.CompareC(ptr2, aLevel, NULL) : ptr1.CompareF(ptr2);
	}
Ejemplo n.º 15
0
void testObj::test<11>(void)
{
  PointerWrapper<Data*> ptr(sp_);

  PointerWrapper<Data*>       ptr1(ptr);
  PointerWrapper<const Data*> ptr2(ptr);
}
Ejemplo n.º 16
0
TEST_F(test_shared_ptr, equals)
{
  std::shared_ptr<double_and_int> ptr2(new double_and_int);
  std::shared_ptr<double_and_int> ptr(ptr2);
  ptr->x = 2.0;

  EXPECT_DOUBLE_EQ(ptr->x, ptr2->x);
}
Ejemplo n.º 17
0
int main () {
  int num = 10;
  SmartPointer<int> ptr1(&num);
  SmartPointer<int> ptr2(ptr1);
  SmartPointer<int> ptr3 = ptr2;
  std::cout << num << std::endl;
  std::cout << ptr1 << std::endl;
  return 0;
}
Ejemplo n.º 18
0
/**
 Returns true if the contents of aBmp1 match the contents of aBmp2
 */
TBool CTestContainer::CompareBitmapsL(CFbsBitmap* aBmp1, CFbsBitmap* aBmp2)
	{
	if (aBmp1 == NULL || aBmp2 == NULL)
		{
		return EFalse;
		}

	TInt width = aBmp1->SizeInPixels().iWidth;
	TInt height = aBmp1->SizeInPixels().iHeight;	

	if (aBmp1->DisplayMode() == aBmp2->DisplayMode())
		{
		TInt lineLen = CFbsBitmap::ScanLineLength(width,aBmp1->DisplayMode());
		HBufC8* buf1 = HBufC8::NewMaxLC(lineLen);	
		HBufC8* buf2 = HBufC8::NewMaxLC(lineLen);
		TPtr8 ptr1(buf1->Des());
		TPtr8 ptr2(buf2->Des());

		TInt row = 0;

		for (row = 0; row < height; ++row)
			{
			aBmp1->GetScanLine(ptr1,TPoint(0,row),width,aBmp1->DisplayMode());
			aBmp2->GetScanLine(ptr2,TPoint(0,row),width,aBmp2->DisplayMode());
			if (ptr1!=ptr2)
				{
				CleanupStack::PopAndDestroy(buf2);
				CleanupStack::PopAndDestroy(buf1);
				return EFalse;
				}
			}
		CleanupStack::PopAndDestroy(buf2);
		CleanupStack::PopAndDestroy(buf1);
		}
	else
		{
		TRgb p1(0,0,0);
		TRgb p2(0,0,0);

		TInt i = 0;
		for(i = 0; i < width; i++)
			{
			TInt j = 0;
			for(j = 0; j < height; j++)
				{
				aBmp1->GetPixel(p1, TPoint(i, j));
				aBmp2->GetPixel(p2, TPoint(i, j));
				if(p1 != p2)
					{
					return EFalse;
					}
				}
			}
		}
	return ETrue;	
	}
Ejemplo n.º 19
0
void testObj::test<10>(void)
{
  PointerWrapper< boost::shared_ptr<Data> > ptr(sp_);

  PointerWrapper< boost::shared_ptr<Data> >       ptr1(ptr);
  PointerWrapper< boost::shared_ptr<const Data> > ptr2(ptr);

  PointerWrapper<Data*>       ptr3(ptr);
  PointerWrapper<const Data*> ptr4(ptr);
}
Ejemplo n.º 20
0
TEST_F(test_shared_ptr, move)
{
  std::shared_ptr<double_and_int> ptr2(new double_and_int);
  ptr2->x = 2.0;
  std::shared_ptr<double_and_int> ptr(std::move(ptr2));
  EXPECT_TRUE(ptr2 == NULL);
  EXPECT_EQ(ptr2.use_count(), 0);
  EXPECT_EQ(ptr.use_count(), 1);

  EXPECT_DOUBLE_EQ(ptr->x, 2.0);
}
Ejemplo n.º 21
0
int		main(int argc, char **argv)
{
	void	*ptr1;
	void	(*ptr2)(void);

	ptr1 = malloc(64);
	ptr2 = malloc(4);
	ptr2 = m;
	strcpy(malloc1, argv[1]);
	ptr2();
}
void FONA808::readGPSInfo(){
	DEBUG_PRINT("ReadGPS\n");	
	if(m_ppp.isPPPLinkOpen()){
		mSerial.setPppPause(true);
		mbed::util::FunctionPointer0<void> ptr(this, &FONA808::readGPSInfoFirst);
		minar::Scheduler::postCallback(ptr.bind()).delay(minar::milliseconds(2000));
	}
	else{
		mbed::util::FunctionPointer0<void> ptr2(this, &FONA808::readGPSInfoSecond);
		minar::Scheduler::postCallback(ptr2.bind());
	}
}
Ejemplo n.º 23
0
void study_unique() {
	// shared
	std::shared_ptr<Base> ptr1(new Base(), delResource);
	{
		std::unique_ptr<Base, void(*)(Base*)> ptr1(new Base(), delResource);
		std::unique_ptr<Base> ptr2(new Base());
		std::unique_ptr<Base> ptr3 = std::move(ptr2);
		std::cout << sizeof(ptr1) << "," << sizeof(ptr2) << std::endl;
	}
	
	std::cout << "unique_ptr" << std::endl;
}
Ejemplo n.º 24
0
void testObj::test<9>(void)
{
  PointerWrapper< Commons::SharedPtrNotNULL<Data> > ptr(spnn_);

  PointerWrapper< Commons::SharedPtrNotNULL<Data> >       ptr1(ptr);
  PointerWrapper< Commons::SharedPtrNotNULL<const Data> > ptr2(ptr);

  PointerWrapper< boost::shared_ptr<Data> >       ptr3(ptr);
  PointerWrapper< boost::shared_ptr<const Data> > ptr4(ptr);

  PointerWrapper<Data*>       ptr5(ptr);
  PointerWrapper<const Data*> ptr6(ptr);
}
Ejemplo n.º 25
0
int main()
{
	struct Complex (*ptr1)(int, int);
	ptr1 = make_Complex;
	struct Complex c = ptr1(2,5);
	struct Complex d = ptr1(3,6);
	struct Complex (*ptr2)(struct Complex, struct Complex);
	ptr2 = add;
	//print_Complex(ptr2(c, d));
	struct Complex (*ptr[2])(int, int);
	ptr[0] = make_Complex;
	ptr[1] = make_Complex;
	print_Complex(ptr2(ptr[0](2,0), ptr[1](-2,0)));
	return 0;
}
TEST(fxcrt, RetainPtrCopyCtor) {
  PseudoRetainable obj;
  {
    CFX_RetainPtr<PseudoRetainable> ptr1(&obj);
    {
      CFX_RetainPtr<PseudoRetainable> ptr2(ptr1);
      EXPECT_EQ(2, obj.retain_count());
      EXPECT_EQ(0, obj.release_count());
    }
    EXPECT_EQ(2, obj.retain_count());
    EXPECT_EQ(1, obj.release_count());
  }
  EXPECT_EQ(2, obj.retain_count());
  EXPECT_EQ(2, obj.release_count());
}
TEST(fxcrt, RetainPtrMoveCtor) {
  PseudoRetainable obj;
  {
    CFX_RetainPtr<PseudoRetainable> ptr1(&obj);
    {
      CFX_RetainPtr<PseudoRetainable> ptr2(std::move(ptr1));
      EXPECT_EQ(1, obj.retain_count());
      EXPECT_EQ(0, obj.release_count());
    }
    EXPECT_EQ(1, obj.retain_count());
    EXPECT_EQ(1, obj.release_count());
  }
  EXPECT_EQ(1, obj.retain_count());
  EXPECT_EQ(1, obj.release_count());
}
Ejemplo n.º 28
0
TEST(fxcrt, WeakPtrCyclic) {
  PseudoDeletable thing1;
  PseudoDeletable thing2;
  {
    UniquePtr unique1(&thing1);
    UniquePtr unique2(&thing2);
    WeakPtr ptr1(std::move(unique1));
    WeakPtr ptr2(std::move(unique2));
    ptr1->SetNext(ptr2);
    ptr2->SetNext(ptr1);
  }
  // Leaks without explicit clear.
  EXPECT_EQ(0, thing1.delete_count());
  EXPECT_EQ(0, thing2.delete_count());
}
Ejemplo n.º 29
0
void smart_ptr_tests::test_case_use_count()
{
    smart_ptr<char> ptr1(new char(45));
    assert(ptr1.use_count() == 1);

    smart_ptr<char> ptr2(nullptr);
    smart_ptr<char> ptr3(nullptr);
    auto ptr4(ptr1);
    assert(ptr4.use_count() == 2);
    assert(ptr3.use_count() == 0);
    assert(ptr2.use_count() == 0);
    assert(ptr1.use_count() == 2);

    ptr2 = ptr4;
    assert(ptr4.use_count() == 3);
    assert(ptr3.use_count() == 0);
    assert(ptr2.use_count() == 3);
    assert(ptr1.use_count() == 3);
    ptr3 = ptr1;
    assert(ptr3.use_count() == 4);
    assert(ptr4.use_count() == 4);
    assert(ptr2.use_count() == 4);
    assert(ptr1.use_count() == 4);
    *ptr1 = 123;
    assert(ptr3.use_count() == 4);
    assert(ptr4.use_count() == 4);
    assert(ptr2.use_count() == 4);
    assert(ptr1.use_count() == 4);


    // testing overloading for nullptr
    ptr4 = nullptr;
    assert(ptr4.use_count() == 0);
    assert(ptr3.use_count() == 3);
    assert(ptr2.use_count() == 3);
    assert(ptr1.use_count() == 3);
    // NULL is r-value so move operator=
    ptr3 = smart_ptr<char>(nullptr);
    assert(ptr4.use_count() == 0);
    assert(ptr3.use_count() == 0);
    assert(ptr2.use_count() == 2);
    assert(ptr1.use_count() == 2);
    ptr1 = ptr2 = ptr3;
    assert(ptr4.use_count() == 0);
    assert(ptr3.use_count() == 0);
    assert(ptr2.use_count() == 0);
    assert(ptr1.use_count() == 0);
}
Ejemplo n.º 30
0
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    
    QSharedPointer<Custom> ptr1 = QSharedPointer<Custom>(new Custom(10));
    QSharedPointer<Custom> ptr2(ptr1);
    ptr2->updateValue(5);

    QSharedPointer<Custom> ptr3 = ptr2;
    ptr3->updateValue(10);

    ptr1->showValue();
    ptr2->showValue();
    ptr3->showValue();
    return a.exec();
}