
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for URLSearchParams.sort()</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="application/javascript">

function compareArray(a, b) {
  is(a.length, b.length, "Length matches");
  for (let i = 0; i < a.length; ++i) {
    is(a[i], b[i], "Values " + i + " match");
  }
}

[
  {
    "input": "z=b&a=b&z=a&a=a",
    "output": [["a", "b"], ["a", "a"], ["z", "b"], ["z", "a"]],
  },
  {
    "input": "\uFFFD=x&\uFFFC&\uFFFD=a",
    "output": [["\uFFFC", ""], ["\uFFFD", "x"], ["\uFFFD", "a"]],
  },
  {
    "input": "ﬃ&🌈", // 🌈 > code point, but < code unit because two code units
    "output": [["🌈", ""], ["ﬃ", ""]],
  },
  {
    "input": "é&e\uFFFD&e\u0301",
    "output": [["e\u0301", ""], ["e\uFFFD", ""], ["é", ""]],
  },
  {
    "input": "z=z&a=a&z=y&a=b&z=x&a=c&z=w&a=d&z=v&a=e&z=u&a=f&z=t&a=g",
    "output": [["a", "a"], ["a", "b"], ["a", "c"], ["a", "d"], ["a", "e"], ["a", "f"], ["a", "g"], ["z", "z"], ["z", "y"], ["z", "x"], ["z", "w"], ["z", "v"], ["z", "u"], ["z", "t"]],
  },
].forEach((val) => {
  info("Run test: " + JSON.stringify(val) + "\n");

  let params = new URLSearchParams(val.input);
  params.sort();

  let i = 0;
  for (let param of params) {
    compareArray(param, val.output[i++]);
  }

  let url = new URL("?" + val.input, "https://example/");
  url.searchParams.sort();
  params = new URLSearchParams(url.search);
  i = 0;
  for (let param of params) {
    compareArray(param, val.output[i++]);
  }
});

</script>
</body>
</html>
