Changeset d466d284 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:18Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
53e8686
Parents:
417296cd
git-author:
Jaroslav Jindrak <dzejrou@…> (2017-11-01 20:29:57)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:18)
Message:

cpp: added tests for std::string::replace

Location:
uspace/lib/cpp
Files:
2 edited

Legend:

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

    r417296cd rd466d284  
    7070            void test_insert();
    7171            void test_erase();
     72            void test_replace();
    7273    };
    7374}
  • uspace/lib/cpp/src/internal/test/string.cpp

    r417296cd rd466d284  
    4040        test_insert();
    4141        test_erase();
     42        test_replace();
    4243
    4344        return true;
     
    265266        );
    266267    }
     268
     269    void string_test::test_replace()
     270    {
     271        std::string check{"hello, world"};
     272
     273        std::string str1{"helXXX world"};
     274        str1.replace(3, 3, "lo,", 3);
     275        test_eq(
     276            "replace with full string",
     277            str1.begin(), str1.end(),
     278            check.begin(), check.end()
     279        );
     280
     281        std::string str2{"helXXX world"};
     282        str2.replace(3, 3, "lo,YYY", 3);
     283        test_eq(
     284            "replace with prefix of a string",
     285            str2.begin(), str2.end(),
     286            check.begin(), check.end()
     287        );
     288
     289        std::string str3{"helXXX world"};
     290        str3.replace(3, 3, "YYlo,YYY", 2, 3);
     291        test_eq(
     292            "replace with substring of a string",
     293            str3.begin(), str3.end(),
     294            check.begin(), check.end()
     295        );
     296
     297        std::string str4{"heXXo, world"};
     298        str4.replace(2, 2, 2, 'l');
     299        test_eq(
     300            "replace with repeated characters",
     301            str4.begin(), str4.end(),
     302            check.begin(), check.end()
     303        );
     304
     305        std::string str5{"heXXXXo, world"};
     306        str5.replace(2, 4, 2, 'l');
     307        test_eq(
     308            "replace with repeated characters (shrinking)",
     309            str5.begin(), str5.end(),
     310            check.begin(), check.end()
     311        );
     312
     313        std::string str6{"helXXXXX world"};
     314        str6.replace(3, 5, "YYlo,YYY", 2, 3);
     315        test_eq(
     316            "replace with substring of a string (shrinking)",
     317            str6.begin(), str6.end(),
     318            check.begin(), check.end()
     319        );
     320
     321        std::string str7{"helXXXXX world"};
     322        std::string replacer{"YYlo,YYY"};
     323        str7.replace(3, 5, replacer, 2, 3);
     324        test_eq(
     325            "replace with substring of a string (shrinking, std::string)",
     326            str7.begin(), str7.end(),
     327            check.begin(), check.end()
     328        );
     329
     330        std::string str8{"helXXXXX world"};
     331        str8.replace(str8.begin() + 3, str8.begin() + 8, "lo,");
     332        test_eq(
     333            "replace with a string (iterators)",
     334            str8.begin(), str8.end(),
     335            check.begin(), check.end()
     336        );
     337
     338        std::string str9{"heXXXXo, world"};
     339        str9.replace(str9.begin() + 2, str9.begin() + 6, 2, 'l');
     340        test_eq(
     341            "replace with repeated characters (shrinking, iterators)",
     342            str9.begin(), str9.end(),
     343            check.begin(), check.end()
     344        );
     345
     346        std::string str10{"helXXXXX world"};
     347        str10.replace(str10.begin() + 3, str10.begin() + 8,
     348                      replacer.begin() + 2, replacer.begin() + 5);
     349        test_eq(
     350            "replace with substring of a string (shrinking, iterators x2)",
     351            str10.begin(), str10.end(),
     352            check.begin(), check.end()
     353        );
     354
     355        std::string str11{"helXXXXX world"};
     356        str11.replace(str11.begin() + 3, str11.begin() + 8,
     357                      {'l', 'o', ','});
     358        test_eq(
     359            "replace with an initializer list (shrinking, iterators)",
     360            str11.begin(), str11.end(),
     361            check.begin(), check.end()
     362        );
     363
     364        std::string str12{"helXXX world"};
     365        str12.replace(str12.begin() + 3, str12.begin() + 6,
     366                      {'l', 'o', ','});
     367        test_eq(
     368            "replace with an initializer list (iterators)",
     369            str12.begin(), str12.end(),
     370            check.begin(), check.end()
     371        );
     372    }
    267373}
Note: See TracChangeset for help on using the changeset viewer.