LCOV - code coverage report
Current view: top level - sal/qa/rtl/strings - test_strings_replace.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 116 116 100.0 %
Date: 2014-11-03 Functions: 16 16 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          48 : 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           4 :     CPPUNIT_TEST_SUITE(Test);
      42           2 :     CPPUNIT_TEST(stringReplaceFirst);
      43           2 :     CPPUNIT_TEST(stringReplaceAll);
      44           2 :     CPPUNIT_TEST(ustringReplaceFirst);
      45           2 :     CPPUNIT_TEST(ustringReplaceFirstAsciiL);
      46           2 :     CPPUNIT_TEST(ustringReplaceFirstAsciiLAsciiL);
      47           2 :     CPPUNIT_TEST(ustringReplaceAll);
      48           2 :     CPPUNIT_TEST(ustringReplaceAllAsciiL);
      49           2 :     CPPUNIT_TEST(ustringReplaceAllAsciiLAsciiL);
      50           4 :     CPPUNIT_TEST_SUITE_END();
      51             : };
      52             : 
      53           2 : void Test::stringReplaceFirst() {
      54           4 :     CPPUNIT_ASSERT_EQUAL(
      55             :         rtl::OString("otherbarfoo"),
      56           2 :         rtl::OString("foobarfoo").replaceFirst("foo", "other"));
      57             : 
      58           4 :     CPPUNIT_ASSERT_EQUAL(
      59             :         rtl::OString("foobarfoo"),
      60           2 :         rtl::OString("foobarfoo").replaceFirst("bars", "other"));
      61             : 
      62             :     {
      63           2 :         sal_Int32 n = 0;
      64           4 :         CPPUNIT_ASSERT_EQUAL(
      65             :             rtl::OString("otherbarfoo"),
      66           2 :             rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
      67           2 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
      68             :     }
      69             : 
      70             :     {
      71           2 :         sal_Int32 n = 1;
      72           4 :         CPPUNIT_ASSERT_EQUAL(
      73             :             rtl::OString("foobarother"),
      74           2 :             rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
      75           2 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
      76             :     }
      77             : 
      78             :     {
      79           2 :         sal_Int32 n = 4;
      80           4 :         CPPUNIT_ASSERT_EQUAL(
      81             :             rtl::OString("foobarfoo"),
      82           2 :             rtl::OString("foobarfoo").replaceFirst("bar", "other", &n));
      83           2 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
      84             :     }
      85           2 : }
      86             : 
      87           2 : void Test::stringReplaceAll() {
      88           4 :     CPPUNIT_ASSERT_EQUAL(
      89             :         rtl::OString("otherbarother"),
      90           2 :         rtl::OString("foobarfoo").replaceAll("foo", "other"));
      91             : 
      92           4 :     CPPUNIT_ASSERT_EQUAL(
      93             :         rtl::OString("foobarfoo"),
      94           2 :         rtl::OString("foobarfoo").replaceAll("bars", "other"));
      95             : 
      96           4 :     CPPUNIT_ASSERT_EQUAL(
      97           2 :         rtl::OString("xxa"), rtl::OString("xaa").replaceAll("xa", "xx"));
      98           2 : }
      99             : 
     100           2 : void Test::ustringReplaceFirst() {
     101           4 :     CPPUNIT_ASSERT_EQUAL(
     102             :         rtl::OUString("otherbarfoo"),
     103             :         rtl::OUString("foobarfoo").replaceFirst(
     104             :             rtl::OUString("foo"),
     105           2 :             rtl::OUString("other")));
     106             : 
     107           4 :     CPPUNIT_ASSERT_EQUAL(
     108             :         rtl::OUString("foobarfoo"),
     109             :         rtl::OUString("foobarfoo").replaceFirst(
     110             :             rtl::OUString("bars"),
     111           2 :             rtl::OUString("other")));
     112             : 
     113             :     {
     114           2 :         sal_Int32 n = 0;
     115           4 :         CPPUNIT_ASSERT_EQUAL(
     116             :             rtl::OUString("otherbarfoo"),
     117             :             (rtl::OUString("foobarfoo").
     118             :              replaceFirst(
     119             :                  rtl::OUString("foo"),
     120           2 :                  rtl::OUString("other"), &n)));
     121           2 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
     122             :     }
     123             : 
     124             :     {
     125           2 :         sal_Int32 n = 1;
     126           4 :         CPPUNIT_ASSERT_EQUAL(
     127             :             rtl::OUString("foobarother"),
     128             :             (rtl::OUString("foobarfoo").
     129             :              replaceFirst(
     130             :                  rtl::OUString("foo"),
     131           2 :                  rtl::OUString("other"), &n)));
     132           2 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
     133             :     }
     134             : 
     135             :     {
     136           2 :         sal_Int32 n = 4;
     137           4 :         CPPUNIT_ASSERT_EQUAL(
     138             :             rtl::OUString("foobarfoo"),
     139             :             (rtl::OUString("foobarfoo").
     140             :              replaceFirst(
     141             :                  rtl::OUString("bar"),
     142           2 :                  rtl::OUString("other"), &n)));
     143           2 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
     144             :     }
     145           2 : }
     146             : 
     147           2 : void Test::ustringReplaceFirstAsciiL() {
     148           4 :     CPPUNIT_ASSERT_EQUAL(
     149             :         rtl::OUString("otherbarfoo"),
     150             :         (rtl::OUString("foobarfoo").
     151             :          replaceFirst("foo",
     152           2 :              rtl::OUString("other"))));
     153             : 
     154           4 :     CPPUNIT_ASSERT_EQUAL(
     155             :         rtl::OUString("foobarfoo"),
     156             :         (rtl::OUString("foobarfoo").
     157             :          replaceFirst("bars",
     158           2 :              rtl::OUString("other"))));
     159             : 
     160             :     {
     161           2 :         sal_Int32 n = 0;
     162           4 :         CPPUNIT_ASSERT_EQUAL(
     163             :             rtl::OUString("otherbarfoo"),
     164             :             (rtl::OUString("foobarfoo").
     165             :              replaceFirst("foo",
     166           2 :                  rtl::OUString("other"), &n)));
     167           2 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
     168             :     }
     169             : 
     170             :     {
     171           2 :         sal_Int32 n = 1;
     172           4 :         CPPUNIT_ASSERT_EQUAL(
     173             :             rtl::OUString("foobarother"),
     174             :             (rtl::OUString("foobarfoo").
     175             :              replaceFirst("foo",
     176           2 :                  rtl::OUString("other"), &n)));
     177           2 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
     178             :     }
     179             : 
     180             :     {
     181           2 :         sal_Int32 n = 4;
     182           4 :         CPPUNIT_ASSERT_EQUAL(
     183             :             rtl::OUString("foobarfoo"),
     184             :             (rtl::OUString("foobarfoo").
     185             :              replaceFirst("bar",
     186           2 :                  rtl::OUString("other"), &n)));
     187           2 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
     188             :     }
     189           2 : }
     190             : 
     191           2 : void Test::ustringReplaceFirstAsciiLAsciiL() {
     192           4 :     CPPUNIT_ASSERT_EQUAL(
     193             :         rtl::OUString("otherbarfoo"),
     194             :         (rtl::OUString("foobarfoo").
     195           2 :          replaceFirst("foo", "other")));
     196             : 
     197           4 :     CPPUNIT_ASSERT_EQUAL(
     198             :         rtl::OUString("foobarfoo"),
     199             :         (rtl::OUString("foobarfoo").
     200           2 :          replaceFirst("bars", "other")));
     201             : 
     202             :     {
     203           2 :         sal_Int32 n = 0;
     204           4 :         CPPUNIT_ASSERT_EQUAL(
     205             :             rtl::OUString("otherbarfoo"),
     206             :             (rtl::OUString("foobarfoo").
     207           2 :              replaceFirst("foo", "other", &n)));
     208           2 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
     209             :     }
     210             : 
     211             :     {
     212           2 :         sal_Int32 n = 1;
     213           4 :         CPPUNIT_ASSERT_EQUAL(
     214             :             rtl::OUString("foobarother"),
     215             :             (rtl::OUString("foobarfoo").
     216           2 :              replaceFirst("foo", "other", &n)));
     217           2 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
     218             :     }
     219             : 
     220             :     {
     221           2 :         sal_Int32 n = 4;
     222           4 :         CPPUNIT_ASSERT_EQUAL(
     223             :             rtl::OUString("foobarfoo"),
     224             :             (rtl::OUString("foobarfoo").
     225           2 :              replaceFirst("bar", "other", &n)));
     226           2 :         CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
     227             :     }
     228           2 : }
     229             : 
     230           2 : void Test::ustringReplaceAll() {
     231           4 :     CPPUNIT_ASSERT_EQUAL(
     232             :         rtl::OUString("otherbarother"),
     233             :         rtl::OUString("foobarfoo").replaceAll(
     234             :             rtl::OUString("foo"),
     235           2 :             rtl::OUString("other")));
     236             : 
     237           4 :     CPPUNIT_ASSERT_EQUAL(
     238             :         rtl::OUString("foobarfoo"),
     239             :         rtl::OUString("foobarfoo").replaceAll(
     240             :             rtl::OUString("bars"),
     241           2 :             rtl::OUString("other")));
     242             : 
     243           4 :     CPPUNIT_ASSERT_EQUAL(
     244             :         rtl::OUString("xxa"),
     245             :         rtl::OUString("xaa").replaceAll(
     246             :             rtl::OUString("xa"),
     247           2 :             rtl::OUString("xx")));
     248           2 : }
     249             : 
     250           2 : void Test::ustringReplaceAllAsciiL() {
     251           4 :     CPPUNIT_ASSERT_EQUAL(
     252             :         rtl::OUString("otherbarother"),
     253             :         (rtl::OUString("foobarfoo").
     254             :          replaceAll("foo",
     255           2 :              rtl::OUString("other"))));
     256             : 
     257           4 :     CPPUNIT_ASSERT_EQUAL(
     258             :         rtl::OUString("foobarfoo"),
     259             :         (rtl::OUString("foobarfoo").
     260             :          replaceAll("bars",
     261           2 :              rtl::OUString("other"))));
     262             : 
     263           4 :     CPPUNIT_ASSERT_EQUAL(
     264             :         rtl::OUString("xxa"),
     265             :         rtl::OUString("xaa").replaceAll(
     266             :             "xa",
     267           2 :             rtl::OUString("xx")));
     268           2 : }
     269             : 
     270           2 : void Test::ustringReplaceAllAsciiLAsciiL() {
     271           4 :     CPPUNIT_ASSERT_EQUAL(
     272             :         rtl::OUString("otherbarother"),
     273             :         (rtl::OUString("foobarfoo").
     274           2 :          replaceAll("foo", "other")));
     275             : 
     276           4 :     CPPUNIT_ASSERT_EQUAL(
     277             :         rtl::OUString("foobarfoo"),
     278             :         (rtl::OUString("foobarfoo").
     279           2 :          replaceAll("bars", "other")));
     280             : 
     281           4 :     CPPUNIT_ASSERT_EQUAL(
     282             :         rtl::OUString("xxa"),
     283             :         (rtl::OUString("xaa").
     284           2 :          replaceAll("xa", "xx")));
     285           2 : }
     286             : 
     287             : }
     288             : 
     289           6 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
     290             : 
     291             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10