From e55a772a298a368f66ca48c55d2a4c90fde5b252 Mon Sep 17 00:00:00 2001 From: commy2 Date: Fri, 26 Feb 2016 05:59:37 +0100 Subject: [PATCH] fix hashes tests --- addons/hashes/fnc_hashCreate.sqf | 2 +- addons/hashes/fnc_hashEachPair.sqf | 5 ++--- addons/hashes/fnc_hashGet.sqf | 9 +++++---- addons/hashes/fnc_hashHasKey.sqf | 3 +-- addons/hashes/fnc_hashRem.sqf | 6 +++--- addons/hashes/fnc_hashSet.sqf | 30 ++++++++++++++++++++---------- addons/hashes/fnc_isHash.sqf | 11 ++--------- addons/hashes/test.sqf | 4 ++-- addons/hashes/test_hashes.sqf | 7 +++++++ 9 files changed, 43 insertions(+), 34 deletions(-) diff --git a/addons/hashes/fnc_hashCreate.sqf b/addons/hashes/fnc_hashCreate.sqf index e2bbc2869..135eb3372 100644 --- a/addons/hashes/fnc_hashCreate.sqf +++ b/addons/hashes/fnc_hashCreate.sqf @@ -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}]; diff --git a/addons/hashes/fnc_hashEachPair.sqf b/addons/hashes/fnc_hashEachPair.sqf index 7c85211f3..00f86b2b7 100644 --- a/addons/hashes/fnc_hashEachPair.sqf +++ b/addons/hashes/fnc_hashEachPair.sqf @@ -36,8 +36,7 @@ Author: SCRIPT(hashEachPair); // ----------------------------------------------------------------------------- - -params ["_hash","_code"]; +params [["_hash", [], [[]]], ["_code", {}, [{}]]]; _hash params ["", "_keys", "_values"]; @@ -48,4 +47,4 @@ _hash params ["", "_keys", "_values"]; call _code; } forEach _keys; -nil; // Return. +nil // Return. diff --git a/addons/hashes/fnc_hashGet.sqf b/addons/hashes/fnc_hashGet.sqf index e043eab34..d0ddb96a9 100644 --- a/addons/hashes/fnc_hashGet.sqf +++ b/addons/hashes/fnc_hashGet.sqf @@ -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 { diff --git a/addons/hashes/fnc_hashHasKey.sqf b/addons/hashes/fnc_hashHasKey.sqf index 3e6c9f615..bf47da059 100644 --- a/addons/hashes/fnc_hashHasKey.sqf +++ b/addons/hashes/fnc_hashHasKey.sqf @@ -23,7 +23,6 @@ Author: SCRIPT(hashHasKey); // ----------------------------------------------------------------------------- - -params ["_hash","_key"]; +params [["_hash", [], [[]]], "_key"]; _key in (_hash select HASH_KEYS); // Return. diff --git a/addons/hashes/fnc_hashRem.sqf b/addons/hashes/fnc_hashRem.sqf index 385b2a281..88109743b 100644 --- a/addons/hashes/fnc_hashRem.sqf +++ b/addons/hashes/fnc_hashRem.sqf @@ -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. diff --git a/addons/hashes/fnc_hashSet.sqf b/addons/hashes/fnc_hashSet.sqf index e9e963f2d..f27e3e2c1 100644 --- a/addons/hashes/fnc_hashSet.sqf +++ b/addons/hashes/fnc_hashSet.sqf @@ -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; @@ -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. @@ -63,4 +73,4 @@ if (_index >= 0) then { }; }; TRACE_1("",_hash); -_hash; // Return. +_hash // Return. diff --git a/addons/hashes/fnc_isHash.sqf b/addons/hashes/fnc_isHash.sqf index 7c0045ed2..f4c7aa1b4 100644 --- a/addons/hashes/fnc_isHash.sqf +++ b/addons/hashes/fnc_isHash.sqf @@ -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} diff --git a/addons/hashes/test.sqf b/addons/hashes/test.sqf index 595cff2db..45e671fe7 100644 --- a/addons/hashes/test.sqf +++ b/addons/hashes/test.sqf @@ -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 diff --git a/addons/hashes/test_hashes.sqf b/addons/hashes/test_hashes.sqf index bb9c6e4ea..8b6ece1c8 100644 --- a/addons/hashes/test_hashes.sqf +++ b/addons/hashes/test_hashes.sqf @@ -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");