| 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 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 11 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 |
* contributor license agreements. See the NOTICE file distributed |
| 13 |
* with this work for additional information regarding copyright |
| 14 |
* ownership. The ASF licenses this file to you under the Apache |
| 15 |
* License, Version 2.0 (the "License"); you may not use this file |
| 16 |
* except in compliance with the License. You may obtain a copy of |
| 17 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 |
*/ |
| 19 |
|
| 20 |
// autogenerated file with codegen.pl |
| 21 |
|
| 22 |
#include <memory> |
| 23 |
#include <math.h> |
| 24 |
#include <stdio.h> |
| 25 |
|
| 26 |
#include <cppunit/TestFixture.h> |
| 27 |
#include <cppunit/extensions/HelperMacros.h> |
| 28 |
|
| 29 |
#include <config_options.h> |
| 30 |
#include <o3tl/cppunittraitshelper.hxx> |
| 31 |
#include <o3tl/safeint.hxx> |
| 32 |
|
| 33 |
#include <stringhelper.hxx> |
| 34 |
#include <valueequal.hxx> |
| 35 |
|
| 36 |
namespace rtl_OUString |
| 37 |
{ |
| 38 |
|
| 39 |
namespace { |
| 40 |
|
| 41 |
float doubleToFloat(double x) { |
| 42 |
return static_cast<float>(x); |
| 43 |
} |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
class number : public CppUnit::TestFixture |
| 48 |
{ |
| 49 |
void number_float_test_impl(float _nValue) |
| 50 |
{ |
| 51 |
OUString suValue(OUString::number(_nValue)); |
| 52 |
OString sValue; |
| 53 |
sValue <<= suValue; |
| 54 |
printf("nFloat := %.9f sValue := %s\n", _nValue, sValue.getStr()); |
| 55 |
|
| 56 |
double nValueATOF = doubleToFloat(atof( sValue.getStr() )); |
| 57 |
|
| 58 |
bool bEqualResult = is_float_equal(_nValue, nValueATOF); |
| 59 |
CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult); |
| 60 |
} |
| 61 |
|
| 62 |
void number_float_test(float _nValue) |
| 63 |
{ |
| 64 |
number_float_test_impl(_nValue); |
| 65 |
|
| 66 |
// test also the negative part. |
| 67 |
float nNegativeValue = -_nValue; |
| 68 |
number_float_test_impl(nNegativeValue); |
| 69 |
} |
| 70 |
|
| 71 |
public: |
| 72 |
// insert your test code here. |
| 73 |
void number_float_test_001() |
| 74 |
{ |
| 75 |
// this is demonstration code |
| 76 |
// CPPUNIT_ASSERT_MESSAGE("a message", 1 == 1); |
| 77 |
float nValue = 3.0f; |
| 78 |
number_float_test(nValue); |
| 79 |
} |
| 80 |
|
| 81 |
void number_float_test_002() |
| 82 |
{ |
| 83 |
float nValue = 3.5f; |
| 84 |
number_float_test(nValue); |
| 85 |
} |
| 86 |
|
| 87 |
void number_float_test_003() |
| 88 |
{ |
| 89 |
float nValue = 3.0625f; |
| 90 |
number_float_test(nValue); |
| 91 |
} |
| 92 |
|
| 93 |
void number_float_test_004() |
| 94 |
{ |
| 95 |
float nValue = 3.502525f; |
| 96 |
number_float_test(nValue); |
| 97 |
} |
| 98 |
|
| 99 |
void number_float_test_005() |
| 100 |
{ |
| 101 |
float nValue = 3.141592f; |
| 102 |
number_float_test(nValue); |
| 103 |
} |
| 104 |
|
| 105 |
void number_float_test_006() |
| 106 |
{ |
| 107 |
float nValue = 3.5025255f; |
| 108 |
number_float_test(nValue); |
| 109 |
} |
| 110 |
|
| 111 |
void number_float_test_007() |
| 112 |
{ |
| 113 |
float nValue = 3.0039062f; |
| 114 |
number_float_test(nValue); |
| 115 |
} |
| 116 |
|
| 117 |
private: |
| 118 |
|
| 119 |
void number_double_test_impl(double _nValue) |
| 120 |
{ |
| 121 |
OUString suValue = OUString::number( _nValue ); |
| 122 |
OString sValue; |
| 123 |
sValue <<= suValue; |
| 124 |
printf("nDouble := %.20f sValue := %s\n", _nValue, sValue.getStr()); |
| 125 |
|
| 126 |
double nValueATOF = atof( sValue.getStr() ); |
| 127 |
|
| 128 |
bool bEqualResult = is_double_equal(_nValue, nValueATOF); |
| 129 |
CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult); |
| 130 |
} |
| 131 |
|
| 132 |
void number_double_test(double _nValue) |
| 133 |
{ |
| 134 |
number_double_test_impl(_nValue); |
| 135 |
|
| 136 |
// test also the negative part. |
| 137 |
double nNegativeValue = -_nValue; |
| 138 |
number_double_test_impl(nNegativeValue); |
| 139 |
} |
| 140 |
public: |
| 141 |
|
| 142 |
// number double |
| 143 |
void number_double_test_001() |
| 144 |
{ |
| 145 |
double nValue = 3.0; |
| 146 |
number_double_test(nValue); |
| 147 |
} |
| 148 |
void number_double_test_002() |
| 149 |
{ |
| 150 |
double nValue = 3.5; |
| 151 |
number_double_test(nValue); |
| 152 |
} |
| 153 |
void number_double_test_003() |
| 154 |
{ |
| 155 |
double nValue = 3.0625; |
| 156 |
number_double_test(nValue); |
| 157 |
} |
| 158 |
void number_double_test_004() |
| 159 |
{ |
| 160 |
double nValue = 3.1415926535; |
| 161 |
number_double_test(nValue); |
| 162 |
} |
| 163 |
void number_double_test_005() |
| 164 |
{ |
| 165 |
double nValue = 3.141592653589793; |
| 166 |
number_double_test(nValue); |
| 167 |
} |
| 168 |
void number_double_test_006() |
| 169 |
{ |
| 170 |
double nValue = 3.1415926535897932; |
| 171 |
number_double_test(nValue); |
| 172 |
} |
| 173 |
void number_double_test_007() |
| 174 |
{ |
| 175 |
double nValue = 3.14159265358979323; |
| 176 |
number_double_test(nValue); |
| 177 |
} |
| 178 |
void number_double_test_008() |
| 179 |
{ |
| 180 |
double nValue = 3.141592653589793238462643; |
| 181 |
number_double_test(nValue); |
| 182 |
} |
| 183 |
|
| 184 |
// Change the following lines only, if you add, remove or rename |
| 185 |
// member functions of the current class, |
| 186 |
// because these macros are need by auto register mechanism. |
| 187 |
|
| 188 |
CPPUNIT_TEST_SUITE(number); |
| 189 |
CPPUNIT_TEST(number_float_test_001); |
| 190 |
CPPUNIT_TEST(number_float_test_002); |
| 191 |
CPPUNIT_TEST(number_float_test_003); |
| 192 |
CPPUNIT_TEST(number_float_test_004); |
| 193 |
CPPUNIT_TEST(number_float_test_005); |
| 194 |
CPPUNIT_TEST(number_float_test_006); |
| 195 |
CPPUNIT_TEST(number_float_test_007); |
| 196 |
|
| 197 |
CPPUNIT_TEST(number_double_test_001); |
| 198 |
CPPUNIT_TEST(number_double_test_002); |
| 199 |
CPPUNIT_TEST(number_double_test_003); |
| 200 |
CPPUNIT_TEST(number_double_test_004); |
| 201 |
CPPUNIT_TEST(number_double_test_005); |
| 202 |
CPPUNIT_TEST(number_double_test_006); |
| 203 |
CPPUNIT_TEST(number_double_test_007); |
| 204 |
CPPUNIT_TEST(number_double_test_008); |
| 205 |
CPPUNIT_TEST_SUITE_END(); |
| 206 |
}; // class number |
| 207 |
|
| 208 |
class toInt: public CppUnit::TestFixture { |
| 209 |
public: |
| 210 |
void test() { |
| 211 |
CPPUNIT_ASSERT_EQUAL( |
| 212 |
static_cast< sal_Int32 >(-0x76543210), |
| 213 |
(u"-76543210"_ustr. |
| 214 |
toInt32(16))); |
| 215 |
// @return 0 if this string represents no number or one of too large magnitude |
| 216 |
CPPUNIT_ASSERT_EQUAL( |
| 217 |
static_cast< sal_Int32 >(0), |
| 218 |
(u"+FEDCBA98"_ustr. |
| 219 |
toInt32(16))); |
| 220 |
CPPUNIT_ASSERT_EQUAL( |
| 221 |
static_cast< sal_Int64 >(-SAL_CONST_INT64(0x76543210FEDCBA98)), |
| 222 |
(u"-76543210FEDCBA98"_ustr. |
| 223 |
toInt64(16))); |
| 224 |
// @return 0 if this string represents no number or one of too large magnitude |
| 225 |
CPPUNIT_ASSERT_EQUAL( |
| 226 |
static_cast< sal_Int64 >(SAL_CONST_INT64(0)), |
| 227 |
(u"+FEDCBA9876543210"_ustr. |
| 228 |
toInt64(16))); |
| 229 |
} |
| 230 |
|
| 231 |
CPPUNIT_TEST_SUITE(toInt); |
| 232 |
CPPUNIT_TEST(test); |
| 233 |
CPPUNIT_TEST_SUITE_END(); |
| 234 |
}; |
| 235 |
|
| 236 |
// - toDouble (tests) |
| 237 |
class toDouble : public CppUnit::TestFixture |
| 238 |
{ |
| 239 |
public: |
| 240 |
void toDouble_test_impl(OString const& _sValue) |
| 241 |
{ |
| 242 |
//printf("the original str is %s\n", _sValue.getStr()); |
| 243 |
double nValueATOF = atof( _sValue.getStr() ); |
| 244 |
//printf("original data is %e\n", nValueATOF); |
| 245 |
OUString suValue = OUString::createFromAscii( _sValue.getStr() ); |
| 246 |
double nValueToDouble = suValue.toDouble(); |
| 247 |
//printf("result data is %e\n", nValueToDouble); |
| 248 |
|
| 249 |
bool bEqualResult = is_double_equal(nValueToDouble, nValueATOF); |
| 250 |
CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult); |
| 251 |
} |
| 252 |
|
| 253 |
void toDouble_test(OString const& _sValue) |
| 254 |
{ |
| 255 |
toDouble_test_impl(_sValue); |
| 256 |
|
| 257 |
// test also the negative part. |
| 258 |
OString sNegativValue("-"_ostr); |
| 259 |
sNegativValue += _sValue; |
| 260 |
toDouble_test_impl(sNegativValue); |
| 261 |
} |
| 262 |
|
| 263 |
// insert your test code here. |
| 264 |
void toDouble_selftest() |
| 265 |
{ |
| 266 |
printf("Start selftest:\n"); |
| 267 |
CPPUNIT_ASSERT (!is_double_equal(1.0, 1.01)); |
| 268 |
CPPUNIT_ASSERT (!is_double_equal(1.0, 1.001)); |
| 269 |
CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0001)); |
| 270 |
CPPUNIT_ASSERT (!is_double_equal(1.0, 1.00001)); |
| 271 |
CPPUNIT_ASSERT (!is_double_equal(1.0, 1.000001)); |
| 272 |
CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0000001)); |
| 273 |
CPPUNIT_ASSERT (!is_double_equal(1.0, 1.00000001)); |
| 274 |
CPPUNIT_ASSERT (!is_double_equal(1.0, 1.000000001)); |
| 275 |
CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0000000001)); |
| 276 |
CPPUNIT_ASSERT (!is_double_equal(1.0, 1.00000000001)); |
| 277 |
CPPUNIT_ASSERT (!is_double_equal(1.0, 1.000000000001)); |
| 278 |
CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0000000000001)); |
| 279 |
// we check til 15 values after comma |
| 280 |
CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000000001)); |
| 281 |
CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000000001)); |
| 282 |
CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000000001)); |
| 283 |
printf("Selftest done.\n"); |
| 284 |
} |
| 285 |
|
| 286 |
void toDouble_test_3() |
| 287 |
{ |
| 288 |
toDouble_test("3"_ostr); |
| 289 |
} |
| 290 |
void toDouble_test_3_5() |
| 291 |
{ |
| 292 |
toDouble_test("3.5"_ostr); |
| 293 |
} |
| 294 |
void toDouble_test_3_0625() |
| 295 |
{ |
| 296 |
toDouble_test("3.0625"_ostr); |
| 297 |
} |
| 298 |
void toDouble_test_pi() |
| 299 |
{ |
| 300 |
// value from http://www.angio.net/pi/digits/50.txt |
| 301 |
toDouble_test("3.141592653589793238462643383279502884197169399375"_ostr); |
| 302 |
} |
| 303 |
|
| 304 |
void toDouble_test_1() |
| 305 |
{ |
| 306 |
toDouble_test("1"_ostr); |
| 307 |
} |
| 308 |
void toDouble_test_10() |
| 309 |
{ |
| 310 |
toDouble_test("10"_ostr); |
| 311 |
} |
| 312 |
void toDouble_test_100() |
| 313 |
{ |
| 314 |
toDouble_test("100"_ostr); |
| 315 |
} |
| 316 |
void toDouble_test_1000() |
| 317 |
{ |
| 318 |
toDouble_test("1000"_ostr); |
| 319 |
} |
| 320 |
void toDouble_test_10000() |
| 321 |
{ |
| 322 |
toDouble_test("10000"_ostr); |
| 323 |
} |
| 324 |
void toDouble_test_1e99() |
| 325 |
{ |
| 326 |
toDouble_test("1e99"_ostr); |
| 327 |
} |
| 328 |
void toDouble_test_1e_n99() |
| 329 |
{ |
| 330 |
toDouble_test("1e-99"_ostr); |
| 331 |
} |
| 332 |
void toDouble_test_1e308() |
| 333 |
{ |
| 334 |
toDouble_test("1e308"_ostr); |
| 335 |
} |
| 336 |
|
| 337 |
// Change the following lines only, if you add, remove or rename |
| 338 |
// member functions of the current class, |
| 339 |
// because these macros are need by auto register mechanism. |
| 340 |
|
| 341 |
CPPUNIT_TEST_SUITE(toDouble); |
| 342 |
CPPUNIT_TEST(toDouble_selftest); |
| 343 |
|
| 344 |
CPPUNIT_TEST(toDouble_test_3); |
| 345 |
CPPUNIT_TEST(toDouble_test_3_5); |
| 346 |
CPPUNIT_TEST(toDouble_test_3_0625); |
| 347 |
CPPUNIT_TEST(toDouble_test_pi); |
| 348 |
CPPUNIT_TEST(toDouble_test_1); |
| 349 |
CPPUNIT_TEST(toDouble_test_10); |
| 350 |
CPPUNIT_TEST(toDouble_test_100); |
| 351 |
CPPUNIT_TEST(toDouble_test_1000); |
| 352 |
CPPUNIT_TEST(toDouble_test_10000); |
| 353 |
CPPUNIT_TEST(toDouble_test_1e99); |
| 354 |
CPPUNIT_TEST(toDouble_test_1e_n99); |
| 355 |
CPPUNIT_TEST(toDouble_test_1e308); |
| 356 |
CPPUNIT_TEST_SUITE_END(); |
| 357 |
}; // class toDouble |
| 358 |
|
| 359 |
// - toFloat (tests) |
| 360 |
class toFloat : public CppUnit::TestFixture |
| 361 |
{ |
| 362 |
public: |
| 363 |
void toFloat_test_impl(OString const& _sValue) |
| 364 |
{ |
| 365 |
//printf("the original str is %s\n", _sValue.getStr()); |
| 366 |
float nValueATOF = doubleToFloat(atof( _sValue.getStr() )); |
| 367 |
//printf("the original str is %.10f\n", nValueATOF); |
| 368 |
OUString suValue = OUString::createFromAscii( _sValue.getStr() ); |
| 369 |
float nValueToFloat = suValue.toFloat(); |
| 370 |
//printf("the result str is %.10f\n", nValueToFloat); |
| 371 |
|
| 372 |
bool bEqualResult = is_float_equal(nValueToFloat, nValueATOF); |
| 373 |
CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult); |
| 374 |
} |
| 375 |
|
| 376 |
void toFloat_test(OString const& _sValue) |
| 377 |
{ |
| 378 |
toFloat_test_impl(_sValue); |
| 379 |
|
| 380 |
// test also the negative part. |
| 381 |
OString sNegativValue("-"_ostr); |
| 382 |
sNegativValue += _sValue; |
| 383 |
toFloat_test_impl(sNegativValue); |
| 384 |
} |
| 385 |
|
| 386 |
// insert your test code here. |
| 387 |
void toFloat_selftest() |
| 388 |
{ |
| 389 |
printf("Start selftest:\n"); |
| 390 |
CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.01f)); |
| 391 |
CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.001f)); |
| 392 |
CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.0001f)); |
| 393 |
CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.00001f)); |
| 394 |
CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.000002f)); |
| 395 |
CPPUNIT_ASSERT (is_float_equal(1.0f, 1.0000001f)); |
| 396 |
CPPUNIT_ASSERT (is_float_equal(1.0f, 1.00000001f)); |
| 397 |
CPPUNIT_ASSERT (is_float_equal(1.0f, 1.000000001f)); |
| 398 |
|
| 399 |
printf("Selftest done.\n"); |
| 400 |
} |
| 401 |
|
| 402 |
void toFloat_test_3() |
| 403 |
{ |
| 404 |
toFloat_test("3"_ostr); |
| 405 |
} |
| 406 |
void toFloat_test_3_5() |
| 407 |
{ |
| 408 |
toFloat_test("3.5"_ostr); |
| 409 |
} |
| 410 |
void toFloat_test_3_0625() |
| 411 |
{ |
| 412 |
toFloat_test("3.0625"_ostr); |
| 413 |
} |
| 414 |
void toFloat_test_3_0625_e() |
| 415 |
{ |
| 416 |
toFloat_test("3.0625e-4"_ostr); |
| 417 |
} |
| 418 |
void toFloat_test_pi() |
| 419 |
{ |
| 420 |
// value from http://www.angio.net/pi/digits/50.txt |
| 421 |
toFloat_test("3.141592653589793238462643383279502884197169399375"_ostr); |
| 422 |
} |
| 423 |
|
| 424 |
void toFloat_test_1() |
| 425 |
{ |
| 426 |
toFloat_test("1"_ostr); |
| 427 |
} |
| 428 |
void toFloat_test_10() |
| 429 |
{ |
| 430 |
toFloat_test("10"_ostr); |
| 431 |
} |
| 432 |
void toFloat_test_100() |
| 433 |
{ |
| 434 |
toFloat_test("100"_ostr); |
| 435 |
} |
| 436 |
void toFloat_test_1000() |
| 437 |
{ |
| 438 |
toFloat_test("1000"_ostr); |
| 439 |
} |
| 440 |
void toFloat_test_10000() |
| 441 |
{ |
| 442 |
toFloat_test("10000"_ostr); |
| 443 |
} |
| 444 |
void toFloat_test_mix() |
| 445 |
{ |
| 446 |
toFloat_test("456789321455.123456789012"_ostr); |
| 447 |
} |
| 448 |
void toFloat_test_1e99() |
| 449 |
{ |
| 450 |
toFloat_test("1e99"_ostr); |
| 451 |
} |
| 452 |
void toFloat_test_1e_n99() |
| 453 |
{ |
| 454 |
toFloat_test("1e-9"_ostr); |
| 455 |
} |
| 456 |
void toFloat_test_1e308() |
| 457 |
{ |
| 458 |
toFloat_test("1e308"_ostr); |
| 459 |
} |
| 460 |
|
| 461 |
// Change the following lines only, if you add, remove or rename |
| 462 |
// member functions of the current class, |
| 463 |
// because these macros are need by auto register mechanism. |
| 464 |
|
| 465 |
CPPUNIT_TEST_SUITE(toFloat); |
| 466 |
CPPUNIT_TEST(toFloat_selftest); |
| 467 |
|
| 468 |
CPPUNIT_TEST(toFloat_test_3); |
| 469 |
CPPUNIT_TEST(toFloat_test_3_5); |
| 470 |
CPPUNIT_TEST(toFloat_test_3_0625); |
| 471 |
CPPUNIT_TEST(toFloat_test_3_0625_e); |
| 472 |
CPPUNIT_TEST(toFloat_test_pi); |
| 473 |
CPPUNIT_TEST(toFloat_test_1); |
| 474 |
CPPUNIT_TEST(toFloat_test_10); |
| 475 |
CPPUNIT_TEST(toFloat_test_100); |
| 476 |
CPPUNIT_TEST(toFloat_test_1000); |
| 477 |
CPPUNIT_TEST(toFloat_test_10000); |
| 478 |
CPPUNIT_TEST(toFloat_test_mix); |
| 479 |
CPPUNIT_TEST(toFloat_test_1e99); |
| 480 |
CPPUNIT_TEST(toFloat_test_1e_n99); |
| 481 |
CPPUNIT_TEST(toFloat_test_1e308); |
| 482 |
CPPUNIT_TEST_SUITE_END(); |
| 483 |
}; // class toFloat |
| 484 |
|
| 485 |
// - lastIndexOf (tests) |
| 486 |
class lastIndexOf : public CppUnit::TestFixture |
| 487 |
{ |
| 488 |
|
| 489 |
public: |
| 490 |
void lastIndexOf_oustring(OUString const& _suStr, OUString const& _suSearchStr, sal_Int32 _nExpectedResultPos) |
| 491 |
{ |
| 492 |
// Algorithm |
| 493 |
// search the string _suSearchStr (OUString) in the string _suStr. |
| 494 |
// check if the _nExpectedResultPos occurs. |
| 495 |
|
| 496 |
sal_Int32 nPos = _suStr.lastIndexOf(_suSearchStr); |
| 497 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("expected position is wrong", _nExpectedResultPos, nPos); |
| 498 |
} |
| 499 |
|
| 500 |
void lastIndexOf_salunicode(OUString const& _suStr, sal_Unicode _cuSearchChar, sal_Int32 _nExpectedResultPos) |
| 501 |
{ |
| 502 |
// Algorithm |
| 503 |
// search the unicode char _suSearchChar (sal_Unicode) in the string _suStr. |
| 504 |
// check if the _nExpectedResultPos occurs. |
| 505 |
|
| 506 |
sal_Int32 nPos = _suStr.lastIndexOf(_cuSearchChar); |
| 507 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("expected position is wrong", _nExpectedResultPos, nPos); |
| 508 |
} |
| 509 |
|
| 510 |
void lastIndexOf_oustring_offset(OUString const& _suStr, OUString const& _suSearchStr, sal_Int32 _nExpectedResultPos, sal_Int32 _nStartOffset) |
| 511 |
{ |
| 512 |
sal_Int32 nPos = _suStr.lastIndexOf(_suSearchStr, _nStartOffset); |
| 513 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("expected position is wrong", _nExpectedResultPos, nPos); |
| 514 |
} |
| 515 |
|
| 516 |
void lastIndexOf_salunicode_offset(OUString const& _suStr, sal_Unicode _cuSearchChar, sal_Int32 _nExpectedResultPos, sal_Int32 _nStartOffset) |
| 517 |
{ |
| 518 |
sal_Int32 nPos = _suStr.lastIndexOf(_cuSearchChar, _nStartOffset); |
| 519 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("expected position is wrong", _nExpectedResultPos, nPos); |
| 520 |
} |
| 521 |
|
| 522 |
void lastIndexOf_test_oustring_offset_001() |
| 523 |
{ |
| 524 |
// search for sun, start at the end, found (pos==0) |
| 525 |
OUString aStr(u"sun java system"_ustr); |
| 526 |
lastIndexOf_oustring_offset(aStr, u"sun"_ustr, 0, aStr.getLength()); |
| 527 |
} |
| 528 |
|
| 529 |
void lastIndexOf_test_oustring_offset_002() |
| 530 |
{ |
| 531 |
// search for sun, start at pos = 3, found (pos==0) |
| 532 |
lastIndexOf_oustring_offset(u"sun java system"_ustr, u"sun"_ustr, 0, 3); |
| 533 |
} |
| 534 |
|
| 535 |
void lastIndexOf_test_oustring_offset_003() |
| 536 |
{ |
| 537 |
// search for sun, start at pos = 2, found (pos==-1) |
| 538 |
lastIndexOf_oustring_offset(u"sun java system"_ustr, u"sun"_ustr, -1, 2); |
| 539 |
} |
| 540 |
|
| 541 |
void lastIndexOf_test_oustring_offset_004() |
| 542 |
{ |
| 543 |
// search for sun, start at the end, found (pos==0) |
| 544 |
lastIndexOf_oustring_offset(u"sun java system"_ustr, u"sun"_ustr, -1, 1); |
| 545 |
} |
| 546 |
|
| 547 |
void lastIndexOf_test_oustring_001() |
| 548 |
{ |
| 549 |
// search for sun, found (pos==0) |
| 550 |
lastIndexOf_oustring(u"sun java system"_ustr, u"sun"_ustr, 0); |
| 551 |
} |
| 552 |
|
| 553 |
void lastIndexOf_test_oustring_002() |
| 554 |
{ |
| 555 |
// search for sun, found (pos==4) |
| 556 |
lastIndexOf_oustring(u"the sun java system"_ustr, u"sun"_ustr, 4); |
| 557 |
} |
| 558 |
|
| 559 |
void lastIndexOf_test_oustring_003() |
| 560 |
{ |
| 561 |
// search for sun, found (pos==8) |
| 562 |
lastIndexOf_oustring(u"the sun sun java system"_ustr, u"sun"_ustr, 8); |
| 563 |
} |
| 564 |
|
| 565 |
void lastIndexOf_test_oustring_004() |
| 566 |
{ |
| 567 |
// search for sun, found (pos==8) |
| 568 |
lastIndexOf_oustring(u"the sun sun"_ustr, u"sun"_ustr, 8); |
| 569 |
} |
| 570 |
|
| 571 |
void lastIndexOf_test_oustring_005() |
| 572 |
{ |
| 573 |
// search for sun, found (pos==4) |
| 574 |
lastIndexOf_oustring(u"the sun su"_ustr, u"sun"_ustr, 4); |
| 575 |
} |
| 576 |
|
| 577 |
void lastIndexOf_test_oustring_006() |
| 578 |
{ |
| 579 |
// search for sun, found (pos==-1) |
| 580 |
lastIndexOf_oustring(u"the su su"_ustr, u"sun"_ustr, -1); |
| 581 |
} |
| 582 |
|
| 583 |
void lastIndexOf_test_oustring_007() |
| 584 |
{ |
| 585 |
// search for earth, not found (-1) |
| 586 |
lastIndexOf_oustring(u"the su su"_ustr, u"earth"_ustr, -1); |
| 587 |
} |
| 588 |
|
| 589 |
void lastIndexOf_test_oustring_008() |
| 590 |
{ |
| 591 |
// search for earth, not found (-1) |
| 592 |
lastIndexOf_oustring(u""_ustr, u"earth"_ustr, -1); |
| 593 |
} |
| 594 |
|
| 595 |
void lastIndexOf_test_oustring_009() |
| 596 |
{ |
| 597 |
// search for earth, not found (-1) |
| 598 |
lastIndexOf_oustring(u""_ustr, u""_ustr, -1); |
| 599 |
|
| 600 |
} |
| 601 |
|
| 602 |
void lastIndexOf_test_salunicode_001() |
| 603 |
{ |
| 604 |
// search for 's', found (19) |
| 605 |
sal_Unicode suChar = L's'; |
| 606 |
lastIndexOf_salunicode(u"the sun sun java system"_ustr, suChar, 19); |
| 607 |
} |
| 608 |
|
| 609 |
void lastIndexOf_test_salunicode_002() |
| 610 |
{ |
| 611 |
// search for 'x', not found (-1) |
| 612 |
sal_Unicode suChar = L'x'; |
| 613 |
lastIndexOf_salunicode(u"the sun sun java system"_ustr, suChar, -1); |
| 614 |
} |
| 615 |
|
| 616 |
void lastIndexOf_test_salunicode_offset_001() |
| 617 |
{ |
| 618 |
// search for 's', start from pos last char, found (19) |
| 619 |
OUString aStr(u"the sun sun java system"_ustr); |
| 620 |
sal_Unicode cuChar = L's'; |
| 621 |
lastIndexOf_salunicode_offset(aStr, cuChar, 19, aStr.getLength()); |
| 622 |
} |
| 623 |
void lastIndexOf_test_salunicode_offset_002() |
| 624 |
{ |
| 625 |
// search for 's', start pos is last occur from search behind, found (17) |
| 626 |
sal_Unicode cuChar = L's'; |
| 627 |
lastIndexOf_salunicode_offset(u"the sun sun java system"_ustr, cuChar, 17, 19); |
| 628 |
} |
| 629 |
void lastIndexOf_test_salunicode_offset_003() |
| 630 |
{ |
| 631 |
// search for 't', start pos is 1, found (0) |
| 632 |
sal_Unicode cuChar = L't'; |
| 633 |
lastIndexOf_salunicode_offset(u"the sun sun java system"_ustr, cuChar, 0, 1); |
| 634 |
} |
| 635 |
|
| 636 |
// Change the following lines only, if you add, remove or rename |
| 637 |
// member functions of the current class, |
| 638 |
// because these macros are need by auto register mechanism. |
| 639 |
|
| 640 |
CPPUNIT_TEST_SUITE(lastIndexOf); |
| 641 |
CPPUNIT_TEST(lastIndexOf_test_oustring_001); |
| 642 |
CPPUNIT_TEST(lastIndexOf_test_oustring_002); |
| 643 |
CPPUNIT_TEST(lastIndexOf_test_oustring_003); |
| 644 |
CPPUNIT_TEST(lastIndexOf_test_oustring_004); |
| 645 |
CPPUNIT_TEST(lastIndexOf_test_oustring_005); |
| 646 |
CPPUNIT_TEST(lastIndexOf_test_oustring_006); |
| 647 |
CPPUNIT_TEST(lastIndexOf_test_oustring_007); |
| 648 |
CPPUNIT_TEST(lastIndexOf_test_oustring_008); |
| 649 |
CPPUNIT_TEST(lastIndexOf_test_oustring_009); |
| 650 |
|
| 651 |
CPPUNIT_TEST(lastIndexOf_test_oustring_offset_001); |
| 652 |
CPPUNIT_TEST(lastIndexOf_test_oustring_offset_002); |
| 653 |
CPPUNIT_TEST(lastIndexOf_test_oustring_offset_003); |
| 654 |
CPPUNIT_TEST(lastIndexOf_test_oustring_offset_004); |
| 655 |
|
| 656 |
CPPUNIT_TEST(lastIndexOf_test_salunicode_001); |
| 657 |
CPPUNIT_TEST(lastIndexOf_test_salunicode_002); |
| 658 |
|
| 659 |
CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_001); |
| 660 |
CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_002); |
| 661 |
CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_003); |
| 662 |
|
| 663 |
CPPUNIT_TEST_SUITE_END(); |
| 664 |
}; // class lastIndexOf |
| 665 |
|
| 666 |
// - getToken (tests) |
| 667 |
class getToken : public CppUnit::TestFixture |
| 668 |
{ |
| 669 |
|
| 670 |
public: |
| 671 |
void getToken_000() |
| 672 |
{ |
| 673 |
OUString suTokenStr; |
| 674 |
|
| 675 |
sal_Int32 nIndex = 0; |
| 676 |
do |
| 677 |
{ |
| 678 |
suTokenStr.getToken( 0, ';', nIndex ); |
| 679 |
} |
| 680 |
while ( nIndex >= 0 ); |
| 681 |
printf("Index %" SAL_PRIdINT32 "\n", nIndex); |
| 682 |
// should not GPF |
| 683 |
} |
| 684 |
|
| 685 |
void getToken_001() |
| 686 |
{ |
| 687 |
OUString suTokenStr(u"a;b"_ustr); |
| 688 |
|
| 689 |
sal_Int32 nIndex = 0; |
| 690 |
|
| 691 |
OUString suToken = suTokenStr.getToken( 0, ';', nIndex ); |
| 692 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'a'", u"a"_ustr, suToken); |
| 693 |
|
| 694 |
/* OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex ); |
| 695 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'b'", u"b"_ustr, suToken); |
| 696 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1), nIndex); |
| 697 |
} |
| 698 |
|
| 699 |
void getToken_002() |
| 700 |
{ |
| 701 |
OUString suTokenStr(u"a;b.c"_ustr); |
| 702 |
|
| 703 |
sal_Int32 nIndex = 0; |
| 704 |
|
| 705 |
OUString suToken = suTokenStr.getToken( 0, ';', nIndex ); |
| 706 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'a'", u"a"_ustr, suToken ); |
| 707 |
|
| 708 |
/* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex ); |
| 709 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'b'", u"b"_ustr, suToken ); |
| 710 |
|
| 711 |
/* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex ); |
| 712 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'c'", u"c"_ustr, suToken ); |
| 713 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1), nIndex); |
| 714 |
} |
| 715 |
|
| 716 |
void getToken_003() |
| 717 |
{ |
| 718 |
OUString suTokenStr(u"a;;b"_ustr); |
| 719 |
|
| 720 |
sal_Int32 nIndex = 0; |
| 721 |
|
| 722 |
OUString suToken = suTokenStr.getToken( 0, ';', nIndex ); |
| 723 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'a'", u"a"_ustr, suToken ); |
| 724 |
|
| 725 |
/* OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex ); |
| 726 |
CPPUNIT_ASSERT_MESSAGE("Token should be empty", suToken.isEmpty()); |
| 727 |
|
| 728 |
/* OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex ); |
| 729 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'b'", u"b"_ustr, suToken ); |
| 730 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1), nIndex); |
| 731 |
} |
| 732 |
|
| 733 |
void getToken_004() |
| 734 |
{ |
| 735 |
OUString suTokenStr(u"longer.then.ever."_ustr); |
| 736 |
|
| 737 |
sal_Int32 nIndex = 0; |
| 738 |
|
| 739 |
OUString suToken = suTokenStr.getToken( 0, '.', nIndex ); |
| 740 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be 'longer'", u"longer"_ustr, suToken ); |
| 741 |
|
| 742 |
/* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex ); |
| 743 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be 'then'", u"then"_ustr, suToken ); |
| 744 |
|
| 745 |
/* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex ); |
| 746 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be 'ever'", u"ever"_ustr, suToken ); |
| 747 |
|
| 748 |
/* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex ); |
| 749 |
CPPUNIT_ASSERT_MESSAGE("Token should be empty", suToken.isEmpty()); |
| 750 |
|
| 751 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1), nIndex); |
| 752 |
} |
| 753 |
|
| 754 |
void getToken_005() { |
| 755 |
OUString ab(u"ab"_ustr); |
| 756 |
sal_Int32 n = 0; |
| 757 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( |
| 758 |
"token should be 'ab'", ab, ab.getToken(0, '-', n)); |
| 759 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("n should be -1", static_cast<sal_Int32>(-1), n); |
| 760 |
CPPUNIT_ASSERT_MESSAGE( |
| 761 |
"token should be empty", ab.getToken(0, '-', n).isEmpty()); |
| 762 |
} |
| 763 |
|
| 764 |
void getToken_006() |
| 765 |
{ |
| 766 |
OUString suTokenStr; |
| 767 |
auto pTokenStr = suTokenStr.getStr(); |
| 768 |
sal_uInt64 n64 = reinterpret_cast<sal_uInt64>(pTokenStr) / sizeof(sal_Unicode); |
| 769 |
// Point either to 0x0, or to some random address -4GiB away from this string |
| 770 |
sal_Int32 n = n64 > o3tl::make_unsigned(SAL_MAX_INT32) ? -SAL_MAX_INT32 |
| 771 |
: -static_cast<sal_Int32>(n64); |
| 772 |
suTokenStr.getToken(0, ';', n); |
| 773 |
// should not GPF with negative index |
| 774 |
} |
| 775 |
|
| 776 |
CPPUNIT_TEST_SUITE(getToken); |
| 777 |
CPPUNIT_TEST(getToken_000); |
| 778 |
CPPUNIT_TEST(getToken_001); |
| 779 |
CPPUNIT_TEST(getToken_002); |
| 780 |
CPPUNIT_TEST(getToken_003); |
| 781 |
CPPUNIT_TEST(getToken_004); |
| 782 |
CPPUNIT_TEST(getToken_005); |
| 783 |
CPPUNIT_TEST(getToken_006); |
| 784 |
CPPUNIT_TEST_SUITE_END(); |
| 785 |
}; // class getToken |
| 786 |
|
| 787 |
class convertToString: public CppUnit::TestFixture { |
| 788 |
public: |
| 789 |
void test(); |
| 790 |
|
| 791 |
CPPUNIT_TEST_SUITE(convertToString); |
| 792 |
CPPUNIT_TEST(test); |
| 793 |
CPPUNIT_TEST_SUITE_END(); |
| 794 |
}; |
| 795 |
|
| 796 |
void convertToString::test() { |
| 797 |
static constexpr OUStringLiteral utf16 = u"A\u00E4a"; |
| 798 |
OString s; |
| 799 |
CPPUNIT_ASSERT( |
| 800 |
OUString(utf16).convertToString( |
| 801 |
&s, RTL_TEXTENCODING_UTF7, |
| 802 |
(RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR | |
| 803 |
RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR))); |
| 804 |
CPPUNIT_ASSERT_EQUAL( |
| 805 |
OString(RTL_CONSTASCII_STRINGPARAM("A+AOQ-a")), s); |
| 806 |
} |
| 807 |
|
| 808 |
// - string construction & interning (tests) |
| 809 |
|
| 810 |
class construction : public CppUnit::TestFixture |
| 811 |
{ |
| 812 |
public: |
| 813 |
void construct() |
| 814 |
{ |
| 815 |
#ifdef RTL_INLINE_STRINGS |
| 816 |
OUString aFoo( "foo" ); |
| 817 |
CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[0] == 'f'); |
| 818 |
CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[1] == 'o'); |
| 819 |
CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[2] == 'o'); |
| 820 |
CPPUNIT_ASSERT_MESSAGE("string length", aFoo.getLength() == 3); |
| 821 |
|
| 822 |
OUString aBaa( "this is a very long string with a lot of long things inside it and it goes on and on and on forever etc." ); |
| 823 |
CPPUNIT_ASSERT_MESSAGE("string length", aBaa.getLength() == 104); |
| 824 |
// Dig at the internals ... FIXME: should we have the bit-flag defines public ? |
| 825 |
CPPUNIT_ASSERT_MESSAGE("string static flags", (aBaa.pData->refCount & 1<<30) != 0); |
| 826 |
#endif |
| 827 |
} |
| 828 |
|
| 829 |
void intern() |
| 830 |
{ |
| 831 |
// The empty string is 'static' a special case ... |
| 832 |
OUString().intern(); |
| 833 |
OUString::intern( "",strlen(""),RTL_TEXTENCODING_ASCII_US ); |
| 834 |
|
| 835 |
OUString aFoo( u"foo"_ustr ); |
| 836 |
OUString aFooIntern = aFoo.intern(); |
| 837 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "string contents", u"foo"_ustr, aFooIntern); |
| 838 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("string length", static_cast<sal_Int32>(3), aFooIntern.getLength()); |
| 839 |
// We have to dup due to no atomic 'intern' bit-set operation |
| 840 |
CPPUNIT_ASSERT_MESSAGE("intern dups", aFoo.pData != aFooIntern.pData); |
| 841 |
|
| 842 |
// Test interning lots of things |
| 843 |
int i; |
| 844 |
static const int nSequence = 4096; |
| 845 |
std::unique_ptr<OUString[]> pStrs(new OUString[nSequence]); |
| 846 |
for (i = 0; i < nSequence; i++) |
| 847 |
{ |
| 848 |
pStrs[i] = OUString::number( sqrt( static_cast<double>(i) ) ).intern(); |
| 849 |
} |
| 850 |
for (i = 0; i < nSequence; i++) |
| 851 |
{ |
| 852 |
OUString aNew = OUString::number( sqrt( static_cast<double>(i) ) ).intern(); |
| 853 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("double intern failed", |
| 854 |
pStrs[i].pData, aNew.pData); |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
CPPUNIT_TEST_SUITE(construction); |
| 859 |
CPPUNIT_TEST(construct); |
| 860 |
CPPUNIT_TEST(intern); |
| 861 |
CPPUNIT_TEST_SUITE_END(); |
| 862 |
}; |
| 863 |
|
| 864 |
class indexOfAscii: public CppUnit::TestFixture { |
| 865 |
public: |
| 866 |
void test(); |
| 867 |
|
| 868 |
CPPUNIT_TEST_SUITE(indexOfAscii); |
| 869 |
CPPUNIT_TEST(test); |
| 870 |
CPPUNIT_TEST_SUITE_END(); |
| 871 |
}; |
| 872 |
|
| 873 |
void indexOfAscii::test() { |
| 874 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), OUString().indexOf("")); |
| 875 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), OUString().lastIndexOf("")); |
| 876 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), u"foo"_ustr.indexOf("foo")); |
| 877 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), u"foo"_ustr.lastIndexOf("foo")); |
| 878 |
CPPUNIT_ASSERT_EQUAL( |
| 879 |
sal_Int32(2), u"fofoobar"_ustr.indexOf("foo")); |
| 880 |
CPPUNIT_ASSERT_EQUAL( |
| 881 |
sal_Int32(3), u"foofoofob"_ustr.lastIndexOf("foo")); |
| 882 |
CPPUNIT_ASSERT_EQUAL( |
| 883 |
sal_Int32(3), u"foofoobar"_ustr.indexOf("foo", 1)); |
| 884 |
} |
| 885 |
|
| 886 |
class endsWith: public CppUnit::TestFixture { |
| 887 |
public: |
| 888 |
void test(); |
| 889 |
|
| 890 |
CPPUNIT_TEST_SUITE(endsWith); |
| 891 |
CPPUNIT_TEST(test); |
| 892 |
CPPUNIT_TEST_SUITE_END(); |
| 893 |
}; |
| 894 |
|
| 895 |
void endsWith::test() { |
| 896 |
CPPUNIT_ASSERT_EQUAL(true, OUString().endsWith("")); |
| 897 |
CPPUNIT_ASSERT_EQUAL(false, OUString().endsWith("foo")); |
| 898 |
CPPUNIT_ASSERT_EQUAL(true, u"bar"_ustr.endsWith("bar")); |
| 899 |
CPPUNIT_ASSERT_EQUAL(true, u"foobar"_ustr.endsWith("bar")); |
| 900 |
CPPUNIT_ASSERT_EQUAL(false, u"FOOBAR"_ustr.endsWith("bar")); |
| 901 |
} |
| 902 |
|
| 903 |
class isEmpty: public CppUnit::TestFixture { |
| 904 |
public: |
| 905 |
void test(); |
| 906 |
|
| 907 |
CPPUNIT_TEST_SUITE(isEmpty); |
| 908 |
CPPUNIT_TEST(test); |
| 909 |
CPPUNIT_TEST_SUITE_END(); |
| 910 |
}; |
| 911 |
|
| 912 |
void isEmpty::test() { |
| 913 |
OUString aString; |
| 914 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "Newly constructed string should be empty", true, aString.isEmpty() ); |
| 915 |
|
| 916 |
aString = "Not empty any more"; |
| 917 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "String should not be empty", false, aString.isEmpty() ); |
| 918 |
|
| 919 |
aString.clear(); |
| 920 |
CPPUNIT_ASSERT_EQUAL_MESSAGE( "aString.clear(), so should now be empty", true, aString.isEmpty() ); |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
class createFromCodePoints: public CppUnit::TestFixture { |
| 925 |
public: |
| 926 |
void test(); |
| 927 |
|
| 928 |
CPPUNIT_TEST_SUITE(createFromCodePoints); |
| 929 |
CPPUNIT_TEST(test); |
| 930 |
CPPUNIT_TEST_SUITE_END(); |
| 931 |
}; |
| 932 |
|
| 933 |
void createFromCodePoints::test() { |
| 934 |
CPPUNIT_ASSERT_EQUAL( |
| 935 |
sal_Int32(0), |
| 936 |
OUString(static_cast< sal_uInt32 const * >(nullptr), 0).getLength()); |
| 937 |
sal_uInt32 cp[] = { 0, 0xD800, 0xFFFF, 0x10000, 0x10FFFF }; |
| 938 |
// non-const, to avoid loplugin:stringliteralvar |
| 939 |
OUString s(cp, SAL_N_ELEMENTS(cp)); |
| 940 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(7), s.getLength()); |
| 941 |
CPPUNIT_ASSERT_EQUAL(u'\0', s[0]); |
| 942 |
CPPUNIT_ASSERT_EQUAL(u'\xD800', s[1]); |
| 943 |
CPPUNIT_ASSERT_EQUAL(u'\xFFFF', s[2]); |
| 944 |
CPPUNIT_ASSERT_EQUAL(u'\xD800', s[3]); |
| 945 |
CPPUNIT_ASSERT_EQUAL(u'\xDC00', s[4]); |
| 946 |
CPPUNIT_ASSERT_EQUAL(u'\xDBFF', s[5]); |
| 947 |
CPPUNIT_ASSERT_EQUAL(u'\xDFFF', s[6]); |
| 948 |
} |
| 949 |
|
| 950 |
class iterateCodePoints: public CppUnit::TestFixture { |
| 951 |
public: |
| 952 |
void testNotWellFormed(); |
| 953 |
|
| 954 |
CPPUNIT_TEST_SUITE(iterateCodePoints); |
| 955 |
CPPUNIT_TEST(testNotWellFormed); |
| 956 |
CPPUNIT_TEST_SUITE_END(); |
| 957 |
}; |
| 958 |
|
| 959 |
void iterateCodePoints::testNotWellFormed() { |
| 960 |
static constexpr OUStringLiteral utf16 = |
| 961 |
u"\U00010000A\U0010FFFF\xDDEF\xD9AB"; |
| 962 |
OUString s(utf16); |
| 963 |
sal_Int32 i = 0; |
| 964 |
CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i)); |
| 965 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(2), i); |
| 966 |
CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x0041), s.iterateCodePoints(&i)); |
| 967 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i); |
| 968 |
CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10FFFF), s.iterateCodePoints(&i)); |
| 969 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(5), i); |
| 970 |
CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDDEF), s.iterateCodePoints(&i)); |
| 971 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(6), i); |
| 972 |
CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xD9AB), s.iterateCodePoints(&i)); |
| 973 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(7), i); |
| 974 |
CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xD9AB), s.iterateCodePoints(&i, -1)); |
| 975 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(6), i); |
| 976 |
CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDDEF), s.iterateCodePoints(&i, -1)); |
| 977 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(5), i); |
| 978 |
CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10FFFF), s.iterateCodePoints(&i, -1)); |
| 979 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i); |
| 980 |
CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x0041), s.iterateCodePoints(&i, -1)); |
| 981 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(2), i); |
| 982 |
CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i, -1)); |
| 983 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); |
| 984 |
i = 1; |
| 985 |
CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDC00), s.iterateCodePoints(&i, 2)); |
| 986 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i); |
| 987 |
i = 4; |
| 988 |
CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i, -3)); |
| 989 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); |
| 990 |
} |
| 991 |
|
| 992 |
class convertFromString: public CppUnit::TestFixture { |
| 993 |
public: |
| 994 |
void test(); |
| 995 |
|
| 996 |
CPPUNIT_TEST_SUITE(convertFromString); |
| 997 |
CPPUNIT_TEST(test); |
| 998 |
CPPUNIT_TEST_SUITE_END(); |
| 999 |
}; |
| 1000 |
|
| 1001 |
void convertFromString::test() { |
| 1002 |
OUString t; |
| 1003 |
CPPUNIT_ASSERT( |
| 1004 |
!rtl_convertStringToUString( |
| 1005 |
&t.pData, RTL_CONSTASCII_STRINGPARAM("\x80"), RTL_TEXTENCODING_UTF8, |
| 1006 |
(RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | |
| 1007 |
RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR | |
| 1008 |
RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))); |
| 1009 |
CPPUNIT_ASSERT( |
| 1010 |
!rtl_convertStringToUString( |
| 1011 |
&t.pData, RTL_CONSTASCII_STRINGPARAM("\xC0"), RTL_TEXTENCODING_UTF8, |
| 1012 |
(RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | |
| 1013 |
RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR | |
| 1014 |
RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))); |
| 1015 |
CPPUNIT_ASSERT( |
| 1016 |
!rtl_convertStringToUString( |
| 1017 |
&t.pData, RTL_CONSTASCII_STRINGPARAM("\xFF"), RTL_TEXTENCODING_UTF8, |
| 1018 |
(RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | |
| 1019 |
RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR | |
| 1020 |
RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))); |
| 1021 |
CPPUNIT_ASSERT( |
| 1022 |
rtl_convertStringToUString( |
| 1023 |
&t.pData, RTL_CONSTASCII_STRINGPARAM("abc"), RTL_TEXTENCODING_UTF8, |
| 1024 |
(RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | |
| 1025 |
RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR | |
| 1026 |
RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))); |
| 1027 |
CPPUNIT_ASSERT_EQUAL( u"abc"_ustr, t ); |
| 1028 |
} |
| 1029 |
|
| 1030 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::number); |
| 1031 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::toInt); |
| 1032 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::toDouble); |
| 1033 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::toFloat); |
| 1034 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::lastIndexOf); |
| 1035 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::getToken); |
| 1036 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::convertToString); |
| 1037 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::construction); |
| 1038 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::indexOfAscii); |
| 1039 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::endsWith); |
| 1040 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::isEmpty); |
| 1041 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::createFromCodePoints); |
| 1042 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::iterateCodePoints); |
| 1043 |
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::convertFromString); |
| 1044 |
|
| 1045 |
} // namespace rtl_OUString |
| 1046 |
|
| 1047 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |