static void test_refCnt(skiatest::Reporter* reporter) { SkRefCnt* ref = new SkRefCnt(); std::thread thing1(bounce_ref, ref); std::thread thing2(bounce_ref, ref); thing1.join(); thing2.join(); REPORTER_ASSERT(reporter, ref->unique()); ref->unref(); }
main() { int a, b; int length=10000000; getchar(); b=0; for(int m=0; m<length; m++) { a=clock(); thing1(a); b+=clock()-a; } cout << "\nfirst: " << b; b=0; for(int m=0; m<length; m++) { a=clock(); thing2(&a); b+=clock()-a; } cout << "\nsecond: " << b; b=0; for(int m=0; m<length; m++) { a=clock(); thing3(&a); b+=clock()-a; } cout << "\nthird: " << b; b=0; for(int m=0; m<length; m++) { a=clock(); thing4(a); b+=clock()-a; } cout << "\nfirst: " << b; b=0; for(int m=0; m<length; m++) { a=clock(); thing5(a); b+=clock()-a; } cout << "\nfirst: " << b; getchar(); }
void work() { std::unique_ptr<CopyNoSwappy> thing(new CopyNoSwappy); art::Wrapper<CopyNoSwappy> wrap(thing); std::unique_ptr<SwappyNoCopy> thing2(new SwappyNoCopy); art::Wrapper<SwappyNoCopy> wrap2(thing2); std::unique_ptr<std::vector<double> > thing3(new std::vector<double>(10,2.2)); assert(thing3->size() == 10); art::Wrapper<std::vector<double> > wrap3(thing3); assert(wrap3->size() == 10); assert(thing3.get() == 0); }
static void test_weakRefCnt(skiatest::Reporter* reporter) { SkWeakRefCnt* ref = new SkWeakRefCnt(); std::thread thing1(bounce_ref, ref); std::thread thing2(bounce_ref, ref); std::thread thing3(bounce_weak_ref, ref); std::thread thing4(bounce_weak_weak_ref, ref); thing1.join(); thing2.join(); thing3.join(); thing4.join(); REPORTER_ASSERT(reporter, ref->unique()); SkDEBUGCODE(REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1)); ref->unref(); }
TEST(fxcrt, MaybeOwnedOwned) { int delete_count = 0; { CFX_MaybeOwned<PseudoDeletable> ptr( pdfium::MakeUnique<PseudoDeletable>(100, &delete_count)); EXPECT_TRUE(ptr.IsOwned()); EXPECT_EQ(100, ptr->GetID()); CFX_MaybeOwned<PseudoDeletable> empty; EXPECT_FALSE(ptr == empty); EXPECT_TRUE(ptr != empty); } EXPECT_EQ(1, delete_count); delete_count = 0; { CFX_MaybeOwned<PseudoDeletable> ptr( pdfium::MakeUnique<PseudoDeletable>(200, &delete_count)); ptr = pdfium::MakeUnique<PseudoDeletable>(300, &delete_count); EXPECT_TRUE(ptr.IsOwned()); EXPECT_EQ(300, ptr->GetID()); EXPECT_EQ(1, delete_count); } EXPECT_EQ(2, delete_count); delete_count = 0; int unowned_delete_count = 0; PseudoDeletable thing2(400, &unowned_delete_count); { CFX_MaybeOwned<PseudoDeletable> ptr( pdfium::MakeUnique<PseudoDeletable>(500, &delete_count)); ptr = &thing2; EXPECT_FALSE(ptr.IsOwned()); EXPECT_EQ(400, ptr->GetID()); EXPECT_EQ(1, delete_count); EXPECT_EQ(0, unowned_delete_count); } EXPECT_EQ(1, delete_count); EXPECT_EQ(0, unowned_delete_count); }
TEST(fxcrt, MaybeOwnedNotOwned) { int delete_count = 0; PseudoDeletable thing1(100, &delete_count); { CFX_MaybeOwned<PseudoDeletable> ptr(&thing1); EXPECT_FALSE(ptr.IsOwned()); EXPECT_EQ(ptr.Get(), &thing1); EXPECT_EQ(100, ptr->GetID()); EXPECT_TRUE(ptr == &thing1); EXPECT_FALSE(ptr != &thing1); CFX_MaybeOwned<PseudoDeletable> empty; EXPECT_FALSE(ptr == empty); EXPECT_TRUE(ptr != empty); } EXPECT_EQ(0, delete_count); delete_count = 0; PseudoDeletable thing2(200, &delete_count); { CFX_MaybeOwned<PseudoDeletable> ptr(&thing1); ptr = &thing2; EXPECT_FALSE(ptr.IsOwned()); EXPECT_EQ(ptr.Get(), &thing2); EXPECT_EQ(200, ptr->GetID()); } EXPECT_EQ(0, delete_count); delete_count = 0; int owned_delete_count = 0; { CFX_MaybeOwned<PseudoDeletable> ptr(&thing1); EXPECT_EQ(100, ptr->GetID()); ptr = pdfium::MakeUnique<PseudoDeletable>(300, &owned_delete_count); EXPECT_TRUE(ptr.IsOwned()); EXPECT_EQ(300, ptr->GetID()); } EXPECT_EQ(0, delete_count); EXPECT_EQ(1, owned_delete_count); }
/* * Name: main * Purpose: demonstrate the parallel between primitive variable declaration * and object construction. */ int main() { // // // Primitive types // Class types // // //declaration //default constructor int a; Thing thing1; //init. from constant //value constructor int b(1); Thing thing2(5); //constructor notation int c = 2; Thing thing3 = Thing(10); //assignment notation //init. from variable //copy constructor int d(c); Thing thing4(thing3); //constructor notation int e = d; Thing thing5 = thing4; //assignment notation //assignment //assignment int f = 0; Thing thing6; f = e; thing6 = thing5; //variable assignment f = 5; thing6 = Thing(15); //"constant" assignment }