Changeset f2ba4c79 in mainline


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:
c7d7368
Parents:
4727aacd
git-author:
Dzejrou <dzejrou@…> (2018-05-14 22:54:41)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:24)
Message:

cpp: added unordered_map tests and fixed bugs found by them

Location:
uspace/lib/cpp
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/Makefile

    r4727aacd rf2ba4c79  
    6464        src/internal/test/test.cpp \
    6565        src/internal/test/tuple.cpp \
     66        src/internal/test/unordered_map.cpp \
    6667        src/internal/test/vector.cpp
    6768
  • uspace/lib/cpp/include/internal/hash_table_iterators.hpp

    r4727aacd rf2ba4c79  
    142142        using non_const_iterator_type = hash_table_iterator<
    143143            Value, get_non_const_ref_t<ConstReference>,
    144             get_non_const_ptr<ConstPointer>, Size
     144            get_non_const_ptr_t<ConstPointer>, Size
    145145        >;
    146146
  • uspace/lib/cpp/include/internal/hash_table_policies.hpp

    r4727aacd rf2ba4c79  
    6060            auto current = head;
    6161
     62            if (!current)
     63                return 0;
     64
    6265            do
    6366            {
     
    246249        static typename Table::size_type count(const Table& table, const Key& key)
    247250        {
    248             auto head = table.table_[get_bucket_idx_(key)].head;
     251            auto head = table.table_[table.get_bucket_idx_(key)].head;
    249252            if (!head)
    250253                return 0;
     
    299302        {
    300303            auto idx = table.get_bucket_idx_(key);
    301             auto it = table.begin(it);
     304            auto head = table.table_[idx].head;
     305            auto current = head;
     306            table.table_[idx].head = nullptr;
     307
     308            if (!current)
     309                return 0;
     310
     311            /**
     312             * We traverse the list and delete if the keys
     313             * equal, if they don't we append the nodes back
     314             * to the bucket.
     315             */
    302316            typename Table::size_type res{};
    303317
    304             while (it != table.end(it))
    305             {
    306                 if (table.keys_equal(key, *it))
     318            do
     319            {
     320                auto tmp = current;
     321                current = current->next;
     322
     323                if (!table.keys_equal(key, tmp->value))
     324                    table.table_[idx].append(tmp);
     325                else
    307326                {
    308                     while (table.keys_equal(key, *it))
    309                     {
    310                         auto node = it.node();
    311                         ++it;
    312                         ++res;
    313 
    314                         node.unlink();
    315                         delete node;
    316                         --table.size_;
    317                     }
    318 
    319                     // Elements with equal keys are next to each other.
    320                     return res;
     327                    ++res;
     328                    --table.size_;
     329
     330                    delete tmp;
    321331                }
    322 
    323                 ++it;
    324             }
     332            }
     333            while (current != head);
    325334
    326335            return res;
     
    407416
    408417            if (!bucket)
    409                 return make_pair(table.end(), false);
     418                table.end();
    410419
    411420            if (target && table.keys_equal(key, target->value))
  • uspace/lib/cpp/include/internal/test/tests.hpp

    r4727aacd rf2ba4c79  
    195195            void test_multi_bounds_and_ranges();
    196196    };
     197
     198    class unordered_map_test: public test_suite
     199    {
     200        public:
     201            bool run(bool) override;
     202            const char* name() override;
     203
     204        private:
     205            void test_constructors_and_assignment();
     206            void test_histogram();
     207            void test_emplace_insert();
     208            void test_multi();
     209
     210            template<class Iterator, class Container>
     211            void test_contains(const char* tname, Iterator first,
     212                               Iterator last, const Container& cont)
     213            {
     214                if (!assert_contains(first, last, cont))
     215                {
     216                    report(false, tname);
     217                    ++failed_;
     218                    ok_ = false;
     219                }
     220                else
     221                {
     222                    report(true, tname);
     223                    ++succeeded_;
     224                }
     225            }
     226
     227            template<class Iterator1, class Iterator2, class Container>
     228            void test_contains_multi(const char* tname, Iterator1 first1,
     229                               Iterator1 last1, Iterator2 first2,
     230                               const Container& cont)
     231            {
     232                if (!assert_contains_multi(first1, last1, first2, cont))
     233                {
     234                    report(false, tname);
     235                    ++failed_;
     236                    ok_ = false;
     237                }
     238                else
     239                {
     240                    report(true, tname);
     241                    ++succeeded_;
     242                }
     243            }
     244
     245            template<class Iterator, class Container>
     246            bool assert_contains(Iterator first, Iterator last, const Container& cont)
     247            {
     248                while (first != last)
     249                {
     250                    if (cont.find(*first++) == cont.end())
     251                        return false;
     252                }
     253
     254                return true;
     255            }
     256
     257            template<class Iterator1, class Iterator2, class Container>
     258            bool assert_contains_multi(Iterator1 first1, Iterator1 last1,
     259                                       Iterator2 first2, const Container& cont)
     260            {
     261                while (first1 != last1)
     262                {
     263                    if (cont.count(*first1++) != *first2++)
     264                        return false;
     265                }
     266
     267                return true;
     268            }
     269    };
    197270}
    198271
Note: See TracChangeset for help on using the changeset viewer.