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

Add 2 New Array Functions #143

Merged
merged 7 commits into from
Sep 6, 2015
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
12 changes: 12 additions & 0 deletions addons/arrays/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
41 changes: 41 additions & 0 deletions addons/arrays/fnc_findMax.sqf
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions addons/arrays/fnc_findMin.sqf
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions addons/arrays/test.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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);

// ----------------------------------------------------------------------------
Expand Down
67 changes: 67 additions & 0 deletions addons/arrays/test_findMax.sqf
Original file line number Diff line number Diff line change
@@ -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;
66 changes: 66 additions & 0 deletions addons/arrays/test_findMin.sqf
Original file line number Diff line number Diff line change
@@ -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;