Mercurial > repos > public > sbplib
changeset 417:effd75b113ba feature/better_map
Add tests and fix a bunch of bugs.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 02 Feb 2017 20:49:06 +0100 |
parents | e97550b5c1e7 |
children | 8bd2e36f1f8b |
files | Map.m MapTest.m |
diffstat | 2 files changed, 111 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
diff -r e97550b5c1e7 -r effd75b113ba Map.m --- a/Map.m Thu Feb 02 16:41:41 2017 +0100 +++ b/Map.m Thu Feb 02 20:49:06 2017 +0100 @@ -8,7 +8,7 @@ methods function obj = Map() - obj.map = containers.Map() + obj.map = containers.Map(); end function set(obj, k, v) @@ -25,7 +25,7 @@ function b = isKey(obj, k) keyByteStream = getByteStreamFromArray(k); - b = obj.map.isKey(keyByteStream); + b = obj.map.isKey(char(keyByteStream)); end function c = keys(obj) @@ -57,11 +57,11 @@ end function v = subsref(obj, S) - switch S.type + switch S(1).type case '()' - k = S.subs; + k = S.subs{1}; try - v = obj.get(k); + v = get(obj, k); catch ME if strcmp(ME.identifier,'MATLAB:Containers:Map:NoKey') error('Reference to non-existent entry %s',toString(S.subs)); @@ -70,17 +70,21 @@ end end otherwise - v = builtin('subsref', obj, S); + try + v = builtin('subsref', obj, S); + catch e + error('You can''t use dot notation for this because Matlab(TM). What is this piece of shit software anyway?') + end end end function obj = subsasgn(obj, S, v); - switch S.type + switch S(1).type case '()' - k = S.subs; - obj.set(k, v); + k = S.subs{1}; + set(obj, k, v); otherwise - error('Unsupported indexing operator: %s',S.type); + error('You can''t use dot notation because Matlab(TM). What is this piece of shit software anyway?') end end end
diff -r e97550b5c1e7 -r effd75b113ba MapTest.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MapTest.m Thu Feb 02 20:49:06 2017 +0100 @@ -0,0 +1,97 @@ +function tests = MatTest() + tests = functiontests(localfunctions); +end + +function kvp = getKeyValuePairs() + kvp = { + {1,3},1; + struct(), [1; 3; 4]; + [1,2; 4 3], struct(); + 'Hej', struct('lol', 6); + 0, 'Nej'; + }; +end + +function testSetAndGet(testCase) + keyValuePairs = getKeyValuePairs(); + + map = Map(); + + % Insert + for i = 1:length(keyValuePairs) + map(keyValuePairs{i,1}) = keyValuePairs{i,2}; + end + + % Validate output + for i = 1:length(keyValuePairs) + v = map(keyValuePairs{i,1}); + testCase.verifyEqual(v, keyValuePairs{i,2}); + end +end + +function map = exampleMap() + keyValuePairs = getKeyValuePairs(); + + map = Map(); + + % Insert + for i = 1:length(keyValuePairs) + map(keyValuePairs{i,1}) = keyValuePairs{i,2}; + end +end + +function testLength(testCase) + map = Map(); + testCase.verifyEqual(map.length, 0); + + map = exampleMap(); + testCase.verifyEqual(map.length, 5) +end + + +function testIsKey(testCase) + map = exampleMap(); + + keyValuePairs = getKeyValuePairs(); + keys = keyValuePairs(:,1); + + for i = 1:length(keys) + testCase.verifyTrue(map.isKey(keys{i})); + end + + notKeys = { + 'hej', + [], + 1, + {2,5}, + }; + + for i = 1:length(notKeys) + testCase.verifyFalse(map.isKey(notKeys{i})); + end +end + + +function testRemove(testCase) + map = exampleMap(); + + remove(map, struct()); + + testCase.verifyFalse(map.isKey(struct())); +end + +% function testValues(testCase) +% keyValuePairs = getKeyValuePairs(); + +% map = exampleMap(); + +% testCase.verifyEqual(values(map), keyValuePairs(:,2)'); +% end + +% function testKeys(testCase) +% keyValuePairs = getKeyValuePairs(); + +% map = exampleMap(); + +% testCase.verifyEqual(keys(map), keyValuePairs(:,1)'); +% end