Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(validators): fix #1288 - add matrix dimension validation functions #1296

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:
brew install icu4c
brew info open-mpi
brew install openmpi
brew unlink pkg-config@0.29.2
brew install pkgconf
brew install gettext
brew link gettext --force
Expand Down Expand Up @@ -103,7 +102,6 @@ jobs:
brew install icu4c
brew info open-mpi
brew install openmpi
brew unlink pkg-config@0.29.2
brew install pkgconf
brew install gettext
brew link gettext --force
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `csvwrite`: Write comma-separated value (CSV) file.
- `dlmread`: Read ASCII-delimited file of numeric data into matrix.
- `realmin`: Smallest normalized floating-point number.
- [#1288](http://github.com/nelson-lang/nelson/issues/1288) `mustBeMatrix`, `mustBeRow`, `mustBeColumn` validator functions.

### Changed

- `narginchk`, `nargoutchk` support for check only minimun arguments `narginchk(3, Inf)`.
- Fedora 41 CI
- `title`: `Visible` property is inherited from the parent if not explicitly defined.
- i18n: migration PO files to JSON.
Expand Down
15 changes: 11 additions & 4 deletions modules/core/builtin/cpp/narginchkBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,21 @@ Nelson::CoreGateway::narginchkBuiltin(Evaluator* eval, int nLhs, const ArrayOfVe
Error(_("Scalar integer value required for #2 argument."));
}
int minArgs = argIn[0].getContentAsInteger32Scalar(false, true);
int maxArgs = argIn[1].getContentAsInteger32Scalar(false, true);

bool maxArgsIsInf = false;
if (argIn[1].isDoubleType(true)) {
double maxValue = argIn[1].getContentAsDoubleScalar();
maxArgsIsInf = std::isinf(maxValue);
}
int nargin = context->getCurrentScope()->getNargIn();
if (nargin < minArgs) {
Error(ERROR_WRONG_NUMBERS_INPUT_ARGS, L"Nelson:narginchk:notEnoughInputs", true);
}
if (nargin > maxArgs) {
Error(ERROR_WRONG_NUMBERS_INPUT_ARGS, L"Nelson:narginchk:tooManyInputs", true);
if (!maxArgsIsInf) {
int maxArgs = argIn[1].getContentAsInteger32Scalar(false, true);

if (nargin > maxArgs) {
Error(ERROR_WRONG_NUMBERS_INPUT_ARGS, L"Nelson:narginchk:tooManyInputs", true);
}
}
return retval;
}
Expand Down
11 changes: 9 additions & 2 deletions modules/core/builtin/cpp/nargoutchkBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ Nelson::CoreGateway::nargoutchkBuiltin(Evaluator* eval, int nLhs, const ArrayOfV
}

int minArgs = argIn[0].getContentAsInteger32Scalar(false, true);
int maxArgs = argIn[1].getContentAsInteger32Scalar(false, true);

bool maxArgsIsInf = false;
if (argIn[1].isDoubleType(true)) {
double maxValue = argIn[1].getContentAsDoubleScalar();
maxArgsIsInf = std::isinf(maxValue);
}
int numArgs = argIn[2].getContentAsInteger32Scalar(false, true);

std::wstring msg = L"";
Expand All @@ -78,7 +83,9 @@ Nelson::CoreGateway::nargoutchkBuiltin(Evaluator* eval, int nLhs, const ArrayOfV
msg = _W("Not enough output arguments.");
id = L"Nelson:nargoutchk:notEnoughOutputs";
}
if (numArgs > maxArgs) {

int maxArgs = argIn[1].getContentAsInteger32Scalar(false, true);
if (!maxArgsIsInf && numArgs > maxArgs) {
msg = _W("Too many output arguments.");
id = L"Nelson:nargoutchk:tooManyOutputs";
}
Expand Down
8 changes: 8 additions & 0 deletions modules/core/help/en_US/xml/narginchk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

<description>
<p><b>narginchk</b> checks the number of input arguments of an function.</p>
<p
>To ensure that a minimum number of arguments is provided, while allowing an unlimited maximum number by setting <b
>maxArgs</b> to <b>inf</b>. For instance, use <b
>narginchk(2, inf)</b> to throw an error if fewer than two inputs are supplied.</p>
</description>


Expand Down Expand Up @@ -59,6 +63,10 @@
<history_version>1.0.0</history_version>
<history_description>initial version</history_description>
</history_item>
<history_item>
<history_version>1.10.0</history_version>
<history_description>narginchk(3, Inf) managed</history_description>
</history_item>
</history>

<authors>
Expand Down
9 changes: 8 additions & 1 deletion modules/core/help/en_US/xml/nargoutchk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@

<description>
<p><b>nargoutchk</b> checks the number of output arguments of an function.</p>
<p>To ensure a minimum number of outputs while imposing no maximum limit, set <b
>maxArgs</b> to <b>inf</b>. For example, <b
>nargoutchk(2, inf)</b> generates an error if fewer than two outputs are specified.</p>
</description>


Expand All @@ -68,7 +71,7 @@ nargoutchk(1, 2, 3, 'struct')]]>
<link linkend="${core}nargin">nargout</link>
</see_also_item>
<see_also_item>
<link linkend="${core}nargoutchk">narginchk</link>
<link linkend="${core}narginchk">narginchk</link>
</see_also_item>

