11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Tests below are not from WPT.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cirequire('../common');
61cb0ef41Sopenharmony_ciconst { test, assert_array_equals } = require('../common/wpt').harness;
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// TODO(joyeecheung): upstream this to WPT, if possible - even
91cb0ef41Sopenharmony_ci// just as a test for large inputs. Other implementations may
101cb0ef41Sopenharmony_ci// have a similar cutoff anyway.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// Test bottom-up iterative stable merge sort because we only use that
131cb0ef41Sopenharmony_ci// algorithm to sort > 100 search params.
141cb0ef41Sopenharmony_ciconst tests = [{ input: '', output: [] }];
151cb0ef41Sopenharmony_ciconst pairs = [];
161cb0ef41Sopenharmony_cifor (let i = 10; i < 100; i++) {
171cb0ef41Sopenharmony_ci  pairs.push([`a${i}`, 'b']);
181cb0ef41Sopenharmony_ci  tests[0].output.push([`a${i}`, 'b']);
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_citests[0].input = pairs.sort(() => Math.random() > 0.5)
211cb0ef41Sopenharmony_ci  .map((pair) => pair.join('=')).join('&');
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_citests.push(
241cb0ef41Sopenharmony_ci  {
251cb0ef41Sopenharmony_ci    'input': 'z=a&=b&c=d',
261cb0ef41Sopenharmony_ci    'output': [['', 'b'], ['c', 'd'], ['z', 'a']]
271cb0ef41Sopenharmony_ci  }
281cb0ef41Sopenharmony_ci);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_citests.forEach((val) => {
311cb0ef41Sopenharmony_ci  test(() => {
321cb0ef41Sopenharmony_ci    const params = new URLSearchParams(val.input);
331cb0ef41Sopenharmony_ci    let i = 0;
341cb0ef41Sopenharmony_ci    params.sort();
351cb0ef41Sopenharmony_ci    for (const param of params) {
361cb0ef41Sopenharmony_ci      assert_array_equals(param, val.output[i]);
371cb0ef41Sopenharmony_ci      i++;
381cb0ef41Sopenharmony_ci    }
391cb0ef41Sopenharmony_ci  }, `Parse and sort: ${val.input}`);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  test(() => {
421cb0ef41Sopenharmony_ci    const url = new URL(`?${val.input}`, 'https://example/');
431cb0ef41Sopenharmony_ci    url.searchParams.sort();
441cb0ef41Sopenharmony_ci    const params = new URLSearchParams(url.search);
451cb0ef41Sopenharmony_ci    let i = 0;
461cb0ef41Sopenharmony_ci    for (const param of params) {
471cb0ef41Sopenharmony_ci      assert_array_equals(param, val.output[i]);
481cb0ef41Sopenharmony_ci      i++;
491cb0ef41Sopenharmony_ci    }
501cb0ef41Sopenharmony_ci  }, `URL parse and sort: ${val.input}`);
511cb0ef41Sopenharmony_ci});
52