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

Added 4 New Array Functions #131

Merged
merged 14 commits into from
Sep 6, 2015
24 changes: 24 additions & 0 deletions addons/arrays/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ class CfgFunctions
description = "Filter each element of an array via a function.";
file = "\x\cba\addons\arrays\fnc_filter.sqf";
};
// CBA_fnc_findNil
class findNil
{
description = "A function that returns the index of the first empty (nil) entry in an array.";
file = "\x\cba\addons\arrays\fnc_findNil.sqf";
};
// CBA_fnc_findTypeName
class findTypeName
{
description = "A function that returns the index of the first entry of a certain type in an array.";
file = "\x\cba\addons\arrays\fnc_findTypeName.sqf";
};
// CBA_fnc_findTypeOf
class findTypeOf
{
description = "A function that returns the index of the first entry of a certain type in an array.";
file = "\x\cba\addons\arrays\fnc_findTypeOf.sqf";
};
// CBA_fnc_findNull
class findNull
{
description = "A function that returns the index of the first null entry in an array.";
file = "\x\cba\addons\arrays\fnc_findNull.sqf";
};
// CBA_fnc_getArrayDiff
class getArrayDiff
{
Expand Down
33 changes: 33 additions & 0 deletions addons/arrays/fnc_findNil.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_findNil

Description:
A function that returns the index of the first empty (nil) entry in an array.

Parameters:
The array to search in.

Example:
(begin example)
_index = ["", Player, "test", nil, VARIABLE, nil] call CBA_fnc_findNil
(end)

Returns:
Index of the first nil entry in the array. If there is no nil entry, the function returns -1

Author:
joko // Jonas
---------------------------------------------------------------------------- */
#include "script_component.hpp"

scopeName "main";

{
private "_current";
_current = _x;
if (isNil "_current") then {
_forEachIndex breakOut "main";
};
} forEach _this;

-1
31 changes: 31 additions & 0 deletions addons/arrays/fnc_findNull.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_findNull

Description:
A function that returns the index of the first null entry in an array.

Parameters:
The array to search in.

Example:
(begin example)
_index = ["", Player, "test", objNull, VARIABLE, ] call CBA_fnc_findNull
(end)

Returns:
Index of the first null entry in the array. If there is no null entry, the function returns -1

Author:
joko // Jonas
---------------------------------------------------------------------------- */
#include "script_component.hpp"

scopeName "main";

{
if (isNull _x) then {
_forEachIndex breakOut "main";
};
} forEach _this;

-1
38 changes: 38 additions & 0 deletions addons/arrays/fnc_findTypeName.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_findTypeName

Description:
A function that returns the index of the first entry of the given type in an array.

Parameters:
0: Entry type to search for. Was not a string with a known type name it take the typeName from the input.
1: A Array with Any type of Variable

Example:
(begin example)
_index = ["OBJECT",["", Player, "test", nil, VARIABLE, nil]] call CBA_fnc_findTypeName
(end)

Returns:
Index of the first entry of the indicated type in the array or -1 if no entry of the type could be found.

Author:
joko // Jonas
---------------------------------------------------------------------------- */
#include "script_component.hpp"

scopeName "main";

params ["_typeName", "_array"];
_typeName = toUpper _typeName;
if !(_typeName in ["ARRAY", "BOOL", "CODE", "CONFIG", "CONTROL", "DISPLAY", "LOCALTION", "OBJECT", "SCALAR", "SCRIPT", "SIDE", "STRING", "TEXT", "TEAM_MEMBER", "NAMESPACE"]) then {
_typeName = typeName _typeName;
};

{
if (typeName _x == _typeName) then {
_forEachIndex breakOut "main";
};
} forEach _array;

-1
40 changes: 40 additions & 0 deletions addons/arrays/fnc_findTypeOf.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_findTypeOf

Description:
A function that returns the index of the first entry of the given type in an array.

Parameters:
0: Entry type to search for. Object or class name (as returned by the typeOf command).
1: A Array with Any type of Variable

Example:
(begin example)
_index = [Player, ["", Player, "test", nil, VARIABLE, nil]] call CBA_fnc_findTypeOf
(end)

Returns:
Index of the first entry of the indicated type in the array or -1 if no entry of the type could be found.

Author:
joko // Jonas
---------------------------------------------------------------------------- */
#include "script_component.hpp"

scopeName "main";

params ["_typeOf", "_array"];
if (typeName _typeOf == "OBJECT") then {
_typeOf = typeOf _typeOf;
};

{
if (_x typeName == "OBJECT" && {typeOf _x == _typeOf}) then {
_forEachIndex breakOut "main";
};
if (_x typeName == "STRING" && {_x == _typeOf}) then {
_forEachIndex breakOut "main";
};
} forEach _array;

-1
2 changes: 1 addition & 1 deletion addons/arrays/test.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define DEBUG_MODE_FULL
#include "script_component.hpp"

#define TESTS ["filter", "inject", "join", "shuffle"]
#define TESTS ["filter", "inject", "join", "shuffle", "find"]

SCRIPT(test-arrays);

Expand Down
185 changes: 185 additions & 0 deletions addons/arrays/test_find.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// ----------------------------------------------------------------------------
#define DEBUG_MODE_FULL
#include "script_component.hpp"

SCRIPT(test_find);

// ----------------------------------------------------------------------------

private ["_original", "_expected", "_result", "_fn"];

_fn = "CBA_fnc_findNil";
LOG("Testing " + _fn);

TEST_DEFINED("CBA_fnc_findNil","");

// Find First Nil Entry
_original = ["", player, "", player, nil, "", player, nil];
_result = ["", player, "", player, nil, "", player, nil] call CBA_fnc_findNil;
_expected = 4;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The three tests below all do the same thing as the first test above - look for the first literal nil element in the test arrays. Keep the first one, and change the three redundant tests to use

