Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / rive-cpp / test / refcnt_test.cpp
1 /*
2  * Copyright 2022 Rive
3  */
4
5 #include <rive/refcnt.hpp>
6 #include <catch.hpp>
7 #include <cstdio>
8
9 using namespace rive;
10
11 class MyRefCnt : public RefCnt {
12 public:
13     MyRefCnt() {}
14
15     void require_count(int value) { REQUIRE(this->debugging_refcnt() == value); }
16 };
17
18 TEST_CASE("refcnt", "[basics]") {
19     MyRefCnt my;
20     REQUIRE(my.debugging_refcnt() == 1);
21     my.ref();
22     REQUIRE(my.debugging_refcnt() == 2);
23     my.unref();
24     REQUIRE(my.debugging_refcnt() == 1);
25
26     safe_ref(&my);
27     REQUIRE(my.debugging_refcnt() == 2);
28     safe_unref(&my);
29     REQUIRE(my.debugging_refcnt() == 1);
30
31     // just exercise these to be sure they don't crash
32     safe_ref((MyRefCnt*)nullptr);
33     safe_unref((MyRefCnt*)nullptr);
34 }
35
36 TEST_CASE("rcp", "[basics]") {
37     rcp<MyRefCnt> r0(nullptr);
38
39     REQUIRE(r0.get() == nullptr);
40     REQUIRE(!r0);
41
42     rcp<MyRefCnt> r1(new MyRefCnt);
43     REQUIRE(r1.get() != nullptr);
44     REQUIRE(r1);
45     REQUIRE(r1 != r0);
46     REQUIRE(r1->debugging_refcnt() == 1);
47
48     auto r2 = r1;
49     REQUIRE(r1.get() == r2.get());
50     REQUIRE(r1 == r2);
51     REQUIRE(r2->debugging_refcnt() == 2);
52
53     auto ptr = r2.release();
54     REQUIRE(r2.get() == nullptr);
55     REQUIRE(r1.get() == ptr);
56
57     // This is important, calling release() does not modify the ref count on the object
58     // We have to manage that explicit since we called release()
59     REQUIRE(r1->debugging_refcnt() == 2);
60     ptr->unref();
61     REQUIRE(r1->debugging_refcnt() == 1);
62
63     r1.reset();
64     REQUIRE(r1.get() == nullptr);
65 }