Skip to content

Commit

Permalink
test: rewrite fuzzy tests to address random failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Jan 4, 2025
1 parent 478b275 commit 0d35871
Show file tree
Hide file tree
Showing 18 changed files with 122 additions and 122 deletions.
9 changes: 9 additions & 0 deletions modules/functions_manager/tests/test_restoredefaultpath.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
u1 = userpath();
%=============================================================================
restoredefaultpath();
%=============================================================================
u2 = userpath();
assert_isequal(u1, u2);
%=============================================================================
REF = path();
path('');
P1 = path();
u3 = userpath();
assert_isequal(u1, u2);
assert_isequal(P1, '');
%=============================================================================
restoredefaultpath();
u3 = userpath();
P2 = path();
assert_isequal(u1, u3);
assert_isequal(REF, P2);
%=============================================================================
119 changes: 48 additions & 71 deletions modules/interpreter/src/cpp/PathFunctionIndexerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ PathFunctionIndexerManager::destroy()
void
PathFunctionIndexerManager::startFileWatcher()
{
for (auto it = _pathWatchFuncVector.begin(); it != _pathWatchFuncVector.end(); ++it) {
PathFunctionIndexer* pf = *it;
for (auto& pf : _pathWatchFuncVector) {
pf->startFileWatcher();
}
if (_userPath) {
Expand All @@ -88,24 +87,19 @@ bool
PathFunctionIndexerManager::hashOnFileWatcher()
{
bool result = false;
for (auto it = _pathWatchFuncVector.begin(); it != _pathWatchFuncVector.end(); ++it) {
PathFunctionIndexer* pf = *it;
for (auto& pf : _pathWatchFuncVector) {
if (pf->wasModified()) {
pf->rehash();
result = true;
}
}
if (_userPath) {
if (_userPath->wasModified()) {
_userPath->rehash();
result = true;
};
if (_userPath && _userPath->wasModified()) {
_userPath->rehash();
result = true;
}
if (_currentPath) {
if (_currentPath->wasModified()) {
_currentPath->rehash();
result = true;
};
if (_currentPath && _currentPath->wasModified()) {
_currentPath->rehash();
result = true;
}

if (result) {
Expand All @@ -118,8 +112,7 @@ void
PathFunctionIndexerManager::refreshFunctionsMap()
{
_pathFuncMap.clear();
for (auto it = _pathFuncVector.begin(); it != _pathFuncVector.end(); ++it) {
PathFunctionIndexer* pf = *it;
for (auto& pf : _pathFuncVector) {
auto fileFunctions = pf->getAllFileFunctions();
_pathFuncMap.reserve(_pathFuncMap.size() + fileFunctions.size());
_pathFuncMap.insert(fileFunctions.begin(), fileFunctions.end());
Expand Down Expand Up @@ -246,24 +239,20 @@ bool
PathFunctionIndexerManager::addPath(const std::wstring& path, bool begin, bool frozen)
{
bool res = false;
std::vector<PathFunctionIndexer*>::iterator it = std::find_if(
auto it = std::find_if(
_pathFuncVector.begin(), _pathFuncVector.end(), [path](PathFunctionIndexer* x) {
return FileSystemWrapper::Path::equivalent(x->getPath(), path);
});
if (it != _pathFuncVector.end()) {
return false;
}
PathFunctionIndexer* pf = nullptr;

try {
pf = new PathFunctionIndexer(
auto pf = new PathFunctionIndexer(
FileSystemWrapper::Path::normalize(path), frozen ? false : true);
if (_filesWatcherStarted) {
pf->startFileWatcher();
}
} catch (const std::bad_alloc&) {
pf = nullptr;
}
if (pf != nullptr) {
FunctionsInMemory::getInstance()->clearMapCache();
if (begin) {
_pathFuncVector.insert(_pathFuncVector.begin(), pf);
Expand All @@ -273,8 +262,10 @@ PathFunctionIndexerManager::addPath(const std::wstring& path, bool begin, bool f
if (!frozen) {
_pathWatchFuncVector.push_back(pf);
}
res = true;
refreshFunctionsMap();
res = true;
} catch (const std::bad_alloc&) {
res = false;
}
return res;
}
Expand Down Expand Up @@ -572,61 +563,47 @@ PathFunctionIndexerManager::userpathCompute()
{
clearUserPath();
std::wstring userpathEnv = GetVariableEnvironment(L"NELSON_USERPATH", L"");
bool bSet = false;
if (!userpathEnv.empty()) {
if (FileSystemWrapper::Path::is_directory(userpathEnv)) {
setUserPath(userpathEnv);
bSet = true;
}
if (!userpathEnv.empty() && FileSystemWrapper::Path::is_directory(userpathEnv)) {
setUserPath(userpathEnv);
return;
}
if (!bSet) {
std::wstring prefDir;
std::wstring userPathFile;
try {
prefDir = NelsonConfiguration::getInstance()->getNelsonPreferencesDirectory();
} catch (const Exception&) {
prefDir.clear();
}

userPathFile = prefDir + L"/userpath.conf";
bool bIsFile = FileSystemWrapper::Path::is_regular_file(userPathFile);
if (bIsFile) {
std::wstring preferedUserPath = loadUserPathFromFile();
if (!preferedUserPath.empty()) {
if (FileSystemWrapper::Path::is_directory(preferedUserPath)) {
setUserPath(preferedUserPath);
bSet = true;
}
} else {
bSet = true;
}
std::wstring prefDir;
try {
prefDir = NelsonConfiguration::getInstance()->getNelsonPreferencesDirectory();
} catch (const Exception&) {
prefDir.clear();
}
std::wstring userPathFile = prefDir + L"/userpath.conf";
if (FileSystemWrapper::Path::is_regular_file(userPathFile)) {
std::wstring preferedUserPath = loadUserPathFromFile();
if (!preferedUserPath.empty() && FileSystemWrapper::Path::is_directory(preferedUserPath)) {
setUserPath(preferedUserPath);
return;
}
}
if (!bSet) {
#ifdef _MSC_VER
std::wstring userprofileEnv = GetVariableEnvironment(L"USERPROFILE", L"");
if (!userprofileEnv.empty()) {
std::wstring userpathDir = userprofileEnv + std::wstring(L"/Documents/Nelson");
if (!FileSystemWrapper::Path::is_directory(userpathDir)) {
FileSystemWrapper::Path::create_directories(userpathDir);
}
if (FileSystemWrapper::Path::is_directory(userpathDir)) {
setUserPath(userpathDir);
}
std::wstring userprofileEnv = GetVariableEnvironment(L"USERPROFILE", L"");
if (!userprofileEnv.empty()) {
std::wstring userpathDir = userprofileEnv + std::wstring(L"/Documents/Nelson");
if (!FileSystemWrapper::Path::is_directory(userpathDir)) {
FileSystemWrapper::Path::create_directories(userpathDir);
}
if (FileSystemWrapper::Path::is_directory(userpathDir)) {
setUserPath(userpathDir);
}
}
#else
std::wstring homeEnv = GetVariableEnvironment(L"HOME", L"");
if (homeEnv != L"") {
std::wstring userpathDir = homeEnv + std::wstring(L"/Documents/Nelson");
if (!FileSystemWrapper::Path::is_directory(userpathDir)) {
FileSystemWrapper::Path::create_directories(userpathDir);
}
if (FileSystemWrapper::Path::is_directory(userpathDir)) {
setUserPath(userpathDir);
}
std::wstring homeEnv = GetVariableEnvironment(L"HOME", L"");
if (homeEnv != L"") {
std::wstring userpathDir = homeEnv + std::wstring(L"/Documents/Nelson");
if (!FileSystemWrapper::Path::is_directory(userpathDir)) {
FileSystemWrapper::Path::create_directories(userpathDir);
}
if (FileSystemWrapper::Path::is_directory(userpathDir)) {
setUserPath(userpathDir);
}
#endif
}
#endif
}
//=============================================================================
std::wstring
Expand Down
1 change: 0 additions & 1 deletion modules/mex/tests/mxDuplicateArray.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ void
mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
mxArray* pOut = NULL;
;
if (nrhs != 1) {
mexErrMsgTxt("Wrong number or type of input argument");
}
Expand Down
26 changes: 13 additions & 13 deletions modules/mex/tests/test_mxDuplicateArray_cell.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
% <--SEQUENTIAL TEST REQUIRED-->
%=============================================================================
if ispc() && ~havecompiler()
configuremsvc();
end
%=============================================================================
assert_istrue(havecompiler());
%=============================================================================
if exist('mxDuplicateArray') == 0
try
test_dir = [tempdir(), 'mxDuplicateArray_cell'];
if isdir(test_dir)
rmdir(test_dir,'s');
end
mkdir(test_dir);
status = copyfile('mxDuplicateArray.c', test_dir);
assert_istrue(status);
cd(test_dir);
mex('mxDuplicateArray.c');
addpath(pwd())
catch NE
rethrow(NE);
test_dir = [tempdir(), 'mxDuplicateArray_cell'];
if isdir(test_dir)
rmdir(test_dir,'s');
end
mkdir(test_dir);
status = copyfile('mxDuplicateArray.c', test_dir);
assert_istrue(status);
cd(test_dir);
mex('mxDuplicateArray.c');
addpath(pwd())
end
%=============================================================================
REF = {1, single(2); 'Nelson Text in a cell', false};
Expand Down
37 changes: 12 additions & 25 deletions modules/os_functions/src/cpp/GetVariableEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,30 @@ GetVariableEnvironment(const std::wstring& envVarName, const std::wstring& defau
{
std::wstring str(defaultValue);
#ifdef _MSC_VER
#define DEFAULT_SIZE_ENV 4096
wchar_t* buf = nullptr;
try {
buf = new wchar_t[DEFAULT_SIZE_ENV];
} catch (const std::bad_alloc&) {
const DWORD DEFAULT_SIZE_ENV = 4096;
std::unique_ptr<wchar_t[]> buf(new (std::nothrow) wchar_t[DEFAULT_SIZE_ENV]);
if (!buf) {
return str;
}
DWORD dwRet = ::GetEnvironmentVariableW(envVarName.c_str(), buf, DEFAULT_SIZE_ENV);

DWORD dwRet = ::GetEnvironmentVariableW(envVarName.c_str(), buf.get(), DEFAULT_SIZE_ENV);
if (dwRet == 0) {
// error
if (buf != nullptr) {
delete[] buf;
buf = nullptr;
}
return str;
}
if (dwRet > DEFAULT_SIZE_ENV) {
// we resize the buffer
delete[] buf;
try {
buf = new wchar_t[dwRet + 1];
} catch (const std::bad_alloc&) {
buf.reset(new (std::nothrow) wchar_t[dwRet + 1]);
if (!buf) {
return str;
}
dwRet = ::GetEnvironmentVariableW(envVarName.c_str(), buf, dwRet);
}
if (dwRet == 0) {
// error
if (buf != nullptr) {
delete[] buf;
buf = nullptr;
dwRet = ::GetEnvironmentVariableW(envVarName.c_str(), buf.get(), dwRet + 1);
if (dwRet == 0) {
// error
return str;
}
return str;
}
str = buf;
delete[] buf;

str.assign(buf.get(), dwRet);
#else
std::string s1 = wstring_to_utf8(envVarName);
str = defaultValue;
Expand Down
4 changes: 3 additions & 1 deletion modules/parallel/builtin/cpp/FevalQueue_cancelAllBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ Nelson::ParallelGateway::FevalQueue_cancelAllBuiltin(int nLhs, const ArrayOfVect
Error(_W("FevalQueue handle expected."));
}
auto* objFevalQueue = (FevalQueueObject*)param1.getContentAsHandleScalar();
objFevalQueue->cancelAll();
if (objFevalQueue != nullptr) {
objFevalQueue->cancelAll();
}
return retval;
}
//=============================================================================
10 changes: 6 additions & 4 deletions modules/parallel/src/cpp/BackgroundPoolObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,12 @@ BackgroundPoolObject::fieldnames()
void
BackgroundPoolObject::resetThreadPool()
{
threadPool->pause();
FevalQueueObject::getInstance()->reset();
threadPool->unpause();
threadPool->reset(NelsonConfiguration::getInstance()->getMaxNumCompThreads());
if (threadPool) {
threadPool->pause();
FevalQueueObject::getInstance()->reset();
threadPool->unpause();
threadPool->reset(NelsonConfiguration::getInstance()->getMaxNumCompThreads());
}
}
//=============================================================================
} // namespace Nelson
Expand Down
10 changes: 8 additions & 2 deletions modules/parallel/src/cpp/FevalQueueObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,26 @@ void
FevalQueueObject::refreshQueue()
{
std::vector<FevalFutureObject*> newQueue;
newQueue.reserve(fEvalQueue.size());
for (auto f : fEvalQueue) {
if (f == nullptr) {
continue;
}
THREAD_STATE state = f->state;
if (state == THREAD_STATE::QUEUED || state == THREAD_STATE::RUNNING) {
newQueue.push_back(f);
}
}
fEvalQueue = newQueue;
fEvalQueue = std::move(newQueue);
}
//=============================================================================
void
FevalQueueObject::reset()
{
for (auto& k : fEvalQueue) {
k->cancel();
if (k != nullptr) {
k->cancel();
}
}
refreshQueue();
}
Expand Down
8 changes: 4 additions & 4 deletions modules/qml_engine/tests/test_QObject_findchildren.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
% <--GUI MODE-->
% <--ADV-CLI MODE-->
% <--WITH DISPLAY-->
%=============================================================================
if semver(qt_version(), '>=6.0')
Expand All @@ -16,11 +16,11 @@
qml_file_ok = [modulepath('qml_engine', 'tests'), '/test_qml_loadfile_window_qt5.qml'];
end
qobj = qml_loadfile(qml_file_ok);
child = QObject_findchildren(QObject_root(), 'text1', false);
assert_isequal(size(child), [0 0]);
%=============================================================================
child = QObject_findchildren(QObject_root(), 'text1', true);
child = QObject_findchildren(qobj, 'text1', true);
assert_isequal(class(child), 'QObject');
assert_isequal(size(child), [1 1]);
assert_isequal(child.className, 'QQuickText');
%=============================================================================
delete(qobj);
%=============================================================================
Loading

0 comments on commit 0d35871

Please sign in to comment.