diff --git a/addons/arrays/CfgFunctions.hpp b/addons/arrays/CfgFunctions.hpp index abb1b1ebf..a337f4046 100644 --- a/addons/arrays/CfgFunctions.hpp +++ b/addons/arrays/CfgFunctions.hpp @@ -15,6 +15,18 @@ class CfgFunctions description = "Filter each element of an array via a function."; file = "\x\cba\addons\arrays\fnc_filter.sqf"; }; + // CBA_fnc_findMax + class findMax + { + description = "Find largest numeric value with index in an array."; + file = "\x\cba\addons\arrays\fnc_findMax.sqf"; + }; + // CBA_fnc_findMin + class findMin + { + description = "Find smallest numeric value with index in an array."; + file = "\x\cba\addons\arrays\fnc_findMin.sqf"; + }; // CBA_fnc_findNil class findNil { diff --git a/addons/arrays/fnc_findMax.sqf b/addons/arrays/fnc_findMax.sqf new file mode 100644 index 000000000..4e643b3cc --- /dev/null +++ b/addons/arrays/fnc_findMax.sqf @@ -0,0 +1,41 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_findMax + +Description: + Find largest numeric value with index in an array. + +Parameters: + _array: Array with Numbers + +Example: + (begin example) + _result = [_array] call CBA_fnc_findMax + (end) + +Returns: + _max: largest value in array + _index: index of the largest value in array + nil on failure + +Author: + joko // Jonas + +---------------------------------------------------------------------------- */ +#include "script_component.hpp" +SCRIPT(findMax); + +private ["_index"]; + +if (!IS_ARRAY(_this)) exitWith {nil}; +if (_this isEqualTo []) exitWith {nil}; + +params ["_max"]; +_index = 0; + +{ + if (isNil "_x" || {(typeName _x) != (typeName 0)}) exitWith {_max = nil; _index = nil;}; + if (_max < _x) then {_max = _x; _index = _forEachIndex}; +} forEach _this; + +if (isNil "_max") exitWith {nil}; +[_max, _index] // Return diff --git a/addons/arrays/fnc_findMin.sqf b/addons/arrays/fnc_findMin.sqf new file mode 100644 index 000000000..8e41f5320 --- /dev/null +++ b/addons/arrays/fnc_findMin.sqf @@ -0,0 +1,41 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_findMin + +Description: + Find smallest numeric value with index in an array. + +Parameters: + _array: Array with Numbers + +Example: + (begin example) + _result = [_array] call CBA_fnc_findMin + (end) + +Returns: + _min: smallest value in array + _index: index of the smallest value in array + nil on failure + +Author: + joko // Jonas + +---------------------------------------------------------------------------- */ +#include "script_component.hpp" +SCRIPT(findMin); + +private ["_index"]; + +if (!IS_ARRAY(_this)) exitWith {nil}; +if (_this isEqualTo []) exitWith {nil}; + +params ["_min"]; +_index = 0; + +{ + if (isNil "_x" || {(typeName _x) != (typeName 0)}) exitWith {_max = nil; _index = nil;}; + if (_min > _x) then {_min = _x; _index = _forEachIndex}; +} forEach _this; + +if (isNil "_max") exitWith {nil}; +[_min, _index] // Return diff --git a/addons/arrays/test.sqf b/addons/arrays/test.sqf index c3aeda4d4..9fccbb554 100644 --- a/addons/arrays/test.sqf +++ b/addons/arrays/test.sqf @@ -5,8 +5,7 @@ #define DEBUG_MODE_FULL #include "script_component.hpp" -#define TESTS ["filter", "inject", "join", "shuffle", "findNil", "findNull", "findTypeName", "findTypeOf"] - +#define TESTS ["filter", "inject", "join", "shuffle", "findNil", "findNull", "findTypeName", "findTypeOf", "findMax", "findMin"] SCRIPT(test-arrays); // ---------------------------------------------------------------------------- diff --git a/addons/arrays/test_findMax.sqf b/addons/arrays/test_findMax.sqf new file mode 100644 index 000000000..238bbb02c --- /dev/null +++ b/addons/arrays/test_findMax.sqf @@ -0,0 +1,67 @@ +// ---------------------------------------------------------------------------- +#define DEBUG_MODE_FULL +#define DEBUG_SYNCHRONOUS +#include "script_component.hpp" + +SCRIPT(test_findMax); + +// ---------------------------------------------------------------------------- + +private ["_original", "_expected", "_result", "_fn"]; + +_fn = "CBA_fnc_findMax"; +LOG("Testing " + _fn); + +TEST_DEFINED("CBA_fnc_findMax",""); + +// Test descending array +_result = [5, 4, 3, 2, 1] call CBA_fnc_findMax; +_expected = [5, 0]; +TEST_OP(_result,isEqualTo,_expected,_fn); + +// Test ascending array +_result = [1, 2, 3, 4, 5] call CBA_fnc_findMax; +_expected = [5, 4]; +TEST_OP(_result,isEqualTo,_expected,_fn); + +// Test unordered array +_result = [2, 15, 0, 4, 1] call CBA_fnc_findMax; +_expected = [15, 1]; +TEST_OP(_result,isEqualTo,_expected,_fn); + +// Test descending negative values array +_result = [-1, -2, -3, -4, -5] call CBA_fnc_findMax; +_expected = [-1, 0]; +TEST_OP(_result,isEqualTo,_expected,_fn); + +// Test unordered array with negative values +_result = [2, 15, 3, -3, 0] call CBA_fnc_findMax; +_expected = [15, 1]; +TEST_OP(_result,isEqualTo,_expected,_fn); + +// Test unordered array with duplicate max values +_result = [2, 15, 15, -3, -3] call CBA_fnc_findMax; +_expected = [15, 1]; +TEST_OP(_result,isEqualTo,_expected,_fn); + +// Test invalid parameter array with bool +_result = [1, true, 3, 4, 5] call CBA_fnc_findMax; +TEST_TRUE(isNil "_result",_fn); + +// Test invalid parameter array with nil +_result = [1, nil, 3, 4, 5] call CBA_fnc_findMax; +TEST_TRUE(isNil "_result",_fn); + +// Test invalid parameter array with string +_result = [1, "not a number", 3, 4, 5] call CBA_fnc_findMax; +TEST_TRUE(isNil "_result",_fn); + +// Test invalid array given +_result = "not an array" call CBA_fnc_findMax; +TEST_TRUE(isNil "_result",_fn); + +// Test empty array given +_result = [] call CBA_fnc_findMax; +TEST_TRUE(isNil "_result",_fn); + +nil; diff --git a/addons/arrays/test_findMin.sqf b/addons/arrays/test_findMin.sqf new file mode 100644 index 000000000..8bf241084 --- /dev/null +++ b/addons/arrays/test_findMin.sqf @@ -0,0 +1,66 @@ +// ---------------------------------------------------------------------------- +#define DEBUG_MODE_FULL +#include "script_component.hpp" + +SCRIPT(test_findMax); + +// ---------------------------------------------------------------------------- + +private ["_original", "_expected", "_result", "_fn"]; + +_fn = "CBA_fnc_findMin"; +LOG("Testing " + _fn); + +TEST_DEFINED("CBA_fnc_findMin",""); + +// Test descending array +_result = [5, 4, 3, 2, 1] call CBA_fnc_findMin; +_expected = [1, 4]; +TEST_OP(_result,isEqualTo,_expected,_fn); + +// Test ascending array +_result = [1, 2, 3, 4, 5] call CBA_fnc_findMin; +_expected = [1, 0]; +TEST_OP(_result,isEqualTo,_expected,_fn); + +// Test unordered array +_result = [2, 15, 0, 4, 1] call CBA_fnc_findMin; +_expected = [0, 2]; +TEST_OP(_result,isEqualTo,_expected,_fn); + +// Test descending negative values array +_result = [-1, -2, -3, -4, -5] call CBA_fnc_findMin; +_expected = [-5, 4]; +TEST_OP(_result,isEqualTo,_expected,_fn); + +// Test unordered array with negative values +_result = [2, 15, 3, -3, 0] call CBA_fnc_findMin; +_expected = [-3, 3]; +TEST_OP(_result,isEqualTo,_expected,_fn); + +// Test unordered array with duplicate max values +_result = [2, 15, 15, -3, -3] call CBA_fnc_findMin; +_expected = [-3, 3]; +TEST_OP(_result,isEqualTo,_expected,_fn); + +// Test invalid parameter array with bool +_result = [1, true, 3, 4, 5] call CBA_fnc_findMin; +TEST_TRUE(isNil "_result",_fn); + +// Test invalid parameter array with nil +_result = [1, nil, 3, 4, 5] call CBA_fnc_findMin; +TEST_TRUE(isNil "_result",_fn); + +// Test invalid parameter array with string +_result = [1, "not a number", 3, 4, 5] call CBA_fnc_findMin; +TEST_TRUE(isNil "_result",_fn); + +// Test invalid array given +_result = "not an array" call CBA_fnc_findMin; +TEST_TRUE(isNil "_result",_fn); + +// Test empty array given +_result = [] call CBA_fnc_findMin; +TEST_TRUE(isNil "_result",_fn); + +nil;