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