Skip to content

Commit

Permalink
Merge pull request #280 from CBATeam/fixhash
Browse files Browse the repository at this point in the history
fix hashes tests
  • Loading branch information
Killswitch00 committed Feb 26, 2016
2 parents e6abdff + e55a772 commit bfa1148
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 34 deletions.
2 changes: 1 addition & 1 deletion addons/hashes/fnc_hashCreate.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ private _keys = _array apply {_x select 0};
private _values = _array apply {_x select 1};

// Return.
[TYPE_HASH, _keys, _values, _defaultValue];
[TYPE_HASH, _keys, _values, if (isNil "_defaultValue") then {nil} else {_defaultValue}];
5 changes: 2 additions & 3 deletions addons/hashes/fnc_hashEachPair.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ Author:
SCRIPT(hashEachPair);

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

params ["_hash","_code"];
params [["_hash", [], [[]]], ["_code", {}, [{}]]];

_hash params ["", "_keys", "_values"];

Expand All @@ -48,4 +47,4 @@ _hash params ["", "_keys", "_values"];
call _code;
} forEach _keys;

nil; // Return.
nil // Return.
9 changes: 5 additions & 4 deletions addons/hashes/fnc_hashGet.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ Author:
SCRIPT(hashGet);

// -----------------------------------------------------------------------------
private ["_index", "_default"];
params ["_hash","_key"];
_default = param [2,_hash select HASH_DEFAULT_VALUE];
params [["_hash", [], [[]]], "_key"];

private _index = (_hash select HASH_KEYS) find _key;

_index = (_hash select HASH_KEYS) find _key;
if (_index >= 0) then {
(_hash select HASH_VALUES) select _index; // Return.
} else {
private _default = param [2, _hash select HASH_DEFAULT_VALUE];

if (isNil "_default") then {
nil // Return
} else {
Expand Down
3 changes: 1 addition & 2 deletions addons/hashes/fnc_hashHasKey.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Author:
SCRIPT(hashHasKey);

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

params ["_hash","_key"];
params [["_hash", [], [[]]], "_key"];

_key in (_hash select HASH_KEYS); // Return.
6 changes: 3 additions & 3 deletions addons/hashes/fnc_hashRem.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Author:
SCRIPT(hashRem);

// ----------------------------------------------------------------------------
params ["_hash","_key"];
params [["_hash", [], [[]]], "_key"];

private _defaultValue = _hash select HASH_DEFAULT_VALUE;
[_hash, _key, _defaultValue] call CBA_fnc_hashSet;
[_hash, _key, if (isNil "_defaultValue") then {nil} else {_defaultValue}] call CBA_fnc_hashSet;

_hash; // Return.
_hash // Return.
30 changes: 20 additions & 10 deletions addons/hashes/fnc_hashSet.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,38 @@ Author:
#include "script_hashes.hpp"

SCRIPT(hashSet);
private ["_index", "_isDefault"];

// ----------------------------------------------------------------------------
params ["_hash","_key","_value"];
params [["_hash", [], [[]]], "_key", "_value"];

if (isNil "_key") exitWith {_hash};
if (isNil "_hash") exitWith {_hash};

// Work out whether the new value is the default value for this assoc.
_isDefault = _value isEqualTo (_hash select HASH_DEFAULT_VALUE);
private _isDefault = false;

private _default = _hash select HASH_DEFAULT_VALUE;

if (isNil "_default") then {
_isDefault = isNil "_value";
} else {
if (!isNil "_value") then {
_isDefault = _value isEqualTo _default;
};
};

private _index = (_hash select HASH_KEYS) find _key;

_index = (_hash select HASH_KEYS) find _key;
if (_index >= 0) then {
if (_isDefault) then {
// Remove the key, if the new value is the default value.
// Do this by copying the key and value of the last element
// in the hash to the position of the element to be removed.
// Then, shrink the key and value arrays by one. (#2407)
private ["_keys", "_values", "_last"];

_keys = _hash select HASH_KEYS;
_values = _hash select HASH_VALUES;
_last = (count _keys) - 1;
private _keys = _hash select HASH_KEYS;
private _values = _hash select HASH_VALUES;
private _last = (count _keys) - 1;

_keys set [_index, _keys select _last];
_keys resize _last;
Expand All @@ -53,7 +63,7 @@ if (_index >= 0) then {
} else {
// Replace the original value for this key.
TRACE_2("VM CHECK SET",_index,_value);
(_hash select HASH_VALUES) set [_index, if (isNil "_value") then { nil } else { _value }];
(_hash select HASH_VALUES) set [_index, if (isNil "_value") then {nil} else {_value}];
};
} else {
// Ignore values that are the same as the default.
Expand All @@ -63,4 +73,4 @@ if (_index >= 0) then {
};
};
TRACE_1("",_hash);
_hash; // Return.
_hash // Return.
11 changes: 2 additions & 9 deletions addons/hashes/fnc_isHash.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ Author:
SCRIPT(isHash);

// -----------------------------------------------------------------------------
params ["_hash"];

params ["_value"];

private _result = false;

if ((typeName _value) == "ARRAY" && {(count _value) == 4} && {(typeName (_value select HASH_ID)) == (typeName TYPE_HASH)}) then {
_result = ((_value select HASH_ID) == TYPE_HASH);
};

_result;
_hash isEqualType [] && {count _hash == 4} && {(_hash select HASH_ID) isEqualTo TYPE_HASH}
4 changes: 2 additions & 2 deletions addons/hashes/test.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ SCRIPT(test-hashes);
LOG("=== Testing Hashes ===");

{
call compile preprocessFileLineNumbers format ["\x\cba\addons\hashes\test_%1.sqf", _x];
0 spawn compile preprocessFileLineNumbers format ["\x\cba\addons\hashes\test_%1.sqf", _x];
} forEach TESTS;

nil;
nil
7 changes: 7 additions & 0 deletions addons/hashes/test_hashes.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ TEST_FALSE(_result,"hashHashKey");
_result = [_hash, "frog"] call CBA_fnc_hashGet;
TEST_TRUE(isNil "_result","hashSet/Get");

// Unsetting a value 2
[_hash, "frog2", 23] call CBA_fnc_hashSet;
[_hash, "frog2"] call CBA_fnc_hashRem;

_result = [_hash, "frog2"] call CBA_fnc_hashGet;
TEST_TRUE(isNil "_result","hashSet/Rem");

// Value never put in is nil.
_result = [_hash, "fish"] call CBA_fnc_hashGet;
TEST_TRUE(isNil "_result","hashSet/Get");
Expand Down

0 comments on commit bfa1148

Please sign in to comment.