diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-07-22 18:21:31 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-07-22 18:21:31 +0100 |
commit | a52bfa87d61294bb93df3b916f008c72ae23b9c9 (patch) | |
tree | 219ca4e143b66741e9c3b1f1e309661df8416c2f | |
parent | 5330106c1eb9b4bad545eb3d41bf3e1db8535e1f (diff) | |
download | rspamd-a52bfa87d61294bb93df3b916f008c72ae23b9c9.tar.gz rspamd-a52bfa87d61294bb93df3b916f008c72ae23b9c9.zip |
[Test] Separate local_ptr unit tests
-rw-r--r-- | test/rspamd_cxx_local_ptr.hxx | 284 | ||||
-rw-r--r-- | test/rspamd_cxx_unit.cxx | 1 | ||||
-rw-r--r-- | test/rspamd_cxx_unit_utils.hxx | 262 |
3 files changed, 287 insertions, 260 deletions
diff --git a/test/rspamd_cxx_local_ptr.hxx b/test/rspamd_cxx_local_ptr.hxx new file mode 100644 index 000000000..c04c3f93d --- /dev/null +++ b/test/rspamd_cxx_local_ptr.hxx @@ -0,0 +1,284 @@ +/*- + * Copyright 2021 Vsevolod Stakhov + * + * 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. + */ + +#ifndef RSPAMD_RSPAMD_CXX_LOCAL_PTR_HXX +#define RSPAMD_RSPAMD_CXX_LOCAL_PTR_HXX + +#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL +#include "doctest/doctest.h" + +#include "libutil/cxx/local_shared_ptr.hxx" + +TEST_SUITE("local_ptr") { +TEST_CASE("shared_ptr from nullptr") +{ + rspamd::local_shared_ptr<int const> pi(static_cast<int *>(nullptr)); + CHECK((!!pi ? false : true)); + CHECK(!pi); + CHECK(pi.get() == nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + + +} +TEST_CASE("shared_ptr from ptr") +{ + int *p = new int(7); + rspamd::local_shared_ptr<int> pi(p); + CHECK((pi? true: false)); + CHECK(!!pi); + CHECK(pi.get() == p); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(*pi == 7); +} + +TEST_CASE("shared_ptr copy") +{ + rspamd::local_shared_ptr<int> pi; + + rspamd::local_shared_ptr<int> pi2(pi); + CHECK(pi2 == pi); + CHECK((pi2? false: true)); + CHECK(!pi2); + CHECK(pi2.get() == nullptr); + CHECK(pi2.use_count() == pi.use_count()); + + rspamd::local_shared_ptr<int> pi3(pi); + CHECK(pi3 == pi); + CHECK((pi3? false: true)); + CHECK(!pi3); + CHECK(pi3.get() == nullptr); + CHECK(pi3.use_count() == pi.use_count()); + + rspamd::local_shared_ptr<int> pi4(pi3); + CHECK(pi4 == pi3); + CHECK((pi4? false: true)); + CHECK(!pi4); + CHECK(pi4.get() == nullptr); + CHECK(pi4.use_count() == pi3.use_count()); + + int * p = new int(7); + rspamd::local_shared_ptr<int> pi5(p); + + rspamd::local_shared_ptr<int> pi6(pi5); + CHECK(pi5 == pi6); + CHECK((pi6? true: false)); + CHECK(!!pi6); + CHECK(pi6.get() == p); + CHECK(pi6.use_count() == 2); + CHECK(!pi6.unique()); + CHECK(*pi6 == 7); + CHECK(pi6.use_count() == pi6.use_count()); + CHECK(!(pi5 < pi6 || pi5 < pi6)); // shared ownership test + + auto pi7 = pi6; + CHECK(pi5 == pi7); + CHECK((pi7? true: false)); + CHECK(!!pi7); + CHECK(pi7.get() == p); + CHECK(pi7.use_count() == 3); + CHECK(!pi7.unique()); + CHECK(*pi7 == 7); + CHECK(pi7.use_count() == pi7.use_count()); + CHECK(!(pi5 < pi7 || pi5 < pi7)); // shared ownership test +} + +TEST_CASE("shared_ptr move") +{ + rspamd::local_shared_ptr<int> pi(new int); + + rspamd::local_shared_ptr<int> pi2(std::move(pi)); + CHECK(!(pi2 == pi)); + CHECK((!pi2? false: true)); + CHECK(!pi); + CHECK(pi.get() == nullptr); + CHECK(pi2.get() != nullptr); + CHECK(pi.use_count() != pi2.use_count()); + + std::swap(pi, pi2); + CHECK(!(pi2 == pi)); + CHECK((!pi? false: true)); + CHECK(!pi2); + CHECK(pi.get() != nullptr); + CHECK(pi2.get() == nullptr); + CHECK(pi.use_count() != pi2.use_count()); +} + +struct deleter_test { + bool *pv; + deleter_test(bool &v) { + v = false; + pv = &v; + } + ~deleter_test() { + *pv = true; + } +}; +TEST_CASE("shared_ptr dtor") { + bool t; + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + } + + CHECK(t == true); + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_shared_ptr<deleter_test> pi2(pi); + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + pi.reset(); + CHECK(!(pi2 == pi)); + CHECK(pi2.use_count() == 1); + CHECK(t == false); + + pi = pi2; + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + CHECK(t == false); + } + + CHECK(t == true); +} + +TEST_CASE("make_shared dtor") { + bool t; + + { + auto pi = rspamd::local_make_shared<deleter_test>(t); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_shared_ptr<deleter_test> pi2(pi); + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + pi.reset(); + CHECK(!(pi2 == pi)); + CHECK(pi2.use_count() == 1); + CHECK(t == false); + + pi = pi2; + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + CHECK(t == false); + } + + CHECK(t == true); +} + +TEST_CASE("shared_ptr dtor") { + bool t; + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + } + + CHECK(t == true); + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_shared_ptr<deleter_test> pi2(pi); + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + pi.reset(); + CHECK(!(pi2 == pi)); + CHECK(pi2.use_count() == 1); + CHECK(t == false); + + pi = pi2; + CHECK(pi2 == pi); + CHECK(pi.use_count() == 2); + CHECK(t == false); + } + + CHECK(t == true); +} + +TEST_CASE("weak_ptr") { + bool t; + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + + CHECK((!pi ? false : true)); + CHECK(!!pi); + CHECK(pi.get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_weak_ptr<deleter_test> wp(pi); + CHECK(wp.lock().get() != nullptr); + CHECK(pi.use_count() == 1); + CHECK(wp.use_count() == 1); + pi.reset(); + CHECK(pi.use_count() == 0); + CHECK(wp.use_count() == 0); + } + + CHECK(t == true); + + rspamd::local_weak_ptr<deleter_test> wp; + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + wp = pi; + CHECK(!wp.expired()); + CHECK(wp.lock().get() != nullptr); + } + + CHECK(t == true); + CHECK(wp.expired()); +} +} + +#endif //RSPAMD_RSPAMD_CXX_LOCAL_PTR_HXX diff --git a/test/rspamd_cxx_unit.cxx b/test/rspamd_cxx_unit.cxx index cf3271d96..37aa45b6f 100644 --- a/test/rspamd_cxx_unit.cxx +++ b/test/rspamd_cxx_unit.cxx @@ -22,6 +22,7 @@ #include "doctest/doctest.h" #include "rspamd_cxx_unit_utils.hxx" +#include "rspamd_cxx_local_ptr.hxx" static gboolean verbose = false; static const GOptionEntry entries[] = diff --git a/test/rspamd_cxx_unit_utils.hxx b/test/rspamd_cxx_unit_utils.hxx index 47e6c889b..d83af5010 100644 --- a/test/rspamd_cxx_unit_utils.hxx +++ b/test/rspamd_cxx_unit_utils.hxx @@ -23,12 +23,12 @@ #include "doctest/doctest.h" #include "libmime/mime_headers.h" -#include "libutil/cxx/local_shared_ptr.hxx" + #include <vector> #include <utility> #include <string> -TEST_SUITE("rspamd utils") { +TEST_SUITE("rspamd_utils") { TEST_CASE("rspamd_strip_smtp_comments_inplace") { @@ -57,264 +57,6 @@ TEST_CASE("rspamd_strip_smtp_comments_inplace") } } - -TEST_CASE("shared_ptr from nullptr") -{ - rspamd::local_shared_ptr<int const> pi(static_cast<int *>(nullptr)); - CHECK((!!pi ? false : true)); - CHECK(!pi); - CHECK(pi.get() == nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - - -} -TEST_CASE("shared_ptr from ptr") -{ - int *p = new int(7); - rspamd::local_shared_ptr<int> pi(p); - CHECK((pi? true: false)); - CHECK(!!pi); - CHECK(pi.get() == p); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(*pi == 7); -} - -TEST_CASE("shared_ptr copy") -{ - rspamd::local_shared_ptr<int> pi; - - rspamd::local_shared_ptr<int> pi2(pi); - CHECK(pi2 == pi); - CHECK((pi2? false: true)); - CHECK(!pi2); - CHECK(pi2.get() == nullptr); - CHECK(pi2.use_count() == pi.use_count()); - - rspamd::local_shared_ptr<int> pi3(pi); - CHECK(pi3 == pi); - CHECK((pi3? false: true)); - CHECK(!pi3); - CHECK(pi3.get() == nullptr); - CHECK(pi3.use_count() == pi.use_count()); - - rspamd::local_shared_ptr<int> pi4(pi3); - CHECK(pi4 == pi3); - CHECK((pi4? false: true)); - CHECK(!pi4); - CHECK(pi4.get() == nullptr); - CHECK(pi4.use_count() == pi3.use_count()); - - int * p = new int(7); - rspamd::local_shared_ptr<int> pi5(p); - - rspamd::local_shared_ptr<int> pi6(pi5); - CHECK(pi5 == pi6); - CHECK((pi6? true: false)); - CHECK(!!pi6); - CHECK(pi6.get() == p); - CHECK(pi6.use_count() == 2); - CHECK(!pi6.unique()); - CHECK(*pi6 == 7); - CHECK(pi6.use_count() == pi6.use_count()); - CHECK(!(pi5 < pi6 || pi5 < pi6)); // shared ownership test - - auto pi7 = pi6; - CHECK(pi5 == pi7); - CHECK((pi7? true: false)); - CHECK(!!pi7); - CHECK(pi7.get() == p); - CHECK(pi7.use_count() == 3); - CHECK(!pi7.unique()); - CHECK(*pi7 == 7); - CHECK(pi7.use_count() == pi7.use_count()); - CHECK(!(pi5 < pi7 || pi5 < pi7)); // shared ownership test -} - -TEST_CASE("shared_ptr move") -{ - rspamd::local_shared_ptr<int> pi(new int); - - rspamd::local_shared_ptr<int> pi2(std::move(pi)); - CHECK(!(pi2 == pi)); - CHECK((!pi2? false: true)); - CHECK(!pi); - CHECK(pi.get() == nullptr); - CHECK(pi2.get() != nullptr); - CHECK(pi.use_count() != pi2.use_count()); - - std::swap(pi, pi2); - CHECK(!(pi2 == pi)); - CHECK((!pi? false: true)); - CHECK(!pi2); - CHECK(pi.get() != nullptr); - CHECK(pi2.get() == nullptr); - CHECK(pi.use_count() != pi2.use_count()); -} - -struct deleter_test { - bool *pv; - deleter_test(bool &v) { - v = false; - pv = &v; - } - ~deleter_test() { - *pv = true; - } -}; -TEST_CASE("shared_ptr dtor") { - bool t; - - { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); - } - - CHECK(t == true); - - { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); - - rspamd::local_shared_ptr<deleter_test> pi2(pi); - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - pi.reset(); - CHECK(!(pi2 == pi)); - CHECK(pi2.use_count() == 1); - CHECK(t == false); - - pi = pi2; - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - CHECK(t == false); - } - - CHECK(t == true); -} - -TEST_CASE("make_shared dtor") { - bool t; - - { - auto pi = rspamd::local_make_shared<deleter_test>(t); - - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); - - rspamd::local_shared_ptr<deleter_test> pi2(pi); - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - pi.reset(); - CHECK(!(pi2 == pi)); - CHECK(pi2.use_count() == 1); - CHECK(t == false); - - pi = pi2; - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - CHECK(t == false); - } - - CHECK(t == true); -} - -TEST_CASE("shared_ptr dtor") { - bool t; - - { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); - } - - CHECK(t == true); - - { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); - - rspamd::local_shared_ptr<deleter_test> pi2(pi); - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - pi.reset(); - CHECK(!(pi2 == pi)); - CHECK(pi2.use_count() == 1); - CHECK(t == false); - - pi = pi2; - CHECK(pi2 == pi); - CHECK(pi.use_count() == 2); - CHECK(t == false); - } - - CHECK(t == true); -} - -TEST_CASE("weak_ptr") { - bool t; - - { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - - CHECK((!pi ? false : true)); - CHECK(!!pi); - CHECK(pi.get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(pi.unique()); - CHECK(t == false); - - rspamd::local_weak_ptr<deleter_test> wp(pi); - CHECK(wp.lock().get() != nullptr); - CHECK(pi.use_count() == 1); - CHECK(wp.use_count() == 1); - pi.reset(); - CHECK(pi.use_count() == 0); - CHECK(wp.use_count() == 0); - } - - CHECK(t == true); - - rspamd::local_weak_ptr<deleter_test> wp; - { - rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); - wp = pi; - CHECK(!wp.expired()); - CHECK(wp.lock().get() != nullptr); - } - - CHECK(t == true); - CHECK(wp.expired()); -} - } #endif |