blob: aca50d93721058d0ac1996170a38764a5236788e [file] [log] [blame]
Jeff Thompson87021f72012-11-18 17:40:06 -08001<?xml version = "1.0" encoding="utf-8" ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3"DTD/xhtml1-strict.dtd">
Jeff Burke92dd8e42012-12-08 11:40:31 -08004<!--
5 See COPYING for copyright and distribution information.
6-->
Jeff Thompson87021f72012-11-18 17:40:06 -08007<html xmlns = "http://www.w3.org/1999/xhtml">
8
9<head>
10 <title>NDN Name</title>
11
12 <script type="text/javascript" src="../Helper.js"></script>
13
14 <script type="text/javascript">
15
16function testName() {
17 var result = document.getElementById('result');
18 result.innerHTML = "";
19
20 var comp1 = new Uint8Array(1);
21 comp1.set(['.'.charCodeAt(0)]);
22
23 var ab2 = new ArrayBuffer(4);
24 var comp2 = new Uint8Array(ab2);
25 comp2.set([0x00, 0x01, 0x02, 0x03]);
26
27 // \u00E9 is e with accent.
28 var entree = "entr\u00E9e";
29
30 // When Name constructor is passed an array, each item is a name component represented with ArrayBuffer,
31 // typed array, or string (parsed as UTF-8)
32 var name1 = new Name([entree, comp1, ab2]);
33 var name1Uri = name1.to_uri();
34 var name1UriExpected = "/entr%C3%A9e/..../%00%01%02%03";
35 result.innerHTML += "Name from '" + entree + "', Uint8Array of '.' and ArrayBuffer of 0,1,2,3:<br>";
36 if (name1Uri == name1UriExpected)
37 result.innerHTML += "SUCCESS: " + name1Uri + ".<br>";
38 else
39 result.innerHTML += "ERROR: got " + name1Uri + ", expected " + name1UriExpected + " .<br>";
40
41 result.innerHTML += "Compare with same Name from '" + entree + "', '.' and ArrayBuffer:<br>";
42 // Equivalent with name1
43 var name2 = new Name([entree, ".", comp2]);
44 if (name2.components.length != name1.components.length)
45 result.innerHTML += "ERROR: Got name with " + name2.components.length +
46 " components, expected " + name1.components.length + ".<br>";
47 else {
48 var allEqual = true;
49 for (var i = 0; i < name1.components.length; ++i) {
50 if (!DataUtils.arraysEqual(name1.components[i], name2.components[i])) {
51 allEqual = false;
52 result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br>";
53 }
54 }
55 if (allEqual)
56 result.innerHTML += "SUCCESS: Names are equal.<br>";
57 }
58
59 result.innerHTML += "Compare with same Name from URI:<br>";
60 // Equivalent with name1; when Name constructor is passed a string, it is treated as a URI
61 var name3 = new Name("/entr%C3%A9e/..../%00%01%02%03");
62 if (name3.components.length != name1.components.length)
63 result.innerHTML += "ERROR: Got name with " + name3.components.length +
64 " components, expected " + name1.components.length + ".<br>";
65 else {
66 var allEqual = true;
67 for (var i = 0; i < name1.components.length; ++i) {
68 if (!DataUtils.arraysEqual(name1.components[i], name3.components[i])) {
69 allEqual = false;
70 result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br>";
71 }
72 }
73 if (allEqual)
74 result.innerHTML += "SUCCESS: Names are equal.<br>";
75 }
76
77 result.innerHTML += "Compare with Name prefix of first 2 components:<br>";
78 // Returns new Name([entree, "."])
79 var name4 = name2.getPrefix(2);
80 if (name4.components.length != 2)
81 result.innerHTML += "ERROR: Got name with " + name4.components.length +
82 " components, expected 2.<br>";
83 else {
84 var allEqual = true;
85 for (var i = 0; i < name4.components.length; ++i) {
86 if (!DataUtils.arraysEqual(name1.components[i], name4.components[i])) {
87 allEqual = false;
88 result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br>";
89 }
90 }
91 if (allEqual)
92 result.innerHTML += "SUCCESS: First 2 components are equal: " + name4.to_uri() + " .<br>";
93 }
94
95 result.innerHTML += "Check with Name component at index 2:<br>";
96 // Returns ArrayBuffer of "%00%01%02%03"
97 var component2Out = new Uint8Array(name1.getComponent(2));
98 if (DataUtils.arraysEqual(component2Out, comp2))
99 result.innerHTML += "SUCCESS: Components at index 2 are equal.<br>";
100 else
101 result.innerHTML += "ERROR: Components at index 2 are different.<br>";
102}
103
104 </script>
105
106</head>
107<body >
108 <button onclick="testName()">Test</button>
109
110 <p id="result"></p>
111
112</body>
113</html>