| 69 | typedef void (*Db)(base*); |
| 70 | |
| 71 | void smart_pointers_test ( |
| 72 | ) |
| 73 | /*! |
| 74 | ensures |
| 75 | - runs tests on the smart pointers for compliance with the specs |
| 76 | !*/ |
| 77 | { |
| 78 | counter = 0; |
| 79 | deleter_called = 0; |
| 80 | |
| 81 | { |
| 82 | DLIB_TEST_MSG(counter == 0,counter); |
| 83 | scoped_ptr<base> p1(new derived); |
| 84 | scoped_ptr<derived> p2(new derived); |
| 85 | scoped_ptr<derived> p3; |
| 86 | DLIB_TEST_MSG(counter == 2,counter); |
| 87 | DLIB_TEST(!p3); |
| 88 | |
| 89 | p1->num = 1; |
| 90 | p2->num = 2; |
| 91 | DLIB_TEST(p1->num == 1); |
| 92 | DLIB_TEST(p2->num == 2); |
| 93 | |
| 94 | (*p1).num = 3; |
| 95 | (*p2).num = 4; |
| 96 | DLIB_TEST(p1->num == 3); |
| 97 | DLIB_TEST(p2->num == 4); |
| 98 | |
| 99 | DLIB_TEST_MSG(counter == 2,counter); |
| 100 | |
| 101 | DLIB_TEST(p1); |
| 102 | DLIB_TEST(p2); |
| 103 | |
| 104 | DLIB_TEST_MSG(counter == 2,counter); |
| 105 | p1.reset(); |
| 106 | DLIB_TEST_MSG(counter == 1,counter); |
| 107 | DLIB_TEST(!p1); |
| 108 | DLIB_TEST(p2); |
| 109 | p1.reset(new derived); |
| 110 | DLIB_TEST_MSG(counter == 2,counter); |
| 111 | DLIB_TEST(p1); |
| 112 | |
| 113 | |
| 114 | DLIB_TEST_MSG(counter == 2,counter); |
| 115 | p2.reset(); |
| 116 | DLIB_TEST_MSG(counter == 1,counter); |
| 117 | DLIB_TEST(!p2); |
| 118 | derived* d = new derived; |
| 119 | p2.reset(d); |
| 120 | DLIB_TEST(p2.get() == d); |
| 121 | DLIB_TEST_MSG(counter == 2,counter); |
| 122 | DLIB_TEST(p2); |
| 123 | DLIB_TEST(!p3); |
| 124 | p2->num = 9; |
| 125 | swap(p2,p3); |
| 126 | DLIB_TEST(!p2); |
| 127 | DLIB_TEST(p3); |
| 128 | DLIB_TEST(p3->num == 9); |