  • an array with a variable in it somewhere that is nil
  • an array with a code element in it that returns nil
  • an empty input array ([])

_original = ["", nil, "", player, nil, "", player, nil];
_result = ["", nil, "", player, nil, "", player, nil] call CBA_fnc_findNil;
_expected = 1;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = ["", player, "", nil, nil, "", player, nil];
_result = ["", player, "", nil, nil, "", player, nil] call CBA_fnc_findNil;
_expected = 3;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = ["", player, "", player, "", "", player, nil];
_result = ["", player, "", player, "", "", player, nil] call CBA_fnc_findNil;
_expected = 7;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = ["", player, "", player, "", "", player, ""];
_result = ["", player, "", player, "", "", player, ""] call CBA_fnc_findNil;
_expected = -1;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);




_fn = "CBA_fnc_findNull";
LOG("Testing " + _fn);

TEST_DEFINED("CBA_fnc_findNull","");


// Find First Null Entry
_original = ["", player, "", player, objNull, "", player, displayNull];
_result = ["", player, "", player, objNull, "", player, displayNull] call CBA_fnc_findNull;
_expected = 4;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = ["", grpNull, "", player, objNull, "", player, displayNull];
_result = ["", grpNull, "", player, objNull, "", player, displayNull] call CBA_fnc_findNull;
_expected = 1;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = ["", player, "", scriptNull, objNull, "", player, displayNull];
_result = ["", player, "", scriptNull, objNull, "", player, displayNull] call CBA_fnc_findNull;
_expected = 3;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = ["", player, "", player, "", "", player, displayNull];
_result = ["", player, "", player, "", "", player, displayNull] call CBA_fnc_findNull;
_expected = 7;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good - these four tests above test for different kinds of null values (but a test for controlNull is missing). It also needs tests for

  • an input array with a variable in it somewhere that is null-valued (for example objNull).
  • an input array with a code element in it that returns one of the null values.
  • an input array that is empty.


_original = ["", player, "", player, "", "", player, ""];
_result = ["", player, "", player, "", "", player, ""] call CBA_fnc_findNil;
_expected = -1;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);






_fn = "CBA_fnc_findTypeName";
LOG("Testing " + _fn);

TEST_DEFINED("CBA_fnc_findTypeName","");


// Find First Type of Entry
_original = ["OBJECT" ,["", player, "", player, objNull, "", player, displayNull]];
_result = ["OBJECT" ,["", player, "", player, objNull, "", player, displayNull]] call CBA_fnc_findTypeName;
_expected = 4;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = ["OBJECT" ,["", "", "", player, objNull, "", player, displayNull]];
_result = ["OBJECT" ,["", "", "", player, objNull, "", player, displayNull]] call CBA_fnc_findTypeName;
_expected = 3;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = ["CODE" ,["", player, {}, {}, objNull, "", player, displayNull]];
_result = ["CODE" ,["", player, {}, {}, objNull, "", player, displayNull]] call CBA_fnc_findTypeName;
_expected = 2;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = ["STRING" ,[objNull, player, {}, player, objNull, "", player, displayNull]];
_result = ["STRING" ,[objNull, player, {}, player, objNull, "", player, displayNull]] call CBA_fnc_findTypeName;
_expected = 5;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = ["OBJECT" ,["", "", "", "", "", "", "", displayNull]];
_result = ["OBJECT" ,["", "", "", "", "", "", "", displayNull]] call CBA_fnc_findTypeName;
_expected = -1;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);




_fn = "CBA_fnc_findTypOf";
LOG("Testing " + _fn);

TEST_DEFINED("CBA_fnc_findTypOf","");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see that this unit test file hasn't been tested at all. Run it using

[] call compile preprocessFileLineNumbers "\x\cba\addons\arrays\test_find.sqf";



// Find First Type of Entry
_original = [typeOf player ,["", player, "", player, objNull, "", player, displayNull]];
_result = [typeOf player ,["", player, "", player, objNull, "", player, displayNull]] call CBA_fnc_findTypOf;
_expected = 4;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = [player ,["", "", "", player, objNull, "", player, displayNull]];
_result = [player ,["", "", "", player, objNull, "", player, displayNull]] call CBA_fnc_findTypOf;
_expected = 3;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = [player ,["", player, {}, {}, objNull, "", player, displayNull]];
_result = [player ,["", player, {}, {}, objNull, "", player, displayNull]] call CBA_fnc_findTypOf;
_expected = 2;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = [player ,[objNull, player, {}, player, objNull, "", player, displayNull]];
_result = [player ,[objNull, player, {}, player, objNull, "", player, displayNull]] call CBA_fnc_findTypOf;
_expected = 5;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);


_original = ["OBJECT" ,["", "", "", "", "", "", "", displayNull]];
_result = ["OBJECT" ,["", "", "", "", "", "", "", displayNull]] call CBA_fnc_findTypOf;
_expected = -1;
TEST_OP(str _original,==,str _expected,_fn);
TEST_OP(str _result,==,str _expected,_fn);

nil;