</see_also>
Expand All @@ -78,6 +81,10 @@ nargoutchk(1, 2, 3, 'struct')]]>
<history_version>1.0.0</history_version>
<history_description>initial version</history_description>
</history_item>
<history_item>
<history_version>1.10.0</history_version>
<history_description>nargoutchk(3, Inf) managed</history_description>
</history_item>
</history>

<authors>
Expand Down
6 changes: 6 additions & 0 deletions modules/validators/builtin/c/nlsValidators_builtin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
<ItemGroup>
<ClCompile Include="..\cpp\Gateway.cpp" />
<ClCompile Include="..\cpp\mustBeABuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeColumnBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeFileBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeFiniteBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeFloatBuiltin.cpp" />
Expand All @@ -194,6 +195,7 @@
<ClCompile Include="..\cpp\mustBeLessThanOrEqualBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeLogicalBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeLogicalScalarBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeMatrixBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeMemberBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeNegativeBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeNonemptyBuiltin.cpp" />
Expand All @@ -208,6 +210,7 @@
<ClCompile Include="..\cpp\mustBeNumericOrLogicalBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBePositiveBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeRealBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeRowBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeScalarOrEmptyBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeTextBuiltin.cpp" />
<ClCompile Include="..\cpp\mustBeTextScalarBuiltin.cpp" />
Expand Down Expand Up @@ -237,6 +240,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\mustBeABuiltin.hpp" />
<ClInclude Include="..\include\mustBeColumnBuiltin.hpp" />
<ClInclude Include="..\include\mustBeFileBuiltin.hpp" />
<ClInclude Include="..\include\mustBeFiniteBuiltin.hpp" />
<ClInclude Include="..\include\mustBeFloatBuiltin.hpp" />
Expand All @@ -249,6 +253,7 @@
<ClInclude Include="..\include\mustBeLessThanOrEqualBuiltin.hpp" />
<ClInclude Include="..\include\mustBeLogicalBuiltin.hpp" />
<ClInclude Include="..\include\mustBeLogicalScalarBuiltin.hpp" />
<ClInclude Include="..\include\mustBeMatrixBuiltin.hpp" />
<ClInclude Include="..\include\mustBeMemberBuiltin.hpp" />
<ClInclude Include="..\include\mustBeNegativeBuiltin.hpp" />
<ClInclude Include="..\include\mustBeNonemptyBuiltin.hpp" />
Expand All @@ -263,6 +268,7 @@
<ClInclude Include="..\include\mustBeNumericOrLogicalBuiltin.hpp" />
<ClInclude Include="..\include\mustBePositiveBuiltin.hpp" />
<ClInclude Include="..\include\mustBeRealBuiltin.hpp" />
<ClInclude Include="..\include\mustBeRowBuiltin.hpp" />
<ClInclude Include="..\include\mustBeTextBuiltin.hpp" />
<ClInclude Include="..\include\mustBeTextScalarBuiltin.hpp" />
<ClInclude Include="..\include\mustBeValidVariableNameBuiltin.hpp" />
Expand Down
24 changes: 21 additions & 3 deletions modules/validators/builtin/c/nlsValidators_builtin.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
<ClCompile Include="..\cpp\mustBeLogicalScalarBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\mustBeFiniteBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\mustBeLogicalBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down Expand Up @@ -117,6 +114,18 @@
<ClCompile Include="..\cpp\dllMain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\mustBeMatrixBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\mustBeFiniteBuiltin.cpp">
<Filter>Header Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\mustBeColumnBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\mustBeRowBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\CMakeLists.txt" />
Expand Down Expand Up @@ -221,6 +230,15 @@
<ClInclude Include="..\include\mustBeInRangeBuiltin.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\mustBeMatrixBuiltin.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\mustBeColumnBuiltin.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\mustBeRowBuiltin.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\loader.m" />
Expand Down
7 changes: 7 additions & 0 deletions modules/validators/builtin/cpp/Gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
#include "mustBeNonzeroLengthTextBuiltin.hpp"
#include "mustBeMemberBuiltin.hpp"
#include "mustBeInRangeBuiltin.hpp"
#include "mustBeMatrixBuiltin.hpp"
#include "mustBeRowBuiltin.hpp"
#include "mustBeColumnBuiltin.hpp"
//=============================================================================
using namespace Nelson;
//=============================================================================
Expand Down Expand Up @@ -91,6 +94,10 @@ static const nlsGateway gateway[] = {
0, -2 },
{ "mustBeMember", (ptrBuiltin)Nelson::ValidatorsGateway::mustBeMemberBuiltin, 0, -2 },
{ "mustBeInRange", (ptrBuiltin)Nelson::ValidatorsGateway::mustBeInRangeBuiltin, 0, -3 },
{ "mustBeMatrix", (ptrBuiltin)Nelson::ValidatorsGateway::mustBeMatrixBuiltin, 0, -1 },
{ "mustBeRow", (ptrBuiltin)Nelson::ValidatorsGateway::mustBeRowBuiltin, 0, -1 },
{ "mustBeColumn", (ptrBuiltin)Nelson::ValidatorsGateway::mustBeColumnBuiltin, 0, -1 },

};
//=============================================================================
static bool
Expand Down
35 changes: 35 additions & 0 deletions modules/validators/builtin/cpp/mustBeColumnBuiltin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//=============================================================================
// Copyright (c) 2016-present Allan CORNET (Nelson)
//=============================================================================
// This file is part of the Nelson.
//=============================================================================
// LICENCE_BLOCK_BEGIN
// SPDX-License-Identifier: LGPL-3.0-or-later
// LICENCE_BLOCK_END
//=============================================================================
#include "mustBeColumnBuiltin.hpp"
#include "ValidatorsInternal.hpp"
#include "Error.hpp"
#include "i18n.hpp"
#include "InputOutputArgumentsCheckers.hpp"
//=============================================================================
using namespace Nelson;
//=============================================================================
ArrayOfVector
Nelson::ValidatorsGateway::mustBeColumnBuiltin(int nLhs, const ArrayOfVector& argIn)
{
ArrayOfVector retval;
nargoutcheck(nLhs, 0, 0);
nargincheck(argIn, 1, 2);
int argPos = -1;
if (argIn.size() == 2) {
ArrayOf param2 = argIn[1];
argPos = param2.getContentAsInteger32Scalar();
if (argPos < 1) {
Error(_W("The last argument must be a positive integer."));
}
}
mustBeColumn(argIn[0], argPos, true);
return retval;
}
//=============================================================================
35 changes: 35 additions & 0 deletions modules/validators/builtin/cpp/mustBeMatrixBuiltin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//=============================================================================
// Copyright (c) 2016-present Allan CORNET (Nelson)
//=============================================================================
// This file is part of the Nelson.
//=============================================================================
// LICENCE_BLOCK_BEGIN
// SPDX-License-Identifier: LGPL-3.0-or-later
// LICENCE_BLOCK_END
//=============================================================================
#include "mustBeMatrixBuiltin.hpp"
#include "ValidatorsInternal.hpp"
#include "Error.hpp"
#include "i18n.hpp"
#include "InputOutputArgumentsCheckers.hpp"
//=============================================================================
using namespace Nelson;
//=============================================================================
ArrayOfVector
Nelson::ValidatorsGateway::mustBeMatrixBuiltin(int nLhs, const ArrayOfVector& argIn)
{
ArrayOfVector retval;
nargoutcheck(nLhs, 0, 0);
nargincheck(argIn, 1, 2);
int argPos = -1;
if (argIn.size() == 2) {
ArrayOf param2 = argIn[1];
argPos = param2.getContentAsInteger32Scalar();
if (argPos < 1) {
Error(_W("The last argument must be a positive integer."));
}
}
mustBeMatrix(argIn[0], argPos, true);
return retval;
}
//=============================================================================
35 changes: 35 additions & 0 deletions modules/validators/builtin/cpp/mustBeRowBuiltin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//=============================================================================
// Copyright (c) 2016-present Allan CORNET (Nelson)
//=============================================================================
// This file is part of the Nelson.
//=============================================================================
// LICENCE_BLOCK_BEGIN
// SPDX-License-Identifier: LGPL-3.0-or-later
// LICENCE_BLOCK_END
//=============================================================================
#include "mustBeRowBuiltin.hpp"
#include "ValidatorsInternal.hpp"
#include "Error.hpp"
#include "i18n.hpp"
#include "InputOutputArgumentsCheckers.hpp"
//=============================================================================
using namespace Nelson;
//=============================================================================
ArrayOfVector
Nelson::ValidatorsGateway::mustBeRowBuiltin(int nLhs, const ArrayOfVector& argIn)
{
ArrayOfVector retval;
nargoutcheck(nLhs, 0, 0);
nargincheck(argIn, 1, 2);
int argPos = -1;
if (argIn.size() == 2) {
ArrayOf param2 = argIn[1];
argPos = param2.getContentAsInteger32Scalar();
if (argPos < 1) {
Error(_W("The last argument must be a positive integer."));
}
}
mustBeRow(argIn[0], argPos, true);
return retval;
}
//=============================================================================
20 changes: 20 additions & 0 deletions modules/validators/builtin/include/mustBeColumnBuiltin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//=============================================================================
// Copyright (c) 2016-present Allan CORNET (Nelson)
//=============================================================================
// This file is part of the Nelson.
//=============================================================================
// LICENCE_BLOCK_BEGIN
// SPDX-License-Identifier: LGPL-3.0-or-later
// LICENCE_BLOCK_END
//=============================================================================
#pragma once
//=============================================================================
#include "ArrayOf.hpp"
//=============================================================================
namespace Nelson::ValidatorsGateway {
//=============================================================================
ArrayOfVector
mustBeColumnBuiltin(int nLhs, const ArrayOfVector& argIn);
//=============================================================================
} // namespace Nelson
//=============================================================================
20 changes: 20 additions & 0 deletions modules/validators/builtin/include/mustBeMatrixBuiltin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//=============================================================================
// Copyright (c) 2016-present Allan CORNET (Nelson)
//=============================================================================
// This file is part of the Nelson.
//=============================================================================
// LICENCE_BLOCK_BEGIN
// SPDX-License-Identifier: LGPL-3.0-or-later
// LICENCE_BLOCK_END
//=============================================================================
#pragma once
//=============================================================================
#include "ArrayOf.hpp"
//=============================================================================
namespace Nelson::ValidatorsGateway {
//=============================================================================
ArrayOfVector
mustBeMatrixBuiltin(int nLhs, const ArrayOfVector& argIn);
//=============================================================================
} // namespace Nelson
//=============================================================================
Loading
Loading