mirror of
https://github.com/OPM/ResInsight.git
synced 2025-01-01 03:37:15 -06:00
212f5bf5ae
Required changes to use Qt6 and disable support for Qt5. There are still some adjustments related to Qt6 to be done, but these changes are not required to make Qt6 compile on relevant systems. * Build system changes Qt6 * Override enterEvent * Update QKeySequence * QtChart changes * Use QScreen to instepct dotsPerInch * Add app->quit() * Required updates for code related to QString * Use RiaQDateTimeTools * Required changes related to regular expressions * Support compile on Qt < 6.5 When version < 6.5 is found, qt_generate_deploy_app_script() is disabled. Compilation of ResInsight will work, but the install target will be incomplete. * Octave: add missing header. * Qt Advanced Docking: force Qt6 where both Qt5 and Qt6 is available. --------- Co-authored-by: magnesj <1793152+magnesj@users.noreply.github.com> Co-authored-by: Kristian Bendiksen <kristian.bendiksen@gmail.com>
332 lines
11 KiB
C++
332 lines
11 KiB
C++
#include "gtest/gtest.h"
|
|
|
|
#include "RiaStdStringTools.h"
|
|
|
|
#include <QLocale>
|
|
#include <charconv>
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
TEST( RiaStdStringToolsTest, ParseNumbers )
|
|
{
|
|
auto decimalPoint = QLocale::c().decimalPoint().toLatin1()[0];
|
|
|
|
{
|
|
std::string text = "8.73705e+06";
|
|
|
|
EXPECT_TRUE( RiaStdStringTools::isNumber( text, decimalPoint ) );
|
|
}
|
|
|
|
{
|
|
std::string text = "-8.73705e-06";
|
|
|
|
EXPECT_TRUE( RiaStdStringTools::isNumber( text, decimalPoint ) );
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
TEST( RiaStdStringToolsTest, EditDistance )
|
|
{
|
|
// Equal string needs zero edits
|
|
EXPECT_EQ( 0, RiaStdStringTools::computeEditDistance( "same", "same" ) );
|
|
|
|
// Empty strings are also zero edits
|
|
EXPECT_EQ( 0, RiaStdStringTools::computeEditDistance( "", "" ) );
|
|
|
|
// Examples from wikipedia
|
|
EXPECT_EQ( 3, RiaStdStringTools::computeEditDistance( "kitten", "sitting" ) );
|
|
EXPECT_EQ( 3, RiaStdStringTools::computeEditDistance( "sitting", "kitten" ) );
|
|
EXPECT_EQ( 3, RiaStdStringTools::computeEditDistance( "Saturday", "Sunday" ) );
|
|
EXPECT_EQ( 3, RiaStdStringTools::computeEditDistance( "Sunday", "Saturday" ) );
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
TEST( RiaStdStringToolsTest, TrimStrings )
|
|
{
|
|
// Test replace of tabs with space
|
|
{
|
|
std::string text = "test\t\tnext word";
|
|
|
|
replace( text.begin(), text.end(), '\t', ' ' );
|
|
|
|
std::string expectedText = "test next word";
|
|
EXPECT_EQ( text, expectedText );
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
TEST( RiaStdStringToolsTest, TrimString )
|
|
{
|
|
std::vector<std::pair<std::string, std::string>> testData = {
|
|
std::make_pair( " bla ", "bla" ),
|
|
std::make_pair( "bla ", "bla" ),
|
|
std::make_pair( " bla", "bla" ),
|
|
std::make_pair( "\tbla", "bla" ),
|
|
std::make_pair( "\tbla \n\t", "bla" ),
|
|
std::make_pair( "\tbla\v", "bla" ),
|
|
std::make_pair( "bla", "bla" ),
|
|
std::make_pair( "", "" ),
|
|
};
|
|
|
|
for ( auto [input, expectedText] : testData )
|
|
{
|
|
EXPECT_EQ( RiaStdStringTools::trimString( input ), expectedText );
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
TEST( RiaStdStringToolsTest, LeftTrimString )
|
|
{
|
|
std::vector<std::pair<std::string, std::string>> testData = {
|
|
std::make_pair( " bla ", "bla " ),
|
|
std::make_pair( "bla ", "bla " ),
|
|
std::make_pair( " bla", "bla" ),
|
|
std::make_pair( "\tbla", "bla" ),
|
|
std::make_pair( "\tbla \n\t", "bla \n\t" ),
|
|
std::make_pair( "\tbla\v", "bla\v" ),
|
|
std::make_pair( "bla", "bla" ),
|
|
std::make_pair( "", "" ),
|
|
};
|
|
|
|
for ( auto [input, expectedText] : testData )
|
|
{
|
|
EXPECT_EQ( RiaStdStringTools::leftTrimString( input ), expectedText );
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
TEST( RiaStdStringToolsTest, RightTrimString )
|
|
{
|
|
std::vector<std::pair<std::string, std::string>> testData = {
|
|
std::make_pair( " bla ", " bla" ),
|
|
std::make_pair( "bla ", "bla" ),
|
|
std::make_pair( " bla", " bla" ),
|
|
std::make_pair( "\tbla", "\tbla" ),
|
|
std::make_pair( "\tbla \n\t", "\tbla" ),
|
|
std::make_pair( "\tbla\v", "\tbla" ),
|
|
std::make_pair( "bla", "bla" ),
|
|
std::make_pair( "", "" ),
|
|
};
|
|
|
|
for ( auto [input, expectedText] : testData )
|
|
{
|
|
EXPECT_EQ( RiaStdStringTools::rightTrimString( input ), expectedText );
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
TEST( RiaStdStringToolsTest, RemoveWhitespace )
|
|
{
|
|
std::vector<std::pair<std::string, std::string>> testData = {
|
|
std::make_pair( " bla ", "bla" ),
|
|
std::make_pair( "bla ", "bla" ),
|
|
std::make_pair( " bla", "bla" ),
|
|
std::make_pair( "\tbla", "bla" ),
|
|
std::make_pair( "\tbla \n\t ,bla", "bla,bla" ),
|
|
std::make_pair( "\tbla\v", "bla" ),
|
|
std::make_pair( "bla", "bla" ),
|
|
std::make_pair( "", "" ),
|
|
};
|
|
|
|
for ( auto [input, expectedText] : testData )
|
|
{
|
|
EXPECT_EQ( RiaStdStringTools::removeWhitespace( input ), expectedText );
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
TEST( RiaStdStringToolsTest, DISABLED_PerformanceConversion )
|
|
{
|
|
size_t itemCount = 1000000;
|
|
std::string valueAsString = "1234567";
|
|
|
|
int intValue = 0;
|
|
|
|
{
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
for ( size_t i = 0; i < itemCount; i++ )
|
|
{
|
|
intValue = std::stoi( valueAsString );
|
|
}
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
std::chrono::duration<double> diff = end - start;
|
|
std::cout << "std::stoi (no try/catch) " << std::setw( 9 ) << diff.count() << " s\n";
|
|
}
|
|
|
|
{
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
for ( size_t i = 0; i < itemCount; i++ )
|
|
{
|
|
try
|
|
{
|
|
intValue = std::stoi( valueAsString );
|
|
}
|
|
catch ( ... )
|
|
{
|
|
}
|
|
}
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
std::chrono::duration<double> diff = end - start;
|
|
std::cout << "std::stoi incl try/catch" << std::setw( 9 ) << diff.count() << " s\n";
|
|
}
|
|
|
|
{
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
for ( size_t i = 0; i < itemCount; i++ )
|
|
{
|
|
std::from_chars( valueAsString.data(), valueAsString.data() + valueAsString.size(), intValue );
|
|
}
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
std::chrono::duration<double> diff = end - start;
|
|
std::cout << "std::from_chars " << std::setw( 9 ) << diff.count() << " s\n";
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
TEST( RiaStdStringToolsTest, ValuesFromRangeSelection )
|
|
{
|
|
std::string testString = "1,2,5-10,15";
|
|
std::set<int> expectedValues = { 1, 2, 5, 6, 7, 8, 9, 10, 15 };
|
|
|
|
auto actualValues = RiaStdStringTools::valuesFromRangeSelection( testString );
|
|
|
|
ASSERT_EQ( expectedValues, actualValues );
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
TEST( RiaStdStringToolsTest, ValuesFromRangeSelectionMinMax )
|
|
{
|
|
std::string testString = "-3, 5, 6-8, 10, 15-";
|
|
int minimumValue = 1;
|
|
int maximumValue = 20;
|
|
std::set<int> expectedValues = { 1, 2, 3, 5, 6, 7, 8, 10, 15, 16, 17, 18, 19, 20 };
|
|
|
|
auto actualValues = RiaStdStringTools::valuesFromRangeSelection( testString, minimumValue, maximumValue );
|
|
|
|
ASSERT_EQ( expectedValues, actualValues );
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
TEST( RiaStdStringToolsTest, TestInvalidRangeStrings )
|
|
{
|
|
int minimumValue = 1;
|
|
int maximumValue = 20;
|
|
|
|
{
|
|
// Handle blank space and inverted from/to
|
|
std::string testString = "5, -3, 9-8";
|
|
std::set<int> expectedValues = { 1, 2, 3, 5, 8, 9 };
|
|
auto actualValues = RiaStdStringTools::valuesFromRangeSelection( testString, minimumValue, maximumValue );
|
|
|
|
ASSERT_EQ( expectedValues, actualValues );
|
|
}
|
|
|
|
{
|
|
// If the range is invalid, the result should be an empty set
|
|
std::string testString = "5, a";
|
|
std::set<int> expectedValues = {};
|
|
auto actualValues = RiaStdStringTools::valuesFromRangeSelection( testString, minimumValue, maximumValue );
|
|
|
|
ASSERT_EQ( expectedValues, actualValues );
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
TEST( RiaStdStringToolsTest, TestToDouble )
|
|
{
|
|
{
|
|
std::string text = " 0.123";
|
|
|
|
double resultValue = 0.0;
|
|
auto isOk = RiaStdStringTools::toDouble( text, resultValue );
|
|
EXPECT_TRUE( isOk );
|
|
|
|
EXPECT_DOUBLE_EQ( resultValue, 0.123 );
|
|
}
|
|
|
|
{
|
|
std::string text = " 0.123 2";
|
|
|
|
double resultValue = 0.0;
|
|
auto isOk = RiaStdStringTools::toDouble( text, resultValue );
|
|
EXPECT_TRUE( isOk );
|
|
|
|
EXPECT_DOUBLE_EQ( resultValue, 0.123 );
|
|
}
|
|
|
|
{
|
|
std::string text = "0.123 2";
|
|
|
|
double resultValue = 0.0;
|
|
auto isOk = RiaStdStringTools::toDouble( text, resultValue );
|
|
EXPECT_TRUE( isOk );
|
|
|
|
EXPECT_DOUBLE_EQ( resultValue, 0.123 );
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
TEST( RiaStdStringToolsTest, TestToInt )
|
|
{
|
|
{
|
|
std::string text = " 12";
|
|
|
|
int resultValue = -1;
|
|
auto isOk = RiaStdStringTools::toInt( text, resultValue );
|
|
EXPECT_TRUE( isOk );
|
|
|
|
EXPECT_EQ( resultValue, 12 );
|
|
}
|
|
|
|
{
|
|
std::string text = " 12 ";
|
|
|
|
int resultValue = -1;
|
|
auto isOk = RiaStdStringTools::toInt( text, resultValue );
|
|
EXPECT_TRUE( isOk );
|
|
|
|
EXPECT_EQ( resultValue, 12 );
|
|
}
|
|
|
|
{
|
|
std::string text = "12";
|
|
|
|
int resultValue = -1;
|
|
auto isOk = RiaStdStringTools::toInt( text, resultValue );
|
|
EXPECT_TRUE( isOk );
|
|
|
|
EXPECT_EQ( resultValue, 12 );
|
|
}
|
|
}
|