michael@0: // |reftest| skip michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ michael@0: * Contributor: Jason Orendorff michael@0: */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: var summary = "Create a tree of threads"; michael@0: michael@0: var N = 50; // number of threads to create michael@0: michael@0: printStatus (summary); michael@0: michael@0: function range(start, stop) { michael@0: var a = []; michael@0: for (var i = start; i < stop; i++) michael@0: a.push(i); michael@0: return a; michael@0: } michael@0: michael@0: function tree(start, stop) { michael@0: sleep(0.001); michael@0: michael@0: if (start >= stop) michael@0: return []; michael@0: else if (start + 1 >= stop) michael@0: return [start]; michael@0: michael@0: sleep(0.001); michael@0: michael@0: let mid = start + Math.floor((stop - start) / 2); michael@0: let halves = scatter([function () { return tree(start, mid); }, michael@0: function () { return tree(mid, stop); }]); michael@0: sleep(0.001); michael@0: return Array.prototype.concat.apply([], halves); michael@0: } michael@0: michael@0: var expect; michael@0: var actual; michael@0: michael@0: if (typeof scatter == 'undefined' || typeof sleep == 'undefined') { michael@0: print('Test skipped. scatter or sleep not defined.'); michael@0: expect = actual = 'Test skipped.'; michael@0: } else { michael@0: expect = range(0, N).toSource(); michael@0: actual = tree(0, N).toSource(); michael@0: } michael@0: michael@0: reportCompare(expect, actual, summary);