blob: aca50d93721058d0ac1996170a38764a5236788e [file] [log] [blame]
<?xml version = "1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<!--
See COPYING for copyright and distribution information.
-->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>NDN Name</title>
<script type="text/javascript" src="../Helper.js"></script>
<script type="text/javascript">
function testName() {
var result = document.getElementById('result');
result.innerHTML = "";
var comp1 = new Uint8Array(1);
comp1.set(['.'.charCodeAt(0)]);
var ab2 = new ArrayBuffer(4);
var comp2 = new Uint8Array(ab2);
comp2.set([0x00, 0x01, 0x02, 0x03]);
// \u00E9 is e with accent.
var entree = "entr\u00E9e";
// When Name constructor is passed an array, each item is a name component represented with ArrayBuffer,
// typed array, or string (parsed as UTF-8)
var name1 = new Name([entree, comp1, ab2]);
var name1Uri = name1.to_uri();
var name1UriExpected = "/entr%C3%A9e/..../%00%01%02%03";
result.innerHTML += "Name from '" + entree + "', Uint8Array of '.' and ArrayBuffer of 0,1,2,3:<br>";
if (name1Uri == name1UriExpected)
result.innerHTML += "SUCCESS: " + name1Uri + ".<br>";
else
result.innerHTML += "ERROR: got " + name1Uri + ", expected " + name1UriExpected + " .<br>";
result.innerHTML += "Compare with same Name from '" + entree + "', '.' and ArrayBuffer:<br>";
// Equivalent with name1
var name2 = new Name([entree, ".", comp2]);
if (name2.components.length != name1.components.length)
result.innerHTML += "ERROR: Got name with " + name2.components.length +
" components, expected " + name1.components.length + ".<br>";
else {
var allEqual = true;
for (var i = 0; i < name1.components.length; ++i) {
if (!DataUtils.arraysEqual(name1.components[i], name2.components[i])) {
allEqual = false;
result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br>";
}
}
if (allEqual)
result.innerHTML += "SUCCESS: Names are equal.<br>";
}
result.innerHTML += "Compare with same Name from URI:<br>";
// Equivalent with name1; when Name constructor is passed a string, it is treated as a URI
var name3 = new Name("/entr%C3%A9e/..../%00%01%02%03");
if (name3.components.length != name1.components.length)
result.innerHTML += "ERROR: Got name with " + name3.components.length +
" components, expected " + name1.components.length + ".<br>";
else {
var allEqual = true;
for (var i = 0; i < name1.components.length; ++i) {
if (!DataUtils.arraysEqual(name1.components[i], name3.components[i])) {
allEqual = false;
result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br>";
}
}
if (allEqual)
result.innerHTML += "SUCCESS: Names are equal.<br>";
}
result.innerHTML += "Compare with Name prefix of first 2 components:<br>";
// Returns new Name([entree, "."])
var name4 = name2.getPrefix(2);
if (name4.components.length != 2)
result.innerHTML += "ERROR: Got name with " + name4.components.length +
" components, expected 2.<br>";
else {
var allEqual = true;
for (var i = 0; i < name4.components.length; ++i) {
if (!DataUtils.arraysEqual(name1.components[i], name4.components[i])) {
allEqual = false;
result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br>";
}
}
if (allEqual)
result.innerHTML += "SUCCESS: First 2 components are equal: " + name4.to_uri() + " .<br>";
}
result.innerHTML += "Check with Name component at index 2:<br>";
// Returns ArrayBuffer of "%00%01%02%03"
var component2Out = new Uint8Array(name1.getComponent(2));
if (DataUtils.arraysEqual(component2Out, comp2))
result.innerHTML += "SUCCESS: Components at index 2 are equal.<br>";
else
result.innerHTML += "ERROR: Components at index 2 are different.<br>";
}
</script>
</head>
<body >
<button onclick="testName()">Test</button>
<p id="result"></p>
</body>
</html>