fpizlo@apple.com | c09dc63 | 2013-08-17 05:50:48 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 | // Copyright (C) 2013 Apple Inc. All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following |
| 12 | // disclaimer in the documentation and/or other materials provided |
| 13 | // with the distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived |
| 16 | // from this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | // Test the performance of Int16Array by implementing a bubble sort. |
| 31 | |
| 32 | function bubbleSort(array) |
| 33 | { |
| 34 | for (var i = 1; i < (array.byteLength >> 1); ++i) { |
| 35 | for (var j = 0; j < (array.byteLength >> 1) - i; ++j) { |
| 36 | if (array[j + 1] < array[j]) { |
| 37 | var tmp = array[j]; |
| 38 | array[j] = array[j + 1]; |
| 39 | array[j + 1] = tmp; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // V8's random function. |
| 46 | myRandom = (function() { |
| 47 | var seed = 49734321; |
| 48 | return function() { |
| 49 | // Robert Jenkins' 32 bit integer hash function. |
| 50 | seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff; |
| 51 | seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff; |
| 52 | seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff; |
| 53 | seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff; |
| 54 | seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff; |
| 55 | seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff; |
| 56 | return (seed & 0xfffffff) / 0x10000000; |
| 57 | }; |
| 58 | })(); |
| 59 | |
| 60 | function arrayToString(array) { |
| 61 | var result = "["; |
| 62 | for (var i = 0; i < (array.byteLength >> 1); ++i) { |
| 63 | if (i) |
| 64 | result += ", "; |
| 65 | result += array[i]; |
| 66 | } |
| 67 | return result + "]"; |
| 68 | } |
| 69 | |
| 70 | function validateSort(array) { |
| 71 | for (var i = 0; i < (array.byteLength >> 1) - 1; ++i) { |
| 72 | if (array[i + 1] < array[i]) |
| 73 | return false; |
| 74 | } |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | var array = new Int16Array(1000); |
| 79 | for (var count = 0; count < 10; ++count) { |
| 80 | for (var i = 0; i < (array.byteLength >> 1); ++i) { |
| 81 | array[i] = myRandom() * 65535; |
| 82 | } |
| 83 | |
| 84 | bubbleSort(array); |
| 85 | |
| 86 | if (!validateSort(array)) |
| 87 | throw "Bad result: " + arrayToString(array); |
| 88 | } |
| 89 | |