diff options
Diffstat (limited to 'sources/shiboken6/tests/libsmart')
-rw-r--r-- | sources/shiboken6/tests/libsmart/CMakeLists.txt | 23 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/libsmartmacros.h | 18 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/smart.cpp | 272 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/smart.h | 16 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/smart_integer.h | 69 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/smart_obj.h | 45 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/smart_registry.h | 43 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/smart_sharedptr.h | 94 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/smart_test.h | 13 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/stdoptionaltestbench.cpp | 58 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/stdoptionaltestbench.h | 30 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/stdsharedptrtestbench.cpp | 106 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/stdsharedptrtestbench.h | 49 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/stduniqueptrtestbench.cpp | 133 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsmart/stduniqueptrtestbench.h | 50 |
15 files changed, 1019 insertions, 0 deletions
diff --git a/sources/shiboken6/tests/libsmart/CMakeLists.txt b/sources/shiboken6/tests/libsmart/CMakeLists.txt new file mode 100644 index 000000000..95f0cffd6 --- /dev/null +++ b/sources/shiboken6/tests/libsmart/CMakeLists.txt @@ -0,0 +1,23 @@ +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +project(libsmart) + +set(libsmart_SRC +libsmartmacros.h +smart.cpp smart.h +smart_integer.h +smart_obj.h +smart_registry.h +smart_sharedptr.h +smart_test.h +stdoptionaltestbench.cpp stdoptionaltestbench.h +stdsharedptrtestbench.cpp stdsharedptrtestbench.h +stduniqueptrtestbench.cpp stduniqueptrtestbench.h +) + +add_library(libsmart SHARED ${libsmart_SRC}) +target_include_directories(libsmart PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_compile_definitions(libsmart PRIVATE LIBSMART_BUILD) +set_property(TARGET libsmart PROPERTY PREFIX "") + diff --git a/sources/shiboken6/tests/libsmart/libsmartmacros.h b/sources/shiboken6/tests/libsmart/libsmartmacros.h new file mode 100644 index 000000000..c1f229b6c --- /dev/null +++ b/sources/shiboken6/tests/libsmart/libsmartmacros.h @@ -0,0 +1,18 @@ +// Copyright (C) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef LIB_SMART_MACROS_H +#define LIB_SMART_MACROS_H + +#include "../libminimal/libminimalmacros.h" + +#define LIB_SMART_EXPORT LIBMINIMAL_EXPORT +#define LIB_SMART_IMPORT LIBMINIMAL_IMPORT + +#ifdef LIBSMART_BUILD +# define LIB_SMART_API LIB_SMART_EXPORT +#else +# define LIB_SMART_API LIB_SMART_IMPORT +#endif + +#endif // LIB_SMART_MACROS_H diff --git a/sources/shiboken6/tests/libsmart/smart.cpp b/sources/shiboken6/tests/libsmart/smart.cpp new file mode 100644 index 000000000..2273040f9 --- /dev/null +++ b/sources/shiboken6/tests/libsmart/smart.cpp @@ -0,0 +1,272 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#include "smart.h" + +#include <algorithm> +#include <iostream> + +static inline bool verbose() +{ + return Registry::getInstance()->verbose(); +} + +void SharedPtrBase::logDefaultConstructor(const char *instantiation, const void *t) +{ + if (verbose()) + std::cout << "SharedPtr<" << instantiation << "> default constructor " << t << '\n'; +} + +void SharedPtrBase::logConstructor(const char *instantiation, const void *t, + const void *pointee) +{ + if (verbose()) { + std::cout << "SharedPtr<" << instantiation << "> constructor " + << t << " with pointer " << pointee << '\n'; + } +} + +void SharedPtrBase::logCopyConstructor(const char *instantiation, const void *t, + const void *refData) +{ + if (verbose()) { + std::cout << "SharedPtr<" << instantiation << ">) copy constructor " + << t << " with pointer " << refData << '\n'; + } +} + +void SharedPtrBase::logAssignment(const char *instantiation, const void *t, const void *refData) +{ + if (verbose()) { + std::cout << "SharedPtr<" << instantiation << ">::operator= " << t + << " with pointer " << refData << "\n"; + } +} + +void SharedPtrBase::logDestructor(const char *instantiation, const void *t, + int remainingRefCount) +{ + if (verbose()) { + std::cout << "~SharedPtr<" << instantiation << "> " << t << ", remaining refcount " + << remainingRefCount << '\n'; + } +} + +Obj::Obj() : m_integer(123), m_internalInteger(new Integer) +{ + Registry::getInstance()->add(this); + if (verbose()) + std::cout << "Obj constructor " << this << '\n'; +} + +Obj::~Obj() +{ + Registry::getInstance()->remove(this); + delete m_internalInteger; + if (verbose()) + std::cout << "~Obj " << this << '\n'; +} + + +void Obj::printObj() { + if (verbose()) { + std::cout << "Obj::printObj(): integer value: " << m_integer + << " internal integer value: " << m_internalInteger->value() << '\n'; + } +} + + +SharedPtr<Obj> Obj::createSharedPtrObj() +{ + SharedPtr<Obj> o(new Obj); + return o; +} + +std::vector<SharedPtr<Obj> > Obj::createSharedPtrObjList(int size) +{ + std::vector<SharedPtr<Obj> > r; + for (int i=0; i < size; i++) + r.push_back(createSharedPtrObj()); + return r; +} + + +SharedPtr<Integer> Obj::createSharedPtrInteger() +{ + SharedPtr<Integer> o(new Integer); + return o; +} + +SharedPtr<Smart::Integer2> Obj::createSharedPtrInteger2() +{ + SharedPtr<Smart::Integer2> o(new Smart::Integer2); + return o; +} + +int Obj::takeSharedPtrToObj(SharedPtr<Obj> pObj) +{ + pObj->printObj(); + return pObj->m_integer; +} + +int Obj::takeSharedPtrToInteger(SharedPtr<Integer> pInt) +{ + if (pInt.isNull()) { + std::cout << "SharedPtr<Integer>(nullptr) passed!\n"; + return -1; + } + pInt->printInteger(); + return pInt->value(); +} + +int Obj::takeSharedPtrToIntegerByConstRef(const SharedPtr<Integer> &pInt) +{ + if (pInt.isNull()) { + std::cout << "SharedPtr<Integer>(nullptr) passed!\n"; + return -1; + } + pInt->printInteger(); + return pInt->value(); +} + +SharedPtr<Integer> Obj::createSharedPtrInteger(int value) +{ + auto *i = new Integer; + i->setValue(value); + return SharedPtr<Integer>(i); +} + +SharedPtr<Integer> Obj::createNullSharedPtrInteger() +{ + return {}; +} + +SharedPtr<const Integer> Obj::createSharedPtrConstInteger() +{ + SharedPtr<const Integer> co(new Integer); + return co; +} + +int Obj::takeSharedPtrToConstInteger(SharedPtr<const Integer> pInt) +{ + return pInt->m_int; +} + +Integer Obj::takeInteger(Integer val) +{ + return val; +} + +Integer::Integer() : m_int(456) +{ + Registry::getInstance()->add(this); + if (verbose()) + std::cout << "Integer constructor " << this << '\n'; +} + +Integer::Integer(const Integer &other) +{ + Registry::getInstance()->add(this); + if (verbose()) + std::cout << "Integer copy constructor " << this << '\n'; + m_int = other.m_int; +} + +Integer &Integer::operator=(const Integer &other) +{ + Registry::getInstance()->add(this); + if (verbose()) + std::cout << "Integer operator= " << this << '\n'; + m_int = other.m_int; + return *this; +} + +Integer::~Integer() +{ + Registry::getInstance()->remove(this); + if (verbose()) + std::cout << "~Integer " << this << " (" << m_int << ")\n"; +} + +int Integer::value() const +{ + return m_int; +} + +void Integer::setValue(int v) +{ + m_int = v; + if (verbose()) + std::cout << "Integer::setValue(" << v << ") " << this << '\n'; +} + +int Integer::compare(const Integer &rhs) const +{ + if (m_int < rhs.m_int) + return -1; + return m_int > rhs.m_int ? 1 : 0; +} + +void Integer::printInteger() const +{ + if (verbose()) + std::cout << "Integer value for object " << this << " is " << m_int << '\n'; +} + +Registry *Registry::getInstance() +{ + static Registry registry; + return ®istry; +} + +Registry::Registry() = default; + +Registry::~Registry() = default; + +void Registry::add(Obj *p) +{ + m_objects.push_back(p); +} + +void Registry::add(Integer *p) +{ + m_integers.push_back(p); +} + +void Registry::remove(Obj *p) +{ + m_objects.erase(std::remove(m_objects.begin(), m_objects.end(), p), m_objects.end()); +} + +void Registry::remove(Integer *p) +{ + m_integers.erase(std::remove(m_integers.begin(), m_integers.end(), p), m_integers.end()); +} + +int Registry::countObjects() const +{ + return static_cast<int>(m_objects.size()); +} + +int Registry::countIntegers() const +{ + return static_cast<int>(m_integers.size()); +} + +bool Registry::verbose() const +{ + return m_verbose; +} + +void Registry::setVerbose(bool flag) +{ + m_verbose = flag; +} + +Smart::Integer2::Integer2() + : Integer () +{ +} + +Smart::Integer2::Integer2(const Smart::Integer2 &) = default; +Smart::Integer2 &Smart::Integer2::operator=(const Integer2 &) = default; diff --git a/sources/shiboken6/tests/libsmart/smart.h b/sources/shiboken6/tests/libsmart/smart.h new file mode 100644 index 000000000..1f610b302 --- /dev/null +++ b/sources/shiboken6/tests/libsmart/smart.h @@ -0,0 +1,16 @@ +// Copyright (C) 2019 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef SMART_H +#define SMART_H + +#include "smart_sharedptr.h" +#include "smart_integer.h" +#include "smart_obj.h" +#include "smart_registry.h" +#include "smart_test.h" +#include "stdsharedptrtestbench.h" +#include "stdoptionaltestbench.h" +#include "stduniqueptrtestbench.h" + +#endif // SMART_H diff --git a/sources/shiboken6/tests/libsmart/smart_integer.h b/sources/shiboken6/tests/libsmart/smart_integer.h new file mode 100644 index 000000000..42a441a00 --- /dev/null +++ b/sources/shiboken6/tests/libsmart/smart_integer.h @@ -0,0 +1,69 @@ +// Copyright (C) 2019 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef SMART_INTEGER_H +#define SMART_INTEGER_H + +#include "libsmartmacros.h" + +class LIB_SMART_API Integer { +public: + Integer(); + Integer(const Integer &other); + Integer &operator=(const Integer &other); + Integer(Integer &&other) noexcept = default; + Integer &operator=(Integer &&other) noexcept = default; + ~Integer(); + void printInteger() const; + + int value() const; + void setValue(int v); + + int compare(const Integer &rhs) const; + + int m_int; // public for testing member field access. +}; + +inline bool operator==(const Integer &lhs, const Integer &rhs) +{ + return lhs.compare(rhs) == 0; +} + +inline bool operator!=(const Integer &lhs, const Integer &rhs) +{ + return lhs.compare(rhs) != 0; +} + +inline bool operator<(const Integer &lhs, const Integer &rhs) +{ + return lhs.compare(rhs) < 0; +} + +inline bool operator<=(const Integer &lhs, const Integer &rhs) +{ + return lhs.compare(rhs) <= 0; +} + +inline bool operator>(const Integer &lhs, const Integer &rhs) +{ + return lhs.compare(rhs) > 0; +} + +inline bool operator>=(const Integer &lhs, const Integer &rhs) +{ + return lhs.compare(rhs) >= 0; +} + +namespace Smart { +class LIB_SMART_API Integer2 : public Integer { +public: + Integer2(); + Integer2(const Integer2 &); + Integer2 &operator=(const Integer2 &); + Integer2(Integer2 &&other) = delete; + Integer2 &operator=(Integer2 &&other) = delete; + ~Integer2() = default; +}; +} // namespace Smart + +#endif // SMART_INTEGER_H diff --git a/sources/shiboken6/tests/libsmart/smart_obj.h b/sources/shiboken6/tests/libsmart/smart_obj.h new file mode 100644 index 000000000..9f4f8425d --- /dev/null +++ b/sources/shiboken6/tests/libsmart/smart_obj.h @@ -0,0 +1,45 @@ +// Copyright (C) 2019 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef SMART_OBJ_H +#define SMART_OBJ_H + +#include "libsmartmacros.h" +#include "smart_sharedptr.h" + +#include <vector> + +class Integer; +class Obj; +namespace Smart { class Integer2; } + +// Couldn't name it Object because it caused some namespace clashes. +class LIB_SMART_API Obj { +public: + Obj(); + Obj(const Obj &other) = delete; + Obj &operator=(const Obj &other) = delete; + Obj(Obj &&other) = delete; + Obj &operator=(Obj &&other) = delete; + virtual ~Obj(); + + void printObj(); + Integer takeInteger(Integer val); + static SharedPtr<Obj> createSharedPtrObj(); + std::vector<SharedPtr<Obj> > createSharedPtrObjList(int size); + virtual SharedPtr<Integer> createSharedPtrInteger(); // virtual for PYSIDE-1188 + SharedPtr<const Integer> createSharedPtrConstInteger(); + int takeSharedPtrToConstInteger(SharedPtr<const Integer> pInt); + SharedPtr<Smart::Integer2> createSharedPtrInteger2(); + int takeSharedPtrToObj(SharedPtr<Obj> pObj); + int takeSharedPtrToInteger(SharedPtr<Integer> pInt); + int takeSharedPtrToIntegerByConstRef(const SharedPtr<Integer> &pInt); + + static SharedPtr<Integer> createSharedPtrInteger(int value); + static SharedPtr<Integer> createNullSharedPtrInteger(); + + int m_integer; // public for testing member field access. + Integer *m_internalInteger; +}; + +#endif // SMART_OBJ_H diff --git a/sources/shiboken6/tests/libsmart/smart_registry.h b/sources/shiboken6/tests/libsmart/smart_registry.h new file mode 100644 index 000000000..abf7edc84 --- /dev/null +++ b/sources/shiboken6/tests/libsmart/smart_registry.h @@ -0,0 +1,43 @@ +// Copyright (C) 2019 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef SMART_REGISTRY_H +#define SMART_REGISTRY_H + +#include <vector> + +#include "libsmartmacros.h" + +class Obj; +class Integer; + +// Used to track which C++ objects are alive. +class LIB_SMART_API Registry { +public: + static Registry *getInstance(); + ~Registry(); + + Registry(const Registry &) = delete; + Registry &operator=(const Registry &) = delete; + Registry(Registry &&) = delete; + Registry &operator=(Registry &&) = delete; + + void add(Obj *p); + void add(Integer *p); + void remove(Obj *p); + void remove(Integer *p); + int countObjects() const; + int countIntegers() const; + bool verbose() const; + void setVerbose(bool flag); + +protected: + Registry(); + +private: + std::vector<Obj *> m_objects; + std::vector<Integer *> m_integers; + bool m_verbose = false; +}; + +#endif // SMART_REGISTRY_H diff --git a/sources/shiboken6/tests/libsmart/smart_sharedptr.h b/sources/shiboken6/tests/libsmart/smart_sharedptr.h new file mode 100644 index 000000000..dc665810a --- /dev/null +++ b/sources/shiboken6/tests/libsmart/smart_sharedptr.h @@ -0,0 +1,94 @@ +// Copyright (C) 2019 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef SMART_SHARED_PTR_H +#define SMART_SHARED_PTR_H + +#include <memory> + +#include "libsmartmacros.h" + +struct SharedPtrBase +{ + LIB_SMART_API static void logDefaultConstructor(const char *instantiation, const void *t); + LIB_SMART_API static void logConstructor(const char *instantiation, const void *t, const void *pointee); + LIB_SMART_API static void logCopyConstructor(const char *instantiation, const void *t, const void *refData); + LIB_SMART_API static void logAssignment(const char *instantiation, const void *t, const void *refData); + LIB_SMART_API static void logDestructor(const char *instantiation, const void *t, int remainingRefCount); +}; + +template <class T> +class SharedPtr : public SharedPtrBase { +public: + LIBMINIMAL_DEFAULT_MOVE(SharedPtr) + + SharedPtr() { logDefaultConstructor(typeid(T).name(), this); } + + SharedPtr(T *v) : mPtr(v) + { + logConstructor(typeid(T).name(), this, v); + } + + SharedPtr(const SharedPtr<T> &other) : mPtr(other.mPtr) + { + logCopyConstructor(typeid(T).name(), this, data()); + } + + template<class X> + SharedPtr(const SharedPtr<X> &other) : mPtr(other.mPtr) + { + logCopyConstructor(typeid(T).name(), this, data()); + } + + SharedPtr &operator=(const SharedPtr &other) + { + if (this != &other) + mPtr = other.mPtr; + return *this; + } + + T *data() const + { + return mPtr.get(); + } + + int useCount() const + { + return mPtr.use_count(); + } + + void dummyMethod1() + { + } + + bool isNull() const + { + return mPtr.get() == nullptr; + } + + T& operator*() const + { + // Crashes if smart pointer is empty (just like std::shared_ptr). + return *mPtr; + } + + T *operator->() const + { + return mPtr.get(); + } + + bool operator!() const + { + return !mPtr; + } + + ~SharedPtr() + { + if (mPtr.use_count() >= 1) + logDestructor(typeid(T).name(), this, mPtr.use_count() - 1); + } + + std::shared_ptr<T> mPtr; +}; + +#endif // SMART_SHARED_PTR_H diff --git a/sources/shiboken6/tests/libsmart/smart_test.h b/sources/shiboken6/tests/libsmart/smart_test.h new file mode 100644 index 000000000..89d8cbc7c --- /dev/null +++ b/sources/shiboken6/tests/libsmart/smart_test.h @@ -0,0 +1,13 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef SMART_TEST_H +#define SMART_TEST_H + +namespace Test { + +enum DummyEnum { Dummy1, Dummy2 }; + +} + +#endif // SMART_TEST_H diff --git a/sources/shiboken6/tests/libsmart/stdoptionaltestbench.cpp b/sources/shiboken6/tests/libsmart/stdoptionaltestbench.cpp new file mode 100644 index 000000000..69100720c --- /dev/null +++ b/sources/shiboken6/tests/libsmart/stdoptionaltestbench.cpp @@ -0,0 +1,58 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#include "stdoptionaltestbench.h" + +#include <iostream> + +std::ostream &operator<<(std::ostream &str, const Integer &i) +{ + str << i.value(); + return str; +} + +template <class T> +std::ostream &operator<<(std::ostream &str, const std::optional<T> &o) +{ + if (o.has_value()) + str << o.value(); + else + str << "nullopt"; + return str; +} + +StdOptionalTestBench::StdOptionalTestBench() = default; + +std::optional<int> StdOptionalTestBench::optionalInt() const +{ + return m_optionalInt; +} + +void StdOptionalTestBench::setOptionalInt(const std::optional<int> &i) +{ + std::cout << __FUNCTION__ << ' ' << i << '\n'; + m_optionalInt = i; +} + +void StdOptionalTestBench::setOptionalIntValue(int i) +{ + std::cout << __FUNCTION__ << ' ' << i << '\n'; + m_optionalInt.emplace(i); +} + +std::optional<Integer> StdOptionalTestBench::optionalInteger() const +{ + return m_optionalInteger; +} + +void StdOptionalTestBench::setOptionalInteger(const std::optional<Integer> &s) +{ + std::cout << __FUNCTION__ << ' ' << s << '\n'; + m_optionalInteger = s; +} + +void StdOptionalTestBench::setOptionalIntegerValue(Integer &s) +{ + std::cout << __FUNCTION__ << ' ' << s << '\n'; + m_optionalInteger.emplace(s); +} diff --git a/sources/shiboken6/tests/libsmart/stdoptionaltestbench.h b/sources/shiboken6/tests/libsmart/stdoptionaltestbench.h new file mode 100644 index 000000000..baa709821 --- /dev/null +++ b/sources/shiboken6/tests/libsmart/stdoptionaltestbench.h @@ -0,0 +1,30 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef OPTIONALTEST_H +#define OPTIONALTEST_H + +#include "libsmartmacros.h" +#include "smart_integer.h" + +#include <optional> + +class LIB_SMART_API StdOptionalTestBench +{ +public: + StdOptionalTestBench(); + + std::optional<int> optionalInt() const; + void setOptionalInt(const std::optional<int> &i); + void setOptionalIntValue(int i); + + std::optional<Integer> optionalInteger() const; + void setOptionalInteger(const std::optional<Integer> &s); + void setOptionalIntegerValue(Integer &s); + +private: + std::optional<int> m_optionalInt; + std::optional<Integer> m_optionalInteger; +}; + +#endif // OPTIONALTEST_H diff --git a/sources/shiboken6/tests/libsmart/stdsharedptrtestbench.cpp b/sources/shiboken6/tests/libsmart/stdsharedptrtestbench.cpp new file mode 100644 index 000000000..472f807f2 --- /dev/null +++ b/sources/shiboken6/tests/libsmart/stdsharedptrtestbench.cpp @@ -0,0 +1,106 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#include "stdsharedptrtestbench.h" +#include "smart_integer.h" + +#include <iostream> + +StdSharedPtrTestBench::StdSharedPtrTestBench() = default; +StdSharedPtrTestBench::~StdSharedPtrTestBench() = default; + +std::shared_ptr<Integer> StdSharedPtrTestBench::createInteger(int v) +{ + auto result = std::make_shared<Integer>(); + result->setValue(v); + return result; +} + +std::shared_ptr<Integer> StdSharedPtrTestBench::createNullInteger() +{ + return {}; +} + +void StdSharedPtrTestBench::printInteger(const std::shared_ptr<Integer> &p) +{ + std::cerr << __FUNCTION__ << ' '; + if (p.get()) + std::cerr << p->value(); + else + std::cerr << "nullptr"; + std::cerr << '\n'; +} + +std::shared_ptr<int> StdSharedPtrTestBench::createInt(int v) +{ + return std::make_shared<int>(v); +} + +std::shared_ptr<int> StdSharedPtrTestBench::createNullInt() +{ + return {}; +} + +void StdSharedPtrTestBench::printInt(const std::shared_ptr<int> &p) +{ + std::cerr << __FUNCTION__ << ' '; + if (p.get()) + std::cerr << *p; + else + std::cerr << "nullptr"; + std::cerr << '\n'; +} + +std::shared_ptr<double> StdSharedPtrTestBench::createDouble(double v) +{ + return std::make_shared<double>(v); +} + +std::shared_ptr<double> StdSharedPtrTestBench::createNullDouble() +{ + return {}; +} + +void StdSharedPtrTestBench::printDouble(const std::shared_ptr<double> &p) +{ + std::cerr << __FUNCTION__ << ' '; + if (p.get()) + std::cerr << *p; + else + std::cerr << "nullptr"; + std::cerr << '\n'; +} + +std::shared_ptr<std::string> StdSharedPtrTestBench::createString(const char *text) +{ + return std::make_shared<std::string>(text); +} + +std::shared_ptr<std::string> StdSharedPtrTestBench::createNullString() +{ + return {}; +} + +void StdSharedPtrTestBench::printString(const std::shared_ptr<std::string> &p) +{ + std::cerr << __FUNCTION__ << ' '; + if (p.get()) + std::cerr << '"' << *p << '"'; + else + std::cerr << "nullptr"; + std::cerr << '\n'; +} + +StdSharedPtrVirtualMethodTester::StdSharedPtrVirtualMethodTester() = default; +StdSharedPtrVirtualMethodTester::~StdSharedPtrVirtualMethodTester() = default; + +std::shared_ptr<Integer> StdSharedPtrVirtualMethodTester::callModifyInteger(const std::shared_ptr<Integer> &p) +{ + return doModifyInteger(p); +} + +std::shared_ptr<Integer> StdSharedPtrVirtualMethodTester::doModifyInteger(std::shared_ptr<Integer> p) +{ + p->setValue(p->value() + 1); + return p; +} diff --git a/sources/shiboken6/tests/libsmart/stdsharedptrtestbench.h b/sources/shiboken6/tests/libsmart/stdsharedptrtestbench.h new file mode 100644 index 000000000..9d4c207b5 --- /dev/null +++ b/sources/shiboken6/tests/libsmart/stdsharedptrtestbench.h @@ -0,0 +1,49 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef STDSHAREDPTRTESTBENCH_H +#define STDSHAREDPTRTESTBENCH_H + +#include "libsmartmacros.h" + +#include <memory> +#include <string> + +class Integer; + +class LIB_SMART_API StdSharedPtrTestBench +{ +public: + StdSharedPtrTestBench(); + ~StdSharedPtrTestBench(); + + static std::shared_ptr<Integer> createInteger(int v = 42); + static std::shared_ptr<Integer> createNullInteger(); + static void printInteger(const std::shared_ptr<Integer> &); + + static std::shared_ptr<int> createInt(int v = 42); + static std::shared_ptr<int> createNullInt(); + static void printInt(const std::shared_ptr<int> &); + + static std::shared_ptr<double> createDouble(double v = 42); + static std::shared_ptr<double> createNullDouble(); + static void printDouble(const std::shared_ptr<double> &); + + static std::shared_ptr<std::string> createString(const char *text); + static std::shared_ptr<std::string> createNullString(); + static void printString(const std::shared_ptr<std::string> &); +}; + +class LIB_SMART_API StdSharedPtrVirtualMethodTester +{ +public: + StdSharedPtrVirtualMethodTester(); + virtual ~StdSharedPtrVirtualMethodTester(); + + std::shared_ptr<Integer> callModifyInteger(const std::shared_ptr<Integer> &p); + +protected: + virtual std::shared_ptr<Integer> doModifyInteger(std::shared_ptr<Integer> p); +}; + +#endif // STDSHAREDPTRTESTBENCH_H diff --git a/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.cpp b/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.cpp new file mode 100644 index 000000000..df4b566fa --- /dev/null +++ b/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.cpp @@ -0,0 +1,133 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#include "stduniqueptrtestbench.h" +#include "smart_integer.h" + +#include <iostream> + +std::ostream &operator<<(std::ostream &str, const std::unique_ptr<Integer> &p) +{ + str << "unique_ptr<Integer>("; + if (p.get()) + str << p->value(); + else + str << "nullptr"; + str << ')'; + return str; +} + +std::ostream &operator<<(std::ostream &str, const std::unique_ptr<Smart::Integer2> &p) +{ + str << "unique_ptr<Integer>("; + if (p.get()) + str << p->value(); + else + str << "nullptr"; + str << ')'; + return str; +} + +std::ostream &operator<<(std::ostream &str, const std::unique_ptr<int> &p) +{ + str << "unique_ptr<int>("; + if (p.get()) + str << *p; + else + str << "nullptr"; + str << ')'; + return str; +} + +StdUniquePtrTestBench::StdUniquePtrTestBench() = default; +StdUniquePtrTestBench::~StdUniquePtrTestBench() = default; + +std::unique_ptr<Integer> StdUniquePtrTestBench::createInteger(int v) +{ + auto result = std::make_unique<Integer>(); + result->setValue(v); + return result; +} + +std::unique_ptr<Integer> StdUniquePtrTestBench::createNullInteger() +{ + return {}; +} + +void StdUniquePtrTestBench::printInteger(const std::unique_ptr<Integer> &p) +{ + std::cerr << __FUNCTION__ << ' ' << p << '\n'; +} + +void StdUniquePtrTestBench::takeInteger(std::unique_ptr<Integer> p) +{ + std::cerr << __FUNCTION__ << ' ' << p << '\n'; +} + +std::unique_ptr<int> StdUniquePtrTestBench::createInt(int v) +{ + return std::make_unique<int>(v); +} + +std::unique_ptr<int> StdUniquePtrTestBench::createNullInt() +{ + return {}; +} + +void StdUniquePtrTestBench::printInt(const std::unique_ptr<int> &p) +{ + std::cerr << __FUNCTION__ << ' ' << p << '\n'; +} + +void StdUniquePtrTestBench::takeInt(std::unique_ptr<int> p) +{ + std::cerr << __FUNCTION__ << ' ' << p << '\n'; +} + +StdUniquePtrVirtualMethodTester::StdUniquePtrVirtualMethodTester() = default; + +StdUniquePtrVirtualMethodTester::~StdUniquePtrVirtualMethodTester() = default; + +bool StdUniquePtrVirtualMethodTester::testModifyIntegerByRef(int value, int expectedValue) +{ + auto p = std::make_unique<Integer>(); + p->setValue(value); + const int actualValue = doModifyIntegerByRef(p); + return p.get() != nullptr && actualValue == expectedValue; +} + +bool StdUniquePtrVirtualMethodTester::testModifyIntegerValue(int value, int expectedValue) +{ + auto p = std::make_unique<Integer>(); + p->setValue(value); + const int actualValue = doModifyIntegerByValue(std::move(p)); + return p.get() == nullptr && actualValue == expectedValue; +} + +bool StdUniquePtrVirtualMethodTester::testCreateInteger(int value, int expectedValue) +{ + auto p = doCreateInteger(value); + return p.get() != nullptr && p->value() == expectedValue; +} + +std::unique_ptr<Integer> StdUniquePtrVirtualMethodTester::doCreateInteger(int v) +{ + auto result = std::make_unique<Integer>(); + result->setValue(v); + return result; +} + +int StdUniquePtrVirtualMethodTester::doModifyIntegerByRef(const std::unique_ptr<Integer> &p) +{ + return p->value() + 1; +} + +int StdUniquePtrVirtualMethodTester::doModifyIntegerByValue(std::unique_ptr<Integer> p) +{ + return p->value() + 1; +} + +void StdUniquePtrTestBench::printInteger2(const std::unique_ptr<Smart::Integer2> &p) +{ + std::cerr << __FUNCTION__ << ' ' << p << '\n'; +} diff --git a/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.h b/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.h new file mode 100644 index 000000000..868c6d08c --- /dev/null +++ b/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.h @@ -0,0 +1,50 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef STDUNIQUEPTRTESTBENCH_H +#define STDUNIQUEPTRTESTBENCH_H + +#include "libsmartmacros.h" + +#include <memory> + +class Integer; +namespace Smart { +class Integer2; +} + +class LIB_SMART_API StdUniquePtrTestBench +{ +public: + StdUniquePtrTestBench(); + ~StdUniquePtrTestBench(); + + static std::unique_ptr<Integer> createInteger(int v = 42); + static std::unique_ptr<Integer> createNullInteger(); + static void printInteger2(const std::unique_ptr<Smart::Integer2> &p); + static void printInteger(const std::unique_ptr<Integer> &p); + static void takeInteger(std::unique_ptr<Integer> p); // Call with std::move() + + static std::unique_ptr<int> createInt(int v = 42); + static std::unique_ptr<int> createNullInt(); + static void printInt(const std::unique_ptr<int> &p); + static void takeInt(std::unique_ptr<int> p); // Call with std::move() +}; + +class LIB_SMART_API StdUniquePtrVirtualMethodTester +{ +public: + StdUniquePtrVirtualMethodTester(); + virtual ~StdUniquePtrVirtualMethodTester(); + + bool testModifyIntegerByRef(int value, int expectedValue); + bool testModifyIntegerValue(int value, int expectedValue); + bool testCreateInteger(int value, int expectedValue); + +protected: + virtual std::unique_ptr<Integer> doCreateInteger(int v); + virtual int doModifyIntegerByRef(const std::unique_ptr<Integer> &p); + virtual int doModifyIntegerByValue(std::unique_ptr<Integer> p); +}; + +#endif // STDUNIQUEPTRTESTBENCH_H |