<html>
<head>
<script src="../../../resources/js-test-pre.js"></script>
<script src="../xpath-test-pre.js"></script>
</head>
<body>
<div id="console"></div>

<script>
function arraysAreEqual(array1, array2) {
   var temp = new Array();
   if ( (!array1[0]) || (!array2[0]) ) { // If either is not an array
      return false;
   }
   if (array1.length != array2.length) {
      return false;
   }
   // Put all the elements from array1 into a "tagged" array
   for (var i = 0; i < array1.length; i++) {
      key = (typeof array1[i]) + "~" + array1[i];
   // Use "typeof" so a number 1 isn't equal to a string "1".
      if (temp[key]) { temp[key]++; } else { temp[key] = 1; }
   // temp[key] = # of occurrences of the value (so an element could appear multiple times)
   }
   // Go through array2 - if same tag missing in "tagged" array, not equal
   for (var i = 0; i < array2.length; i++) {
      key = (typeof array2[i]) + "~" + array2[i];
      if (temp[key]) {
         if (temp[key] == 0) { return false; } else { temp[key]--; }
      // Subtract to keep track of # of appearances in array2
      } else { // Key didn't appear in array1, arrays are not equal.
         return false;
      }
   }
   // If we get to this point, then every generated key in array1 showed up the exact same
   // number of times in array2, so the arrays are equal.
   return true;
}


var doc = (new DOMParser).parseFromString(
    '<doc id="0">' +
        '<chapter id="1">' +
            '<section id="1.1">' +
                '<item id="1.1.1" />' +
            '</section>' +
        '</chapter>' +
        '<chapter id="2">' +
            '<section id="2.1">' +
                '<item id="2.1.1" />' +
            '</section>' +
            '<section id="2.2">' +
                '<item id="2.2.1" /><item id="2.2.2" /><item id="2.2.3" />' +
            '</section>' +
            '<section id="2.3">' +
                '<item id="2.3.1" />' +
            '</section>' +
        '</chapter>' +
        '<chapter id="3">' +
            '<section id="3.1">' +
                '<item id="3.1.1" />' +
            '</section>' +
        '</chapter>' +
    '</doc>',
    'application/xml');

var ROOT = doc.documentElement;
var CHAPTER1 = ROOT.firstChild;
var CHAPTER2 = CHAPTER1.nextSibling;
var CHAPTER3 = CHAPTER2.nextSibling;
var SECTION11 = CHAPTER1.firstChild;
var SECTION21 = CHAPTER2.firstChild;
var SECTION22 = SECTION21.nextSibling;
var SECTION23 = SECTION22.nextSibling;
var SECTION31 = CHAPTER3.firstChild;
var ITEM111 = SECTION11.firstChild;
var ITEM211 = SECTION21.firstChild;
var ITEM221 = SECTION22.firstChild;
var ITEM222 = ITEM221.nextSibling;
var ITEM223 = ITEM222.nextSibling;
var ITEM231 = SECTION23.firstChild;
var ITEM311 = SECTION31.firstChild;

test(doc, doc.documentElement, '//*[@id="2"]/child::*', [SECTION21, SECTION22, SECTION23]);
test(doc, doc.documentElement, '//*[@id="2.2"]/parent::*', [CHAPTER2]);
test(doc, doc.documentElement, '//*[@id="2.2"]/ancestor::*', [ROOT, CHAPTER2]);
test(doc, doc.documentElement, '//*[@id="2.2"]/following-sibling::*', [SECTION23]);
test(doc, doc.documentElement, '//*[@id="2.2"]/preceding-sibling::*', [SECTION21]);
test(doc, doc.documentElement, '//*[@id="2.2"]/following::*', [SECTION23, ITEM231, CHAPTER3, SECTION31, ITEM311]);
test(doc, doc.documentElement, '//*[@id="2.2"]/preceding::*', [CHAPTER1, SECTION11, ITEM111, SECTION21, ITEM211]);
test(doc, doc.documentElement, '//*[@id="2.2"]/attribute::*', [SECTION22.getAttributeNode("id")]);
test(doc, doc.documentElement, '//*[@id="2.2"]/self::*', [SECTION22]);
test(doc, doc.documentElement, '//*[@id="1"]/descendant-or-self::*', [CHAPTER1, SECTION11, ITEM111]);
test(doc, doc.documentElement, '//*[@id="2.2"]/ancestor-or-self::*', [ROOT, CHAPTER2, SECTION22]);

debug("Test that the ancestor, descendant, following, preceding, and self axes partition the document");
var nodeCount = doc.evaluate("count(//*)", doc.documentElement, null, XPathResult.ANY_TYPE, null).numberValue;
shouldBe('nodeCount', '16');
var allNodes = doc.evaluate("//*", doc.documentElement, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var allNodesSet = []
for (i = 0; i < allNodes.snapshotLength; ++i) {
    allNodesSet.push(allNodes.snapshotItem(i));
}
for (i = 0; i < allNodes.snapshotLength; ++i) {
    var node = allNodes.snapshotItem(i);
    var resultNodes = [];
    var axes = ['ancestor','descendant','following','preceding','self'];
    for (axis in axes) {
        var res = doc.evaluate(axes[axis] + "::*", node, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
        while (n = res.iterateNext()) {
            resultNodes.push(n);
        }
    }
    if (arraysAreEqual(resultNodes, allNodesSet))
        testPassed(node.getAttribute("id"));
    else
        testFailed(node.getAttribute("id"));
}

</script>
<script src="../../../resources/js-test-post.js"></script>
</body>
</html>
