Loading [MathJax]/extensions/tex2jax.js
Radium Engine  1.5.29
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
string.cpp
1#include <Core/Utils/StringUtils.hpp>
2#include <catch2/catch_test_macros.hpp>
3
4TEST_CASE( "Core/Utils/StringUtils", "[unittests][Core][Core/Utils][StringUtils]" ) {
5 SECTION( "Test getFileExt" ) {
6 using Ra::Core::Utils::getFileExt;
7 // File extension
8 REQUIRE( getFileExt( "aaa.xyz" ) == std::string( "xyz" ) );
9 // Extension of relative path
10 REQUIRE( getFileExt( "aaa/bbb.xyz" ) == std::string( "xyz" ) );
11 // Extension of absolute path
12 REQUIRE( getFileExt( "/aaa/bbb/ccc.xyz" ) == std::string( "xyz" ) );
13 // File with no extension
14 REQUIRE( getFileExt( "aaa/bbb/xyz" ) == std::string( "" ) );
15 REQUIRE( getFileExt( "aaa/bbb/xyz." ) == std::string( "" ) );
16 }
17 SECTION( "Test getDirName" ) {
18 using Ra::Core::Utils::getDirName;
19 // File with no directory
20 REQUIRE( getDirName( "aaa.xyz" ) == std::string( "." ) );
21 // Relative path
22 REQUIRE( getDirName( "aaa/bbb.xyz" ) == std::string( "aaa" ) );
23 // Absolute path
24 REQUIRE( getDirName( "/aaa/bbb/ccc.xyz" ) == std::string( "/aaa/bbb" ) );
25 // Trailing slashes
26 REQUIRE( getDirName( "/aaa/bbb/ccc.xyz///" ) == std::string( "/aaa/bbb" ) );
27 // File with no extension
28 REQUIRE( getDirName( "aaa/bbb/xyz" ) == std::string( "aaa/bbb" ) );
29 }
30 SECTION( "Test getBaseName" ) {
31 using Ra::Core::Utils::getBaseName;
32 // File with no directory
33 REQUIRE( getBaseName( "aaa.xyz", true ) == std::string( "aaa.xyz" ) );
34 REQUIRE( getBaseName( "aaa.xyz", false ) == std::string( "aaa" ) );
35 // Relative path
36 REQUIRE( getBaseName( "aaa/bbb.xyz", true ) == std::string( "bbb.xyz" ) );
37 REQUIRE( getBaseName( "aaa/bbb.xyz", false ) == std::string( "bbb" ) );
38 // Absolute path
39 REQUIRE( getBaseName( "/aaa/bbb/ccc.xyz", true ) == std::string( "ccc.xyz" ) );
40 REQUIRE( getBaseName( "/aaa/bbb/ccc.xyz", false ) == std::string( "ccc" ) );
41 // Trailing slashes
42 REQUIRE( getBaseName( "/aaa/bbb/ccc.xyz///", false ) == std::string( "ccc" ) );
43 // File with no extension
44 REQUIRE( getBaseName( "aaa/bbb/xyz", true ) == std::string( "xyz" ) );
45 REQUIRE( getBaseName( "aaa/bbb/xyz", false ) == std::string( "xyz" ) );
46 }
47 SECTION( "Test jointly getDirName, getBaseName and getFileExt" ) {
48 using Ra::Core::Utils::getBaseName;
49 using Ra::Core::Utils::getDirName;
50 using Ra::Core::Utils::getFileExt;
51 // Path reconstruction
52 std::string path = "/aaa/bbb/ccc.xyz";
53 REQUIRE( getDirName( path ) + "/" + getBaseName( path, false ) + "." + getFileExt( path ) ==
54 path );
55 }
56 SECTION( "Test replace/remove" ) {
57 using Ra::Core::Utils::removeAllInString;
58 using Ra::Core::Utils::replaceAllInString;
59
60 std::string initial { "abbbbcdcba" };
61 auto n = removeAllInString( initial, "b" );
62 REQUIRE( n == 5 );
63 REQUIRE( initial == "acdca" );
64
65 n = replaceAllInString( initial, "c", "x" );
66 REQUIRE( n == 2 );
67 REQUIRE( initial == "axdxa" );
68 }
69}