Changeset d9a9e7b 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:
f2f85283
Parents:
71f713a
git-author:
Dzejrou <dzejrou@…> (2018-06-05 13:05:16)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:24)
Message:

cpp: added tests for mutating algorithms and fixed bugs found by them

Location:
uspace/lib/cpp
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/__bits/test/tests.hpp

    r71f713a rd9a9e7b  
    293293        private:
    294294            void test_non_modifying();
     295            void test_mutating();
    295296    };
    296297}
  • uspace/lib/cpp/include/impl/algorithm.hpp

    r71f713a rd9a9e7b  
    400400    {
    401401        while (first != last)
    402             *result++ = move(first++);
     402            *result++ = move(*first++);
    403403
    404404        return result;
     
    444444        while (first != last)
    445445            *result++ = op(*first++);
     446
     447        return result;
    446448    }
    447449
     
    454456        while (first1 != last1)
    455457            *result++ = op(*first1++, *first2++);
     458
     459        return result;
    456460    }
    457461
  • uspace/lib/cpp/src/__bits/test/algorithm.cpp

    r71f713a rd9a9e7b  
    3030#include <algorithm>
    3131#include <array>
     32#include <string>
    3233#include <utility>
    3334
     
    4041
    4142        test_non_modifying();
     43        test_mutating();
    4244
    4345        return end();
     
    185187        // TODO: is_permutation, search
    186188    }
     189
     190    void algorithm_test::test_mutating()
     191    {
     192        auto check1 = {1, 2, 3, 10, 20, 30, 40};
     193        std::array<int, 4> data1{10, 20, 30, 40};
     194        std::array<int, 7> data2{1, 2, 3, 4, 5, 6, 7};
     195
     196        auto res1 = std::copy(
     197            data1.begin(), data1.end(), data2.begin() + 3
     198        );
     199
     200        test_eq(
     201            "copy pt1", check1.begin(), check1.end(),
     202            data2.begin(), data2.end()
     203        );
     204        test_eq("copy pt2", res1, data2.end());
     205
     206        auto check2 = {1, 2, 3, 10, 20, 30, 7, 8};
     207        std::array<int, 8> data3{1, 2, 3, 4, 5, 6, 7, 8};
     208
     209        auto res2 = std::copy_n(
     210            data1.begin(), 3, data3.begin() + 3
     211        );
     212        test_eq(
     213            "copy_n pt1", check2.begin(), check2.end(),
     214            data3.begin(), data3.end()
     215        );
     216        test_eq("copy_n pt2", res2, &data3[6]);
     217
     218        auto check3 = {2, 4, 6, 8};
     219        std::array<int, 4> data4{0, 0, 0, 0};
     220        std::array<int, 9> data5{1, 2, 3, 4, 5, 6, 7, 8, 9};
     221
     222        auto res3 = std::copy_if(
     223            data5.begin(), data5.end(), data4.begin(),
     224            [](auto x){ return x % 2 == 0; }
     225        );
     226
     227        test_eq(
     228            "copy_if pt1", check3.begin(), check3.end(),
     229            data4.begin(), data4.end()
     230        );
     231        test_eq("copy_if pt2", res3, data4.end());
     232
     233        /**
     234         * Note: Not testing copy_backwards as it isn't
     235         *       really easy to test, but since it is
     236         *       widely used in the rest of the library
     237         *       (mainly right shifting in sequence
     238         *       containers) it's tested by other tests.
     239         */
     240
     241        auto check4 = {std::string{"A"}, std::string{"B"}, std::string{"C"}};
     242        std::array<std::string, 3> data6{"A", "B", "C"};
     243        std::array<std::string, 3> data7{"", "", ""};
     244
     245        auto res4 = std::move(
     246            data6.begin(), data6.end(), data7.begin()
     247        );
     248        test_eq(
     249            "move pt1", check4.begin(), check4.end(),
     250            data7.begin(), data7.end()
     251        );
     252        test(
     253            "move pt2", (data6[0].empty() && data6[1].empty(),
     254            data6[2].empty())
     255        );
     256        test_eq("move pt3", res4, data7.end());
     257
     258        auto check5 = {1, 2, 3, 4};
     259        auto check6 = {10, 20, 30, 40};
     260        std::array<int, 4> data8{1, 2, 3, 4};
     261        std::array<int, 4> data9{10, 20, 30, 40};
     262
     263        auto res5 = std::swap_ranges(
     264            data8.begin(), data8.end(),
     265            data9.begin()
     266        );
     267
     268        test_eq(
     269            "swap_ranges pt1", check6.begin(), check6.end(),
     270            data8.begin(), data8.end()
     271        );
     272        test_eq(
     273            "swap_ranges pt2", check5.begin(), check5.end(),
     274            data9.begin(), data9.end()
     275        );
     276        test_eq("swap_ranges pt3", res5, data9.end());
     277
     278        std::iter_swap(data8.begin(), data9.begin());
     279        test_eq("swap_iter pt1", data8[0], 1);
     280        test_eq("swap_iter pt2", data9[0], 10);
     281
     282        auto check7 = {2, 3, 4, 5};
     283        std::array<int, 4> data10{1, 2, 3, 4};
     284
     285        auto res6 = std::transform(
     286            data10.begin(), data10.end(), data10.begin(),
     287            [](auto x) { return x + 1; }
     288        );
     289        test_eq(
     290            "transform pt1",
     291            check7.begin(), check7.end(),
     292            data10.begin(), data10.end()
     293        );
     294        test_eq("transform pt2", res6, data10.end());
     295    }
    187296}
Note: See TracChangeset for help on using the changeset viewer.