Ignore:
Timestamp:
2018-07-05T21:41:24Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ca8d393
Parents:
17012fcf
git-author:
Dzejrou <dzejrou@…> (2018-05-16 00:49:54)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:24)
Message:

cpp: moved type getters for allocator and pointer traits to a separate header, fully implemented pointer_traits and added pointer_traits tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/src/internal/test/memory.cpp

    r17012fcf rbfc972e  
    3131#include <internal/test/tests.hpp>
    3232#include <memory>
     33#include <type_traits>
    3334#include <utility>
    3435
    3536namespace std::test
    3637{
     38    namespace aux
     39    {
     40        struct dummy_pointer1
     41        {
     42            using element_type    = int;
     43            using difference_type = bool;
     44
     45            template<class U>
     46            using rebind = unsigned;
     47
     48            int tag{};
     49
     50            static dummy_pointer1 pointer_to(element_type& x)
     51            {
     52                dummy_pointer1 res;
     53                res.tag = x;
     54                return res;
     55            }
     56        };
     57
     58        template<class T, class... Args>
     59        struct dummy_pointer2
     60        {
     61            using element_type    = signed char;
     62            using difference_type = unsigned char;
     63        };
     64
     65        struct dummy_allocator1
     66        {
     67            using value_type = int;
     68        };
     69
     70        struct dummy_allocator2
     71        {
     72            using value_type    = int;
     73            using pointer       = char*;
     74            using const_pointer = const void*;
     75        };
     76    }
     77
    3778    bool memory_test::run(bool report)
    3879    {
     
    4283        test_unique_ptr();
    4384        test_shared_ptr();
     85        test_weak_ptr();
     86        test_allocators();
     87        test_pointers();
    4488
    4589        return end();
     
    110154            {
    111155                auto ptr2 = ptr1;
    112                 test_eq("shared_ptr copy pt1", ptr1.use_count(), 2U);
    113                 test_eq("shared_ptr copy pt2", ptr2.use_count(), 2U);
     156                test_eq("shared_ptr copy pt1", ptr1.use_count(), 2L);
     157                test_eq("shared_ptr copy pt2", ptr2.use_count(), 2L);
    114158                test_eq("shared_ptr copy no constructor call", mock::copy_constructor_calls, 0U);
    115159                test_eq("shared_ptr not unique", ptr1.unique(), false);
    116160
    117161                auto ptr3 = std::move(ptr2);
    118                 test_eq("shared_ptr move pt1", ptr1.use_count(), 2U);
    119                 test_eq("shared_ptr move pt2", ptr3.use_count(), 2U);
     162                test_eq("shared_ptr move pt1", ptr1.use_count(), 2L);
     163                test_eq("shared_ptr move pt2", ptr3.use_count(), 2L);
     164                test_eq("shared_ptr move pt3", ptr2.use_count(), 0L);
    120165
    121166                test_eq("shared_ptr move origin empty", (bool)ptr2, false);
     
    125170        test_eq("shared_ptr original out of scope", mock::destructor_calls, 1U);
    126171    }
     172
     173    void memory_test::test_weak_ptr()
     174    {
     175        mock::clear();
     176        {
     177            std::weak_ptr<mock> wptr1{};
     178            {
     179                auto ptr1 = std::make_shared<mock>();
     180                wptr1 = ptr1;
     181                {
     182                    std::weak_ptr<mock> wptr2 = ptr1;
     183                    test_eq("weak_ptr shares use count", wptr2.use_count(), 1L);
     184                    test_eq("weak_ptr not expired", wptr2.expired(), false);
     185
     186                    auto ptr2 = wptr2.lock();
     187                    test_eq("locked ptr increases use count", ptr1.use_count(), 2L);
     188                }
     189            }
     190            test_eq("weak_ptr expired after all shared_ptrs die", wptr1.expired(), true);
     191            test_eq("shared object destroyed while weak_ptr exists", mock::destructor_calls, 1U);
     192        }
     193    }
     194
     195    void memory_test::test_allocators()
     196    {
     197        using dummy_traits1 = std::allocator_traits<aux::dummy_allocator1>;
     198        using dummy_traits2 = std::allocator_traits<aux::dummy_allocator2>;
     199
     200        /* static_assert(std::is_same_v<typename dummy_traits1::pointer, int*>); */
     201        /* static_assert(std::is_same_v<typename dummy_traits2::pointer, char*>); */
     202    }
     203
     204    void memory_test::test_pointers()
     205    {
     206        using dummy_traits1 = std::pointer_traits<aux::dummy_pointer1>;
     207        using dummy_traits2 = std::pointer_traits<aux::dummy_pointer2<int, char>>;
     208        using int_traits    = std::pointer_traits<int*>;
     209
     210        static_assert(std::is_same_v<typename dummy_traits1::pointer, aux::dummy_pointer1>);
     211        static_assert(std::is_same_v<typename dummy_traits1::element_type, int>);
     212        static_assert(std::is_same_v<typename dummy_traits1::difference_type, bool>);
     213        static_assert(std::is_same_v<typename dummy_traits1::template rebind<long>, unsigned>);
     214
     215        int x{10};
     216        test_eq("pointer_traits<Ptr>::pointer_to", dummy_traits1::pointer_to(x).tag, 10);
     217
     218        static_assert(std::is_same_v<typename dummy_traits2::pointer, aux::dummy_pointer2<int, char>>);
     219        static_assert(std::is_same_v<typename dummy_traits2::element_type, signed char>);
     220        static_assert(std::is_same_v<typename dummy_traits2::difference_type, unsigned char>);
     221
     222        static_assert(std::is_same_v<typename int_traits::pointer, int*>);
     223        static_assert(std::is_same_v<typename int_traits::element_type, int>);
     224        static_assert(std::is_same_v<typename int_traits::difference_type, ptrdiff_t>);
     225        static_assert(std::is_same_v<typename int_traits::rebind<char>, char*>);
     226    }
    127227}
Note: See TracChangeset for help on using the changeset viewer.