Skip to content

Commit

Permalink
Merge pull request #1377 from CBATeam/hashArrayDeep
Browse files Browse the repository at this point in the history
hashValues - don't copy arrays
  • Loading branch information
PabstMirror authored Nov 3, 2020
2 parents fc63a09 + 0a5a938 commit 311e599
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion addons/hashes/fnc_hashKeys.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ SCRIPT(hashKeys);
// -----------------------------------------------------------------------------
params [["_hash", [[], []], [[]]]];

+(_hash select HASH_KEYS)
[] + (_hash select HASH_KEYS) // flat-copy
2 changes: 1 addition & 1 deletion addons/hashes/fnc_hashValues.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ SCRIPT(hashValues);
// -----------------------------------------------------------------------------
params [["_hash", [[], []], [[]]]];

+(_hash select HASH_VALUES)
[] + (_hash select HASH_VALUES) // flat-copy
35 changes: 35 additions & 0 deletions addons/hashes/test_hashes.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ TEST_DEFINED("CBA_fnc_hashHasKey","");
TEST_DEFINED("CBA_fnc_isHash","");
TEST_DEFINED("CBA_fnc_hashSize","");
TEST_DEFINED("CBA_fnc_hashKeys","");
TEST_DEFINED("CBA_fnc_hashValues","");

TEST_FALSE([[]] call CBA_fnc_isHash,"CBA_fnc_isHash");
_hash = [5, [4], [1], 2]; // Not a real hash.
Expand Down Expand Up @@ -128,4 +129,38 @@ TEST_OP(_keys,isEqualTo,[],"hashKeys");
_keys = [_hash] call CBA_fnc_hashKeys;
TEST_OP(_keys,isEqualTo,[ARR_3("123","124",125)],"hashKeys");

// Using an array as a key
_hash = [] call CBA_fnc_hashCreate;
private _arrayKey = [];
[_hash, _arrayKey, "whyWouldYouDoThis"] call CBA_fnc_hashSet;
_keys = [_hash] call CBA_fnc_hashKeys;
(_keys select 0) pushBack 7;
TEST_OP(_arrayKey,isEqualTo,[7],"hashKeysDepth");
_result = [_hash, [7]] call CBA_fnc_hashGet;
TEST_OP(_result,==,"whyWouldYouDoThis","hashKeysDepth");

// Test CBA_fnc_hashValues
_hash = [] call CBA_fnc_hashCreate;
private _values = [_hash] call CBA_fnc_hashValues;
TEST_OP(_values,isEqualTo,[],"hashValues - empty");

[_hash, "a", 1] call CBA_fnc_hashSet;
[_hash, "b", 2] call CBA_fnc_hashSet;
_values = [_hash] call CBA_fnc_hashValues;
TEST_OP(_values,isEqualTo,[ARR_2(1,2)],"hashValues - values");

[_hash, "a"] call CBA_fnc_hashRem;
_values = [_hash] call CBA_fnc_hashValues;
TEST_OP(_values,isEqualTo,[2],"hashValues - removed");

_hash = [] call CBA_fnc_hashCreate;
private _data = [3];
[_hash, "c", _data] call CBA_fnc_hashSet;
_values = [_hash] call CBA_fnc_hashValues;
TEST_OP(_values,isEqualTo,[[3]],"hashValues - array");

_data pushBack [7];
TEST_OP(_values,isEqualTo,[[ARR_2(3,[7])]],"hashValues - deep array copy");


nil;

0 comments on commit 311e599

Please sign in to comment.