LCOV - code coverage report
Current view: top level - sal/qa/rtl/strings - test_strings_replace.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 116 116 100.0 %
Date: 2015-06-13 12:38:46 Functions: 19 19 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  */
       9             : 
      10             : #include "sal/config.h"
      11             : 
      12             : #include <sal/types.h>
      13             : #include "cppunit/TestAssert.h"
      14             : #include "cppunit/TestFixture.h"
      15             : #include "cppunit/extensions/HelperMacros.h"
      16             : #include "rtl/string.h"
      17             : #include "rtl/string.hxx"
      18             : #include "rtl/ustring.h"
      19             : #include "rtl/ustring.hxx"
      20             : 
      21             : namespace {
      22             : 
      23          24 : class Test: public CppUnit::TestFixture {
      24             : private:
      25             :     void stringReplaceFirst();
      26             : 
      27             :     void stringReplaceAll();
      28             : 
      29             :     void ustringReplaceFirst();
      30             : 
      31             :     void ustringReplaceFirstAsciiL();
      32             : 
      33             :     void ustringReplaceFirstAsciiLAsciiL();
      34             : 
      35             :     void ustringReplaceAll();
      36             : 
      37             :     void ustringReplaceAllAsciiL();
      38             : 
      39             :     void ustringReplaceAllAsciiLAsciiL();
      40             : 
      41           2 :     CPPUNIT_TEST_SUITE(Test);
      42           1 :     CPPUNIT_TEST(stringReplaceFirst);
      43           1 :     CPPUNIT_TEST(stringReplaceAll);
      44           1 :     CPPUNIT_TEST(ustringReplaceFirst);
      45           1 :     CPPUNIT_TEST(ustringReplaceFirstAsciiL);
      46           1 :     CPPUNIT_TEST(ustringReplaceFirstAsciiLAsciiL);
      47           1 :     CPPUNIT_TEST(ustringReplaceAll);
      48           1 :     CPPUNIT_TEST(ustringReplaceAllAsciiL);
      49           1 :     CPPUNIT_TEST(ustringReplaceAllAsciiLAsciiL);
      50           5 :     CPPUNIT_TEST_SUITE_END();
      51             : };
      52             : 
      53           1 : void Test::stringReplaceFirst() {
      54           2 :     CPPUNIT_ASSERT_EQUAL(
      55             :         rtl::OString("otherbarfoo"),
      56           1 :         rtl::OString("foobarfoo").replaceFirst("foo", "other"));
      57             : 
      58           2 :     CPPUNIT_ASSERT_EQUAL(
      59             :         rtl::OString("foobarfoo"),
      60           1 :         rtl::OString("foobarfoo").replaceFirst("bars", "other"));
      61             : 
      62             :     {
      63           1 :         sal_Int32 n = 0;
      64           2 :         CPPUNIT_ASSERT_EQUAL(
      65             :             rtl::OString("otherbarfoo"),
      66           1 :             rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
      67           1 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
      68             :     }
      69             : 
      70             :     {
      71           1 :         sal_Int32 n = 1;
      72           2 :         CPPUNIT_ASSERT_EQUAL(
      73             :             rtl::OString("foobarother"),
      74           1 :             rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
      75           1 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
      76             :     }
      77             : 
      78             :     {
      79           1 :         sal_Int32 n = 4;
      80           2 :         CPPUNIT_ASSERT_EQUAL(
      81             :             rtl::OString("foobarfoo"),
      82           1 :             rtl::OString("foobarfoo").replaceFirst("bar", "other", &n));
      83           1 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
      84             :     }
      85           1 : }
      86             : 
      87           1 : void Test::stringReplaceAll() {
      88           2 :     CPPUNIT_ASSERT_EQUAL(
      89             :         rtl::OString("otherbarother"),
      90           1 :         rtl::OString("foobarfoo").replaceAll("foo", "other"));
      91             : 
      92           2 :     CPPUNIT_ASSERT_EQUAL(
      93             :         rtl::OString("foobarfoo"),
      94           1 :         rtl::OString("foobarfoo").replaceAll("bars", "other"));
      95             : 
      96           2 :     CPPUNIT_ASSERT_EQUAL(
      97           1 :         rtl::OString("xxa"), rtl::OString("xaa").replaceAll("xa", "xx"));
      98           1 : }
      99             : 
     100           1 : void Test::ustringReplaceFirst() {
     101           2 :     CPPUNIT_ASSERT_EQUAL(
     102             :         rtl::OUString("otherbarfoo"),
     103             :         rtl::OUString("foobarfoo").replaceFirst(
     104             :             rtl::OUString("foo"),
     105           1 :             rtl::OUString("other")));
     106             : 
     107           2 :     CPPUNIT_ASSERT_EQUAL(
     108             :         rtl::OUString("foobarfoo"),
     109             :         rtl::OUString("foobarfoo").replaceFirst(
     110             :             rtl::OUString("bars"),
     111           1 :             rtl::OUString("other")));
     112             : 
     113             :     {
     114           1 :         sal_Int32 n = 0;
     115           2 :         CPPUNIT_ASSERT_EQUAL(
     116             :             rtl::OUString("otherbarfoo"),
     117             :             (rtl::OUString("foobarfoo").
     118             :              replaceFirst(
     119             :                  rtl::OUString("foo"),
     120           1 :                  rtl::OUString("other"), &n)));
     121           1 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
     122             :     }
     123             : 
     124             :     {
     125           1 :         sal_Int32 n = 1;
     126           2 :         CPPUNIT_ASSERT_EQUAL(
     127             :             rtl::OUString("foobarother"),
     128             :             (rtl::OUString("foobarfoo").
     129             :              replaceFirst(
     130             :                  rtl::OUString("foo"),
     131           1 :                  rtl::OUString("other"), &n)));
     132           1 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
     133             :     }
     134             : 
     135             :     {
     136           1 :         sal_Int32 n = 4;
     137           2 :         CPPUNIT_ASSERT_EQUAL(
     138             :             rtl::OUString("foobarfoo"),
     139             :             (rtl::OUString("foobarfoo").
     140             :              replaceFirst(
     141             :                  rtl::OUString("bar"),
     142           1 :                  rtl::OUString("other"), &n)));
     143           1 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
     144             :     }
     145           1 : }
     146             : 
     147           1 : void Test::ustringReplaceFirstAsciiL() {
     148           2 :     CPPUNIT_ASSERT_EQUAL(
     149             :         rtl::OUString("otherbarfoo"),
     150             :         (rtl::OUString("foobarfoo").
     151             :          replaceFirst("foo",
     152           1 :              rtl::OUString("other"))));
     153             : 
     154           2 :     CPPUNIT_ASSERT_EQUAL(
     155             :         rtl::OUString("foobarfoo"),
     156             :         (rtl::OUString("foobarfoo").
     157             :          replaceFirst("bars",
     158           1 :              rtl::OUString("other"))));
     159             : 
     160             :     {
     161           1 :         sal_Int32 n = 0;
     162           2 :         CPPUNIT_ASSERT_EQUAL(
     163             :             rtl::OUString("otherbarfoo"),
     164             :             (rtl::OUString("foobarfoo").
     165             :              replaceFirst("foo",
     166           1 :                  rtl::OUString("other"), &n)));
     167           1 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
     168             :     }
     169             : 
     170             :     {
     171           1 :         sal_Int32 n = 1;
     172           2 :         CPPUNIT_ASSERT_EQUAL(
     173             :             rtl::OUString("foobarother"),
     174             :             (rtl::OUString("foobarfoo").
     175             :              replaceFirst("foo",
     176           1 :                  rtl::OUString("other"), &n)));
     177           1 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
     178             :     }
     179             : 
     180             :     {
     181           1 :         sal_Int32 n = 4;
     182           2 :         CPPUNIT_ASSERT_EQUAL(
     183             :             rtl::OUString("foobarfoo"),
     184             :             (rtl::OUString("foobarfoo").
     185             :              replaceFirst("bar",
     186           1 :                  rtl::OUString("other"), &n)));
     187           1 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
     188             :     }
     189           1 : }
     190             : 
     191           1 : void Test::ustringReplaceFirstAsciiLAsciiL() {
     192           2 :     CPPUNIT_ASSERT_EQUAL(
     193             :         rtl::OUString("otherbarfoo"),
     194             :         (rtl::OUString("foobarfoo").
     195           1 :          replaceFirst("foo", "other")));
     196             : 
     197           2 :     CPPUNIT_ASSERT_EQUAL(
     198             :         rtl::OUString("foobarfoo"),
     199             :         (rtl::OUString("foobarfoo").
     200           1 :          replaceFirst("bars", "other")));
     201             : 
     202             :     {
     203           1 :         sal_Int32 n = 0;
     204           2 :         CPPUNIT_ASSERT_EQUAL(
     205             :             rtl::OUString("otherbarfoo"),
     206             :             (rtl::OUString("foobarfoo").
     207           1 :              replaceFirst("foo", "other", &n)));
     208           1 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
     209             :     }
     210             : 
     211             :     {
     212           1 :         sal_Int32 n = 1;
     213           2 :         CPPUNIT_ASSERT_EQUAL(
     214             :             rtl::OUString("foobarother"),
     215             :             (rtl::OUString("foobarfoo").
     216           1 :              replaceFirst("foo", "other", &n)));
     217           1 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
     218             :     }
     219             : 
     220             :     {
     221           1 :         sal_Int32 n = 4;
     222           2 :         CPPUNIT_ASSERT_EQUAL(
     223             :             rtl::OUString("foobarfoo"),
     224             :             (rtl::OUString("foobarfoo").
     225           1 :              replaceFirst("bar", "other", &n)));
     226           1 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
     227             :     }
     228           1 : }
     229             : 
     230           1 : void Test::ustringReplaceAll() {
     231           2 :     CPPUNIT_ASSERT_EQUAL(
     232             :         rtl::OUString("otherbarother"),
     233             :         rtl::OUString("foobarfoo").replaceAll(
     234             :             rtl::OUString("foo"),
     235           1 :             rtl::OUString("other")));
     236             : 
     237           2 :     CPPUNIT_ASSERT_EQUAL(
     238             :         rtl::OUString("foobarfoo"),
     239             :         rtl::OUString("foobarfoo").replaceAll(
     240             :             rtl::OUString("bars"),
     241           1 :             rtl::OUString("other")));
     242             : 
     243           2 :     CPPUNIT_ASSERT_EQUAL(
     244             :         rtl::OUString("xxa"),
     245             :         rtl::OUString("xaa").replaceAll(
     246             :             rtl::OUString("xa"),
     247           1 :             rtl::OUString("xx")));
     248           1 : }
     249             : 
     250           1 : void Test::ustringReplaceAllAsciiL() {
     251           2 :     CPPUNIT_ASSERT_EQUAL(
     252             :         rtl::OUString("otherbarother"),
     253             :         (rtl::OUString("foobarfoo").
     254             :          replaceAll("foo",
     255           1 :              rtl::OUString("other"))));
     256             : 
     257           2 :     CPPUNIT_ASSERT_EQUAL(
     258             :         rtl::OUString("foobarfoo"),
     259             :         (rtl::OUString("foobarfoo").
     260             :          replaceAll("bars",
     261           1 :              rtl::OUString("other"))));
     262             : 
     263           2 :     CPPUNIT_ASSERT_EQUAL(
     264             :         rtl::OUString("xxa"),
     265             :         rtl::OUString("xaa").replaceAll(
     266             :             "xa",
     267           1 :             rtl::OUString("xx")));
     268           1 : }
     269             : 
     270           1 : void Test::ustringReplaceAllAsciiLAsciiL() {
     271           2 :     CPPUNIT_ASSERT_EQUAL(
     272             :         rtl::OUString("otherbarother"),
     273             :         (rtl::OUString("foobarfoo").
     274           1 :          replaceAll("foo", "other")));
     275             : 
     276           2 :     CPPUNIT_ASSERT_EQUAL(
     277             :         rtl::OUString("foobarfoo"),
     278             :         (rtl::OUString("foobarfoo").
     279           1 :          replaceAll("bars", "other")));
     280             : 
     281           2 :     CPPUNIT_ASSERT_EQUAL(
     282             :         rtl::OUString("xxa"),
     283             :         (rtl::OUString("xaa").
     284           1 :          replaceAll("xa", "xx")));
     285           1 : }
     286             : 
     287             : }
     288             : 
     289           3 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
     290             : 
     291             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11