blob: dd2a875b6da115db14e1a0466fe1ed8bce8033a4 [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
Alexander Afanasyev2a2c9d22013-03-13 16:21:32 -070012 <script type="text/javascript" src="../build/ndn-js.js"></script>
Jeff Thompson87021f72012-11-18 17:40:06 -080013
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";
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080035 result.innerHTML += "Name from '" + entree + "', Uint8Array of '.' and ArrayBuffer of 0,1,2,3:<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080036 if (name1Uri == name1UriExpected)
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080037 result.innerHTML += "SUCCESS: " + name1Uri + ".<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080038 else
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080039 result.innerHTML += "ERROR: got " + name1Uri + ", expected " + name1UriExpected + " .<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080040
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080041 result.innerHTML += "Compare with same Name from '" + entree + "', '.' and ArrayBuffer:<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080042 // 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 +
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080046 " components, expected " + name1.components.length + ".<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080047 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;
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080052 result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080053 }
54 }
55 if (allEqual)
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080056 result.innerHTML += "SUCCESS: Names are equal.<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080057 }
58
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080059 result.innerHTML += "Compare with same Name from URI:<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080060 // 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 +
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080064 " components, expected " + name1.components.length + ".<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080065 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;
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080070 result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080071 }
72 }
73 if (allEqual)
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080074 result.innerHTML += "SUCCESS: Names are equal.<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080075 }
76
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080077 result.innerHTML += "Compare with Name prefix of first 2 components:<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080078 // 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 +
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080082 " components, expected 2.<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080083 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;
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080088 result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080089 }
90 }
91 if (allEqual)
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080092 result.innerHTML += "SUCCESS: First 2 components are equal: " + name4.to_uri() + " .<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080093 }
94
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080095 result.innerHTML += "Check with Name component at index 2:<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -080096 // Returns ArrayBuffer of "%00%01%02%03"
97 var component2Out = new Uint8Array(name1.getComponent(2));
98 if (DataUtils.arraysEqual(component2Out, comp2))
Jeff Thompsonb2d46f62013-02-28 22:25:16 -080099 result.innerHTML += "SUCCESS: Components at index 2 are equal.<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -0800100 else
Jeff Thompsonb2d46f62013-02-28 22:25:16 -0800101 result.innerHTML += "ERROR: Components at index 2 are different.<br/>";
102
103 var name5 = new Name("/localhost/user/folders/files/%00%0F");
104 result.innerHTML += "Check chaining calls to add() to create Name: " + name5.to_uri() + "<br/>";
105 if (name5.equalsName(new Name(new Name("/localhost")).add(new Name("/user/folders")).add
106 ("files").addSegment(15)))
107 result.innerHTML += "SUCCESS: Names are equal.<br/>";
108 else
109 result.innerHTML += "ERROR: Names are different.<br/>";
Jeff Thompson87021f72012-11-18 17:40:06 -0800110}
111
112 </script>
113
114</head>
115<body >
116 <button onclick="testName()">Test</button>
117
118 <p id="result"></p>
119
120</body>
121</html>