Skip to content

Commit

Permalink
fix #512 Assign must not change left assign type when it is possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Sep 23, 2021
1 parent a0e4a24 commit 8d5c120
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 0.5.9 (UNRELEASED)

- [#512](http://github.com/Nelson-numerical-software/nelson/issues/512): Assign must not change left assign type when it is possible.

- [#509](http://github.com/Nelson-numerical-software/nelson/issues/509): horzcat vertcat generic support for class object.

- [#508](http://github.com/Nelson-numerical-software/nelson/issues/508): Change default seed for 'rand' with Mersenne Twister algo.
Expand Down
48 changes: 30 additions & 18 deletions modules/types/src/cpp/ArrayOf_Assign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,28 +574,38 @@ ArrayOf::setNDimSubset(ArrayOfVector& index, ArrayOf& rightData)
} else {
advance = 1;
}
if (isStringArray()) {
bool needToOverload = false;
ArrayOf promute = ArrayOf::toStringArray(rightData, needToOverload);
if (needToOverload) {
Error(_W("Cannot promote to string array."));
}
rightData = promute;

} else if (!isEmpty() && (rightData.getDataClass() == NLS_STRUCT_ARRAY)
&& (getDataClass() == NLS_STRUCT_ARRAY)) {
if (rightData.dp->fieldNames.size() > dp->fieldNames.size()) {
promoteType(NLS_STRUCT_ARRAY, rightData.dp->fieldNames);
} else {
rightData.promoteType(NLS_STRUCT_ARRAY, dp->fieldNames);
if (getDataClass() != rightData.getDataClass()) {
if (isStringArray()) {
bool needToOverload = false;
ArrayOf promute = ArrayOf::toStringArray(rightData, needToOverload);
if (needToOverload) {
Error(_W("Cannot promote to string array."));
}
rightData = promute;
}
else {
if (!isEmpty()) {
if (rightData.isComplex() && !isComplex()) {
promoteType(rightData.dp->dataClass, rightData.dp->fieldNames);
} else {
rightData.promoteType(dp->dataClass, dp->fieldNames);
}
} else {
promoteType(rightData.dp->dataClass, rightData.dp->fieldNames);
}
}
} else {
if (isEmpty() || rightData.getDataClass() > getDataClass()) {
promoteType(rightData.dp->dataClass, rightData.dp->fieldNames);
} else if (rightData.dp->dataClass <= dp->dataClass) {
rightData.promoteType(dp->dataClass, dp->fieldNames);
if (!isEmpty() && (rightData.getDataClass() == NLS_STRUCT_ARRAY)
&& (getDataClass() == NLS_STRUCT_ARRAY)) {
if (rightData.dp->fieldNames.size() > dp->fieldNames.size()) {
promoteType(NLS_STRUCT_ARRAY, rightData.dp->fieldNames);
} else {
rightData.promoteType(NLS_STRUCT_ARRAY, dp->fieldNames);
}
}
}

if (isSparse()) {
if (L > 2) {
Error(_W("Multidimensional indexing not legal for sparse "
Expand Down Expand Up @@ -723,7 +733,9 @@ ArrayOf::setNDimSubset(ArrayOfVector& index, ArrayOf& rightData)
static_cast<const ArrayOf*>(rightData.getDataPointer()), outDimsInt, srcDimsInt,
indx, L, dp->fieldNames.size(), advance);
break;
default: { } break; }
default: {
} break;
}
delete[] indx;
dp->dimensions.simplify();
} catch (const Exception& e) {
Expand Down
42 changes: 42 additions & 0 deletions modules/types/tests/bug_github_issue_#512.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
%=============================================================================
% Copyright (c) 2017 Allan CORNET (Nelson)
%=============================================================================
% This file is part of the Nelson.
%=============================================================================
% LICENCE_BLOCK_BEGIN
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU Lesser General Public
% License as published by the Free Software Foundation; either
% version 2.1 of the License, or (at your option) any later version.
%
% Alternatively, you can redistribute it and/or
% modify it under the terms of the GNU General Public License as
% published by the Free Software Foundation; either version 2 of
% the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public
% License along with this program. If not, see <http://www.gnu.org/licenses/>.
% LICENCE_BLOCK_END
%=============================================================================
% <-- Issue URL -->
% https://github.com/Nelson-numerical-software/nelson/issues/512
% <-- Short Description -->
% Assign must not change left assign type when it is possible
%=============================================================================
A = single(eye(3,3));
A(1,1) = 'f';
assert_isequal(class(A), 'single')
A(2,2) = int32(4);
assert_isequal(class(A), 'single')
A(3,3) = 3;
assert_isequal(class(A), 'single')
REF = single([102, 0, 0; 0 4 0; 0 0 3]);
assert_isequal(A, REF)
A(1,:) = [0 1 0];
assert_isequal(class(A), 'single')
%=============================================================================

0 comments on commit 8d5c120

Please sign in to comment.