Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 1 | <?xml version = "1.0" encoding="utf-8" ?>
|
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
| 3 | "DTD/xhtml1-strict.dtd">
|
Jeff Burke | 92dd8e4 | 2012-12-08 11:40:31 -0800 | [diff] [blame] | 4 | <!--
|
| 5 | See COPYING for copyright and distribution information.
|
| 6 | -->
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 7 | <html xmlns = "http://www.w3.org/1999/xhtml">
|
| 8 |
|
| 9 | <head>
|
| 10 | <title>NDN Name</title>
|
| 11 |
|
Alexander Afanasyev | 2a2c9d2 | 2013-03-13 16:21:32 -0700 | [diff] [blame] | 12 | <script type="text/javascript" src="../build/ndn-js.js"></script>
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 13 |
|
| 14 | <script type="text/javascript">
|
| 15 |
|
| 16 | function 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 Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 35 | result.innerHTML += "Name from '" + entree + "', Uint8Array of '.' and ArrayBuffer of 0,1,2,3:<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 36 | if (name1Uri == name1UriExpected)
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 37 | result.innerHTML += "SUCCESS: " + name1Uri + ".<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 38 | else
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 39 | result.innerHTML += "ERROR: got " + name1Uri + ", expected " + name1UriExpected + " .<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 40 |
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 41 | result.innerHTML += "Compare with same Name from '" + entree + "', '.' and ArrayBuffer:<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 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 +
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 46 | " components, expected " + name1.components.length + ".<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 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;
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 52 | result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 53 | }
|
| 54 | }
|
| 55 | if (allEqual)
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 56 | result.innerHTML += "SUCCESS: Names are equal.<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 57 | }
|
| 58 |
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 59 | result.innerHTML += "Compare with same Name from URI:<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 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 +
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 64 | " components, expected " + name1.components.length + ".<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 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;
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 70 | result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 71 | }
|
| 72 | }
|
| 73 | if (allEqual)
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 74 | result.innerHTML += "SUCCESS: Names are equal.<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 75 | }
|
| 76 |
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 77 | result.innerHTML += "Compare with Name prefix of first 2 components:<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 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 +
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 82 | " components, expected 2.<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 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;
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 88 | result.innerHTML += "ERROR: Names differ at component at index " + i + ".<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 89 | }
|
| 90 | }
|
| 91 | if (allEqual)
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 92 | result.innerHTML += "SUCCESS: First 2 components are equal: " + name4.to_uri() + " .<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 93 | }
|
| 94 |
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 95 | result.innerHTML += "Check with Name component at index 2:<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 96 | // Returns ArrayBuffer of "%00%01%02%03"
|
| 97 | var component2Out = new Uint8Array(name1.getComponent(2));
|
| 98 | if (DataUtils.arraysEqual(component2Out, comp2))
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 99 | result.innerHTML += "SUCCESS: Components at index 2 are equal.<br/>";
|
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 100 | else
|
Jeff Thompson | b2d46f6 | 2013-02-28 22:25:16 -0800 | [diff] [blame] | 101 | 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 Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 110 | }
|
| 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>
|