Skip to content

Instantly share code, notes, and snippets.

@mpenick
Created October 2, 2014 18:24
Show Gist options
  • Save mpenick/2351fbace3ad463a2a2a to your computer and use it in GitHub Desktop.
Save mpenick/2351fbace3ad463a2a2a to your computer and use it in GitHub Desktop.
/*
Copyright (c) 2014 DataStax
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#define BOOST_TEST_DYN_LINK
#ifdef STAND_ALONE
# define BOOST_TEST_MODULE cassandra
#endif
#include "ref_counted.hpp"
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(ref_counted)
class TestObject : public cass::RefCounted<TestObject> {
public:
TestObject() {
//printf("ctor\n");
}
~TestObject() {
//printf("dtor\n");
}
};
cass::SharedRefPtr<TestObject> func_by_ref_with_ret(const cass::SharedRefPtr<TestObject>& p) {
cass::SharedRefPtr<TestObject> p1(p);
return p;
}
cass::SharedRefPtr<TestObject> func_by_val_w_ret(cass::SharedRefPtr<TestObject> p) {
cass::SharedRefPtr<TestObject> p1(p);
return p;
}
void func_by_ref(const cass::SharedRefPtr<TestObject>& p) {
cass::SharedRefPtr<TestObject> p1(p);
cass::SharedRefPtr<TestObject> p2 = p;
cass::SharedRefPtr<TestObject> p3;
p3.reset(p.get());
cass::SharedRefPtr<TestObject> p4 = func_by_ref_with_ret(p);
cass::SharedRefPtr<TestObject> p5 = func_by_ref_with_ret(p1);
cass::SharedRefPtr<TestObject> p6 = func_by_ref_with_ret(p2);
cass::SharedRefPtr<TestObject> p7 = func_by_val_w_ret(p);
cass::SharedRefPtr<TestObject> p8 = func_by_val_w_ret(p1);
cass::SharedRefPtr<TestObject> p9 = func_by_val_w_ret(p2);
cass::SharedRefPtr<TestObject> p10;
new (&p10) cass::SharedRefPtr<TestObject>(p);
cass::SharedRefPtr<TestObject> a[2];
new (&a[0]) cass::SharedRefPtr<TestObject>(p);
a[1] = p;
p1 = p;
p2 = p;
p3 = p;
BOOST_CHECK(p1 == p);
BOOST_CHECK(p2 == p);
BOOST_CHECK(p3 == p);
BOOST_CHECK(p4 == p);
BOOST_CHECK(p5 == p);
BOOST_CHECK(p6 == p);
BOOST_CHECK(p7 == p);
BOOST_CHECK(p8 == p);
BOOST_CHECK(p9 == p);
BOOST_CHECK(p->ref_count() == 16);
}
void func_by_val(cass::SharedRefPtr<TestObject> p) {
cass::SharedRefPtr<TestObject> p1(p);
cass::SharedRefPtr<TestObject> p2 = p;
BOOST_CHECK(p.get() != NULL);
cass::SharedRefPtr<TestObject> p3;
p3.reset(p.get());
cass::SharedRefPtr<TestObject> p4 = func_by_ref_with_ret(p);
cass::SharedRefPtr<TestObject> p5 = func_by_ref_with_ret(p1);
cass::SharedRefPtr<TestObject> p6 = func_by_ref_with_ret(p2);
cass::SharedRefPtr<TestObject> p7 = func_by_val_w_ret(p);
cass::SharedRefPtr<TestObject> p8 = func_by_val_w_ret(p1);
cass::SharedRefPtr<TestObject> p9 = func_by_val_w_ret(p2);
cass::SharedRefPtr<TestObject> p10;
new (&p10) cass::SharedRefPtr<TestObject>(p);
cass::SharedRefPtr<TestObject> a[2];
new (&a[0]) cass::SharedRefPtr<TestObject>(p);
a[1] = p;
p1 = p;
p2 = p;
p3 = p;
BOOST_CHECK(p1 == p);
BOOST_CHECK(p2 == p);
BOOST_CHECK(p3 == p);
BOOST_CHECK(p4 == p);
BOOST_CHECK(p5 == p);
BOOST_CHECK(p6 == p);
BOOST_CHECK(p7 == p);
BOOST_CHECK(p8 == p);
BOOST_CHECK(p9 == p);
BOOST_CHECK(p->ref_count() == 17);
}
BOOST_AUTO_TEST_CASE(simple)
{
TestObject* t = new TestObject;
t->inc_ref();
{
cass::SharedRefPtr<TestObject> p1(t);
cass::SharedRefPtr<TestObject> p2 = p1;
cass::SharedRefPtr<TestObject> p3;
p3.reset(t);
BOOST_CHECK(t->ref_count() == 4);
p3 = p1;
p2 = p1;
BOOST_CHECK(t->ref_count() == 4);
func_by_ref(p1);
func_by_ref(p2);
func_by_val(p1);
func_by_val(p2);
BOOST_CHECK(t->ref_count() == 4);
}
BOOST_CHECK(t->ref_count() == 1);
t->dec_ref();
}
BOOST_AUTO_TEST_SUITE_END()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment