5 changed files with 200 additions and 79 deletions
-
19js/hash.js
-
78js/hash_test.html
-
76tests/js/hash_test.js
-
20tests/js/index.html
-
86tests/js/tests.js
@ -1,78 +0,0 @@ |
|||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
|||
"http://www.w3.org/TR/html4/loose.dtd"> |
|||
<html> |
|||
<head> |
|||
<script type="text/javascript" src="hash.js"></script> |
|||
<script type="text/javascript"> |
|||
function print(text) |
|||
{ |
|||
document.getElementById("page").innerHTML += text; |
|||
} |
|||
|
|||
function array_to_string(array) |
|||
{ |
|||
for(i in array) { |
|||
print(array[i] + "<br />"); |
|||
} |
|||
} |
|||
|
|||
function runtests() |
|||
{ |
|||
// Starting tests. |
|||
print("Creating the Hash.<br />"); |
|||
var myhash = new Hash(); |
|||
|
|||
print("Adding associative elements.<br />"); |
|||
myhash.set("toto", "tata"); |
|||
myhash.set("prout", "tagada"); |
|||
print("<br />"); |
|||
|
|||
print("Dumping contents.<br />"); |
|||
print(myhash.to_string()); |
|||
print("<br /><br />"); |
|||
|
|||
print("Changing the value of 'prout' to 'tigidi'.<br />"); |
|||
myhash.set('prout', 'tigidi'); |
|||
print(myhash.to_string()); |
|||
print("<br /><br />"); |
|||
|
|||
print("The value of 'toto' is '" + myhash.get('toto') + "'<br />"); |
|||
print("<br />"); |
|||
|
|||
print("Re-creating hash with constructor.<br />"); |
|||
myhash = H({toto: 'tata', couac: 'prout', pouet: 'tagada'}); |
|||
print(myhash.to_string()); |
|||
print("<br /><br />"); |
|||
|
|||
print("Iterating through the array.<br />"); |
|||
|
|||
var iter = myhash.iterate(); |
|||
while(iter.next()) { |
|||
print(iter.key() +" => '"+ iter.val() +"'<br />"); |
|||
} |
|||
print("<br />"); |
|||
|
|||
print("And reversed!<br />"); |
|||
|
|||
var iter = myhash.iterate(); |
|||
iter.end(); |
|||
while(iter.prev()) { |
|||
print(iter.key() +" => '"+ iter.val() +"'<br />"); |
|||
} |
|||
print("<br />"); |
|||
|
|||
print("Getting all the keys.<br />"); |
|||
array_to_string(myhash.keys()); |
|||
print("<br />"); |
|||
|
|||
print("Getting all the values.<br />"); |
|||
array_to_string(myhash.values()); |
|||
print("<br />"); |
|||
} |
|||
</script> |
|||
</head> |
|||
<body onload="runtests()"> |
|||
<div id="page"> |
|||
</div> |
|||
</body> |
|||
</html> |
|||
@ -0,0 +1,76 @@ |
|||
var myhash = null; |
|||
|
|||
tests.set('hash', H({ |
|||
creation: function() |
|||
{ |
|||
object = {test: 'prout'}; |
|||
myhash = H(object); |
|||
equals(myhash.container, object); |
|||
|
|||
myhash = new Hash(); |
|||
different(myhash, null); |
|||
}, |
|||
|
|||
ishash: function() |
|||
{ |
|||
var obj = new Array(); |
|||
assert(isHash(myhash)); |
|||
nassert(isHash(obj)); |
|||
}, |
|||
|
|||
get: function() |
|||
{ |
|||
hash = H({toto: "tata", prout: "tagada"}); |
|||
equals(hash.get("toto"), "tata"); |
|||
}, |
|||
|
|||
set: function() |
|||
{ |
|||
myhash.set("toto", "tata"); |
|||
myhash.set("prout", "tagada"); |
|||
|
|||
equals(myhash.get("toto"), "tata"); |
|||
equals(myhash.get("prout"), "tagada"); |
|||
|
|||
myhash.set("toto", "tigidi"); |
|||
equals(myhash.get("toto"), "tigidi"); |
|||
}, |
|||
|
|||
tostring: function() |
|||
{ |
|||
var hash = H({toto: "tata", prout: "tagada"}); |
|||
equals(hash.to_string(), 'toto: tata, prout: tagada'); |
|||
}, |
|||
|
|||
change: function() |
|||
{ |
|||
myhash.set('prout', 'tigidi'); |
|||
equals(myhash.get('prout'), 'tigidi'); |
|||
}, |
|||
|
|||
iterate: function() |
|||
{ |
|||
var iter = myhash.iterate(); |
|||
while(iter.next()) { |
|||
equals(iter.val(), myhash.get(iter.key())) |
|||
} |
|||
|
|||
iter.end(); |
|||
while(iter.prev()) { |
|||
equals(iter.val(), myhash.get(iter.key())) |
|||
} |
|||
}, |
|||
|
|||
keys: function() |
|||
{ |
|||
hash = H({toto: "tata", prout: "tagada"}); |
|||
keys = new Array('toto', 'prout'); |
|||
vals = new Array('tata', 'tagada'); |
|||
|
|||
hashkeys = hash.keys(); |
|||
hashvals = hash.values(); |
|||
|
|||
assert(keys[0] == hashkeys[0] && keys[1] == hashkeys[1]); |
|||
assert(vals[0] == hashvals[0] && vals[1] == hashvals[1]); |
|||
}, |
|||
})); |
|||
@ -0,0 +1,20 @@ |
|||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
|||
"http://www.w3.org/TR/html4/loose.dtd"> |
|||
<html> |
|||
<head> |
|||
<!-- Libraries to test --> |
|||
<script type="text/javascript" src="../../js/hash.js"></script> |
|||
<script type="text/javascript" src="../../js/events.js"></script> |
|||
|
|||
<!-- Test framework --> |
|||
<script type="text/javascript" src="tests.js"></script> |
|||
|
|||
<!-- Tests --> |
|||
<script type="text/javascript" src="hash_test.js"></script> |
|||
<script type="text/javascript" src="events_test.js"></script> |
|||
</head> |
|||
<body onload="runtests()"> |
|||
<div id="page"> |
|||
</div> |
|||
</body> |
|||
</html> |
|||
@ -0,0 +1,86 @@ |
|||
/* Testing framework's tools. */ |
|||
var failures = 0; |
|||
var success = 0; |
|||
var tests = H(); |
|||
|
|||
/************* Common stuff ***************/ |
|||
|
|||
function print(text) |
|||
{ |
|||
document.getElementById("page").innerHTML += text; |
|||
} |
|||
|
|||
function printtest(result) |
|||
{ |
|||
if(!result) { |
|||
failures++; |
|||
print('x'); |
|||
} else { |
|||
success++; |
|||
print('.'); |
|||
} |
|||
} |
|||
|
|||
function print_summary() |
|||
{ |
|||
var total = failures + success; |
|||
|
|||
print('<h2>Test summary:</h2>'); |
|||
print('<p>Total tests: ' + total + '<br />'); |
|||
print('Success: ' + success + '<br />'); |
|||
print('Failures: ' + failures + '<br />'); |
|||
print('Success rate: ' + parseInt(success / Math.max(1, total) * 100) + '%</p>'); |
|||
} |
|||
|
|||
// Runs the test suites
|
|||
function runtests() |
|||
{ |
|||
if(isHash(tests)) { |
|||
var iter = tests.iterate(); |
|||
while(iter.next()) { |
|||
if(isHash(iter.val())) { // This is a test suite.
|
|||
print("<h2>Running test " + iter.key() + "</h2>"); |
|||
var suite_iter = iter.val().iterate(); |
|||
while(suite_iter.next()) { |
|||
print('<b>' + suite_iter.key() + '</b> '); |
|||
var test = suite_iter.val(); |
|||
try { |
|||
test(); |
|||
} |
|||
catch(err) |
|||
{ |
|||
printtest(false); |
|||
} |
|||
print('<br />'); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
print_summary(); |
|||
} |
|||
|
|||
/*********** Testing funtions *************/ |
|||
// Asserts that thing returns true.
|
|||
function assert(thing) |
|||
{ |
|||
printtest(thing); |
|||
} |
|||
|
|||
// Ensures that thing returns false.
|
|||
function nassert(thing) |
|||
{ |
|||
printtest(!thing); |
|||
} |
|||
|
|||
// Ensures that thing is equal to exp_thing
|
|||
function equals(thing, exp_thing) |
|||
{ |
|||
printtest(thing == exp_thing); |
|||
} |
|||
|
|||
// Ensures that thing is not equal to exp_thing
|
|||
function different(thing, exp_thing) |
|||
{ |
|||
printtest(thing != exp_thing); |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue